use of org.apache.poi.openxml4j.opc.PackagePartName in project poi by apache.
the class TestContentTypeManager method testContentTypeAddition.
/**
* Test the addition of several default and override content types.
*/
@Test
public void testContentTypeAddition() throws Exception {
ContentTypeManager ctm = new ZipContentTypeManager(null, null);
PackagePartName name1 = PackagingURIHelper.createPartName("/foo/foo.XML");
PackagePartName name2 = PackagingURIHelper.createPartName("/foo/foo2.xml");
PackagePartName name3 = PackagingURIHelper.createPartName("/foo/doc.rels");
PackagePartName name4 = PackagingURIHelper.createPartName("/foo/doc.RELS");
// Add content types
ctm.addContentType(name1, "foo-type1");
ctm.addContentType(name2, "foo-type2");
ctm.addContentType(name3, "text/xml+rel");
ctm.addContentType(name4, "text/xml+rel");
assertEquals(ctm.getContentType(name1), "foo-type1");
assertEquals(ctm.getContentType(name2), "foo-type2");
assertEquals(ctm.getContentType(name3), "text/xml+rel");
assertEquals(ctm.getContentType(name3), "text/xml+rel");
}
use of org.apache.poi.openxml4j.opc.PackagePartName in project poi by apache.
the class TestContentTypeManager method testContentTypeRemoval.
/**
* Test the addition then removal of content types.
*/
@Test
public void testContentTypeRemoval() throws Exception {
ContentTypeManager ctm = new ZipContentTypeManager(null, null);
PackagePartName name1 = PackagingURIHelper.createPartName("/foo/foo.xml");
PackagePartName name2 = PackagingURIHelper.createPartName("/foo/foo2.xml");
PackagePartName name3 = PackagingURIHelper.createPartName("/foo/doc.rels");
PackagePartName name4 = PackagingURIHelper.createPartName("/foo/doc.RELS");
// Add content types
ctm.addContentType(name1, "foo-type1");
ctm.addContentType(name2, "foo-type2");
ctm.addContentType(name3, "text/xml+rel");
ctm.addContentType(name4, "text/xml+rel");
ctm.removeContentType(name2);
ctm.removeContentType(name3);
assertEquals(ctm.getContentType(name1), "foo-type1");
assertEquals(ctm.getContentType(name2), "foo-type1");
assertEquals(ctm.getContentType(name3), null);
ctm.removeContentType(name1);
assertEquals(ctm.getContentType(name1), null);
assertEquals(ctm.getContentType(name2), null);
}
use of org.apache.poi.openxml4j.opc.PackagePartName in project poi by apache.
the class TestOPCCompliancePartName method testPartNameComparaison.
/**
* Part name equivalence is determined by comparing part names as
* case-insensitive ASCII strings. [M1.12]
*/
@Test
public void testPartNameComparaison() throws Exception {
String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" };
String[] partName2 = { "/WORD/DocUment.XML", "/docProps/core.xml", "/rels/.rels" };
for (int i = 0; i < partName1.length || i < partName2.length; ++i) {
PackagePartName p1 = PackagingURIHelper.createPartName(partName1[i]);
PackagePartName p2 = PackagingURIHelper.createPartName(partName2[i]);
assertTrue(p1.equals(p2));
assertTrue(p1.compareTo(p2) == 0);
assertTrue(p1.hashCode() == p2.hashCode());
}
}
use of org.apache.poi.openxml4j.opc.PackagePartName in project poi by apache.
the class TestOPCCompliancePackageModel method testPartNameDerivationAdditionFailure.
/**
* A package implementer shall neither create nor recognize a part with a
* part name derived from another part name by appending segments to it.
* [M1.11]
*/
@Test
public void testPartNameDerivationAdditionFailure() {
OPCPackage pkg = OPCPackage.create("TODELETEIFEXIST.docx");
try {
PackagePartName name = PackagingURIHelper.createPartName("/word/document.xml");
PackagePartName nameDerived = PackagingURIHelper.createPartName("/word/document.xml/image1.gif");
pkg.createPart(name, ContentTypes.XML);
pkg.createPart(nameDerived, ContentTypes.EXTENSION_GIF);
} catch (InvalidOperationException e) {
pkg.revert();
return;
} catch (InvalidFormatException e) {
fail(e.getMessage());
}
fail("A package implementer shall neither create nor recognize a part with a" + " part name derived from another part name by appending segments to it." + " [M1.11]");
}
use of org.apache.poi.openxml4j.opc.PackagePartName 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]");
}
Aggregations