poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1207333 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/EntryUtils.java testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java
Date Mon, 28 Nov 2011 15:54:52 GMT
Author: nick
Date: Mon Nov 28 15:54:51 2011
New Revision: 1207333

URL: http://svn.apache.org/viewvc?rev=1207333&view=rev
Log:
Unit tests for POIFS EntryUtils

Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java
    poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java?rev=1207333&r1=1207332&r2=1207333&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/EntryUtils.java Mon Nov 28 15:54:51
2011
@@ -104,11 +104,16 @@ public class EntryUtils
      * Checks to see if the two Directories hold the same contents.
      * For this to be true, they must have entries with the same names,
      *  no entries in one but not the other, and the size+contents
-     *  of each entry must match.
+     *  of each entry must match, and they must share names
      * TODO Some sort of excepts support
      */
-    public static boolean areDirectoriesIdentical(DirectoryNode dirA, DirectoryNode dirB)
{
-       // First up, check they have the same number of children
+    public static boolean areDirectoriesIdentical(DirectoryEntry dirA, DirectoryEntry dirB)
{
+       // First, check names
+       if (! dirA.getName().equals(dirB.getName())) {
+          return false;
+       }
+       
+       // Next up, check they have the same number of children
        if (dirA.getEntryCount() != dirB.getEntryCount()) {
           return false;
        }
@@ -181,7 +186,7 @@ public class EntryUtils
      *  and the same contents. (Their parent directories are
      *  not checked)
      */
-    public static boolean areDocumentsIdentical(DocumentNode docA, DocumentNode docB) throws
IOException {
+    public static boolean areDocumentsIdentical(DocumentEntry docA, DocumentEntry docB) throws
IOException {
        if (! docA.getName().equals(docB.getName())) {
           // Names don't match, not the same
           return false;

Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java?rev=1207333&r1=1207332&r2=1207333&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestEntryUtils.java Mon Nov 28
15:54:51 2011
@@ -17,22 +17,149 @@
 
 package org.apache.poi.poifs.filesystem;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+
 import junit.framework.TestCase;
 
 import org.apache.poi.POIDataSamples;
 
 public class TestEntryUtils extends TestCase {
     private static final POIDataSamples dataSamples = POIDataSamples.getPOIFSInstance();
+    private byte[] dataSmallA = new byte[] { 12, 42, 11, -12, -121 };
+    private byte[] dataSmallB = new byte[] { 11, 73, 21, -92, -103 };
 
     public void testCopyRecursively() throws Exception {
-       // TODO
+       POIFSFileSystem fsD = new POIFSFileSystem();
+       POIFSFileSystem fs = new POIFSFileSystem();
+       DirectoryEntry dirA = fs.createDirectory("DirA");
+       DirectoryEntry dirB = fs.createDirectory("DirB");
+       
+       DocumentEntry entryR = fs.createDocument(new ByteArrayInputStream(dataSmallA), "EntryRoot");
+       DocumentEntry entryA1 = dirA.createDocument("EntryA1", new ByteArrayInputStream(dataSmallA));
+       DocumentEntry entryA2 = dirA.createDocument("EntryA2", new ByteArrayInputStream(dataSmallB));
+       
+       // Copy docs
+       assertEquals(0, fsD.getRoot().getEntryCount());
+       EntryUtils.copyNodeRecursively(entryR, fsD.getRoot());
+       
+       assertEquals(1, fsD.getRoot().getEntryCount());
+       assertNotNull(fsD.getRoot().getEntry("EntryRoot"));
+       
+       EntryUtils.copyNodeRecursively(entryA1, fsD.getRoot());
+       assertEquals(2, fsD.getRoot().getEntryCount());
+       assertNotNull(fsD.getRoot().getEntry("EntryRoot"));
+       assertNotNull(fsD.getRoot().getEntry("EntryA1"));
+       
+       EntryUtils.copyNodeRecursively(entryA2, fsD.getRoot());
+       assertEquals(3, fsD.getRoot().getEntryCount());
+       assertNotNull(fsD.getRoot().getEntry("EntryRoot"));
+       assertNotNull(fsD.getRoot().getEntry("EntryA1"));
+       assertNotNull(fsD.getRoot().getEntry("EntryA2"));
+       
+       // Copy directories
+       fsD = new POIFSFileSystem();
+       assertEquals(0, fsD.getRoot().getEntryCount());
+       
+       EntryUtils.copyNodeRecursively(dirB, fsD.getRoot());
+       assertEquals(1, fsD.getRoot().getEntryCount());
+       assertNotNull(fsD.getRoot().getEntry("DirB"));
+       assertEquals(0, ((DirectoryEntry)fsD.getRoot().getEntry("DirB")).getEntryCount());
+       
+       EntryUtils.copyNodeRecursively(dirA, fsD.getRoot());
+       assertEquals(2, fsD.getRoot().getEntryCount());
+       assertNotNull(fsD.getRoot().getEntry("DirB"));
+       assertEquals(0, ((DirectoryEntry)fsD.getRoot().getEntry("DirB")).getEntryCount());
+       assertNotNull(fsD.getRoot().getEntry("DirA"));
+       assertEquals(2, ((DirectoryEntry)fsD.getRoot().getEntry("DirA")).getEntryCount());
+       
+       // Copy the whole lot
+       fsD = new POIFSFileSystem();
+       assertEquals(0, fsD.getRoot().getEntryCount());
+       
+       EntryUtils.copyNodes(fs, fsD, new ArrayList<String>());
+       assertEquals(3, fsD.getRoot().getEntryCount());
+       assertNotNull(fsD.getRoot().getEntry(dirA.getName()));
+       assertNotNull(fsD.getRoot().getEntry(dirB.getName()));
+       assertNotNull(fsD.getRoot().getEntry(entryR.getName()));
+       assertEquals(0, ((DirectoryEntry)fsD.getRoot().getEntry("DirB")).getEntryCount());
+       assertEquals(2, ((DirectoryEntry)fsD.getRoot().getEntry("DirA")).getEntryCount());
     }
 
     public void testAreDocumentsIdentical() throws Exception {
-       // TODO
+       POIFSFileSystem fs = new POIFSFileSystem();
+       DirectoryEntry dirA = fs.createDirectory("DirA");
+       DirectoryEntry dirB = fs.createDirectory("DirB");
+       
+       DocumentEntry entryA1 = dirA.createDocument("Entry1", new ByteArrayInputStream(dataSmallA));
+       DocumentEntry entryA1b = dirA.createDocument("Entry1b", new ByteArrayInputStream(dataSmallA));
+       DocumentEntry entryA2 = dirA.createDocument("Entry2", new ByteArrayInputStream(dataSmallB));
+       DocumentEntry entryB1 = dirB.createDocument("Entry1", new ByteArrayInputStream(dataSmallA));
+       
+       
+       // Names must match
+       assertEquals(false, entryA1.getName().equals(entryA1b.getName()));
+       assertEquals(false, EntryUtils.areDocumentsIdentical(entryA1, entryA1b));
+       
+       // Contents must match
+       assertEquals(false, EntryUtils.areDocumentsIdentical(entryA1, entryA2));
+       
+       // Parents don't matter if contents + names are the same
+       assertEquals(false, entryA1.getParent().equals(entryB1.getParent()));
+       assertEquals(true, EntryUtils.areDocumentsIdentical(entryA1, entryB1));
+       
+       
+       // Can work with NPOIFS + POIFS
+       ByteArrayOutputStream tmpO = new ByteArrayOutputStream();
+       fs.writeFilesystem(tmpO);
+       ByteArrayInputStream tmpI = new ByteArrayInputStream(tmpO.toByteArray());
+       NPOIFSFileSystem nfs = new NPOIFSFileSystem(tmpI);
+       
+       DirectoryEntry dN1 = (DirectoryEntry)nfs.getRoot().getEntry("DirA");
+       DirectoryEntry dN2 = (DirectoryEntry)nfs.getRoot().getEntry("DirB");
+       DocumentEntry eNA1 = (DocumentEntry)dN1.getEntry(entryA1.getName());
+       DocumentEntry eNA2 = (DocumentEntry)dN1.getEntry(entryA2.getName());
+       DocumentEntry eNB1 = (DocumentEntry)dN2.getEntry(entryB1.getName());
+       
+       assertEquals(false, EntryUtils.areDocumentsIdentical(eNA1, eNA2));
+       assertEquals(true, EntryUtils.areDocumentsIdentical(eNA1, eNB1));
+       
+       assertEquals(false, EntryUtils.areDocumentsIdentical(eNA1, entryA1b));
+       assertEquals(false, EntryUtils.areDocumentsIdentical(eNA1, entryA2));
+       
+       assertEquals(true, EntryUtils.areDocumentsIdentical(eNA1, entryA1));
+       assertEquals(true, EntryUtils.areDocumentsIdentical(eNA1, entryB1));
     }
 
     public void testAreDirectoriesIdentical() throws Exception {
-       // TODO
+       POIFSFileSystem fs = new POIFSFileSystem();
+       DirectoryEntry dirA = fs.createDirectory("DirA");
+       DirectoryEntry dirB = fs.createDirectory("DirB");
+       
+       // Names must match
+       assertEquals(false, EntryUtils.areDirectoriesIdentical(dirA, dirB));
+       
+       // Empty dirs are fine
+       DirectoryEntry dirA1 = dirA.createDirectory("TheDir"); 
+       DirectoryEntry dirB1 = dirB.createDirectory("TheDir");
+       assertEquals(0, dirA1.getEntryCount());
+       assertEquals(0, dirB1.getEntryCount());
+       assertEquals(true, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));
+       
+       // Otherwise children must match
+       dirA1.createDocument("Entry1", new ByteArrayInputStream(dataSmallA));
+       assertEquals(false, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));
+       
+       dirB1.createDocument("Entry1", new ByteArrayInputStream(dataSmallA));
+       assertEquals(true, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));
+       
+       dirA1.createDirectory("DD");
+       assertEquals(false, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));
+       dirB1.createDirectory("DD");
+       assertEquals(true, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));
+       
+       
+       // TODO Excludes support
     }
 }



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


Mime
View raw message