Adding some simple unit tests.

This commit is contained in:
Rob Greene 2018-05-24 12:56:18 -05:00
parent e706201ae2
commit 307c5eab3f
3 changed files with 70 additions and 9 deletions

View File

@ -123,7 +123,7 @@ public class AppleSingle {
int resourceForkOffset = prodosFileInfoOffset + 8;
int dataForkOffset = resourceForkOffset + (hasResourceFork ? resourceFork.length : 0);
writeFileHeader(outputStream);
writeFileHeader(outputStream, entries);
writeHeader(outputStream, 3, realNameOffset, realName.length());
writeHeader(outputStream, 11, prodosFileInfoOffset, 8);
if (hasResourceFork) writeHeader(outputStream, 2, resourceForkOffset, resourceFork.length);
@ -145,17 +145,20 @@ public class AppleSingle {
}
}
private void writeFileHeader(OutputStream outputStream) throws IOException {
ByteBuffer buf = ByteBuffer.allocate(24).order(ByteOrder.BIG_ENDIAN);
buf.putLong(MAGIC_NUMBER);
buf.putLong(VERSION_NUMBER);
private void writeFileHeader(OutputStream outputStream, int numberOfEntries) throws IOException {
final byte[] filler = new byte[16];
ByteBuffer buf = ByteBuffer.allocate(26).order(ByteOrder.BIG_ENDIAN);
buf.putInt(MAGIC_NUMBER);
buf.putInt(VERSION_NUMBER);
buf.put(filler);
buf.putShort((short)numberOfEntries);
outputStream.write(buf.array());
}
private void writeHeader(OutputStream outputStream, int entryId, int offset, int length) throws IOException {
ByteBuffer buf = ByteBuffer.allocate(12).order(ByteOrder.BIG_ENDIAN);
buf.putLong(entryId);
buf.putLong(offset);
buf.putLong(length);
buf.putInt(entryId);
buf.putInt(offset);
buf.putInt(length);
outputStream.write(buf.array());
}
private void writeRealName(OutputStream outputStream) throws IOException {
@ -165,7 +168,7 @@ public class AppleSingle {
ByteBuffer buf = ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN);
buf.putShort((short)prodosFileInfo.access);
buf.putShort((short)prodosFileInfo.fileType);
buf.putLong(prodosFileInfo.fileType);
buf.putInt(prodosFileInfo.fileType);
outputStream.write(buf.array());
}
private void writeResourceFork(OutputStream outputStream) throws IOException {

View File

@ -0,0 +1,58 @@
package io.github.applecommander.applesingle;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.Test;
public class AppleSingleTest {
private static final String AS_HELLO_BIN = "/hello.applesingle.bin";
@Test
public void testSampleFromCc65() throws IOException {
AppleSingle as = AppleSingle.read(getClass().getResourceAsStream(AS_HELLO_BIN));
assertNull(as.getRealName());
assertNull(as.getResourceFork());
assertNotNull(as.getDataFork());
assertNotNull(as.getProdosFileInfo());
ProdosFileInfo info = as.getProdosFileInfo();
assertEquals(0xc3, info.getAccess());
assertEquals(0x06, info.getFileType());
assertEquals(0x0803, info.getAuxType());
}
@Test
public void testCreateAndReadAppleSingle() throws IOException {
final byte[] dataFork = "testing testing 1-2-3".getBytes();
final String realName = "test.as";
// Using default ProDOS info and skipping resource fork
AppleSingle createdAS = AppleSingle.builder()
.dataFork(dataFork)
.realName(realName)
.build();
assertNotNull(createdAS);
assertEquals(realName, createdAS.getRealName());
assertArrayEquals(dataFork, createdAS.getDataFork());
assertNull(createdAS.getResourceFork());
assertNotNull(createdAS.getProdosFileInfo());
ByteArrayOutputStream actualBytes = new ByteArrayOutputStream();
createdAS.save(actualBytes);
assertNotNull(actualBytes);
AppleSingle readAS = AppleSingle.read(actualBytes.toByteArray());
assertNotNull(readAS);
assertEquals(realName, readAS.getRealName());
assertArrayEquals(dataFork, readAS.getDataFork());
assertNull(readAS.getResourceFork());
assertNotNull(readAS.getProdosFileInfo());
}
}

Binary file not shown.