From 8e1e6fe70ebfda1b4f75602a13d9cff8dbb5b595 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 31 Dec 2016 16:53:33 +0000 Subject: [PATCH] Cover some dev-tools with a few simple tests, close resources correctly git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1776798 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/dev/OOXMLLister.java | 29 ++++++++----- .../org/apache/poi/dev/TestOOXMLLister.java | 27 ++++++++++++ .../apache/poi/dev/TestOOXMLPrettyPrint.java | 28 +++++++++++++ .../apache/poi/dev/TestRecordGenerator.java | 41 +++++++++++++++++++ .../org/apache/poi/hdgf/dev/VSDDumper.java | 16 ++++---- .../org/apache/poi/hmef/dev/HMEFDumper.java | 21 ++++++---- .../apache/poi/hdgf/dev/TestVSDDumper.java | 14 +++++++ .../apache/poi/hmef/dev/TestHMEFDumper.java | 30 ++++++++++++++ 8 files changed, 179 insertions(+), 27 deletions(-) create mode 100644 src/ooxml/testcases/org/apache/poi/dev/TestOOXMLLister.java create mode 100644 src/ooxml/testcases/org/apache/poi/dev/TestOOXMLPrettyPrint.java create mode 100644 src/ooxml/testcases/org/apache/poi/dev/TestRecordGenerator.java create mode 100644 src/scratchpad/testcases/org/apache/poi/hdgf/dev/TestVSDDumper.java create mode 100644 src/scratchpad/testcases/org/apache/poi/hmef/dev/TestHMEFDumper.java diff --git a/src/ooxml/java/org/apache/poi/dev/OOXMLLister.java b/src/ooxml/java/org/apache/poi/dev/OOXMLLister.java index 2e5f339b00..79d7bed610 100644 --- a/src/ooxml/java/org/apache/poi/dev/OOXMLLister.java +++ b/src/ooxml/java/org/apache/poi/dev/OOXMLLister.java @@ -16,10 +16,7 @@ ==================================================================== */ 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 index 0000000000..d643d04834 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/dev/TestOOXMLLister.java @@ -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 index 0000000000..3400fb58f7 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/dev/TestOOXMLPrettyPrint.java @@ -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 index 0000000000..6d6fcde590 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/dev/TestRecordGenerator.java @@ -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 diff --git a/src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java b/src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java index 26663e50f1..3301743d52 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java @@ -52,14 +52,16 @@ public final class VSDDumper { } NPOIFSFileSystem poifs = new NPOIFSFileSystem(new File(args[0])); - HDGFDiagram hdgf = new HDGFDiagram(poifs); + try { + HDGFDiagram hdgf = new HDGFDiagram(poifs); - PrintStream ps = System.out; - ps.println("Opened " + args[0]); - VSDDumper vd = new VSDDumper(ps, hdgf); - vd.dumpFile(); - - poifs.close(); + PrintStream ps = System.out; + ps.println("Opened " + args[0]); + VSDDumper vd = new VSDDumper(ps, hdgf); + vd.dumpFile(); + } finally { + poifs.close(); + } } public void dumpFile() { diff --git a/src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java b/src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java index 3d7bcfefe6..2e64a9ab25 100644 --- a/src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java +++ b/src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java @@ -41,20 +41,23 @@ public final class HMEFDumper { } boolean truncatePropData = true; - for(int i=0; i