diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java index 7a0d0369ec..4e97633285 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java @@ -18,21 +18,28 @@ package org.apache.poi.openxml4j.opc; import java.io.InputStream; +import java.net.URL; +import org.apache.poi.ooxml.util.POIXMLConstants; import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.internal.ContentType; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.junit.Assert.*; +import javax.xml.parsers.DocumentBuilderFactory; + /** * Tests for content type (ContentType class). - * - * @author Julien Chable */ public final class TestContentType { + @Rule + public ExpectedException exception = ExpectedException.none(); + /** * Check rule M1.13: Package implementers shall only create and only * recognize parts with a content type; format designers shall specify a @@ -144,9 +151,14 @@ public final class TestContentType { /** * OOXML content types don't need entities and we shouldn't * barf if we get one from a third party system that added them + * (expected = InvalidFormatException.class) */ - @Test(expected = InvalidFormatException.class) + @Test public void testFileWithContentTypeEntities() throws Exception { + if (!isOldXercesActive()) { + exception.expect(InvalidFormatException.class); + } + InputStream is = OpenXML4JTestDataSamples.openSampleStream("ContentTypeHasEntities.ooxml"); OPCPackage.open(is); } @@ -225,4 +237,13 @@ public final class TestContentType { private static void assertContains(String needle, String haystack) { assertTrue(haystack.contains(needle)); } + + public static boolean isOldXercesActive() { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + try { + dbf.setFeature(POIXMLConstants.FEATURE_DISALLOW_DOCTYPE_DECL, true); + return false; + } catch (Exception|AbstractMethodError ignored) {} + return true; + } } 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 17bf1828af..7f726d63c4 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java @@ -36,6 +36,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Test; import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty; +import static org.apache.poi.openxml4j.opc.TestContentType.isOldXercesActive; import static org.junit.Assert.*; public final class TestPackageCoreProperties { @@ -249,9 +250,9 @@ public final class TestPackageCoreProperties { // Get the Core Properties PackagePropertiesPart props = (PackagePropertiesPart)p.getPackageProperties(); - // used to resolve a vale but now we ignore DTD entities for security reasons - assertFalse(props.getCreatorProperty().isPresent()); - + // used to resolve a value but now we ignore DTD entities for security reasons + assertEquals(isOldXercesActive(), props.getCreatorProperty().isPresent()); + p.close(); } 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 846eac2a1a..691f0b1b98 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java @@ -17,6 +17,8 @@ package org.apache.poi.openxml4j.opc; +import static org.apache.poi.openxml4j.opc.TestContentType.isOldXercesActive; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; @@ -418,8 +420,8 @@ public class TestRelationships extends TestCase { if (pr.getRelationshipType().equals(PackageRelationshipTypes.EXTENDED_PROPERTIES)) foundExtPropRel = true; } - assertFalse("Core/Doc Relationship not found in " + p.getRelationships(), foundDocRel); - assertFalse("Core Props Relationship not found in " + p.getRelationships(), foundCorePropRel); - assertFalse("Ext Props Relationship not found in " + p.getRelationships(), foundExtPropRel); + assertEquals("Core/Doc Relationship not found in " + p.getRelationships(), isOldXercesActive(), foundDocRel); + assertEquals("Core Props Relationship not found in " + p.getRelationships(), isOldXercesActive(), foundCorePropRel); + assertEquals("Ext Props Relationship not found in " + p.getRelationships(), isOldXercesActive(), foundExtPropRel); } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 379109303b..9a3d700fce 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel; +import static org.apache.poi.openxml4j.opc.TestContentType.isOldXercesActive; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -1969,7 +1970,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { fail("should have thrown SAXParseException"); } catch (SAXParseException e) { assertNotNull(e.getMessage()); - assertTrue(e.getMessage().contains("DOCTYPE is disallowed when the feature")); + assertNotEquals(isOldXercesActive(), e.getMessage().contains("DOCTYPE is disallowed when the feature")); } }