]> source.dussan.org Git - poi.git/commitdiff
fix old xerces errors because of not available disallow-doctype parser feature
authorAndreas Beeker <kiwiwings@apache.org>
Wed, 19 Dec 2018 19:14:55 +0000 (19:14 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Wed, 19 Dec 2018 19:14:55 +0000 (19:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849336 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestContentType.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

index 7a0d0369ece67b43809060ba774a0b447fc8b6ef..4e97633285bf20a9e4d74bd36804111671909723 100644 (file)
 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;
+    }
 }
index 17bf1828af2fd7f4df28cbb761beb624b967111a..7f726d63c4fff557114447c6ae1b1af9de528149 100644 (file)
@@ -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();
     }
     
index 846eac2a1ab3417958050e20eefb6535ba2fb5dd..691f0b1b98d605d9a820df3cb651c3e1002f7628 100644 (file)
@@ -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);
     }
 }
index 379109303b81494623f934a4442e9e34c2c7ee41..9a3d700fcecde16268cbaeb1afc1281b9ad4036f 100644 (file)
@@ -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"));
         }
     }