More NPOIFS tests, and a partial mini-stream fix

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590083 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-04-25 17:06:10 +00:00
parent 4540a3c0ab
commit 4b6ae05a04
2 changed files with 77 additions and 19 deletions

View File

@ -191,6 +191,7 @@ public class NPOIFSMiniStore extends BlockStore
if(_header.getSBATCount() == 0) { if(_header.getSBATCount() == 0) {
_header.setSBATStart(batForSBAT); _header.setSBATStart(batForSBAT);
_header.setSBATBlockCount(1); _header.setSBATBlockCount(1);
_filesystem._get_property_table().getRoot().setStartBlock(batForSBAT);
_mini_stream = new NPOIFSStream(_filesystem, batForSBAT); _mini_stream = new NPOIFSStream(_filesystem, batForSBAT);
} else { } else {
// Find the end of the SBAT stream, and add the sbat in there // Find the end of the SBAT stream, and add the sbat in there

View File

@ -673,6 +673,9 @@ public final class TestNPOIFSFileSystem {
// Check that the SBAT is empty // Check that the SBAT is empty
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock()); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock());
// Check that no properties table has been written yet
assertEquals(POIFSConstants.END_OF_CHAIN, fs._get_property_table().getStartBlock());
// Write and read it // Write and read it
fs = writeOutAndReadBack(fs); fs = writeOutAndReadBack(fs);
@ -683,6 +686,7 @@ public final class TestNPOIFSFileSystem {
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(3)); assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(3));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock()); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock());
assertEquals(2, fs._get_property_table().getStartBlock());
// Put everything within a new directory // Put everything within a new directory
@ -794,17 +798,17 @@ public final class TestNPOIFSFileSystem {
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(7)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(7));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(8)); assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(8));
} }
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock()); // Check the mini stream location was set
assertEquals(22, fs.getRoot().getProperty().getStartBlock());
// Write and read back // Write and read back
fs = writeOutAndReadBack(fs); fs = writeOutAndReadBack(fs);
// Check it's all unchanged // Check it's all unchanged
// TODO Fix it so that it is....
if (1==0) {
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0)); assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
if (1==0) {
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
if (fs.getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE) { if (fs.getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE) {
assertEquals(4, fs.getNextBlock(3)); assertEquals(4, fs.getNextBlock(3));
@ -838,8 +842,9 @@ if (1==0) {
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(7)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(7));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(8)); assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(8));
} }
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock()); }
} assertEquals(22, fs.getRoot().getProperty().getStartBlock());
// Check some data // Check some data
// TODO // TODO
@ -853,18 +858,36 @@ if (1==0) {
public void addBeforeWrite() throws Exception { public void addBeforeWrite() throws Exception {
NPOIFSFileSystem fs = new NPOIFSFileSystem(); NPOIFSFileSystem fs = new NPOIFSFileSystem();
NDocumentInputStream inp; NDocumentInputStream inp;
DocumentEntry miniDoc;
DocumentEntry normDoc;
HeaderBlock hdr;
// Initially has BAT + Properties but nothing else // Initially has BAT + Properties but nothing else
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0)); assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(2)); 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 parentDir = fs.createDirectory("Parent Directory");
DirectoryEntry testDir = parentDir.createDirectory("Test Directory"); DirectoryEntry testDir = parentDir.createDirectory("Test Directory");
// Add to the mini stream // Add to the mini stream
byte[] mini = new byte[] { 0, 1, 2, 3, 4 }; byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
testDir.createDocument("Mini", new ByteArrayInputStream(mini)); testDir.createDocument("Mini", new ByteArrayInputStream(mini));
// Add to the main stream // Add to the main stream
@ -888,14 +911,39 @@ if (1==0) {
assertEquals(11, fs.getNextBlock(10)); assertEquals(11, fs.getNextBlock(10));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(11)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(11));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(12)); 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");
inp = new NDocumentInputStream(miniDoc);
byte[] miniRead = new byte[miniDoc.getSize()];
assertEquals(miniDoc.getSize(), inp.read(miniRead));
assertThat(mini, equalTo(miniRead));
inp.close();
normDoc = (DocumentEntry)testDir.getEntry("Normal4096");
inp = new NDocumentInputStream(normDoc);
byte[] normRead = new byte[normDoc.getSize()];
assertEquals(normDoc.getSize(), inp.read(normRead));
assertThat(main4096, equalTo(normRead));
inp.close();
// Write, read, check // Write, read, check
hdr = writeOutAndReadHeader(fs);
fs = writeOutAndReadBack(fs); fs = writeOutAndReadBack(fs);
// Check it was unchanged // Check the header details - will have the sbat near the start,
// TODO Fix this // then the properties at the end
if (1==0) { 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.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
assertEquals(3, fs.getNextBlock(2)); assertEquals(3, fs.getNextBlock(2));
@ -908,8 +956,11 @@ if (1==0) {
assertEquals(10, fs.getNextBlock(9)); assertEquals(10, fs.getNextBlock(9));
assertEquals(11, fs.getNextBlock(10)); assertEquals(11, fs.getNextBlock(10));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(11)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(11));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(12)); assertEquals(13, fs.getNextBlock(12));
} assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(13));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(14));
assertEquals(POIFSConstants.SMALLER_BIG_BLOCK_SIZE*15, fs.size());
// Check the data // Check the data
DirectoryEntry fsRoot = fs.getRoot(); DirectoryEntry fsRoot = fs.getRoot();
@ -921,22 +972,28 @@ if (1==0) {
testDir = (DirectoryEntry)parentDir.getEntry("Test Directory"); testDir = (DirectoryEntry)parentDir.getEntry("Test Directory");
assertEquals(2, testDir.getEntryCount()); assertEquals(2, testDir.getEntryCount());
// TODO Fix mini stream reading miniDoc = (DocumentEntry)testDir.getEntry("Mini");
if(1==0){
DocumentEntry miniDoc = (DocumentEntry)testDir.getEntry("Mini");
inp = new NDocumentInputStream(miniDoc); inp = new NDocumentInputStream(miniDoc);
byte[] miniRead = new byte[miniDoc.getSize()]; miniRead = new byte[miniDoc.getSize()];
assertEquals(miniDoc.getSize(), inp.read(miniRead)); assertEquals(miniDoc.getSize(), inp.read(miniRead));
// TODO Fix mini stream write/read corruption
if(1==0) {
assertThat(mini, equalTo(miniRead)); assertThat(mini, equalTo(miniRead));
inp.close();
} }
inp.close();
DocumentEntry normDoc = (DocumentEntry)testDir.getEntry("Normal4096"); normDoc = (DocumentEntry)testDir.getEntry("Normal4096");
inp = new NDocumentInputStream(normDoc); inp = new NDocumentInputStream(normDoc);
byte[] normRead = new byte[normDoc.getSize()]; normRead = new byte[normDoc.getSize()];
assertEquals(normDoc.getSize(), inp.read(normRead)); assertEquals(normDoc.getSize(), inp.read(normRead));
assertThat(main4096, equalTo(normRead)); assertThat(main4096, equalTo(normRead));
inp.close(); inp.close();
// Add one more stream to each, then save and re-load
// Recheck
// TODO
} }
/** /**