poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1053791 [9/18] - in /poi/branches/NIO_32_BRANCH: ./ src/contrib/src/org/apache/poi/contrib/poibrowser/ src/java/org/apache/poi/ src/java/org/apache/poi/hpsf/ src/java/org/apache/poi/hpsf/extractor/ src/java/org/apache/poi/hssf/record/ src/...
Date Thu, 30 Dec 2010 02:35:11 GMT
Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDirectoryNode.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDirectoryNode.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDirectoryNode.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDirectoryNode.java Thu Dec 30 02:35:06 2010
@@ -15,7 +15,6 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.poifs.filesystem;
 
@@ -33,31 +32,12 @@ import org.apache.poi.poifs.property.Doc
  *
  * @author Marc Johnson
  */
-
-public class TestDirectoryNode
-    extends TestCase
-{
-
-    /**
-     * Constructor TestDirectoryNode
-     *
-     * @param name
-     */
-
-    public TestDirectoryNode(String name)
-    {
-        super(name);
-    }
+public final class TestDirectoryNode extends TestCase {
 
     /**
      * test trivial constructor (a DirectoryNode with no children)
-     *
-     * @exception IOException
      */
-
-    public void testEmptyConstructor()
-        throws IOException
-    {
+    public void testEmptyConstructor() {
         POIFSFileSystem   fs        = new POIFSFileSystem();
         DirectoryProperty property1 = new DirectoryProperty("parent");
         DirectoryProperty property2 = new DirectoryProperty("child");
@@ -113,13 +93,8 @@ public class TestDirectoryNode
 
     /**
      * test non-trivial constructor (a DirectoryNode with children)
-     *
-     * @exception IOException
      */
-
-    public void testNonEmptyConstructor()
-        throws IOException
-    {
+    public void testNonEmptyConstructor() throws IOException {
         DirectoryProperty property1 = new DirectoryProperty("parent");
         DirectoryProperty property2 = new DirectoryProperty("child1");
 
@@ -177,49 +152,49 @@ public class TestDirectoryNode
 
     /**
      * test deletion methods
-     *
-     * @exception IOException
      */
-
-    public void testDeletion()
-        throws IOException
-    {
+    public void testDeletion() throws IOException {
         POIFSFileSystem fs   = new POIFSFileSystem();
         DirectoryEntry  root = fs.getRoot();
 
         // verify cannot delete the root directory
-        assertTrue(!root.delete());
+        assertFalse(root.delete());
+        assertTrue(root.isEmpty());
+        
         DirectoryEntry dir = fs.createDirectory("myDir");
 
-        assertTrue(!root.isEmpty());
+        assertFalse(root.isEmpty());
+        assertTrue(dir.isEmpty());
 
         // verify can delete empty directory
+        assertFalse(root.delete());
         assertTrue(dir.delete());
+        
+        // Now look at a non-empty one
         dir = fs.createDirectory("NextDir");
         DocumentEntry doc =
             dir.createDocument("foo",
                                new ByteArrayInputStream(new byte[ 1 ]));
 
-        assertTrue(!dir.isEmpty());
+        assertFalse(root.isEmpty());
+        assertFalse(dir.isEmpty());
 
-        // verify cannot delete empty directory
-        assertTrue(!dir.delete());
+        // verify cannot delete non-empty directory
+        assertFalse(dir.delete());
+        
+        // but we can delete it if we remove the document
         assertTrue(doc.delete());
-
-        // verify now we can delete it
+        assertTrue(dir.isEmpty());
         assertTrue(dir.delete());
+        
+        // It's really gone!
         assertTrue(root.isEmpty());
     }
 
     /**
      * test change name methods
-     *
-     * @exception IOException
      */
-
-    public void testRename()
-        throws IOException
-    {
+    public void testRename() throws IOException {
         POIFSFileSystem fs   = new POIFSFileSystem();
         DirectoryEntry  root = fs.getRoot();
 
@@ -237,17 +212,4 @@ public class TestDirectoryNode
         assertTrue(dir2.renameTo("foo"));
         assertEquals("foo", dir2.getName());
     }
-
-    /**
-     * main method to run the unit tests
-     *
-     * @param ignored_args
-     */
-
-    public static void main(String [] ignored_args)
-    {
-        System.out
-            .println("Testing org.apache.poi.poifs.filesystem.DirectoryNode");
-        junit.textui.TestRunner.run(TestDirectoryNode.class);
-    }
 }

Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocument.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,18 +14,15 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.poifs.filesystem;
 
-import java.io.*;
-
-import java.util.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 
-import junit.framework.*;
+import junit.framework.TestCase;
 
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.util.LittleEndianConsts;
 import org.apache.poi.poifs.property.DocumentProperty;
 import org.apache.poi.poifs.storage.RawDataBlock;
 import org.apache.poi.poifs.storage.SmallDocumentBlock;
@@ -36,31 +32,12 @@ import org.apache.poi.poifs.storage.Smal
  *
  * @author Marc Johnson
  */
-
-public class TestDocument
-    extends TestCase
-{
-
-    /**
-     * Constructor TestDocument
-     *
-     * @param name
-     */
-
-    public TestDocument(String name)
-    {
-        super(name);
-    }
+public final class TestDocument extends TestCase {
 
     /**
      * Integration test -- really about all we can do
-     *
-     * @exception IOException
      */
-
-    public void testPOIFSDocument()
-        throws IOException
-    {
+    public void testPOIFSDocument() throws IOException {
 
         // verify correct number of blocks get created for document
         // that is exact multituple of block size
@@ -158,10 +135,8 @@ public class TestDocument
         }
     }
 
-    private POIFSDocument makeCopy(POIFSDocument document, byte [] input,
-                                   byte [] data)
-        throws IOException
-    {
+    private static POIFSDocument makeCopy(POIFSDocument document, byte[] input, byte[] data)
+            throws IOException {
         POIFSDocument copy = null;
 
         if (input.length >= 4096)
@@ -194,10 +169,8 @@ public class TestDocument
         return copy;
     }
 
-    private void checkDocument(final POIFSDocument document,
-                               final byte [] input)
-        throws IOException
-    {
+    private static void checkDocument(final POIFSDocument document, final byte[] input)
+            throws IOException {
         int big_blocks   = 0;
         int small_blocks = 0;
         int total_output = 0;
@@ -221,11 +194,8 @@ public class TestDocument
                 input)), input);
     }
 
-    private byte [] checkValues(int big_blocks, int small_blocks,
-                                int total_output, POIFSDocument document,
-                                byte [] input)
-        throws IOException
-    {
+    private static byte[] checkValues(int big_blocks, int small_blocks, int total_output,
+            POIFSDocument document, byte[] input) throws IOException {
         assertEquals(document, document.getDocumentProperty().getDocument());
         int increment = ( int ) Math.sqrt(input.length);
 
@@ -267,17 +237,4 @@ public class TestDocument
         }
         return output;
     }
-
-    /**
-     * main method to run the unit tests
-     *
-     * @param ignored_args
-     */
-
-    public static void main(String [] ignored_args)
-    {
-        System.out
-            .println("Testing org.apache.poi.poifs.filesystem.POIFSDocument");
-        junit.textui.TestRunner.run(TestDocument.class);
-    }
 }

Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentDescriptor.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentDescriptor.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentDescriptor.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentDescriptor.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,53 +14,30 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.poifs.filesystem;
 
-import junit.framework.*;
+import junit.framework.TestCase;
 
 /**
  * Class to test DocumentDescriptor functionality
  *
  * @author Marc Johnson
  */
-
-public class TestDocumentDescriptor
-    extends TestCase
-{
-
-    /**
-     * Constructor TestDocumentDescriptor
-     *
-     * @param name
-     */
-
-    public TestDocumentDescriptor(String name)
-    {
-        super(name);
-    }
+public final class TestDocumentDescriptor extends TestCase {
 
     /**
      * test equality
      */
-
-    public void testEquality()
-    {
-        String[]            names =
-        {
-            "c1", "c2", "c3", "c4", "c5"
-        };
+    public void testEquality() {
+        String[] names = { "c1", "c2", "c3", "c4", "c5" };
         POIFSDocumentPath   a1    = new POIFSDocumentPath();
         POIFSDocumentPath   a2    = new POIFSDocumentPath(null);
         POIFSDocumentPath   a3    = new POIFSDocumentPath(new String[ 0 ]);
         POIFSDocumentPath   a4    = new POIFSDocumentPath(a1, null);
         POIFSDocumentPath   a5    = new POIFSDocumentPath(a1,
                                         new String[ 0 ]);
-        POIFSDocumentPath[] paths =
-        {
-            a1, a2, a3, a4, a5
-        };
+        POIFSDocumentPath[] paths = { a1, a2, a3, a4, a5 };
 
         for (int j = 0; j < paths.length; j++)
         {
@@ -196,17 +172,4 @@ public class TestDocumentDescriptor
             }
         }
     }
-
-    /**
-     * main method to run the unit tests
-     *
-     * @param ignored_args
-     */
-
-    public static void main(String [] ignored_args)
-    {
-        System.out.println(
-            "Testing org.apache.poi.poifs.eventfilesystem.DocumentDescriptor");
-        junit.textui.TestRunner.run(TestDocumentDescriptor.class);
-    }
 }

Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,18 +14,16 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.poifs.filesystem;
 
-import java.io.*;
-
-import java.util.*;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Arrays;
 
-import junit.framework.*;
+import junit.framework.TestCase;
 
 import org.apache.poi.poifs.property.DirectoryProperty;
-import org.apache.poi.poifs.property.DocumentProperty;
 import org.apache.poi.poifs.storage.RawDataBlock;
 
 /**
@@ -35,22 +32,9 @@ import org.apache.poi.poifs.storage.RawD
  * @author Marc Johnson
  */
 
-public class TestDocumentInputStream
-    extends TestCase
-{
+public final class TestDocumentInputStream extends TestCase {
 
-    /**
-     * Constructor TestDocumentInputStream
-     *
-     * @param name
-     *
-     * @exception IOException
-     */
-
-    public TestDocumentInputStream(String name)
-        throws IOException
-    {
-        super(name);
+	protected void setUp() throws Exception {
         int blocks = (_workbook_size + 511) / 512;
 
         _workbook_data = new byte[ 512 * blocks ];
@@ -73,7 +57,7 @@ public class TestDocumentInputStream
         _workbook = new DocumentNode(
             document.getDocumentProperty(),
             new DirectoryNode(
-                new DirectoryProperty("Root Entry"), null, null));
+                new DirectoryProperty("Root Entry"), (POIFSFileSystem)null, null));
     }
 
     private DocumentNode     _workbook;
@@ -86,13 +70,8 @@ public class TestDocumentInputStream
 
     /**
      * test constructor
-     *
-     * @exception IOException
      */
-
-    public void testConstructor()
-        throws IOException
-    {
+    public void testConstructor() throws IOException {
         DocumentInputStream stream = new DocumentInputStream(_workbook);
 
         assertEquals(_workbook_size, stream.available());
@@ -100,13 +79,8 @@ public class TestDocumentInputStream
 
     /**
      * test available() behavior
-     *
-     * @exception IOException
      */
-
-    public void testAvailable()
-        throws IOException
-    {
+    public void testAvailable() throws IOException {
         DocumentInputStream stream = new DocumentInputStream(_workbook);
 
         assertEquals(_workbook_size, stream.available());
@@ -115,9 +89,7 @@ public class TestDocumentInputStream
         {
             stream.available();
             fail("Should have caught IOException");
-        }
-        catch (IOException ignored)
-        {
+        } catch (IllegalStateException ignored) {
 
             // as expected
         }
@@ -125,13 +97,8 @@ public class TestDocumentInputStream
 
     /**
      * test mark/reset/markSupported.
-     *
-     * @exception IOException
      */
-
-    public void testMarkFunctions()
-        throws IOException
-    {
+    public void testMarkFunctions() throws IOException {
         DocumentInputStream stream = new DocumentInputStream(_workbook);
         byte[]              buffer = new byte[ _workbook_size / 5 ];
 
@@ -169,13 +136,8 @@ public class TestDocumentInputStream
 
     /**
      * test simple read method
-     *
-     * @exception IOException
      */
-
-    public void testReadSingleByte()
-        throws IOException
-    {
+    public void testReadSingleByte() throws IOException {
         DocumentInputStream stream    = new DocumentInputStream(_workbook);
         int                 remaining = _workbook_size;
 
@@ -205,13 +167,8 @@ public class TestDocumentInputStream
 
     /**
      * Test buffered read
-     *
-     * @exception IOException
      */
-
-    public void testBufferRead()
-        throws IOException
-    {
+    public void testBufferRead() throws IOException {
         DocumentInputStream stream = new DocumentInputStream(_workbook);
 
         try
@@ -275,23 +232,14 @@ public class TestDocumentInputStream
 
     /**
      * Test complex buffered read
-     *
-     * @exception IOException
      */
-
-    public void testComplexBufferRead()
-        throws IOException
-    {
+    public void testComplexBufferRead() throws IOException {
         DocumentInputStream stream = new DocumentInputStream(_workbook);
 
-        try
-        {
+        try {
             stream.read(null, 0, 1);
             fail("Should have caught NullPointerException");
-        }
-        catch (NullPointerException ignored)
-        {
-
+        } catch (IllegalArgumentException ignored) {
             // as expected
         }
 
@@ -391,13 +339,8 @@ public class TestDocumentInputStream
 
     /**
      * test skip
-     *
-     * @exception IOException
      */
-
-    public void testSkip()
-        throws IOException
-    {
+    public void testSkip() throws IOException {
         DocumentInputStream stream = new DocumentInputStream(_workbook);
 
         assertEquals(_workbook_size, stream.available());
@@ -422,17 +365,4 @@ public class TestDocumentInputStream
                      stream.skip(2 + ( long ) Integer.MAX_VALUE));
         assertEquals(0, stream.available());
     }
-
-    /**
-     * main method to run the unit tests
-     *
-     * @param ignored_args
-     */
-
-    public static void main(String [] ignored_args)
-    {
-        System.out.println(
-            "Testing org.apache.poi.poifs.filesystem.DocumentInputStream");
-        junit.textui.TestRunner.run(TestDocumentInputStream.class);
-    }
 }

Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentNode.java Thu Dec 30 02:35:06 2010
@@ -15,7 +15,6 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.poifs.filesystem;
 
@@ -32,31 +31,12 @@ import org.apache.poi.poifs.storage.RawD
  *
  * @author Marc Johnson
  */
-
-public class TestDocumentNode
-    extends TestCase
-{
-
-    /**
-     * Constructor TestDocumentNode
-     *
-     * @param name
-     */
-
-    public TestDocumentNode(String name)
-    {
-        super(name);
-    }
+public final class TestDocumentNode extends TestCase {
 
     /**
      * test constructor
-     *
-     * @exception IOException
      */
-
-    public void testConstructor()
-        throws IOException
-    {
+    public void testConstructor() throws IOException {
         DirectoryProperty    property1 = new DirectoryProperty("directory");
         RawDataBlock[]       rawBlocks = new RawDataBlock[ 4 ];
         ByteArrayInputStream stream    =
@@ -69,7 +49,7 @@ public class TestDocumentNode
         POIFSDocument    document  = new POIFSDocument("document", rawBlocks,
                                          2000);
         DocumentProperty property2 = document.getDocumentProperty();
-        DirectoryNode    parent    = new DirectoryNode(property1, null, null);
+        DirectoryNode    parent    = new DirectoryNode(property1, (POIFSFileSystem)null, null);
         DocumentNode     node      = new DocumentNode(property2, parent);
 
         // verify we can retrieve the document
@@ -90,17 +70,4 @@ public class TestDocumentNode
         // verify getParent behaves correctly
         assertEquals(parent, node.getParent());
     }
-
-    /**
-     * main method to run the unit tests
-     *
-     * @param ignored_args
-     */
-
-    public static void main(String [] ignored_args)
-    {
-        System.out
-            .println("Testing org.apache.poi.poifs.filesystem.DocumentNode");
-        junit.textui.TestRunner.run(TestDocumentNode.class);
-    }
 }

Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java Thu Dec 30 02:35:06 2010
@@ -15,53 +15,26 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.poifs.filesystem;
 
-import java.io.*;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
 
-import java.util.*;
-
-import junit.framework.*;
-
-import org.apache.poi.poifs.property.DirectoryProperty;
-import org.apache.poi.poifs.property.DocumentProperty;
-import org.apache.poi.poifs.storage.RawDataBlock;
+import junit.framework.TestCase;
 
 /**
  * Class to test DocumentOutputStream functionality
  *
  * @author Marc Johnson
  */
-
-public class TestDocumentOutputStream
-    extends TestCase
-{
-
-    /**
-     * Constructor TestDocumentOutputStream
-     *
-     * @param name
-     *
-     * @exception IOException
-     */
-
-    public TestDocumentOutputStream(String name)
-        throws IOException
-    {
-        super(name);
-    }
+public final class TestDocumentOutputStream extends TestCase {
 
     /**
      * test write(int) behavior
-     *
-     * @exception IOException
      */
-
-    public void testWrite1()
-        throws IOException
-    {
+    public void testWrite1() throws IOException {
         ByteArrayOutputStream stream  = new ByteArrayOutputStream();
         DocumentOutputStream  dstream = new DocumentOutputStream(stream, 25);
 
@@ -89,13 +62,8 @@ public class TestDocumentOutputStream
 
     /**
      * test write(byte[]) behavior
-     *
-     * @exception IOException
      */
-
-    public void testWrite2()
-        throws IOException
-    {
+    public void testWrite2() throws IOException {
         ByteArrayOutputStream stream  = new ByteArrayOutputStream();
         DocumentOutputStream  dstream = new DocumentOutputStream(stream, 25);
 
@@ -133,13 +101,8 @@ public class TestDocumentOutputStream
 
     /**
      * test write(byte[], int, int) behavior
-     *
-     * @exception IOException
      */
-
-    public void testWrite3()
-        throws IOException
-    {
+    public void testWrite3() throws IOException {
         ByteArrayOutputStream stream  = new ByteArrayOutputStream();
         DocumentOutputStream  dstream = new DocumentOutputStream(stream, 25);
         byte[]                array   = new byte[ 50 ];
@@ -169,13 +132,8 @@ public class TestDocumentOutputStream
 
     /**
      * test writeFiller()
-     *
-     * @exception IOException
      */
-
-    public void testWriteFiller()
-        throws IOException
-    {
+    public void testWriteFiller() throws IOException {
         ByteArrayOutputStream stream  = new ByteArrayOutputStream();
         DocumentOutputStream  dstream = new DocumentOutputStream(stream, 25);
 
@@ -205,17 +163,4 @@ public class TestDocumentOutputStream
         }
         stream.close();
     }
-
-    /**
-     * main method to run the unit tests
-     *
-     * @param ignored_args
-     */
-
-    public static void main(String [] ignored_args)
-    {
-        System.out.println(
-            "Testing org.apache.poi.poifs.filesystem.DocumentOutputStream");
-        junit.textui.TestRunner.run(TestDocumentOutputStream.class);
-    }
 }

Modified: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java (original)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java Thu Dec 30 02:35:06 2010
@@ -30,143 +30,88 @@ import org.apache.poi.poifs.filesystem.P
 import org.apache.poi.poifs.filesystem.POIFSWriterListener;
 import org.apache.poi.poifs.filesystem.DirectoryEntry;
 
-public class TestEmptyDocument extends TestCase {
+public final class TestEmptyDocument extends TestCase {
 
-  public static void main(String[] args) {
-    TestEmptyDocument driver = new TestEmptyDocument();
-
-    System.out.println();
-    System.out.println("As only file...");
-    System.out.println();
-
-    System.out.print("Trying using createDocument(String,InputStream): ");
-    try {
-      driver.testSingleEmptyDocument();
-      System.out.println("Worked!");
-    } catch (IOException exception) {
-      System.out.println("failed! ");
-      System.out.println(exception.toString());
-    }
-    System.out.println();
-
-    System.out.print
-      ("Trying using createDocument(String,int,POIFSWriterListener): ");
-    try {
-      driver.testSingleEmptyDocumentEvent();
-      System.out.println("Worked!");
-    } catch (IOException exception) {
-      System.out.println("failed!");
-      System.out.println(exception.toString());
-    }
-    System.out.println();
-
-    System.out.println();
-    System.out.println("After another file...");
-    System.out.println();
-
-    System.out.print("Trying using createDocument(String,InputStream): ");
-    try {
-      driver.testEmptyDocumentWithFriend();
-      System.out.println("Worked!");
-    } catch (IOException exception) {
-      System.out.println("failed! ");
-      System.out.println(exception.toString());
-    }
-    System.out.println();
-
-    System.out.print
-      ("Trying using createDocument(String,int,POIFSWriterListener): ");
-    try {
-      driver.testEmptyDocumentWithFriend();
-      System.out.println("Worked!");
-    } catch (IOException exception) {
-      System.out.println("failed!");
-      System.out.println(exception.toString());
-    }
-    System.out.println();
-  }
-
-  public void testSingleEmptyDocument() throws IOException {
-    POIFSFileSystem fs = new POIFSFileSystem();
-    DirectoryEntry dir = fs.getRoot();
-    dir.createDocument("Foo", new ByteArrayInputStream(new byte[] { }));
-    
-    ByteArrayOutputStream out = new ByteArrayOutputStream();
-    fs.writeFilesystem(out);
-    new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
-  }
-
-  public void testSingleEmptyDocumentEvent() throws IOException {
-    POIFSFileSystem fs = new POIFSFileSystem();
-    DirectoryEntry dir = fs.getRoot();
-    dir.createDocument("Foo", 0, new POIFSWriterListener() {
-      public void processPOIFSWriterEvent(POIFSWriterEvent event) {
-      	System.out.println("written");
-      }
-    });
-    
-    ByteArrayOutputStream out = new ByteArrayOutputStream();
-    fs.writeFilesystem(out);
-    new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
-  }
-
-  public void testEmptyDocumentWithFriend() throws IOException {
-    POIFSFileSystem fs = new POIFSFileSystem();
-    DirectoryEntry dir = fs.getRoot();
-    dir.createDocument("Bar", new ByteArrayInputStream(new byte[] { 0 }));
-    dir.createDocument("Foo", new ByteArrayInputStream(new byte[] { }));
-    
-    ByteArrayOutputStream out = new ByteArrayOutputStream();
-    fs.writeFilesystem(out);
-    new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
-  }
-
-  public void testEmptyDocumentEventWithFriend() throws IOException {
-    POIFSFileSystem fs = new POIFSFileSystem();
-    DirectoryEntry dir = fs.getRoot();
-    dir.createDocument("Bar", 1, new POIFSWriterListener() {
-      public void processPOIFSWriterEvent(POIFSWriterEvent event) {
-        try {
-          event.getStream().write(0);
-        } catch (IOException exception) {
-          throw new RuntimeException("exception on write: " + exception);
-        }
-      }
-    });
-    dir.createDocument("Foo", 0, new POIFSWriterListener() {
-      public void processPOIFSWriterEvent(POIFSWriterEvent event) {
-      }
-    });
-    
-    ByteArrayOutputStream out = new ByteArrayOutputStream();
-    fs.writeFilesystem(out);
-    new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
-  }
-
-  public void testEmptyDocumentBug11744() throws Exception {
-        byte[] testData = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
-
-        POIFSFileSystem fs = new POIFSFileSystem();
-        fs.createDocument(new ByteArrayInputStream(new byte[0]), "Empty");
-        fs.createDocument(new ByteArrayInputStream(testData), "NotEmpty");
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        fs.writeFilesystem(out);
-        out.toByteArray();
-
-        // This line caused the error.
-        fs = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
-
-        DocumentEntry entry = (DocumentEntry) fs.getRoot().getEntry("Empty");
-        assertEquals("Expected zero size", 0, entry.getSize());
-        byte[] actualReadbackData;
-        actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry));
-        assertEquals("Expected zero read from stream", 0,
-                     actualReadbackData.length);
-
-        entry = (DocumentEntry) fs.getRoot().getEntry("NotEmpty");
-        actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry));
-        assertEquals("Expected size was wrong", testData.length, entry.getSize());
-        assertTrue("Expected different data read from stream", 
-                Arrays.equals(testData, actualReadbackData));
-    }
+	public void testSingleEmptyDocument() throws IOException {
+		POIFSFileSystem fs = new POIFSFileSystem();
+		DirectoryEntry dir = fs.getRoot();
+		dir.createDocument("Foo", new ByteArrayInputStream(new byte[] {}));
+
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		fs.writeFilesystem(out);
+		new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
+	}
+
+	public void testSingleEmptyDocumentEvent() throws IOException {
+		POIFSFileSystem fs = new POIFSFileSystem();
+		DirectoryEntry dir = fs.getRoot();
+		dir.createDocument("Foo", 0, new POIFSWriterListener() {
+			public void processPOIFSWriterEvent(POIFSWriterEvent event) {
+				System.out.println("written");
+			}
+		});
+
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		fs.writeFilesystem(out);
+		new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
+	}
+
+	public void testEmptyDocumentWithFriend() throws IOException {
+		POIFSFileSystem fs = new POIFSFileSystem();
+		DirectoryEntry dir = fs.getRoot();
+		dir.createDocument("Bar", new ByteArrayInputStream(new byte[] { 0 }));
+		dir.createDocument("Foo", new ByteArrayInputStream(new byte[] {}));
+
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		fs.writeFilesystem(out);
+		new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
+	}
+
+	public void testEmptyDocumentEventWithFriend() throws IOException {
+		POIFSFileSystem fs = new POIFSFileSystem();
+		DirectoryEntry dir = fs.getRoot();
+		dir.createDocument("Bar", 1, new POIFSWriterListener() {
+			public void processPOIFSWriterEvent(POIFSWriterEvent event) {
+				try {
+					event.getStream().write(0);
+				} catch (IOException exception) {
+					throw new RuntimeException("exception on write: " + exception);
+				}
+			}
+		});
+		dir.createDocument("Foo", 0, new POIFSWriterListener() {
+			public void processPOIFSWriterEvent(POIFSWriterEvent event) {
+			}
+		});
+
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		fs.writeFilesystem(out);
+		new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
+	}
+
+	public void testEmptyDocumentBug11744() throws Exception {
+		byte[] testData = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+
+		POIFSFileSystem fs = new POIFSFileSystem();
+		fs.createDocument(new ByteArrayInputStream(new byte[0]), "Empty");
+		fs.createDocument(new ByteArrayInputStream(testData), "NotEmpty");
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		fs.writeFilesystem(out);
+		out.toByteArray();
+
+		// This line caused the error.
+		fs = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
+
+		DocumentEntry entry = (DocumentEntry) fs.getRoot().getEntry("Empty");
+		assertEquals("Expected zero size", 0, entry.getSize());
+		byte[] actualReadbackData;
+		actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry));
+		assertEquals("Expected zero read from stream", 0, actualReadbackData.length);
+
+		entry = (DocumentEntry) fs.getRoot().getEntry("NotEmpty");
+		actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry));
+		assertEquals("Expected size was wrong", testData.length, entry.getSize());
+		assertTrue("Expected different data read from stream", Arrays.equals(testData,
+				actualReadbackData));
+	}
 }

Added: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java (added)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,475 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.poifs.filesystem;
+
+import java.nio.ByteBuffer;
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.hpsf.PropertySet;
+import org.apache.poi.hpsf.PropertySetFactory;
+import org.apache.poi.hpsf.SummaryInformation;
+import org.apache.poi.poifs.common.POIFSConstants;
+import org.apache.poi.poifs.property.NPropertyTable;
+import org.apache.poi.poifs.property.Property;
+import org.apache.poi.poifs.property.RootProperty;
+
+/**
+ * Tests for the new NIO POIFSFileSystem implementation
+ */
+public final class TestNPOIFSFileSystem extends TestCase {
+   private static final POIDataSamples _inst = POIDataSamples.getPOIFSInstance();
+
+   public void testBasicOpen() throws Exception {
+      NPOIFSFileSystem fsA, fsB;
+      
+      // With a simple 512 block file
+      fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) {
+         assertEquals(512, fs.getBigBlockSize());
+      }
+      
+      // Now with a simple 4096 block file
+      fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
+      fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) {
+         assertEquals(4096, fs.getBigBlockSize());
+      }
+   }
+
+   public void testPropertiesAndFatOnRead() throws Exception {
+      NPOIFSFileSystem fsA, fsB;
+      
+      // With a simple 512 block file
+      fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) {
+         // Check the FAT was properly processed:
+         // Verify we only got one block
+         fs.getBATBlockAndIndex(0);
+         fs.getBATBlockAndIndex(1);
+         try {
+            fs.getBATBlockAndIndex(140);
+            fail("Should only be one BAT, but a 2nd was found");
+         } catch(IndexOutOfBoundsException e) {}
+         
+         // Verify a few next offsets
+         // 97 -> 98 -> END
+         assertEquals(98, fs.getNextBlock(97));
+         assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(98));
+         
+         
+         // Check the properties
+         NPropertyTable props = fs._get_property_table();
+         assertEquals(90, props.getStartBlock());
+         assertEquals(7, props.countBlocks());
+         
+         // Root property tells us about the Mini Stream
+         RootProperty root = props.getRoot();
+         assertEquals("Root Entry", root.getName());
+         assertEquals(11564, root.getSize());
+         assertEquals(0, root.getStartBlock());
+         
+         // Check its children too
+         Property prop;
+         Iterator<Property> pi = root.getChildren();
+         prop = pi.next();
+         assertEquals("Thumbnail", prop.getName());
+         prop = pi.next();
+         assertEquals("\u0005DocumentSummaryInformation", prop.getName());
+         prop = pi.next();
+         assertEquals("\u0005SummaryInformation", prop.getName());
+         prop = pi.next();
+         assertEquals("Image", prop.getName());
+         prop = pi.next();
+         assertEquals("Tags", prop.getName());
+         assertEquals(false, pi.hasNext());
+         
+         
+         // Check the SBAT (Small Blocks FAT) was properly processed
+         NPOIFSMiniStore ministore = fs.getMiniStore();
+         
+         // Verify we only got two SBAT blocks
+         ministore.getBATBlockAndIndex(0);
+         ministore.getBATBlockAndIndex(128);
+         try {
+            ministore.getBATBlockAndIndex(256);
+            fail("Should only be two SBATs, but a 3rd was found");
+         } catch(IndexOutOfBoundsException e) {}
+         
+         // Verify a few offsets: 0->50 is a stream
+         for(int i=0; i<50; i++) {
+            assertEquals(i+1, ministore.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(50));
+      }
+      
+      // Now with a simple 4096 block file
+      fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
+      fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) {
+         // Check the FAT was properly processed
+         // Verify we only got one block
+         fs.getBATBlockAndIndex(0);
+         fs.getBATBlockAndIndex(1);
+         try {
+            fs.getBATBlockAndIndex(1040);
+            fail("Should only be one BAT, but a 2nd was found");
+         } catch(IndexOutOfBoundsException e) {}
+         
+         // Verify a few next offsets
+         // 0 -> 1 -> 2 -> END
+         assertEquals(1, fs.getNextBlock(0));
+         assertEquals(2, fs.getNextBlock(1));
+         assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
+
+         
+         // Check the properties
+         NPropertyTable props = fs._get_property_table();
+         assertEquals(12, props.getStartBlock());
+         assertEquals(1, props.countBlocks());
+         
+         // Root property tells us about the Mini Stream
+         RootProperty root = props.getRoot();
+         assertEquals("Root Entry", root.getName());
+         assertEquals(11564, root.getSize());
+         assertEquals(0, root.getStartBlock());
+         
+         // Check its children too
+         Property prop;
+         Iterator<Property> pi = root.getChildren();
+         prop = pi.next();
+         assertEquals("Thumbnail", prop.getName());
+         prop = pi.next();
+         assertEquals("\u0005DocumentSummaryInformation", prop.getName());
+         prop = pi.next();
+         assertEquals("\u0005SummaryInformation", prop.getName());
+         prop = pi.next();
+         assertEquals("Image", prop.getName());
+         prop = pi.next();
+         assertEquals("Tags", prop.getName());
+         assertEquals(false, pi.hasNext());
+         
+         
+         // Check the SBAT (Small Blocks FAT) was properly processed
+         NPOIFSMiniStore ministore = fs.getMiniStore();
+         
+         // Verify we only got one SBAT block
+         ministore.getBATBlockAndIndex(0);
+         ministore.getBATBlockAndIndex(128);
+         ministore.getBATBlockAndIndex(1023);
+         try {
+            ministore.getBATBlockAndIndex(1024);
+            fail("Should only be one SBAT, but a 2nd was found");
+         } catch(IndexOutOfBoundsException e) {}
+         
+         // Verify a few offsets: 0->50 is a stream
+         for(int i=0; i<50; i++) {
+            assertEquals(i+1, ministore.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(50));
+      }
+   }
+   
+   /**
+    * Check that for a given block, we can correctly figure
+    *  out what the next one is
+    */
+   public void testNextBlock() throws Exception {
+      NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) {
+         // 0 -> 21 are simple
+         for(int i=0; i<21; i++) {
+            assertEquals(i+1, fs.getNextBlock(i));
+         }
+         // 21 jumps to 89, then ends
+         assertEquals(89, fs.getNextBlock(21));
+         assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(89));
+         
+         // 22 -> 88 simple sequential stream
+         for(int i=22; i<88; i++) {
+            assertEquals(i+1, fs.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(88));
+         
+         // 90 -> 96 is another stream
+         for(int i=90; i<96; i++) {
+            assertEquals(i+1, fs.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(96));
+         
+         // 97+98 is another
+         assertEquals(98, fs.getNextBlock(97));
+         assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(98));
+         
+         // 99 is our FAT block
+         assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(99));
+         
+         // 100 onwards is free
+         for(int i=100; i<fs.getBigBlockSizeDetails().getBATEntriesPerBlock(); i++) {
+            assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(i));
+         }
+      }
+      
+      // Quick check on 4096 byte blocks too
+      fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
+      fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) {
+         // 0 -> 1 -> 2 -> end
+         assertEquals(1, fs.getNextBlock(0));
+         assertEquals(2, fs.getNextBlock(1));
+         assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
+         
+         // 4 -> 11 then end
+         for(int i=4; i<11; i++) {
+            assertEquals(i+1, fs.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(11));
+      }
+   }
+
+   /**
+    * Check we get the right data back for each block
+    */
+   public void testGetBlock() throws Exception {
+      NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) {
+         ByteBuffer b;
+         
+         // The 0th block is the first data block
+         b = fs.getBlockAt(0);
+         assertEquals((byte)0x9e, b.get());
+         assertEquals((byte)0x75, b.get());
+         assertEquals((byte)0x97, b.get());
+         assertEquals((byte)0xf6, b.get());
+         
+         // And the next block
+         b = fs.getBlockAt(1);
+         assertEquals((byte)0x86, b.get());
+         assertEquals((byte)0x09, b.get());
+         assertEquals((byte)0x22, b.get());
+         assertEquals((byte)0xfb, b.get());
+         
+         // Check the final block too
+         b = fs.getBlockAt(99);
+         assertEquals((byte)0x01, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x02, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+      }
+      
+      // Quick check on 4096 byte blocks too
+      fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
+      fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) {
+         ByteBuffer b;
+         
+         // The 0th block is the first data block
+         b = fs.getBlockAt(0);
+         assertEquals((byte)0x9e, b.get());
+         assertEquals((byte)0x75, b.get());
+         assertEquals((byte)0x97, b.get());
+         assertEquals((byte)0xf6, b.get());
+         
+         // And the next block
+         b = fs.getBlockAt(1);
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x03, b.get());
+         assertEquals((byte)0x00, b.get());
+
+         // The 14th block is the FAT
+         b = fs.getBlockAt(14);
+         assertEquals((byte)0x01, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x02, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+      }
+   }
+   
+   /**
+    * Ask for free blocks where there are some already
+    *  to be had from the FAT
+    */
+   public void testGetFreeBlockWithSpare() throws Exception {
+      NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      
+      // Our first BAT block has spares
+      assertEquals(true, fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+      
+      // First free one is 100
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(100));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(101));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(102));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(103));
+      
+      // Ask, will get 100
+      assertEquals(100, fs.getFreeBlock());
+      
+      // Ask again, will still get 100 as not written to
+      assertEquals(100, fs.getFreeBlock());
+      
+      // Allocate it, then ask again
+      fs.setNextBlock(100, POIFSConstants.END_OF_CHAIN);
+      assertEquals(101, fs.getFreeBlock());
+   }
+
+   /**
+    * Ask for free blocks where no free ones exist, and so the
+    *  file needs to be extended and another BAT/XBAT added
+    */
+   public void testGetFreeBlockWithNoneSpare() throws Exception {
+      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
+      for(int i=100; i<128; i++) {
+         assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(i));
+      }
+      
+      // Check our BAT knows it's free
+      assertEquals(true, fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+      
+      // Allocate all the spare ones
+      for(int i=100; i<128; i++) {
+         fs.setNextBlock(i, POIFSConstants.END_OF_CHAIN);
+      }
+      
+      // BAT is now full, but there's only the one
+      assertEquals(false, fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+      try {
+         assertEquals(false, fs.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
+         fail("Should only be one BAT");
+      } catch(IndexOutOfBoundsException e) {}
+      
+      // Now ask for a free one, will need to extend the file
+      assertEquals(129, fs.getFreeBlock());
+      
+      assertEquals(false, fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+      assertEquals(true, fs.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
+      assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(128));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(129));
+      
+      
+      // Fill up to hold 109 BAT blocks
+      // TODO
+      
+      // Ask for another, will get our first XBAT
+      // TODO
+      
+      // Fill the XBAT
+      // TODO
+      
+      // Ask for another, will get our 2nd XBAT
+      // TODO
+      
+      // Write it out and read it back in again
+      // Ensure it's correct
+      // TODO
+   }
+   
+   /**
+    * Test that we can correctly get the list of directory
+    *  entries, and the details on the files in them
+    */
+   public void testListEntries() throws Exception {
+      NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
+      NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD}) {
+         DirectoryEntry root = fs.getRoot();
+         assertEquals(5, root.getEntryCount());
+         
+         // Check by the names
+         Entry thumbnail = root.getEntry("Thumbnail");
+         Entry dsi = root.getEntry("\u0005DocumentSummaryInformation");
+         Entry si = root.getEntry("\u0005SummaryInformation");
+         Entry image = root.getEntry("Image");
+         Entry tags = root.getEntry("Tags");
+         
+         assertEquals(false, thumbnail.isDirectoryEntry());
+         assertEquals(false, dsi.isDirectoryEntry());
+         assertEquals(false, si.isDirectoryEntry());
+         assertEquals(true, image.isDirectoryEntry());
+         assertEquals(false, tags.isDirectoryEntry());
+         
+         // Check via the iterator
+         Iterator<Entry> it = root.getEntries();
+         assertEquals(thumbnail.getName(), it.next().getName());
+         assertEquals(dsi.getName(), it.next().getName());
+         assertEquals(si.getName(), it.next().getName());
+         assertEquals(image.getName(), it.next().getName());
+         assertEquals(tags.getName(), it.next().getName());
+         
+         // Look inside another
+         DirectoryEntry imageD = (DirectoryEntry)image;
+         assertEquals(7, imageD.getEntryCount());
+      }
+   }
+   
+   /**
+    * Tests that we can get the correct contents for
+    *  a document in the filesystem 
+    */
+   public void testGetDocumentEntry() throws Exception {
+      NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
+      NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD}) {
+         DirectoryEntry root = fs.getRoot();
+         Entry si = root.getEntry("\u0005SummaryInformation");
+         
+         assertEquals(true, si.isDocumentEntry());
+         DocumentNode doc = (DocumentNode)si;
+         
+         // Check we can read it
+         NDocumentInputStream inp = new NDocumentInputStream(doc);
+         byte[] contents = new byte[doc.getSize()];
+         assertEquals(doc.getSize(), inp.read(contents));
+         
+         // Now try to build the property set
+         inp = new NDocumentInputStream(doc);
+         PropertySet ps = PropertySetFactory.create(inp);
+         SummaryInformation inf = (SummaryInformation)ps;
+         
+         // Check some bits in it
+         assertEquals(null, inf.getApplicationName());
+         assertEquals(null, inf.getAuthor());
+         assertEquals(null, inf.getSubject());
+      }
+   }
+   
+   // TODO Directory/Document write tests
+}

Added: poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java (added)
+++ poi/branches/NIO_32_BRANCH/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,251 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.poifs.filesystem;
+
+import java.nio.ByteBuffer;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.poifs.common.POIFSConstants;
+
+/**
+ * Tests for the Mini Store in the NIO POIFS
+ */
+public final class TestNPOIFSMiniStore extends TestCase {
+   private static final POIDataSamples _inst = POIDataSamples.getPOIFSInstance();
+   
+   /**
+    * Check that for a given mini block, we can correctly figure
+    *  out what the next one is
+    */
+   public void testNextBlock() throws Exception {
+      // It's the same on 512 byte and 4096 byte block files!
+      NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
+      NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD}) {
+         NPOIFSMiniStore ministore = fs.getMiniStore();
+         
+         // 0 -> 51 is one stream
+         for(int i=0; i<50; i++) {
+            assertEquals(i+1, ministore.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(50));
+         
+         // 51 -> 103 is the next
+         for(int i=51; i<103; i++) {
+            assertEquals(i+1, ministore.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(103));
+         
+         // Then there are 3 one block ones
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(104));
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(105));
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(106));
+         
+         // 107 -> 154 is the next
+         for(int i=107; i<154; i++) {
+            assertEquals(i+1, ministore.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(154));
+
+         // 155 -> 160 is the next
+         for(int i=155; i<160; i++) {
+            assertEquals(i+1, ministore.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(160));
+         
+         // 161 -> 166 is the next
+         for(int i=161; i<166; i++) {
+            assertEquals(i+1, ministore.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(166));
+         
+         // 167 -> 172 is the next
+         for(int i=167; i<172; i++) {
+            assertEquals(i+1, ministore.getNextBlock(i));
+         }
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(172));
+         
+         // Now some short ones
+         assertEquals(174                        , ministore.getNextBlock(173));
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(174));
+         
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(175));
+         
+         assertEquals(177                        , ministore.getNextBlock(176));
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(177));
+         
+         assertEquals(179                        , ministore.getNextBlock(178));
+         assertEquals(180                        , ministore.getNextBlock(179));
+         assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(180));
+         
+         // 181 onwards is free
+         for(int i=181; i<fs.getBigBlockSizeDetails().getBATEntriesPerBlock(); i++) {
+            assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(i));
+         }
+      }
+   }
+
+   /**
+    * Check we get the right data back for each block
+    */
+   public void testGetBlock() throws Exception {
+      // It's the same on 512 byte and 4096 byte block files!
+      NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
+      NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
+      for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD}) {
+         // Mini stream should be at big block zero
+         assertEquals(0, fs._get_property_table().getRoot().getStartBlock());
+         
+         // Grab the ministore
+         NPOIFSMiniStore ministore = fs.getMiniStore();
+         ByteBuffer b;
+         
+         // Runs from the start of the data section in 64 byte chungs
+         b = ministore.getBlockAt(0);
+         assertEquals((byte)0x9e, b.get());
+         assertEquals((byte)0x75, b.get());
+         assertEquals((byte)0x97, b.get());
+         assertEquals((byte)0xf6, b.get());
+         assertEquals((byte)0xff, b.get());
+         assertEquals((byte)0x21, b.get());
+         assertEquals((byte)0xd2, b.get());
+         assertEquals((byte)0x11, b.get());
+         
+         // And the next block
+         b = ministore.getBlockAt(1);
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x03, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x12, b.get());
+         assertEquals((byte)0x02, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         
+         // Check the last data block
+         b = ministore.getBlockAt(180);
+         assertEquals((byte)0x30, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x00, b.get());
+         assertEquals((byte)0x80, b.get());
+         
+         // And the rest until the end of the big block is zeros
+         for(int i=181; i<184; i++) {
+            b = ministore.getBlockAt(i);
+            assertEquals((byte)0, b.get());
+            assertEquals((byte)0, b.get());
+            assertEquals((byte)0, b.get());
+            assertEquals((byte)0, b.get());
+            assertEquals((byte)0, b.get());
+            assertEquals((byte)0, b.get());
+            assertEquals((byte)0, b.get());
+            assertEquals((byte)0, b.get());
+         }
+      }
+   }
+   
+   /**
+    * Ask for free blocks where there are some already
+    *  to be had from the SFAT
+    */
+   public void testGetFreeBlockWithSpare() throws Exception {
+      NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
+      NPOIFSMiniStore ministore = fs.getMiniStore();
+      
+      // Our 2nd SBAT block has spares
+      assertEquals(false, ministore.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+      assertEquals(true,  ministore.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
+      
+      // First free one at 181
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(181));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(182));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(183));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(184));
+      
+      // Ask, will get 181
+      assertEquals(181, ministore.getFreeBlock());
+      
+      // Ask again, will still get 181 as not written to
+      assertEquals(181, ministore.getFreeBlock());
+      
+      // Allocate it, then ask again
+      ministore.setNextBlock(181, POIFSConstants.END_OF_CHAIN);
+      assertEquals(182, ministore.getFreeBlock());
+   }
+
+   /**
+    * Ask for free blocks where no free ones exist, and so the
+    *  stream needs to be extended and another SBAT added
+    */
+   public void testGetFreeBlockWithNoneSpare() throws Exception {
+      NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      NPOIFSMiniStore ministore = fs.getMiniStore();
+      
+      // We've spare ones from 181 to 255
+      for(int i=181; i<256; i++) {
+         assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(i));
+      }
+      
+      // Check our SBAT free stuff is correct
+      assertEquals(false, ministore.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+      assertEquals(true,  ministore.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
+      
+      // Allocate all the spare ones
+      for(int i=181; i<256; i++) {
+         ministore.setNextBlock(i, POIFSConstants.END_OF_CHAIN);
+      }
+      
+      // SBAT are now full, but there's only the two
+      assertEquals(false, ministore.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+      assertEquals(false, ministore.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
+      try {
+         assertEquals(false, ministore.getBATBlockAndIndex(256).getBlock().hasFreeSectors());
+         fail("Should only be two SBATs");
+      } catch(IndexOutOfBoundsException e) {}
+      
+      // Now ask for a free one, will need to extend the SBAT chain
+      assertEquals(256, ministore.getFreeBlock());
+      
+      assertEquals(false, ministore.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
+      assertEquals(false, ministore.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
+      assertEquals(true, ministore.getBATBlockAndIndex(256).getBlock().hasFreeSectors());
+      assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(254)); // 2nd SBAT 
+      assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(255)); // 2nd SBAT
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(256)); // 3rd SBAT
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(257)); // 3rd SBAT
+   }
+   
+   /**
+    * Test that we will extend the underlying chain of 
+    *  big blocks that make up the ministream as needed
+    */
+   public void testCreateBlockIfNeeded() throws Exception {
+      // TODO Add underlying implementation
+      // TODO Add unit test
+   }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message