aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/testcases/org/apache/poi/openxml4j/opc
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2014-12-28 09:16:57 +0000
committerDominik Stadler <centic@apache.org>2014-12-28 09:16:57 +0000
commitdec03ba45908c2116327242f2a546e2fab28409a (patch)
tree032064a4a2aa1a71ec49892e182c40b55afe4adb /src/ooxml/testcases/org/apache/poi/openxml4j/opc
parent96cfbb9cc5c59abd7e747897a8a44911d805c2a8 (diff)
downloadpoi-dec03ba45908c2116327242f2a546e2fab28409a.tar.gz
poi-dec03ba45908c2116327242f2a546e2fab28409a.zip
Add missing close() of resources in both production code and tests
Use revert() instead of close() on OCPPackage in some places to not re-write the file unnecessarily. This should now run tests without leftover file handles when checked with file leak detector and allows to find newly introduced cases more easily. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1648160 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/testcases/org/apache/poi/openxml4j/opc')
-rw-r--r--src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java168
-rw-r--r--src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java70
-rw-r--r--src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageThumbnail.java30
-rw-r--r--src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java11
4 files changed, 166 insertions, 113 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
index c42ce63d43..07a2b333d6 100644
--- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
+++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
@@ -58,12 +58,17 @@ public final class TestPackage extends TestCase {
File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageOpenSaveTMP.docx");
OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE);
- p.save(targetFile.getAbsoluteFile());
-
- // Compare the original and newly saved document
- assertTrue(targetFile.exists());
- ZipFileAssert.assertEquals(new File(originalFile), targetFile);
- assertTrue(targetFile.delete());
+ try {
+ p.save(targetFile.getAbsoluteFile());
+
+ // Compare the original and newly saved document
+ assertTrue(targetFile.exists());
+ ZipFileAssert.assertEquals(new File(originalFile), targetFile);
+ assertTrue(targetFile.delete());
+ } finally {
+ // use revert to not re-write the input file
+ p.revert();
+ }
}
/**
@@ -168,6 +173,8 @@ public final class TestPackage extends TestCase {
PackageRelationship rel =
corePart.addRelationship(sheetPartName, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet", "rSheet1");
PackagePart part = pkg.createPart(sheetPartName, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml");
+ assertNotNull(part);
+
// Dummy content again
coreOut = corePart.getOutputStream();
coreOut.write("<dummy-xml2 />".getBytes());
@@ -189,28 +196,35 @@ public final class TestPackage extends TestCase {
// Save and re-load
pkg.close();
File tmp = TempFile.createTempFile("testCreatePackageWithCoreDocument", ".zip");
- FileOutputStream fout = new FileOutputStream(tmp);
- fout.write(baos.toByteArray());
- fout.close();
+ OutputStream fout = new FileOutputStream(tmp);
+ try {
+ fout.write(baos.toByteArray());
+ } finally {
+ fout.close();
+ }
pkg = OPCPackage.open(tmp.getPath());
//tmp.delete();
- // Check still right
- coreRels = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
- assertEquals(1, coreRels.size());
- coreRel = coreRels.getRelationship(0);
-
- assertEquals("/", coreRel.getSourceURI().toString());
- assertEquals("/xl/workbook.xml", coreRel.getTargetURI().toString());
- corePart = pkg.getPart(coreRel);
- assertNotNull(corePart);
-
- PackageRelationshipCollection rels = corePart.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
- assertEquals(1, rels.size());
- rel = rels.getRelationship(0);
- assertEquals("Sheet1!A1", rel.getTargetURI().getRawFragment());
-
- assertMSCompatibility(pkg);
+ try {
+ // Check still right
+ coreRels = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
+ assertEquals(1, coreRels.size());
+ coreRel = coreRels.getRelationship(0);
+
+ assertEquals("/", coreRel.getSourceURI().toString());
+ assertEquals("/xl/workbook.xml", coreRel.getTargetURI().toString());
+ corePart = pkg.getPart(coreRel);
+ assertNotNull(corePart);
+
+ PackageRelationshipCollection rels = corePart.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
+ assertEquals(1, rels.size());
+ rel = rels.getRelationship(0);
+ assertEquals("Sheet1!A1", rel.getTargetURI().getRawFragment());
+
+ assertMSCompatibility(pkg);
+ } finally {
+ pkg.close();
+ }
}
private void assertMSCompatibility(OPCPackage pkg) throws Exception {
@@ -297,14 +311,22 @@ public final class TestPackage extends TestCase {
File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageOpenSaveTMP.docx");
OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE);
- FileOutputStream fout = new FileOutputStream(targetFile);
- p.save(fout);
- fout.close();
-
- // Compare the original and newly saved document
- assertTrue(targetFile.exists());
- ZipFileAssert.assertEquals(new File(originalFile), targetFile);
- assertTrue(targetFile.delete());
+ try {
+ FileOutputStream fout = new FileOutputStream(targetFile);
+ try {
+ p.save(fout);
+ } finally {
+ fout.close();
+ }
+
+ // Compare the original and newly saved document
+ assertTrue(targetFile.exists());
+ ZipFileAssert.assertEquals(new File(originalFile), targetFile);
+ assertTrue(targetFile.delete());
+ } finally {
+ // use revert to not re-write the input file
+ p.revert();
+ }
}
/**
@@ -511,48 +533,56 @@ public final class TestPackage extends TestCase {
String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ_WRITE);
- List<PackagePart> rs = pkg.getPartsByName(Pattern.compile("/word/.*?\\.xml"));
- HashMap<String, PackagePart> selected = new HashMap<String, PackagePart>();
-
- for(PackagePart p : rs)
- selected.put(p.getPartName().getName(), p);
-
- assertEquals(6, selected.size());
- assertTrue(selected.containsKey("/word/document.xml"));
- assertTrue(selected.containsKey("/word/fontTable.xml"));
- assertTrue(selected.containsKey("/word/settings.xml"));
- assertTrue(selected.containsKey("/word/styles.xml"));
- assertTrue(selected.containsKey("/word/theme/theme1.xml"));
- assertTrue(selected.containsKey("/word/webSettings.xml"));
+ try {
+ List<PackagePart> rs = pkg.getPartsByName(Pattern.compile("/word/.*?\\.xml"));
+ HashMap<String, PackagePart> selected = new HashMap<String, PackagePart>();
+
+ for(PackagePart p : rs)
+ selected.put(p.getPartName().getName(), p);
+
+ assertEquals(6, selected.size());
+ assertTrue(selected.containsKey("/word/document.xml"));
+ assertTrue(selected.containsKey("/word/fontTable.xml"));
+ assertTrue(selected.containsKey("/word/settings.xml"));
+ assertTrue(selected.containsKey("/word/styles.xml"));
+ assertTrue(selected.containsKey("/word/theme/theme1.xml"));
+ assertTrue(selected.containsKey("/word/webSettings.xml"));
+ } finally {
+ // use revert to not re-write the input file
+ pkg.revert();
+ }
}
public void testGetPartSize() throws Exception {
String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ);
-
- int checked = 0;
- for (PackagePart part : pkg.getParts()) {
- // Can get the size of zip parts
- if (part.getPartName().getName().equals("/word/document.xml")) {
- checked++;
- assertEquals(ZipPackagePart.class, part.getClass());
- assertEquals(6031l, part.getSize());
- }
- if (part.getPartName().getName().equals("/word/fontTable.xml")) {
- checked++;
- assertEquals(ZipPackagePart.class, part.getClass());
- assertEquals(1312l, part.getSize());
- }
-
- // But not from the others
- if (part.getPartName().getName().equals("/docProps/core.xml")) {
- checked++;
- assertEquals(PackagePropertiesPart.class, part.getClass());
- assertEquals(-1, part.getSize());
- }
+ try {
+ int checked = 0;
+ for (PackagePart part : pkg.getParts()) {
+ // Can get the size of zip parts
+ if (part.getPartName().getName().equals("/word/document.xml")) {
+ checked++;
+ assertEquals(ZipPackagePart.class, part.getClass());
+ assertEquals(6031l, part.getSize());
+ }
+ if (part.getPartName().getName().equals("/word/fontTable.xml")) {
+ checked++;
+ assertEquals(ZipPackagePart.class, part.getClass());
+ assertEquals(1312l, part.getSize());
+ }
+
+ // But not from the others
+ if (part.getPartName().getName().equals("/docProps/core.xml")) {
+ checked++;
+ assertEquals(PackagePropertiesPart.class, part.getClass());
+ assertEquals(-1, part.getSize());
+ }
+ }
+ // Ensure we actually found the parts we want to check
+ assertEquals(3, checked);
+ } finally {
+ pkg.close();
}
- // Ensure we actually found the parts we want to check
- assertEquals(3, checked);
}
public void testReplaceContentType() throws Exception {
diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java
index 0cf3efa53c..ddc36af0be 100644
--- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java
+++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java
@@ -70,37 +70,45 @@ public final class TestPackageCoreProperties extends TestCase {
// Open package
OPCPackage p = OPCPackage.open(inputPath, PackageAccess.READ_WRITE);
-
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
- df.setTimeZone(TimeZone.getTimeZone("UTC"));
- Date dateToInsert = df.parse("2007-05-12T08:00:00Z", new ParsePosition(
- 0));
-
- PackageProperties props = p.getPackageProperties();
- props.setCategoryProperty("MyCategory");
- props.setContentStatusProperty("MyContentStatus");
- props.setContentTypeProperty("MyContentType");
- props.setCreatedProperty(new Nullable<Date>(dateToInsert));
- props.setCreatorProperty("MyCreator");
- props.setDescriptionProperty("MyDescription");
- props.setIdentifierProperty("MyIdentifier");
- props.setKeywordsProperty("MyKeywords");
- props.setLanguageProperty("MyLanguage");
- props.setLastModifiedByProperty("Julien Chable");
- props.setLastPrintedProperty(new Nullable<Date>(dateToInsert));
- props.setModifiedProperty(new Nullable<Date>(dateToInsert));
- props.setRevisionProperty("2");
- props.setTitleProperty("MyTitle");
- props.setSubjectProperty("MySubject");
- props.setVersionProperty("2");
- // Save the package in the output directory
- p.save(outputFile);
-
- // Open the newly created file to check core properties saved values.
- OPCPackage p2 = OPCPackage.open(outputFile.getAbsolutePath(), PackageAccess.READ);
- compareProperties(p2);
- p2.revert();
- outputFile.delete();
+ try {
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+ df.setTimeZone(TimeZone.getTimeZone("UTC"));
+ Date dateToInsert = df.parse("2007-05-12T08:00:00Z", new ParsePosition(
+ 0));
+
+ PackageProperties props = p.getPackageProperties();
+ props.setCategoryProperty("MyCategory");
+ props.setContentStatusProperty("MyContentStatus");
+ props.setContentTypeProperty("MyContentType");
+ props.setCreatedProperty(new Nullable<Date>(dateToInsert));
+ props.setCreatorProperty("MyCreator");
+ props.setDescriptionProperty("MyDescription");
+ props.setIdentifierProperty("MyIdentifier");
+ props.setKeywordsProperty("MyKeywords");
+ props.setLanguageProperty("MyLanguage");
+ props.setLastModifiedByProperty("Julien Chable");
+ props.setLastPrintedProperty(new Nullable<Date>(dateToInsert));
+ props.setModifiedProperty(new Nullable<Date>(dateToInsert));
+ props.setRevisionProperty("2");
+ props.setTitleProperty("MyTitle");
+ props.setSubjectProperty("MySubject");
+ props.setVersionProperty("2");
+ // Save the package in the output directory
+ p.save(outputFile);
+
+ // Open the newly created file to check core properties saved values.
+ OPCPackage p2 = OPCPackage.open(outputFile.getAbsolutePath(), PackageAccess.READ);
+ try {
+ compareProperties(p2);
+ p2.revert();
+ } finally {
+ p2.close();
+ }
+ outputFile.delete();
+ } finally {
+ // use revert to not re-write the input file
+ p.revert();
+ }
}
private void compareProperties(OPCPackage p) throws InvalidFormatException {
diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageThumbnail.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageThumbnail.java
index 01a44327c7..432bd896c9 100644
--- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageThumbnail.java
+++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageThumbnail.java
@@ -42,16 +42,24 @@ public final class TestPackageThumbnail extends TestCase {
// Open package
OPCPackage p = OPCPackage.open(inputPath, PackageAccess.READ_WRITE);
- p.addThumbnail(imagePath);
- // Save the package in the output directory
- p.save(outputFile);
-
- // Open the newly created file to check core properties saved values.
- OPCPackage p2 = OPCPackage.open(outputFile.getAbsolutePath(), PackageAccess.READ);
- if (p2.getRelationshipsByType(PackageRelationshipTypes.THUMBNAIL)
- .size() == 0)
- fail("Thumbnail not added to the package !");
- p2.revert();
- outputFile.delete();
+ try {
+ p.addThumbnail(imagePath);
+ // Save the package in the output directory
+ p.save(outputFile);
+
+ // Open the newly created file to check core properties saved values.
+ OPCPackage p2 = OPCPackage.open(outputFile.getAbsolutePath(), PackageAccess.READ);
+ try {
+ if (p2.getRelationshipsByType(PackageRelationshipTypes.THUMBNAIL)
+ .size() == 0)
+ fail("Thumbnail not added to the package !");
+ } finally {
+ p2.revert();
+ p2.close();
+ }
+ } finally {
+ p.revert();
+ outputFile.delete();
+ }
}
}
diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
index cdd74c07ac..cf485ed1f6 100644
--- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
+++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
@@ -188,7 +188,11 @@ public class TestRelationships extends TestCase {
// Write out and re-load
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pkg.save(baos);
- ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+
+ // use revert to not re-write the input file
+ pkg.revert();
+
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
pkg = OPCPackage.open(bais);
// Check again
@@ -280,7 +284,6 @@ public class TestRelationships extends TestCase {
public void testTargetWithSpecialChars() throws Exception{
-
OPCPackage pkg;
String filepath = OpenXML4JTestDataSamples.getSampleFileName("50154.xlsx");
@@ -289,6 +292,10 @@ public class TestRelationships extends TestCase {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pkg.save(baos);
+
+ // use revert to not re-write the input file
+ pkg.revert();
+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
pkg = OPCPackage.open(bais);