@Test
public void addBeforeWrite() throws Exception {
NPOIFSFileSystem fs = new NPOIFSFileSystem();
DocumentEntry miniDoc;
DocumentEntry normDoc;
HeaderBlock hdr;
// Initially has BAT + Properties but nothing else
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(2));
hdr = writeOutAndReadHeader(fs);
// No mini stream, and no xbats
// Will have fat then properties stream
assertEquals(1, hdr.getBATCount());
assertEquals(0, hdr.getBATArray()[0]);
assertEquals(2, hdr.getPropertyStart());
assertEquals(POIFSConstants.END_OF_CHAIN, hdr.getSBATStart());
assertEquals(POIFSConstants.END_OF_CHAIN, hdr.getXBATIndex());
assertEquals(POIFSConstants.SMALLER_BIG_BLOCK_SIZE*4, fs.size());
// Get a clean filesystem to start with
fs = new NPOIFSFileSystem();
// Put our test files in a non-standard place
DirectoryEntry parentDir = fs.createDirectory("Parent Directory");
DirectoryEntry testDir = parentDir.createDirectory("Test Directory");
// Add to the mini stream
byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
testDir.createDocument("Mini", new ByteArrayInputStream(mini));
// Add to the main stream
byte[] main4096 = new byte[4096];
main4096[0] = -10;
main4096[4095] = -11;
testDir.createDocument("Normal4096", new ByteArrayInputStream(main4096));
// Check the mini stream was added, then the main stream
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2)); // Mini Fat
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3)); // Mini Stream
assertEquals(5, fs.getNextBlock(4)); // Main Stream
assertEquals(6, fs.getNextBlock(5));
assertEquals(7, fs.getNextBlock(6));
assertEquals(8, fs.getNextBlock(7));
assertEquals(9, fs.getNextBlock(8));
assertEquals(10, fs.getNextBlock(9));
assertEquals(11, fs.getNextBlock(10));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(11));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(12));
assertEquals(POIFSConstants.SMALLER_BIG_BLOCK_SIZE*13, fs.size());
// Check that we can read the right data pre-write
miniDoc = (DocumentEntry)testDir.getEntry("Mini");
assertContentsMatches(mini, miniDoc);
normDoc = (DocumentEntry)testDir.getEntry("Normal4096");
assertContentsMatches(main4096, normDoc);
// Write, read, check
hdr = writeOutAndReadHeader(fs);
fs = writeOutAndReadBack(fs);
// Check the header details - will have the sbat near the start,
// then the properties at the end
assertEquals(1, hdr.getBATCount());
assertEquals(0, hdr.getBATArray()[0]);
assertEquals(2, hdr.getSBATStart());
assertEquals(12, hdr.getPropertyStart());
assertEquals(POIFSConstants.END_OF_CHAIN, hdr.getXBATIndex());
// Check the block allocation is unchanged, other than
// the properties stream going in at the end
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));