You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2016/09/10 19:33:32 UTC
svn commit: r1760206 - in /poi/trunk/src/ooxml:
java/org/apache/poi/POIXMLDocument.java
testcases/org/apache/poi/TestPOIXMLDocument.java
Author: onealj
Date: Sat Sep 10 19:33:32 2016
New Revision: 1760206
URL: http://svn.apache.org/viewvc?rev=1760206&view=rev
Log:
bug 60102: throw IOException when writing a closed document
Modified:
poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocument.java
poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java
Modified: poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocument.java?rev=1760206&r1=1760205&r2=1760206&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocument.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/POIXMLDocument.java Sat Sep 10 19:33:32 2016
@@ -40,6 +40,7 @@ import org.apache.xmlbeans.impl.common.S
/**
* This holds the common functionality for all POI OOXML Document classes.
*/
+// TODO: implements AutoCloseable in Java 7+ when POI drops support for Java 6.
public abstract class POIXMLDocument extends POIXMLDocumentPart implements Closeable {
public static final String DOCUMENT_CREATOR = "Apache POI";
@@ -230,6 +231,11 @@ public abstract class POIXMLDocument ext
*/
@SuppressWarnings("resource")
public final void write(OutputStream stream) throws IOException {
+ OPCPackage p = getPackage();
+ if(p == null) {
+ throw new IOException("Cannot write data, document seems to have been closed already");
+ }
+
//force all children to commit their changes into the underlying OOXML Package
// TODO Shouldn't they be committing to the new one instead?
Set<PackagePart> context = new HashSet<PackagePart>();
@@ -239,10 +245,6 @@ public abstract class POIXMLDocument ext
//save extended and custom properties
getProperties().commit();
- OPCPackage p = getPackage();
- if(p == null) {
- throw new IOException("Cannot write data, document seems to have been closed already");
- }
p.save(stream);
}
}
Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java?rev=1760206&r1=1760205&r2=1760206&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java Sat Sep 10 19:33:32 2016
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertFal
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileOutputStream;
@@ -33,9 +34,11 @@ import java.util.HashSet;
import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
+import org.apache.poi.util.NullOutputStream;
import org.apache.poi.util.PackageHelper;
import org.apache.poi.util.TempFile;
import org.junit.Test;
@@ -120,12 +123,43 @@ public final class TestPOIXMLDocument {
FileOutputStream out = new FileOutputStream(tmp);
doc.write(out);
out.close();
+
+ // Should not be able to write to an output stream that has been closed
+ try {
+ doc.write(out);
+ fail("Should not be able to write to an output stream that has been closed.");
+ } catch (final OpenXML4JRuntimeException e) {
+ // FIXME: A better exception class (IOException?) and message should be raised
+ // indicating that the document could not be written because the output stream is closed.
+ // see {@link org.apache.poi.openxml4j.opc.ZipPackage#saveImpl(java.io.OutputStream)}
+ if (e.getMessage().matches("Fail to save: an error occurs while saving the package : The part .+ fail to be saved in the stream with marshaller .+")) {
+ // expected
+ } else {
+ throw e;
+ }
+ }
+
+ // Should not be able to write a document that has been closed
doc.close();
+ try {
+ doc.write(new NullOutputStream());
+ fail("Should not be able to write a document that has been closed.");
+ } catch (final IOException e) {
+ if (e.getMessage().equals("Cannot write data, document seems to have been closed already")) {
+ // expected
+ } else {
+ throw e;
+ }
+ }
+
+ // Should be able to close a document multiple times, though subsequent closes will have no effect.
+ doc.close();
+
@SuppressWarnings("resource")
OPCPackage pkg2 = OPCPackage.open(tmp.getAbsolutePath());
+ doc = new OPCParser(pkg1);
try {
- doc = new OPCParser(pkg1);
doc.parse(new TestFactory());
context = new HashMap<String,POIXMLDocumentPart>();
traverse(doc, context);
@@ -150,6 +184,7 @@ public final class TestPOIXMLDocument {
}
} finally {
doc.close();
+ pkg1.close();
pkg2.close();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org