aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2016-12-06 13:52:46 +0000
committerNick Burch <nick@apache.org>2016-12-06 13:52:46 +0000
commit9bfdfff2b988d8b8a3d4c6a9d352820bb56622e8 (patch)
treef0240b71850952f578ba0580966194097ff06efe /src
parentf09674c8e292fb4efcfef3e14a9b2978e08e3f84 (diff)
downloadpoi-9bfdfff2b988d8b8a3d4c6a9d352820bb56622e8.tar.gz
poi-9bfdfff2b988d8b8a3d4c6a9d352820bb56622e8.zip
Recursive delete unit test based on users@ query - working fine already
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1772894 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
index f4a18a03d2..c600c32120 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
@@ -34,6 +34,7 @@ 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.DirectoryProperty;
import org.apache.poi.poifs.property.NPropertyTable;
import org.apache.poi.poifs.property.Property;
import org.apache.poi.poifs.property.RootProperty;
@@ -1483,4 +1484,58 @@ public final class TestNPOIFSFileSystem {
assertThat(wbDataExp, equalTo(wbDataAct));
}
+
+ /**
+ * Ensure that you can recursively delete directories and their
+ * contents
+ */
+ @Test
+ public void RecursiveDelete() throws Exception {
+ File testFile = POIDataSamples.getSpreadSheetInstance().getFile("SimpleMacro.xls");
+ NPOIFSFileSystem src = new NPOIFSFileSystem(testFile);
+
+ // Starts out with 5 entries:
+ // _VBA_PROJECT_CUR
+ // SummaryInformation <(0x05)SummaryInformation>
+ // DocumentSummaryInformation <(0x05)DocumentSummaryInformation>
+ // Workbook
+ // CompObj <(0x01)CompObj>
+ assertEquals(5, _countChildren(src._get_property_table().getRoot()));
+ assertEquals(5, src.getRoot().getEntryCount());
+
+ // Grab the VBA project root
+ DirectoryEntry vbaProj = (DirectoryEntry)src.getRoot().getEntry("_VBA_PROJECT_CUR");
+ assertEquals(3, vbaProj.getEntryCount());
+ // Can't delete yet, has stuff
+ assertEquals(false, vbaProj.delete());
+ // Recursively delete
+ _recursiveDeletee(vbaProj);
+
+ // Entries gone
+ assertEquals(4, _countChildren(src._get_property_table().getRoot()));
+ assertEquals(4, src.getRoot().getEntryCount());
+
+ // Done
+ src.close();
+ }
+ private void _recursiveDeletee(Entry entry) throws IOException {
+ if (entry.isDocumentEntry()) {
+ assertEquals(true, entry.delete());
+ return;
+ }
+
+ DirectoryEntry dir = (DirectoryEntry)entry;
+ String[] names = dir.getEntryNames().toArray(new String[dir.getEntryCount()]);
+ for (String name : names) {
+ Entry ce = dir.getEntry(name);
+ _recursiveDeletee(ce);
+ }
+ assertEquals(true, dir.delete());
+ }
+ @SuppressWarnings("unused")
+ private int _countChildren(DirectoryProperty p) {
+ int count = 0;
+ for (Property cp : p) { count++; }
+ return count;
+ }
}