use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestOPCCompliancePackageModel method testAddPackageAlreadyAddFailure2.
/**
* Rule M1.12 : Packages shall not contain equivalent part names and package
* implementers shall neither create nor recognize packages with equivalent
* part names.
*/
@Test
public void testAddPackageAlreadyAddFailure2() throws Exception {
OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
PackagePartName partName = null;
try {
partName = PackagingURIHelper.createPartName("/word/document.xml");
} catch (InvalidFormatException e) {
throw new Exception(e.getMessage());
}
pkg.createPart(partName, ContentTypes.XML);
try {
pkg.createPart(partName, ContentTypes.XML);
} catch (InvalidOperationException e) {
return;
}
fail("Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]");
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestOPCCompliancePackageModel method testAddRelationshipRelationshipsPartFailure.
/**
* Try to add a relationship to a relationship part.
*
* Check rule M1.25: The Relationships part shall not have relationships to
* any other part. Package implementers shall enforce this requirement upon
* the attempt to create such a relationship and shall treat any such
* relationship as invalid.
*/
@Test
public void testAddRelationshipRelationshipsPartFailure() {
OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
PackagePartName name1 = null;
try {
name1 = PackagingURIHelper.createPartName("/test/_rels/document.xml.rels");
} catch (InvalidFormatException e) {
fail("This exception should never happen !");
}
try {
pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
} catch (InvalidOperationException e) {
return;
}
fail("Fail test -> M1.25: The Relationships part shall not have relationships to any other part");
}
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());
}
Aggregations