From 9354393a9aa1fb8a7b1cd2bfbf8d3d1d38000831 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Fri, 8 Jul 2016 11:55:21 +0000 Subject: [PATCH] whitespace and +svn:eol-style=native git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751890 13f79535-47bb-0310-9956-ffa450edef68 --- .../TestOPCComplianceCoreProperties.java | 260 +++++++-------- .../TestOPCCompliancePackageModel.java | 218 ++++++------ .../compliance/TestOPCCompliancePartName.java | 314 +++++++++--------- 3 files changed, 396 insertions(+), 396 deletions(-) diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java index 319c3f38b3..08dd9baa52 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java @@ -75,47 +75,47 @@ import junit.framework.TestCase; */ public final class TestOPCComplianceCoreProperties extends TestCase { - 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(); - } + 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) { - // no longer required for successful test - return e.getMessage(); - } catch (IOException e) { - throw new RuntimeException(e); - } - pkg.revert(); - throw new AssertionFailedError("expected OPC compliance exception was not thrown"); - } - - /** - * Test M4.1 rule. - */ - 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 + private static String extractInvalidFormatMessage(String sampleNameSuffix) { + InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_" + sampleNameSuffix); + OPCPackage pkg; + try { + pkg = OPCPackage.open(is); + } catch (InvalidFormatException e) { + // no longer required for successful test + return e.getMessage(); + } catch (IOException e) { + throw new RuntimeException(e); + } + pkg.revert(); + throw new AssertionFailedError("expected OPC compliance exception was not thrown"); + } + + /** + * Test M4.1 rule. + */ + 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); @@ -128,104 +128,104 @@ public final class TestOPCComplianceCoreProperties extends TestCase { "/docProps/core.xml", pkg.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).get(0).getPartName().toString() ); - } - - private static URI createURI(String text) { - try { - return new URI(text); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - } + } + + private static URI createURI(String text) { + try { + return new URI(text); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } - /** - * 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); - // no longer fail on compliance error - //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(); - } + /** + * 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); + // no longer fail on compliance error + //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(); + } - /** - * Test M4.1 rule. - */ - public void testOnlyOneCorePropertiesPart_AddPart() throws InvalidFormatException { - String sampleFileName = "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx"; - OPCPackage pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath()); + /** + * Test M4.1 rule. + */ + public void testOnlyOneCorePropertiesPart_AddPart() throws InvalidFormatException { + String sampleFileName = "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx"; + OPCPackage pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath()); - URI partUri = createURI("/docProps/core2.xml"); - try { - pkg.createPart(PackagingURIHelper.createPartName(partUri), - ContentTypes.CORE_PROPERTIES_PART); - // no longer fail on compliance error - //fail("expected OPC compliance exception was not thrown"); - } catch (InvalidOperationException e) { - // expected during successful test - assertEquals("OPC Compliance error [M4.1]: you try to add more than one core properties relationship in the package !", e.getMessage()); - } - pkg.revert(); - } + URI partUri = createURI("/docProps/core2.xml"); + try { + pkg.createPart(PackagingURIHelper.createPartName(partUri), + ContentTypes.CORE_PROPERTIES_PART); + // no longer fail on compliance error + //fail("expected OPC compliance exception was not thrown"); + } catch (InvalidOperationException e) { + // expected during successful test + assertEquals("OPC Compliance error [M4.1]: you try to add more than one core properties relationship in the package !", e.getMessage()); + } + pkg.revert(); + } - /** - * Test M4.2 rule. - */ - public void testDoNotUseCompatibilityMarkup() { - String msg = extractInvalidFormatMessage("DoNotUseCompatibilityMarkupFAIL.docx"); - assertEquals("OPC Compliance error [M4.2]: A format consumer shall consider the use of the Markup Compatibility namespace to be an error.", msg); - } + /** + * Test M4.2 rule. + */ + public void testDoNotUseCompatibilityMarkup() { + String msg = extractInvalidFormatMessage("DoNotUseCompatibilityMarkupFAIL.docx"); + assertEquals("OPC Compliance error [M4.2]: A format consumer shall consider the use of the Markup Compatibility namespace to be an error.", msg); + } - /** - * Test M4.3 rule. - */ - public void testDCTermsNamespaceLimitedUse() { - String msg = extractInvalidFormatMessage("DCTermsNamespaceLimitedUseFAIL.docx"); - assertEquals("OPC Compliance error [M4.3]: Producers shall not create a document element that contains refinements to the Dublin Core elements, except for the two specified in the schema: and Consumers shall consider a document element that violates this constraint to be an error.", msg); - } + /** + * Test M4.3 rule. + */ + public void testDCTermsNamespaceLimitedUse() { + String msg = extractInvalidFormatMessage("DCTermsNamespaceLimitedUseFAIL.docx"); + assertEquals("OPC Compliance error [M4.3]: Producers shall not create a document element that contains refinements to the Dublin Core elements, except for the two specified in the schema: and Consumers shall consider a document element that violates this constraint to be an error.", msg); + } - /** - * Test M4.4 rule. - */ - public void testUnauthorizedXMLLangAttribute() { - String msg = extractInvalidFormatMessage("UnauthorizedXMLLangAttributeFAIL.docx"); - assertEquals("OPC Compliance error [M4.4]: Producers shall not create a document element that contains the xml:lang attribute. Consumers shall consider a document element that violates this constraint to be an error.", msg); - } + /** + * Test M4.4 rule. + */ + public void testUnauthorizedXMLLangAttribute() { + String msg = extractInvalidFormatMessage("UnauthorizedXMLLangAttributeFAIL.docx"); + assertEquals("OPC Compliance error [M4.4]: Producers shall not create a document element that contains the xml:lang attribute. Consumers shall consider a document element that violates this constraint to be an error.", msg); + } - /** - * Test M4.5 rule. - */ - public void testLimitedXSITypeAttribute_NotPresent() { - String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_NotPresentFAIL.docx"); - assertEquals("The element 'created' must have the 'xsi:type' attribute present !", msg); - } + /** + * Test M4.5 rule. + */ + public void testLimitedXSITypeAttribute_NotPresent() { + String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_NotPresentFAIL.docx"); + assertEquals("The element 'created' must have the 'xsi:type' attribute present !", msg); + } - /** - * Test M4.5 rule. - */ - public void testLimitedXSITypeAttribute_PresentWithUnauthorizedValue() { - String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_PresentWithUnauthorizedValueFAIL.docx"); - assertEquals("The element 'modified' must have the 'xsi:type' attribute with the value 'dcterms:W3CDTF', but had 'W3CDTF' !", msg); - } - + /** + * Test M4.5 rule. + */ + public void testLimitedXSITypeAttribute_PresentWithUnauthorizedValue() { + String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_PresentWithUnauthorizedValueFAIL.docx"); + assertEquals("The element 'modified' must have the 'xsi:type' attribute with the value 'dcterms:W3CDTF', but had 'W3CDTF' !", msg); + } + /** * Document with no core properties - testing at the OPC level, * saving into a new stream diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java index a39edd3a62..768612525b 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java @@ -42,118 +42,118 @@ import org.apache.poi.openxml4j.opc.TargetMode; */ public class TestOPCCompliancePackageModel extends TestCase { - /** - * 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] - */ - 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]"); - } + /** + * 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] + */ + 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]"); + } - /** - * 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] - */ - public void testPartNameDerivationReadingFailure() throws IOException { - String filename = "OPCCompliance_DerivedPartNameFAIL.docx"; - try { - OPCPackage.open(POIDataSamples.getOpenXML4JInstance().openResourceAsStream(filename)); - } catch (InvalidFormatException e) { - return; - } - 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]"); - } + /** + * 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] + */ + public void testPartNameDerivationReadingFailure() throws IOException { + String filename = "OPCCompliance_DerivedPartNameFAIL.docx"; + try { + OPCPackage.open(POIDataSamples.getOpenXML4JInstance().openResourceAsStream(filename)); + } catch (InvalidFormatException e) { + return; + } + 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]"); + } - /** - * Rule M1.12 : Packages shall not contain equivalent part names and package - * implementers shall neither create nor recognize packages with equivalent - * part names. - */ - public void testAddPackageAlreadyAddFailure() throws Exception { - OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); - PackagePartName name1 = null; - PackagePartName name2 = null; - try { - name1 = PackagingURIHelper.createPartName("/word/document.xml"); - name2 = PackagingURIHelper.createPartName("/word/document.xml"); - } catch (InvalidFormatException e) { - throw new Exception(e.getMessage()); - } - pkg.createPart(name1, ContentTypes.XML); - try { - pkg.createPart(name2, ContentTypes.XML); - } catch (PartAlreadyExistsException 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]"); - } + /** + * Rule M1.12 : Packages shall not contain equivalent part names and package + * implementers shall neither create nor recognize packages with equivalent + * part names. + */ + public void testAddPackageAlreadyAddFailure() throws Exception { + OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); + PackagePartName name1 = null; + PackagePartName name2 = null; + try { + name1 = PackagingURIHelper.createPartName("/word/document.xml"); + name2 = PackagingURIHelper.createPartName("/word/document.xml"); + } catch (InvalidFormatException e) { + throw new Exception(e.getMessage()); + } + pkg.createPart(name1, ContentTypes.XML); + try { + pkg.createPart(name2, ContentTypes.XML); + } catch (PartAlreadyExistsException 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]"); + } - /** - * Rule M1.12 : Packages shall not contain equivalent part names and package - * implementers shall neither create nor recognize packages with equivalent - * part names. - */ - 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]"); - } + /** + * Rule M1.12 : Packages shall not contain equivalent part names and package + * implementers shall neither create nor recognize packages with equivalent + * part names. + */ + 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]"); + } - /** - * 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. - */ - 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 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. + */ + 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"); - } + 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"); + } } diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePartName.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePartName.java index 2593594522..5ecad86970 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePartName.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePartName.java @@ -79,161 +79,161 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper; */ public final class TestOPCCompliancePartName extends TestCase { - /** - * Test some common invalid names. - * - * A segment shall not contain percent-encoded unreserved characters. [M1.8] - */ - public void testInvalidPartNames() { - String[] invalidNames = { "/", "/xml./doc.xml", "[Content_Types].xml", "//xml/." }; - for (String s : invalidNames) { - URI uri = null; - try { - uri = new URI(s); - } catch (URISyntaxException e) { - assertTrue(s.equals("[Content_Types].xml")); - continue; - } - assertFalse("This part name SHOULD NOT be valid: " + s, - PackagingURIHelper.isValidPartName(uri)); - } - } - - /** - * Test some common valid names. - */ - public void testValidPartNames() throws URISyntaxException { - String[] validNames = { "/xml/item1.xml", "/document.xml", - "/a/%D1%86.xml" }; - for (String s : validNames) - assertTrue("This part name SHOULD be valid: " + s, - PackagingURIHelper.isValidPartName(new URI(s))); - } - - /** - * A part name shall not be empty. [M1.1] - */ - public void testEmptyPartNameFailure() throws URISyntaxException { - try { - PackagingURIHelper.createPartName(new URI("")); - fail("A part name shall not be empty. [M1.1]"); - } catch (InvalidFormatException e) { - // Normal behaviour - } - } - - /** - * A part name shall not have empty segments. [M1.3] - * - * A segment shall not end with a dot ('.') character. [M1.9] - * - * A segment shall include at least one non-dot character. [M1.10] - */ - public void testPartNameWithInvalidSegmentsFailure() { - String[] invalidNames = { "//document.xml", "//word/document.xml", - "/word//document.rels", "/word//rels//document.rels", - "/xml./doc.xml", "/document.", "/./document.xml", - "/word/./doc.rels", "/%2F/document.xml" }; - try { - for (String s : invalidNames) - assertFalse( - "A part name shall not have empty segments. [M1.3]", - PackagingURIHelper.isValidPartName(new URI(s))); - } catch (URISyntaxException e) { - fail(); - } - } - - /** - * A segment shall not hold any characters other than ipchar (RFC 3987) characters. - * [M1.6]. - */ - public void testPartNameWithNonPCharCharacters() { - String[] validNames = { "/doc&.xml" }; - try { - for (String s : validNames) - assertTrue( - "A segment shall not contain non pchar characters [M1.6] : " - + s, PackagingURIHelper - .isValidPartName(new URI(s))); - } catch (URISyntaxException e) { - fail(); - } - } - - /** - * A segment shall not contain percent-encoded unreserved characters [M1.8]. - */ - public void testPartNameWithUnreservedEncodedCharactersFailure() { - String[] invalidNames = { "/a/docum%65nt.xml" }; - try { - for (String s : invalidNames) - assertFalse( - "A segment shall not contain percent-encoded unreserved characters [M1.8] : " - + s, PackagingURIHelper - .isValidPartName(new URI(s))); - } catch (URISyntaxException e) { - fail(); - } - } - - /** - * A part name shall start with a forward slash ('/') character. [M1.4] - */ - public void testPartNameStartsWithAForwardSlashFailure() - throws URISyntaxException { - try { - PackagingURIHelper.createPartName(new URI("document.xml")); - fail("A part name shall start with a forward slash ('/') character. [M1.4]"); - } catch (InvalidFormatException e) { - // Normal behaviour - } - } - - /** - * A part name shall not have a forward slash as the last character. [M1.5] - */ - public void testPartNameEndsWithAForwardSlashFailure() - throws URISyntaxException { - try { - PackagingURIHelper.createPartName(new URI("/document.xml/")); - fail("A part name shall not have a forward slash as the last character. [M1.5]"); - } catch (InvalidFormatException e) { - // Normal behaviour - } - } - - /** - * Part name equivalence is determined by comparing part names as - * case-insensitive ASCII strings. [M1.12] - */ - 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()); - } - } - - /** - * Part name equivalence is determined by comparing part names as - * case-insensitive ASCII strings. [M1.12]. - * - * All the comparisons MUST FAIL ! - */ - public void testPartNameComparaisonFailure() throws Exception { - String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" }; - String[] partName2 = { "/WORD/DocUment.XML2", "/docProp/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]); - assertFalse(p1.equals(p2)); - assertFalse(p1.compareTo(p2) == 0); - assertFalse(p1.hashCode() == p2.hashCode()); - } - } + /** + * Test some common invalid names. + * + * A segment shall not contain percent-encoded unreserved characters. [M1.8] + */ + public void testInvalidPartNames() { + String[] invalidNames = { "/", "/xml./doc.xml", "[Content_Types].xml", "//xml/." }; + for (String s : invalidNames) { + URI uri = null; + try { + uri = new URI(s); + } catch (URISyntaxException e) { + assertTrue(s.equals("[Content_Types].xml")); + continue; + } + assertFalse("This part name SHOULD NOT be valid: " + s, + PackagingURIHelper.isValidPartName(uri)); + } + } + + /** + * Test some common valid names. + */ + public void testValidPartNames() throws URISyntaxException { + String[] validNames = { "/xml/item1.xml", "/document.xml", + "/a/%D1%86.xml" }; + for (String s : validNames) + assertTrue("This part name SHOULD be valid: " + s, + PackagingURIHelper.isValidPartName(new URI(s))); + } + + /** + * A part name shall not be empty. [M1.1] + */ + public void testEmptyPartNameFailure() throws URISyntaxException { + try { + PackagingURIHelper.createPartName(new URI("")); + fail("A part name shall not be empty. [M1.1]"); + } catch (InvalidFormatException e) { + // Normal behaviour + } + } + + /** + * A part name shall not have empty segments. [M1.3] + * + * A segment shall not end with a dot ('.') character. [M1.9] + * + * A segment shall include at least one non-dot character. [M1.10] + */ + public void testPartNameWithInvalidSegmentsFailure() { + String[] invalidNames = { "//document.xml", "//word/document.xml", + "/word//document.rels", "/word//rels//document.rels", + "/xml./doc.xml", "/document.", "/./document.xml", + "/word/./doc.rels", "/%2F/document.xml" }; + try { + for (String s : invalidNames) + assertFalse( + "A part name shall not have empty segments. [M1.3]", + PackagingURIHelper.isValidPartName(new URI(s))); + } catch (URISyntaxException e) { + fail(); + } + } + + /** + * A segment shall not hold any characters other than ipchar (RFC 3987) characters. + * [M1.6]. + */ + public void testPartNameWithNonPCharCharacters() { + String[] validNames = { "/doc&.xml" }; + try { + for (String s : validNames) + assertTrue( + "A segment shall not contain non pchar characters [M1.6] : " + + s, PackagingURIHelper + .isValidPartName(new URI(s))); + } catch (URISyntaxException e) { + fail(); + } + } + + /** + * A segment shall not contain percent-encoded unreserved characters [M1.8]. + */ + public void testPartNameWithUnreservedEncodedCharactersFailure() { + String[] invalidNames = { "/a/docum%65nt.xml" }; + try { + for (String s : invalidNames) + assertFalse( + "A segment shall not contain percent-encoded unreserved characters [M1.8] : " + + s, PackagingURIHelper + .isValidPartName(new URI(s))); + } catch (URISyntaxException e) { + fail(); + } + } + + /** + * A part name shall start with a forward slash ('/') character. [M1.4] + */ + public void testPartNameStartsWithAForwardSlashFailure() + throws URISyntaxException { + try { + PackagingURIHelper.createPartName(new URI("document.xml")); + fail("A part name shall start with a forward slash ('/') character. [M1.4]"); + } catch (InvalidFormatException e) { + // Normal behaviour + } + } + + /** + * A part name shall not have a forward slash as the last character. [M1.5] + */ + public void testPartNameEndsWithAForwardSlashFailure() + throws URISyntaxException { + try { + PackagingURIHelper.createPartName(new URI("/document.xml/")); + fail("A part name shall not have a forward slash as the last character. [M1.5]"); + } catch (InvalidFormatException e) { + // Normal behaviour + } + } + + /** + * Part name equivalence is determined by comparing part names as + * case-insensitive ASCII strings. [M1.12] + */ + 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()); + } + } + + /** + * Part name equivalence is determined by comparing part names as + * case-insensitive ASCII strings. [M1.12]. + * + * All the comparisons MUST FAIL ! + */ + public void testPartNameComparaisonFailure() throws Exception { + String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" }; + String[] partName2 = { "/WORD/DocUment.XML2", "/docProp/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]); + assertFalse(p1.equals(p2)); + assertFalse(p1.compareTo(p2) == 0); + assertFalse(p1.hashCode() == p2.hashCode()); + } + } } -- 2.39.5