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
/**
* 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);
}
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;
+ }
}
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 {
// 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();
}
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;
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);
}
}
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;
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"));
}
}