Search in sources :

Example 91 with OPCPackage

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]");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException) Test(org.junit.Test)

Example 92 with OPCPackage

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");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Test(org.junit.Test)

Example 93 with OPCPackage

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();
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 94 with OPCPackage

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();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) URI(java.net.URI)

Example 95 with OPCPackage

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AssertionFailedError(junit.framework.AssertionFailedError) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Aggregations

OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)116 Test (org.junit.Test)54 InputStream (java.io.InputStream)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 File (java.io.File)19 IOException (java.io.IOException)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 SignatureConfig (org.apache.poi.poifs.crypt.dsig.SignatureConfig)10 SignatureInfo (org.apache.poi.poifs.crypt.dsig.SignatureInfo)10 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)10 OutputStream (java.io.OutputStream)9 ArrayList (java.util.ArrayList)9 FileOutputStream (java.io.FileOutputStream)8 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)8 XmlException (org.apache.xmlbeans.XmlException)8 FileInputStream (java.io.FileInputStream)7