More NPOIFS ministream tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1688534 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-06-30 23:50:35 +00:00
parent 51af6e58b4
commit 75af780517
2 changed files with 95 additions and 3 deletions

View File

@ -34,6 +34,21 @@ public class POITestCase extends TestCase {
); );
} }
public static <T> void assertEquals(T[] expected, T[] actual)
{
assertEquals("Non-matching lengths", expected.length, actual.length);
for (int i=0; i<expected.length; i++) {
assertEquals("Mis-match at offset " + i, expected[i], actual[i]);
}
}
public static void assertEquals(byte[] expected, byte[] actual)
{
assertEquals("Non-matching lengths", expected.length, actual.length);
for (int i=0; i<expected.length; i++) {
assertEquals("Mis-match at offset " + i, expected[i], actual[i]);
}
}
public static <T> void assertContains(T needle, T[] haystack) public static <T> void assertContains(T needle, T[] haystack)
{ {
// Check // Check

View File

@ -21,15 +21,15 @@ import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Iterator; import java.util.Iterator;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.POITestCase;
import org.apache.poi.poifs.common.POIFSConstants; import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IOUtils;
/** /**
* Tests for the Mini Store in the NIO POIFS * Tests for the Mini Store in the NIO POIFS
*/ */
public final class TestNPOIFSMiniStore extends TestCase { public final class TestNPOIFSMiniStore extends POITestCase {
private static final POIDataSamples _inst = POIDataSamples.getPOIFSInstance(); private static final POIDataSamples _inst = POIDataSamples.getPOIFSInstance();
/** /**
@ -337,6 +337,8 @@ public final class TestNPOIFSMiniStore extends TestCase {
public void testCreateMiniStoreFirst() throws Exception { public void testCreateMiniStoreFirst() throws Exception {
NPOIFSFileSystem fs = new NPOIFSFileSystem(); NPOIFSFileSystem fs = new NPOIFSFileSystem();
NPOIFSMiniStore ministore = fs.getMiniStore(); NPOIFSMiniStore ministore = fs.getMiniStore();
DocumentInputStream dis;
DocumentEntry entry;
// Initially has Properties + BAT but nothing else // Initially has Properties + BAT but nothing else
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(0)); assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(0));
@ -377,7 +379,82 @@ public final class TestNPOIFSMiniStore extends TestCase {
assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(1)); assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(1));
assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(2)); assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(2));
// Check the data is unchanged and the right length
entry = (DocumentEntry)fs.getRoot().getEntry("mini");
assertEquals(data.length, entry.getSize());
byte[] rdata = new byte[data.length];
dis = new DocumentInputStream(entry);
IOUtils.readFully(dis, rdata);
assertEquals(data, rdata);
dis.close();
entry = (DocumentEntry)fs.getRoot().getEntry("mini2");
assertEquals(data.length, entry.getSize());
rdata = new byte[data.length];
dis = new DocumentInputStream(entry);
IOUtils.readFully(dis, rdata);
assertEquals(data, rdata);
dis.close();
// Done // Done
fs.close(); fs.close();
} }
public void testMultiBlockStream() throws Exception {
byte[] data1B = new byte[63];
byte[] data2B = new byte[64+14];
for (int i=0; i<data1B.length; i++) {
data1B[i] = (byte)(i+2);
}
for (int i=0; i<data2B.length; i++) {
data2B[i] = (byte)(i+4);
}
// New filesystem and store to use
NPOIFSFileSystem fs = new NPOIFSFileSystem();
NPOIFSMiniStore ministore = fs.getMiniStore();
// Initially has Properties + BAT but nothing else
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(0));
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(1));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(2));
// Store the 2 block one, should use 2 mini blocks, and request
// the use of 2 big blocks
ministore = fs.getMiniStore();
fs.getRoot().createDocument("mini2", new ByteArrayInputStream(data2B));
// Check
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(0));
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(1));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2)); // SBAT
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3)); // Mini
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(4));
assertEquals(2, ministore.getFreeBlock());
// Add one more mini-stream, and check
fs.getRoot().createDocument("mini1", new ByteArrayInputStream(data1B));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(0));
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(1));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2)); // SBAT
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3)); // Mini
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(4));
assertEquals(3, ministore.getFreeBlock());
// Check the contents too
byte[] r1 = new byte[data1B.length];
DocumentInputStream dis = fs.createDocumentInputStream("mini1");
IOUtils.readFully(dis, r1);
dis.close();
assertEquals(data1B, r1);
byte[] r2 = new byte[data2B.length];
dis = fs.createDocumentInputStream("mini2");
IOUtils.readFully(dis, r2);
dis.close();
assertEquals(data2B, r2);
}
} }