]> source.dussan.org Git - poi.git/commitdiff
More NPOIFS ministream tests
authorNick Burch <nick@apache.org>
Tue, 30 Jun 2015 23:50:35 +0000 (23:50 +0000)
committerNick Burch <nick@apache.org>
Tue, 30 Jun 2015 23:50:35 +0000 (23:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1688534 13f79535-47bb-0310-9956-ffa450edef68

src/testcases/org/apache/poi/POITestCase.java
src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java

index 7a1a0274c786621a8a32860bc4f150c441217fb3..e38e46d4fe57d17fd3f703e30cd86965fc0c4649 100644 (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)
     {
        // Check
index f420683c379039b9d51ac75963d20c11d35d605d..44e066a6f7b3036f231beaa7b5f5c7e07479faa7 100644 (file)
@@ -21,15 +21,15 @@ import java.io.ByteArrayInputStream;
 import java.nio.ByteBuffer;
 import java.util.Iterator;
 
-import junit.framework.TestCase;
-
 import org.apache.poi.POIDataSamples;
+import org.apache.poi.POITestCase;
 import org.apache.poi.poifs.common.POIFSConstants;
+import org.apache.poi.util.IOUtils;
 
 /**
  * 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();
    
    /**
@@ -337,6 +337,8 @@ public final class TestNPOIFSMiniStore extends TestCase {
    public void testCreateMiniStoreFirst() throws Exception {
        NPOIFSFileSystem fs = new NPOIFSFileSystem();
        NPOIFSMiniStore ministore = fs.getMiniStore();
+       DocumentInputStream dis;
+       DocumentEntry entry;
 
        // Initially has Properties + BAT but nothing else
        assertEquals(POIFSConstants.END_OF_CHAIN,     fs.getNextBlock(0));
@@ -376,8 +378,83 @@ public final class TestNPOIFSMiniStore extends TestCase {
        assertEquals(POIFSConstants.END_OF_CHAIN,    ministore.getNextBlock(0));
        assertEquals(POIFSConstants.END_OF_CHAIN,    ministore.getNextBlock(1));
        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
        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);
+   }
 }