Unit tests for POIFS EntryUtils

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1207333 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-11-28 15:54:51 +00:00
parent c63f86cf66
commit bf477712be
2 changed files with 139 additions and 7 deletions

View File

@ -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;

View File

@ -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
}
}