convert OPC Compliance unit tests to junit4

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751891 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-07-08 12:07:20 +00:00
parent 9354393a9a
commit f8fcefcd13
5 changed files with 44 additions and 43 deletions

View File

@ -17,7 +17,6 @@
package org.apache.poi.openxml4j.opc; package org.apache.poi.openxml4j.opc;
import org.apache.poi.openxml4j.opc.compliance.AllOpenXML4JComplianceTests;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;
@ -32,7 +31,6 @@ import org.junit.runners.Suite;
, TestPackageThumbnail.class , TestPackageThumbnail.class
, TestPackagingURIHelper.class , TestPackagingURIHelper.class
, TestRelationships.class , TestRelationships.class
, AllOpenXML4JComplianceTests.class
}) })
public final class AllOpenXML4JTests { public final class AllOpenXML4JTests {
} }

View File

@ -1,33 +0,0 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.openxml4j.opc.compliance;
import junit.framework.Test;
import junit.framework.TestSuite;
public final class AllOpenXML4JComplianceTests {
public static Test suite() {
TestSuite suite = new TestSuite(AllOpenXML4JComplianceTests.class.getName());
suite.addTestSuite(TestOPCCompliancePartName.class);
suite.addTestSuite(TestOPCComplianceCoreProperties.class);
suite.addTestSuite(TestOPCCompliancePackageModel.class);
return suite;
}
}

View File

