/** Test M4.1 rule. */
 public void testOnlyOneCorePropertiesPart_AddRelationship() {
   InputStream is =
       OpenXML4JTestDataSamples.openComplianceSampleStream(
           "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
   OPCPackage pkg;
   try {
     pkg = OPCPackage.open(is);
   } catch (InvalidFormatException e) {
     throw new RuntimeException(e);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
   URI partUri = createURI("/docProps/core2.xml");
   try {
     pkg.addRelationship(
         PackagingURIHelper.createPartName(partUri),
         TargetMode.INTERNAL,
         PackageRelationshipTypes.CORE_PROPERTIES);
     fail("expected OPC compliance exception was not thrown");
   } catch (InvalidFormatException e) {
     throw new RuntimeException(e);
   } catch (InvalidOperationException e) {
     // expected during successful test
     assertEquals(
         "OPC Compliance error [M4.1]: can't add another core properties part ! Use the built-in package method instead.",
         e.getMessage());
   }
   pkg.revert();
 }
Пример #2
0
  @Test
  public void testBug56479() throws Exception {
    InputStream is = OpenXML4JTestDataSamples.openSampleStream("dcterms_bug_56479.zip");
    OPCPackage p = OPCPackage.open(is);

    // Check we found the contents of it
    boolean foundCoreProps = false, foundDocument = false, foundTheme1 = false;
    for (PackagePart part : p.getParts()) {
      if (part.getPartName().toString().equals("/docProps/core.xml")) {
        assertEquals(ContentTypes.CORE_PROPERTIES_PART, part.getContentType());
        foundCoreProps = true;
      }
      if (part.getPartName().toString().equals("/word/document.xml")) {
        assertEquals(XWPFRelation.DOCUMENT.getContentType(), part.getContentType());
        foundDocument = true;
      }
      if (part.getPartName().toString().equals("/word/theme/theme1.xml")) {
        assertEquals(XWPFRelation.THEME.getContentType(), part.getContentType());
        foundTheme1 = true;
      }
    }
    assertTrue("Core not found in " + p.getParts(), foundCoreProps);
    assertFalse("Document should not be found in " + p.getParts(), foundDocument);
    assertFalse("Theme1 should not found in " + p.getParts(), foundTheme1);
  }
 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();
 }
  private static String extractInvalidFormatMessage(String sampleNameSuffix) {

    InputStream is =
        OpenXML4JTestDataSamples.openComplianceSampleStream(
            "OPCCompliance_CoreProperties_" + sampleNameSuffix);
    OPCPackage pkg;
    try {
      pkg = OPCPackage.open(is);
    } catch (InvalidFormatException e) {
      // expected during successful test
      return e.getMessage();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    pkg.revert();
    // Normally must thrown an InvalidFormatException exception.
    throw new AssertionFailedError("expected OPC compliance exception was not thrown");
  }