use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestContentTypeManager method testContentType.
/**
* Test the properties part content parsing.
*/
@Test
public void testContentType() throws Exception {
String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
// Retrieves core properties part
OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ);
try {
PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
PackagePart coreDocument = p.getPart(corePropertiesRelationship);
assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType());
// TODO - finish writing this test
assumeTrue("finish writing this test", false);
ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p);
assertNotNull(ctm);
} finally {
p.close();
}
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestPackagingURIHelper method testCreatePartNameRelativeURI.
/**
* Test createPartName(URI, y)
*/
public void testCreatePartNameRelativeURI() throws Exception {
PackagePartName partNameToValid = PackagingURIHelper.createPartName("/word/media/image1.gif");
OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
// Base part
PackagePartName nameBase = PackagingURIHelper.createPartName("/word/document.xml");
PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
// Relative part name
PackagePartName relativeName = PackagingURIHelper.createPartName(new URI("media/image1.gif"), partBase);
assertTrue("The part name must be equal to " + partNameToValid.getName(), partNameToValid.equals(relativeName));
pkg.revert();
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestOPCComplianceCoreProperties method testOnlyOneCorePropertiesPart.
/**
* Test M4.1 rule.
*/
@Test
public void testOnlyOneCorePropertiesPart() throws Exception {
// We have relaxed this check, so we can read the file anyway
try {
extractInvalidFormatMessage("OnlyOneCorePropertiesPartFAIL.docx");
fail("M4.1 should be being relaxed");
} catch (AssertionFailedError e) {
// expected here
}
// We will use the first core properties, and ignore the others
InputStream is = OpenXML4JTestDataSamples.openSampleStream("MultipleCoreProperties.docx");
OPCPackage pkg = OPCPackage.open(is);
// We can see 2 by type
assertEquals(2, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
// But only the first one by relationship
assertEquals(1, pkg.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).size());
// It should be core.xml not the older core1.xml
assertEquals("/docProps/core.xml", pkg.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).get(0).getPartName().toString());
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestOPCComplianceCoreProperties method testCorePropertiesPart.
@Test
public void testCorePropertiesPart() {
OPCPackage pkg;
try {
InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
pkg = OPCPackage.open(is);
} catch (InvalidFormatException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
pkg.revert();
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestSignatureInfo method testManipulation.
@Test
public void testManipulation() throws Exception {
// sign & validate
String testFile = "hello-world-unsigned.xlsx";
@SuppressWarnings("resource") OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
sign(pkg, "Test", "CN=Test", 1);
// manipulate
XSSFWorkbook wb = new XSSFWorkbook(pkg);
wb.setSheetName(0, "manipulated");
// ... I don't know, why commit is protected ...
POITestCase.callMethod(XSSFWorkbook.class, wb, Void.class, "commit", new Class[0], new Object[0]);
// todo: test a manipulation on a package part, which is not signed
// ... maybe in combination with #56164
// validate
SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo();
si.setSignatureConfig(sic);
boolean b = si.verifySignature();
assertFalse("signature should be broken", b);
wb.close();
}
Aggregations