@ -17,6 +17,12 @@
package org.apache.poi.openxml4j.opc.compliance; package org.apache.poi.openxml4j.opc.compliance;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -37,9 +43,9 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import org.junit.Test;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
/** /**
* Test core properties Open Packaging Convention compliance. * Test core properties Open Packaging Convention compliance.
@ -73,8 +79,9 @@ import junit.framework.TestCase;
* *
* @author Julien Chable * @author Julien Chable
*/ */
public final class TestOPCComplianceCoreProperties extends TestCase { public final class TestOPCComplianceCoreProperties {
@Test
public void testCorePropertiesPart() { public void testCorePropertiesPart() {
OPCPackage pkg; OPCPackage pkg;
try { try {
@ -106,6 +113,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
/** /**
* Test M4.1 rule. * Test M4.1 rule.
*/ */
@Test
public void testOnlyOneCorePropertiesPart() throws Exception { public void testOnlyOneCorePropertiesPart() throws Exception {
// We have relaxed this check, so we can read the file anyway // We have relaxed this check, so we can read the file anyway
try { try {
@ -141,6 +149,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
/** /**
* Test M4.1 rule. * Test M4.1 rule.
*/ */
@Test
public void testOnlyOneCorePropertiesPart_AddRelationship() { public void testOnlyOneCorePropertiesPart_AddRelationship() {
InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx"); InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
OPCPackage pkg; OPCPackage pkg;
@ -169,6 +178,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
/** /**
* Test M4.1 rule. * Test M4.1 rule.
*/ */
@Test
public void testOnlyOneCorePropertiesPart_AddPart() throws InvalidFormatException { public void testOnlyOneCorePropertiesPart_AddPart() throws InvalidFormatException {
String sampleFileName = "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx"; String sampleFileName = "OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx";
OPCPackage pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath()); OPCPackage pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
@ -189,6 +199,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
/** /**
* Test M4.2 rule. * Test M4.2 rule.
*/ */
@Test
public void testDoNotUseCompatibilityMarkup() { public void testDoNotUseCompatibilityMarkup() {
String msg = extractInvalidFormatMessage("DoNotUseCompatibilityMarkupFAIL.docx"); 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); assertEquals("OPC Compliance error [M4.2]: A format consumer shall consider the use of the Markup Compatibility namespace to be an error.", msg);
@ -197,6 +208,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
/** /**
* Test M4.3 rule. * Test M4.3 rule.
*/ */
@Test
public void testDCTermsNamespaceLimitedUse() { public void testDCTermsNamespaceLimitedUse() {
String msg = extractInvalidFormatMessage("DCTermsNamespaceLimitedUseFAIL.docx"); 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: <dcterms:created> and <dcterms:modified> Consumers shall consider a document element that violates this constraint to be an error.", msg); 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: <dcterms:created> and <dcterms:modified> Consumers shall consider a document element that violates this constraint to be an error.", msg);
@ -205,6 +217,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
/** /**
* Test M4.4 rule. * Test M4.4 rule.
*/ */
@Test
public void testUnauthorizedXMLLangAttribute() { public void testUnauthorizedXMLLangAttribute() {
String msg = extractInvalidFormatMessage("UnauthorizedXMLLangAttributeFAIL.docx"); 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); 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);
@ -213,6 +226,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
/** /**
* Test M4.5 rule. * Test M4.5 rule.
*/ */
@Test
public void testLimitedXSITypeAttribute_NotPresent() { public void testLimitedXSITypeAttribute_NotPresent() {
String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_NotPresentFAIL.docx"); String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_NotPresentFAIL.docx");
assertEquals("The element 'created' must have the 'xsi:type' attribute present !", msg); assertEquals("The element 'created' must have the 'xsi:type' attribute present !", msg);
@ -221,6 +235,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
/** /**
* Test M4.5 rule. * Test M4.5 rule.
*/ */
@Test
public void testLimitedXSITypeAttribute_PresentWithUnauthorizedValue() { public void testLimitedXSITypeAttribute_PresentWithUnauthorizedValue() {
String msg = extractInvalidFormatMessage("LimitedXSITypeAttribute_PresentWithUnauthorizedValueFAIL.docx"); 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); assertEquals("The element 'modified' must have the 'xsi:type' attribute with the value 'dcterms:W3CDTF', but had 'W3CDTF' !", msg);
@ -230,6 +245,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
* Document with no core properties - testing at the OPC level, * Document with no core properties - testing at the OPC level,
* saving into a new stream * saving into a new stream
*/ */
@Test
public void testNoCoreProperties_saveNew() throws Exception { public void testNoCoreProperties_saveNew() throws Exception {
String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx"; String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx";
OPCPackage pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath()); OPCPackage pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
@ -277,6 +293,7 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
* Document with no core properties - testing at the OPC level, * Document with no core properties - testing at the OPC level,
* from a temp-file, saving in-place * from a temp-file, saving in-place
*/ */
@Test
public void testNoCoreProperties_saveInPlace() throws Exception { public void testNoCoreProperties_saveInPlace() throws Exception {
String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx"; String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx";

View File

@ -17,9 +17,9 @@
package org.apache.poi.openxml4j.opc.compliance; package org.apache.poi.openxml4j.opc.compliance;
import java.io.IOException; import static org.junit.Assert.fail;
import junit.framework.TestCase; import java.io.IOException;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@ -31,6 +31,7 @@ import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.openxml4j.opc.TargetMode;
import org.junit.Test;
/** /**
* Test Open Packaging Convention package model compliance. * Test Open Packaging Convention package model compliance.
@ -40,13 +41,14 @@ import org.apache.poi.openxml4j.opc.TargetMode;
* *
* @author Julien Chable * @author Julien Chable
*/ */
public class TestOPCCompliancePackageModel extends TestCase { public class TestOPCCompliancePackageModel {
/** /**
* A package implementer shall neither create nor recognize a part with a * A package implementer shall neither create nor recognize a part with a
* part name derived from another part name by appending segments to it. * part name derived from another part name by appending segments to it.
* [M1.11] * [M1.11]
*/ */
@Test
public void testPartNameDerivationAdditionFailure() { public void testPartNameDerivationAdditionFailure() {
OPCPackage pkg = OPCPackage.create("TODELETEIFEXIST.docx"); OPCPackage pkg = OPCPackage.create("TODELETEIFEXIST.docx");
try { try {
@ -72,6 +74,7 @@ public class TestOPCCompliancePackageModel extends TestCase {
* part name derived from another part name by appending segments to it. * part name derived from another part name by appending segments to it.
* [M1.11] * [M1.11]
*/ */
@Test
public void testPartNameDerivationReadingFailure() throws IOException { public void testPartNameDerivationReadingFailure() throws IOException {
String filename = "OPCCompliance_DerivedPartNameFAIL.docx"; String filename = "OPCCompliance_DerivedPartNameFAIL.docx";
try { try {
@ -89,6 +92,7 @@ public class TestOPCCompliancePackageModel extends TestCase {
* implementers shall neither create nor recognize packages with equivalent * implementers shall neither create nor recognize packages with equivalent
* part names. * part names.
*/ */
@Test
public void testAddPackageAlreadyAddFailure() throws Exception { public void testAddPackageAlreadyAddFailure() throws Exception {
OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
PackagePartName name1 = null; PackagePartName name1 = null;
@ -113,6 +117,7 @@ public class TestOPCCompliancePackageModel extends TestCase {
* implementers shall neither create nor recognize packages with equivalent * implementers shall neither create nor recognize packages with equivalent
* part names. * part names.
*/ */
@Test
public void testAddPackageAlreadyAddFailure2() throws Exception { public void testAddPackageAlreadyAddFailure2() throws Exception {
OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
PackagePartName partName = null; PackagePartName partName = null;
@ -138,6 +143,7 @@ public class TestOPCCompliancePackageModel extends TestCase {
* the attempt to create such a relationship and shall treat any such * the attempt to create such a relationship and shall treat any such
* relationship as invalid. * relationship as invalid.
*/ */
@Test
public void testAddRelationshipRelationshipsPartFailure() { public void testAddRelationshipRelationshipsPartFailure() {
OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx"); OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
PackagePartName name1 = null; PackagePartName name1 = null;

View File

@ -17,14 +17,17 @@
package org.apache.poi.openxml4j.opc.compliance; package org.apache.poi.openxml4j.opc.compliance;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import junit.framework.TestCase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.junit.Test;
/** /**
* Test part name Open Packaging Convention compliance. * Test part name Open Packaging Convention compliance.
@ -77,13 +80,14 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
* *
* @author Julien Chable * @author Julien Chable
*/ */
public final class TestOPCCompliancePartName extends TestCase { public final class TestOPCCompliancePartName {
/** /**
* Test some common invalid names. * Test some common invalid names.
* *
* A segment shall not contain percent-encoded unreserved characters. [M1.8] * A segment shall not contain percent-encoded unreserved characters. [M1.8]
*/ */
@Test
public void testInvalidPartNames() { public void testInvalidPartNames() {
String[] invalidNames = { "/", "/xml./doc.xml", "[Content_Types].xml", "//xml/." }; String[] invalidNames = { "/", "/xml./doc.xml", "[Content_Types].xml", "//xml/." };
for (String s : invalidNames) { for (String s : invalidNames) {
@ -102,6 +106,7 @@ public final class TestOPCCompliancePartName extends TestCase {
/** /**
* Test some common valid names. * Test some common valid names.
*/ */
@Test
public void testValidPartNames() throws URISyntaxException { public void testValidPartNames() throws URISyntaxException {
String[] validNames = { "/xml/item1.xml", "/document.xml", String[] validNames = { "/xml/item1.xml", "/document.xml",
"/a/%D1%86.xml" }; "/a/%D1%86.xml" };
@ -113,6 +118,7 @@ public final class TestOPCCompliancePartName extends TestCase {
/** /**
* A part name shall not be empty. [M1.1] * A part name shall not be empty. [M1.1]
*/ */
@Test
public void testEmptyPartNameFailure() throws URISyntaxException { public void testEmptyPartNameFailure() throws URISyntaxException {
try { try {
PackagingURIHelper.createPartName(new URI("")); PackagingURIHelper.createPartName(new URI(""));
@ -129,6 +135,7 @@ public final class TestOPCCompliancePartName extends TestCase {
* *
* A segment shall include at least one non-dot character. [M1.10] * A segment shall include at least one non-dot character. [M1.10]
*/ */
@Test
public void testPartNameWithInvalidSegmentsFailure() { public void testPartNameWithInvalidSegmentsFailure() {
String[] invalidNames = { "//document.xml", "//word/document.xml", String[] invalidNames = { "//document.xml", "//word/document.xml",
"/word//document.rels", "/word//rels//document.rels", "/word//document.rels", "/word//rels//document.rels",
@ -148,6 +155,7 @@ public final class TestOPCCompliancePartName extends TestCase {
* A segment shall not hold any characters other than ipchar (RFC 3987) characters. * A segment shall not hold any characters other than ipchar (RFC 3987) characters.
* [M1.6]. * [M1.6].
*/ */
@Test
public void testPartNameWithNonPCharCharacters() { public void testPartNameWithNonPCharCharacters() {
String[] validNames = { "/doc&.xml" }; String[] validNames = { "/doc&.xml" };
try { try {
@ -164,6 +172,7 @@ public final class TestOPCCompliancePartName extends TestCase {
/** /**
* A segment shall not contain percent-encoded unreserved characters [M1.8]. * A segment shall not contain percent-encoded unreserved characters [M1.8].
*/ */
@Test
public void testPartNameWithUnreservedEncodedCharactersFailure() { public void testPartNameWithUnreservedEncodedCharactersFailure() {
String[] invalidNames = { "/a/docum%65nt.xml" }; String[] invalidNames = { "/a/docum%65nt.xml" };
try { try {
@ -180,6 +189,7 @@ public final class TestOPCCompliancePartName extends TestCase {
/** /**
* A part name shall start with a forward slash ('/') character. [M1.4] * A part name shall start with a forward slash ('/') character. [M1.4]
*/ */
@Test
public void testPartNameStartsWithAForwardSlashFailure() public void testPartNameStartsWithAForwardSlashFailure()
throws URISyntaxException { throws URISyntaxException {
try { try {
@ -193,6 +203,7 @@ public final class TestOPCCompliancePartName extends TestCase {
/** /**
* A part name shall not have a forward slash as the last character. [M1.5] * A part name shall not have a forward slash as the last character. [M1.5]
*/ */
@Test
public void testPartNameEndsWithAForwardSlashFailure() public void testPartNameEndsWithAForwardSlashFailure()
throws URISyntaxException { throws URISyntaxException {
try { try {
@ -207,6 +218,7 @@ public final class TestOPCCompliancePartName extends TestCase {
* Part name equivalence is determined by comparing part names as * Part name equivalence is determined by comparing part names as
* case-insensitive ASCII strings. [M1.12] * case-insensitive ASCII strings. [M1.12]
*/ */
@Test
public void testPartNameComparaison() throws Exception { public void testPartNameComparaison() throws Exception {
String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" }; String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" };
String[] partName2 = { "/WORD/DocUment.XML", "/docProps/core.xml", "/rels/.rels" }; String[] partName2 = { "/WORD/DocUment.XML", "/docProps/core.xml", "/rels/.rels" };
@ -225,6 +237,7 @@ public final class TestOPCCompliancePartName extends TestCase {
* *
* All the comparisons MUST FAIL ! * All the comparisons MUST FAIL !
*/ */
@Test
public void testPartNameComparaisonFailure() throws Exception { public void testPartNameComparaisonFailure() throws Exception {
String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" }; String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" };
String[] partName2 = { "/WORD/DocUment.XML2", "/docProp/core.xml", "/rels/rels" }; String[] partName2 = { "/WORD/DocUment.XML2", "/docProp/core.xml", "/rels/rels" };