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
This commit is contained in:
Dominik Stadler 2016-12-31 16:53:33 +00:00
parent 7a9f81a948
commit 8e1e6fe70e
8 changed files with 179 additions and 27 deletions

View File

@ -16,10 +16,7 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.dev; package org.apache.poi.dev;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 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 * Useful for seeing what parts are defined, and how
* they're all related to each other. * they're all related to each other.
*/ */
public class OOXMLLister { public class OOXMLLister implements Closeable {
private final OPCPackage container; private final OPCPackage container;
private final PrintStream disp; private final PrintStream disp;
@ -110,6 +107,7 @@ public class OOXMLLister {
displayRelation(rel, ""); displayRelation(rel, "");
} }
} }
private void displayRelation(PackageRelationship rel, String indent) { private void displayRelation(PackageRelationship rel, String indent) {
disp.println(indent+"Relationship:"); disp.println(indent+"Relationship:");
disp.println(indent+"\tFrom: "+ rel.getSourceURI()); disp.println(indent+"\tFrom: "+ rel.getSourceURI());
@ -118,7 +116,12 @@ public class OOXMLLister {
disp.println(indent+"\tMode: " + rel.getTargetMode()); disp.println(indent+"\tMode: " + rel.getTargetMode());
disp.println(indent+"\tType: " + rel.getRelationshipType()); disp.println(indent+"\tType: " + rel.getRelationshipType());
} }
@Override
public void close() throws IOException {
container.close();
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if(args.length == 0) { if(args.length == 0) {
System.err.println("Use:"); System.err.println("Use:");
@ -136,10 +139,14 @@ public class OOXMLLister {
OOXMLLister lister = new OOXMLLister( OOXMLLister lister = new OOXMLLister(
OPCPackage.open(f.toString(), PackageAccess.READ) OPCPackage.open(f.toString(), PackageAccess.READ)
); );
lister.disp.println(f.toString() + "\n"); try {
lister.displayParts(); lister.disp.println(f.toString() + "\n");
lister.disp.println(); lister.displayParts();
lister.displayRelations(); lister.disp.println();
lister.displayRelations();
} finally {
lister.close();
}
} }
} }

View File

@ -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();
}
}

View File

@ -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());
}
}

View File

@ -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(),
});
}
}

View File

@ -52,14 +52,16 @@ public final class VSDDumper {
} }
NPOIFSFileSystem poifs = new NPOIFSFileSystem(new File(args[0])); NPOIFSFileSystem poifs = new NPOIFSFileSystem(new File(args[0]));
HDGFDiagram hdgf = new HDGFDiagram(poifs); try {
HDGFDiagram hdgf = new HDGFDiagram(poifs);
PrintStream ps = System.out; PrintStream ps = System.out;
ps.println("Opened " + args[0]); ps.println("Opened " + args[0]);
VSDDumper vd = new VSDDumper(ps, hdgf); VSDDumper vd = new VSDDumper(ps, hdgf);
vd.dumpFile(); vd.dumpFile();
} finally {
poifs.close(); poifs.close();
}
} }
public void dumpFile() { public void dumpFile() {

View File

@ -41,20 +41,23 @@ public final class HMEFDumper {
} }
boolean truncatePropData = true; boolean truncatePropData = true;
for(int i=0; i<args.length; i++) { for (String arg : args) {
if(args[i].equalsIgnoreCase("--full")) { if (arg.equalsIgnoreCase("--full")) {
truncatePropData = false; truncatePropData = false;
continue; continue;
} }
HMEFDumper dumper = new HMEFDumper( InputStream stream = new FileInputStream(arg);
new FileInputStream(args[i]) try {
); HMEFDumper dumper = new HMEFDumper(stream);
dumper.setTruncatePropertyData(truncatePropData); dumper.setTruncatePropertyData(truncatePropData);
dumper.dump(); dumper.dump();
} finally {
stream.close();
}
} }
} }
private InputStream inp; private InputStream inp;
private boolean truncatePropertyData; private boolean truncatePropertyData;

View File

@ -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() });
}
}

View File

@ -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()
});
}
}