]> source.dussan.org Git - poi.git/commitdiff
Cover some dev-tools with a few simple tests, close resources correctly
authorDominik Stadler <centic@apache.org>
Sat, 31 Dec 2016 16:53:33 +0000 (16:53 +0000)
committerDominik Stadler <centic@apache.org>
Sat, 31 Dec 2016 16:53:33 +0000 (16:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1776798 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/dev/OOXMLLister.java
src/ooxml/testcases/org/apache/poi/dev/TestOOXMLLister.java [new file with mode: 0644]
src/ooxml/testcases/org/apache/poi/dev/TestOOXMLPrettyPrint.java [new file with mode: 0644]
src/ooxml/testcases/org/apache/poi/dev/TestRecordGenerator.java [new file with mode: 0644]
src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java
src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java
src/scratchpad/testcases/org/apache/poi/hdgf/dev/TestVSDDumper.java [new file with mode: 0644]
src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java [new file with mode: 0644]

index 2e5f339b00af8d7bd02cff5b0d81eae3fa0943e9..79d7bed6108cb8017be7006fabf43b1d4af07d43 100644 (file)
 ==================================================================== */
 package org.apache.poi.dev;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintStream;
+import java.io.*;
 import java.util.ArrayList;
 
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@@ -34,7 +31,7 @@ import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  * Useful for seeing what parts are defined, and how
  *  they're all related to each other.
  */
-public class OOXMLLister {
+public class OOXMLLister implements Closeable {
        private final OPCPackage container;
        private final PrintStream disp;
        
@@ -110,6 +107,7 @@ public class OOXMLLister {
                        displayRelation(rel, "");
                }
        }
+
        private void displayRelation(PackageRelationship rel, String indent) {
                disp.println(indent+"Relationship:");
                disp.println(indent+"\tFrom: "+ rel.getSourceURI());
@@ -118,7 +116,12 @@ public class OOXMLLister {
                disp.println(indent+"\tMode: " + rel.getTargetMode());
                disp.println(indent+"\tType: " + rel.getRelationshipType());
        }
-       
+
+       @Override
+       public void close() throws IOException {
+               container.close();
+       }
+
        public static void main(String[] args) throws Exception {
                if(args.length == 0) {
                        System.err.println("Use:");
@@ -136,10 +139,14 @@ public class OOXMLLister {
                OOXMLLister lister = new OOXMLLister(
                                OPCPackage.open(f.toString(), PackageAccess.READ)
                );
-               
-               lister.disp.println(f.toString() + "\n");
-               lister.displayParts();
-               lister.disp.println();
-               lister.displayRelations();
+
+               try {
+                       lister.disp.println(f.toString() + "\n");
+                       lister.displayParts();
+                       lister.disp.println();
+                       lister.displayRelations();
+               } finally {
+                       lister.close();
+               }
        }
 }
diff --git a/src/ooxml/testcases/org/apache/poi/dev/TestOOXMLLister.java b/src/ooxml/testcases/org/apache/poi/dev/TestOOXMLLister.java
new file mode 100644 (file)
index 0000000..d643d04
--- /dev/null
@@ -0,0 +1,27 @@
+package org.apache.poi.dev;
+
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackageAccess;
+import org.apache.poi.util.NullOutputStream;
+import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.PrintStream;
+
+public class TestOOXMLLister {
+    @Test
+    public void testMain() throws Exception {
+        File file = XSSFTestDataSamples.getSampleFile("Formatting.xlsx");
+        OOXMLLister.main(new String[] {file.getAbsolutePath()});
+    }
+
+    @Test
+    public void testWithPrintStream() throws Exception {
+        File file = XSSFTestDataSamples.getSampleFile("Formatting.xlsx");
+        OOXMLLister lister = new OOXMLLister(OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ), new PrintStream(new NullOutputStream()));
+        lister.displayParts();
+        lister.displayRelations();
+        lister.close();
+    }
+}
diff --git a/src/ooxml/testcases/org/apache/poi/dev/TestOOXMLPrettyPrint.java b/src/ooxml/testcases/org/apache/poi/dev/TestOOXMLPrettyPrint.java
new file mode 100644 (file)
index 0000000..3400fb5
--- /dev/null
@@ -0,0 +1,28 @@
+package org.apache.poi.dev;
+
+import org.apache.poi.util.TempFile;
+import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.junit.Test;
+
+import java.io.File;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class TestOOXMLPrettyPrint {
+    @Test
+    public void testMain() throws Exception {
+        File file = XSSFTestDataSamples.getSampleFile("Formatting.xlsx");
+        File outFile = TempFile.createTempFile("Formatting", "-pretty.xlsx");
+
+        assertTrue(outFile.delete());
+        assertFalse(outFile.exists());
+
+        OOXMLPrettyPrint.main(new String[] {
+            file.getAbsolutePath(), outFile.getAbsolutePath()
+        });
+
+        assertTrue(outFile.exists());
+        assertTrue(outFile.delete());
+    }
+}
\ No newline at end of file
diff --git a/src/ooxml/testcases/org/apache/poi/dev/TestRecordGenerator.java b/src/ooxml/testcases/org/apache/poi/dev/TestRecordGenerator.java
new file mode 100644 (file)
index 0000000..6d6fcde
--- /dev/null
@@ -0,0 +1,41 @@
+package org.apache.poi.dev;
+
+import org.apache.poi.util.TempFile;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.io.File;
+
+public class TestRecordGenerator {
+    @Ignore("Could not get this to run, probably the dev-application does not work any more at all")
+    @Test
+    public void testNotEnoughArgs() throws Exception {
+        RecordGenerator.main(new String[] {});
+    }
+
+    @Ignore("Could not get this to run, probably the dev-application does not work any more at all")
+    @Test
+    public void testMainRecords() throws Exception {
+        File dir = TempFile.createTempDirectory("TestRecordGenerator");
+
+        RecordGenerator.main(new String[] {
+                "src/records/definitions/",
+                "src/records/styles/",
+                dir.getAbsolutePath(),
+                dir.getAbsolutePath(),
+        });
+    }
+
+    @Ignore("Could not get this to run, probably the dev-application does not work any more at all")
+    @Test
+    public void testMainTypes() throws Exception {
+        File dir = TempFile.createTempDirectory("TestRecordGenerator");
+
+        RecordGenerator.main(new String[] {
+                "src/types/definitions/",
+                "src/types/styles/",
+                dir.getAbsolutePath(),
+                dir.getAbsolutePath(),
+        });
+    }
+}
\ No newline at end of file
index 26663e50f1f6185a5d653b9d69cb14602a93a494..3301743d52a23ff46838b3e917907201461a2010 100644 (file)
@@ -52,14 +52,16 @@ public final class VSDDumper {
                }
 
                NPOIFSFileSystem poifs = new NPOIFSFileSystem(new File(args[0]));
-               HDGFDiagram hdgf = new HDGFDiagram(poifs);
-
-               PrintStream ps = System.out;
-               ps.println("Opened " + args[0]);
-               VSDDumper vd = new VSDDumper(ps, hdgf);
-               vd.dumpFile();
-               
-               poifs.close();
+               try {
+                       HDGFDiagram hdgf = new HDGFDiagram(poifs);
+
+                       PrintStream ps = System.out;
+                       ps.println("Opened " + args[0]);
+                       VSDDumper vd = new VSDDumper(ps, hdgf);
+                       vd.dumpFile();
+               } finally {
+                       poifs.close();
+               }
        }
 
        public void dumpFile() {
index 3d7bcfefe68f2e582b20b03dc18a7e3cbe7adc1f..2e64a9ab25ef4e6cdc9d24800d9b9ad192b13db4 100644 (file)
@@ -41,20 +41,23 @@ public final class HMEFDumper {
       }
       
       boolean truncatePropData = true;
-      for(int i=0; i<args.length; i++) {
-         if(args[i].equalsIgnoreCase("--full")) {
+      for (String arg : args) {
+         if (arg.equalsIgnoreCase("--full")) {
             truncatePropData = false;
             continue;
          }
-         
-         HMEFDumper dumper = new HMEFDumper(
-               new FileInputStream(args[i])
-         );
-         dumper.setTruncatePropertyData(truncatePropData);
-         dumper.dump();
+
+         InputStream stream = new FileInputStream(arg);
+         try {
+            HMEFDumper dumper = new HMEFDumper(stream);
+            dumper.setTruncatePropertyData(truncatePropData);
+            dumper.dump();
+         } finally {
+            stream.close();
+         }
       }
    }
-   
+
    private InputStream inp;
    private boolean truncatePropertyData;
    
diff --git a/src/scratchpad/testcases/org/apache/poi/hdgf/dev/TestVSDDumper.java b/src/scratchpad/testcases/org/apache/poi/hdgf/dev/TestVSDDumper.java
new file mode 100644 (file)
index 0000000..f8dc7c3
--- /dev/null
@@ -0,0 +1,14 @@
+package org.apache.poi.hdgf.dev;
+
+import org.apache.poi.POIDataSamples;
+import org.junit.Test;
+
+import java.io.File;
+
+public class TestVSDDumper {
+    @Test
+    public void main() throws Exception {
+        File file = POIDataSamples.getDiagramInstance().getFile("Test_Visio-Some_Random_Text.vsd");
+        VSDDumper.main(new String[] { file.getAbsolutePath() });
+    }
+}
\ No newline at end of file
diff --git a/src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java b/src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java
new file mode 100644 (file)
index 0000000..27efcff
--- /dev/null
@@ -0,0 +1,30 @@
+package org.apache.poi.hmef.dev;
+
+import org.apache.poi.POIDataSamples;
+import org.junit.Test;
+
+import java.io.File;
+
+public class TestHMEFDumper {
+    @Test(expected = IllegalArgumentException.class)
+    public void noArguments() throws Exception {
+        HMEFDumper.main(new String[] {});
+    }
+
+    @Test
+    public void main() throws Exception {
+        File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat");
+        HMEFDumper.main(new String[] {
+                file.getAbsolutePath()
+        });
+    }
+
+    @Test
+    public void mainFull() throws Exception {
+        File file = POIDataSamples.getHMEFInstance().getFile("quick-winmail.dat");
+        HMEFDumper.main(new String[] {
+                "--full",
+                file.getAbsolutePath()
+        });
+    }
+}
\ No newline at end of file