More NPOIFSStream unit tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1053250 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-12-28 04:43:19 +00:00
parent 14fbfb1321
commit 1df87f0c0f
2 changed files with 79 additions and 4 deletions

View File

@ -346,6 +346,9 @@ public final class TestNPOIFSFileSystem extends TestCase {
public void testGetFreeBlockWithNoneSpare() throws Exception { public void testGetFreeBlockWithNoneSpare() throws Exception {
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi")); NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
// We have one BAT at block 99
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(99));
// We've spare ones from 100 to 128 // We've spare ones from 100 to 128
for(int i=100; i<128; i++) { for(int i=100; i<128; i++) {
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(i)); assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(i));

View File

@ -510,9 +510,38 @@ public final class TestNPOIFSStream extends TestCase {
* to support this * to support this
*/ */
public void testWriteNewStreamExtraFATs() throws Exception { public void testWriteNewStreamExtraFATs() throws Exception {
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi")); NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
// TODO // Allocate almost all the blocks
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(99));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(100));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(127));
for(int i=100; i<127; i++) {
fs.setNextBlock(i, POIFSConstants.END_OF_CHAIN);
}
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(127));
assertEquals(true, fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
// Write a 3 block stream
byte[] data = new byte[512*3];
for(int i=0; i<data.length; i++) {
data[i] = (byte)(i%256);
}
NPOIFSStream stream = new NPOIFSStream(fs);
stream.updateContents(data);
// Check we got another BAT
assertEquals(false, fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
assertEquals(true, fs.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
// the BAT will be in the first spot of the new block
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(126));
assertEquals(129, fs.getNextBlock(127));
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(128));
assertEquals(130, fs.getNextBlock(129));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(130));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(131));
} }
/** /**
@ -520,9 +549,52 @@ public final class TestNPOIFSStream extends TestCase {
* more data than before, in a 4096 byte block file * more data than before, in a 4096 byte block file
*/ */
public void testWriteStream4096() throws Exception { public void testWriteStream4096() throws Exception {
NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi")); NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
// TODO // 0 -> 1 -> 2 -> end
assertEquals(1, fs.getNextBlock(0));
assertEquals(2, fs.getNextBlock(1));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
assertEquals(4, fs.getNextBlock(3));
// First free one is at 15
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(14));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(15));
// Write a 5 block file
byte[] data = new byte[4096*5];
for(int i=0; i<data.length; i++) {
data[i] = (byte)(i%256);
}
NPOIFSStream stream = new NPOIFSStream(fs, 0);
stream.updateContents(data);
// Check it
assertEquals(1, fs.getNextBlock(0));
assertEquals(2, fs.getNextBlock(1));
assertEquals(15, fs.getNextBlock(2)); // Jumps
assertEquals(4, fs.getNextBlock(3)); // Next stream
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(14));
assertEquals(16, fs.getNextBlock(15)); // Continues
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(16)); // Ends
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(17)); // Free
// Check the contents too
Iterator<ByteBuffer> it = stream.getBlockIterator();
int count = 0;
while(it.hasNext()) {
ByteBuffer b = it.next();
data = new byte[512];
b.get(data);
for(int i=0; i<data.length; i++) {
byte exp = (byte)(i%256);
assertEquals(exp, data[i]);
}
count++;
}
assertEquals(5, count);
} }
/** /**