From 24c4f94962bba1f5fa884148b78ac71422d81f99 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Wed, 1 Apr 2020 23:25:33 +0000 Subject: [PATCH] Sonar Fixes and remove obsolete classes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1876019 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/BaseIntegrationTest.java | 171 ---- src/java/org/apache/poi/util/XMLHelper.java | 3 +- .../apache/poi/openxml4j/opc/TestPackage.java | 928 +++++++++--------- .../poi/ss/format/TestCellFormatPart.java | 170 ++-- .../poi/ss/format/CellFormatTestBase.java | 309 ------ 5 files changed, 550 insertions(+), 1031 deletions(-) delete mode 100644 src/integrationtest/org/apache/poi/BaseIntegrationTest.java delete mode 100644 src/testcases/org/apache/poi/ss/format/CellFormatTestBase.java diff --git a/src/integrationtest/org/apache/poi/BaseIntegrationTest.java b/src/integrationtest/org/apache/poi/BaseIntegrationTest.java deleted file mode 100644 index 7f60dbb74b..0000000000 --- a/src/integrationtest/org/apache/poi/BaseIntegrationTest.java +++ /dev/null @@ -1,171 +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; - -import org.apache.poi.poifs.filesystem.OfficeXmlFileException; -import org.apache.poi.stress.FileHandler; -import org.apache.poi.stress.HSLFFileHandler; -import org.apache.poi.stress.HSSFFileHandler; -import org.apache.poi.stress.HWPFFileHandler; -import org.apache.poi.stress.XSLFFileHandler; -import org.apache.poi.stress.XSSFFileHandler; -import org.apache.poi.stress.XWPFFileHandler; -import org.junit.Assume; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.zip.ZipException; - -import static org.junit.Assert.assertNotNull; - -public class BaseIntegrationTest { - private final File rootDir; - private String file; - private FileHandler handler; - - public BaseIntegrationTest(File rootDir, String file, FileHandler handler) { - this.rootDir = rootDir; - this.file = file; - this.handler = handler; - } - - public void test() throws Exception { - assertNotNull("Unknown file extension for file: " + file + ": " + TestAllFiles.getExtension(file), handler); - testOneFile(new File(rootDir, file)); - } - - protected void testOneFile(File inputFile) throws Exception { - try { - handleFile(inputFile); - } catch (OfficeXmlFileException e) { - // switch XWPF and HWPF and so forth depending on the error message - handleWrongOLE2XMLExtension(inputFile, e); - } catch (OldFileFormatException e) { - if (e.getClass().equals(OldFileFormatException.class)) { - // Not even text extraction is supported for these: handler.handleExtracting(inputFile); - Assume.assumeFalse("File " + file + " excluded because it is unsupported old Excel format", true); - } - // otherwise try at least to perform extracting tests on these old files - } catch (EncryptedDocumentException e) { - // Do not try to read encrypted files - Assume.assumeFalse("File " + file + " excluded because it is password-encrypted", true); - } catch (ZipException e) { - // some files are corrupted - if (e.getMessage().equals("unexpected EOF") || e.getMessage().equals("Truncated ZIP file")) { - Assume.assumeFalse("File " + file + " excluded because the Zip file is incomplete", true); - } - - throw e; - } catch (IOException e) { - // ignore some other ways of corrupted files - String message = e.getMessage(); - if(message != null && message.contains("Truncated ZIP file")) { - Assume.assumeFalse("File " + file + " excluded because the Zip file is incomplete", true); - } - - // sometimes binary format has XML-format-extension... - if(message != null && message.contains("rong file format or file extension for OO XML file")) { - handleWrongOLE2XMLExtension(inputFile, e); - return; - } - - throw e; - } catch (IllegalArgumentException e) { - // ignore errors for documents with incorrect extension - String message = e.getMessage(); - if(message != null && (message.equals("The document is really a RTF file") || - message.equals("The document is really a PDF file") || - message.equals("The document is really a HTML file"))) { - Assume.assumeFalse("File " + file + " excluded because it is actually a PDF/RTF/HTML file", true); - } - - if(message != null && message.equals("The document is really a OOXML file")) { - handleWrongOLE2XMLExtension(inputFile, e); - return; - } - - throw e; - } - - try { - handler.handleExtracting(inputFile); - } catch (EncryptedDocumentException e) { - // Do not try to read encrypted files - Assume.assumeFalse("File " + file + " excluded because it is password-encrypted", true); - } - } - - void handleWrongOLE2XMLExtension(File inputFile, Exception e) throws Exception { - // we sometimes have wrong extensions, so for some exceptions we try to handle it - // with the correct FileHandler instead - String message = e.getMessage(); - - // ignore some file-types that we do not want to handle here - Assume.assumeFalse("File " + file + " excluded because it is actually a PDF/RTF/HTML file", - message != null && (message.equals("The document is really a RTF file") || - message.equals("The document is really a PDF file") || - message.equals("The document is really a HTML file"))); - - if(message != null && (message.equals("The document is really a XLS file"))) { - handler = TestAllFiles.HANDLERS.get(".xls"); - handleFile(inputFile); - } else if(message != null && (message.equals("The document is really a PPT file"))) { - handler = TestAllFiles.HANDLERS.get(".ppt"); - handleFile(inputFile); - } else if(message != null && (message.equals("The document is really a DOC file"))) { - handler = TestAllFiles.HANDLERS.get(".doc"); - handleFile(inputFile); - } else if(message != null && (message.equals("The document is really a VSD file"))) { - handler = TestAllFiles.HANDLERS.get(".vsd"); - handleFile(inputFile); - - // use XWPF instead of HWPF and XSSF instead of HSSF as the file seems to have the wrong extension - } else if (handler instanceof HWPFFileHandler) { - handler = TestAllFiles.HANDLERS.get(".docx"); - handleFile(inputFile); - } else if (handler instanceof HSSFFileHandler) { - handler = TestAllFiles.HANDLERS.get(".xlsx"); - handleFile(inputFile); - } else if (handler instanceof HSLFFileHandler) { - handler = TestAllFiles.HANDLERS.get(".pptx"); - handleFile(inputFile); - - // and the other way around, use HWPF instead of XWPF and so forth - } else if(handler instanceof XWPFFileHandler) { - handler = TestAllFiles.HANDLERS.get(".doc"); - handleFile(inputFile); - } else if(handler instanceof XSSFFileHandler) { - handler = TestAllFiles.HANDLERS.get(".xls"); - handleFile(inputFile); - } else if(handler instanceof XSLFFileHandler) { - handler = TestAllFiles.HANDLERS.get(".ppt"); - handleFile(inputFile); - } else { - // nothing matched => throw the exception to the outside - throw e; - } - } - - private void handleFile(File inputFile) throws Exception { - try (InputStream newStream = new BufferedInputStream(new FileInputStream(inputFile), 64*1024)) { - handler.handleFile(newStream, inputFile.getAbsolutePath()); - } - } -} diff --git a/src/java/org/apache/poi/util/XMLHelper.java b/src/java/org/apache/poi/util/XMLHelper.java index beca533611..e99e7d9ecb 100644 --- a/src/java/org/apache/poi/util/XMLHelper.java +++ b/src/java/org/apache/poi/util/XMLHelper.java @@ -213,7 +213,7 @@ public final class XMLHelper { return XMLEventFactory.newInstance(); } - @SuppressWarnings("squid:S4435") + @SuppressWarnings({"squid:S4435","java:S2755"}) public static TransformerFactory getTransformerFactory() { TransformerFactory factory = TransformerFactory.newInstance(); trySet(factory::setFeature, FEATURE_SECURE_PROCESSING, true); @@ -232,6 +232,7 @@ public final class XMLHelper { return serializer; } + @SuppressWarnings("java:S2755") public static SchemaFactory getSchemaFactory() { SchemaFactory factory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); trySet(factory::setFeature, FEATURE_SECURE_PROCESSING, true); diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java index fcc6171b4a..e9a62f8c79 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java @@ -17,55 +17,18 @@ package org.apache.poi.openxml4j.opc; -import com.google.common.hash.Hashing; -import com.google.common.io.Files; -import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; -import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; -import org.apache.commons.compress.archivers.zip.ZipFile; -import org.apache.poi.EncryptedDocumentException; -import org.apache.poi.POIDataSamples; -import org.apache.poi.POITestCase; -import org.apache.poi.UnsupportedFileFormatException; -import org.apache.poi.extractor.POITextExtractor; -import org.apache.poi.ooxml.POIXMLException; -import org.apache.poi.ooxml.extractor.ExtractorFactory; -import org.apache.poi.ooxml.util.DocumentHelper; -import org.apache.poi.openxml4j.OpenXML4JTestDataSamples; -import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.openxml4j.exceptions.InvalidOperationException; -import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; -import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException; -import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException; -import org.apache.poi.openxml4j.exceptions.OpenXML4JException; -import org.apache.poi.openxml4j.opc.internal.ContentTypeManager; -import org.apache.poi.openxml4j.opc.internal.FileHelper; -import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; -import org.apache.poi.openxml4j.opc.internal.ZipHelper; -import org.apache.poi.openxml4j.util.ZipSecureFile; -import org.apache.poi.sl.usermodel.SlideShow; -import org.apache.poi.sl.usermodel.SlideShowFactory; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.usermodel.WorkbookFactory; -import org.apache.poi.util.IOUtils; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; -import org.apache.poi.util.TempFile; -import org.apache.poi.xssf.XSSFTestDataSamples; -import org.apache.poi.xssf.streaming.SXSSFWorkbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.xwpf.usermodel.XWPFRelation; -import org.apache.xmlbeans.XmlException; -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; +import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.getOutputFile; +import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.getSampleFile; +import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.getSampleFileName; +import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.openSampleStream; +import static org.apache.poi.openxml4j.opc.PackagingURIHelper.createPartName; +import static org.hamcrest.core.StringContains.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; @@ -88,15 +51,61 @@ import java.util.TreeMap; import java.util.function.BiConsumer; import java.util.regex.Pattern; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import com.google.common.hash.Hashing; +import com.google.common.io.Files; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; +import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.poi.EncryptedDocumentException; +import org.apache.poi.POIDataSamples; +import org.apache.poi.POITestCase; +import org.apache.poi.UnsupportedFileFormatException; +import org.apache.poi.extractor.POITextExtractor; +import org.apache.poi.ooxml.POIXMLException; +import org.apache.poi.ooxml.extractor.ExtractorFactory; +import org.apache.poi.ooxml.util.DocumentHelper; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.exceptions.InvalidOperationException; +import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; +import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException; +import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException; +import org.apache.poi.openxml4j.exceptions.OpenXML4JException; +import org.apache.poi.openxml4j.opc.internal.ContentTypeManager; +import org.apache.poi.openxml4j.opc.internal.FileHelper; +import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; +import org.apache.poi.openxml4j.opc.internal.ZipHelper; +import org.apache.poi.openxml4j.util.ZipSecureFile; +import org.apache.poi.sl.usermodel.SlideShowFactory; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.util.IOUtils; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; +import org.apache.poi.util.TempFile; +import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFRelation; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xwpf.usermodel.XWPFRelation; +import org.apache.xmlbeans.XmlException; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; +import org.hamcrest.core.AllOf; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; public final class TestPackage { private static final POILogger logger = POILogFactory.getLogger(TestPackage.class); + private static final String NS_OOXML_WP_MAIN = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; + private static final String CONTENT_EXT_PROPS = "application/vnd.openxmlformats-officedocument.extended-properties+xml"; + private static final POIDataSamples xlsSamples = POIDataSamples.getSpreadSheetInstance(); @Rule public ExpectedException expectedEx = ExpectedException.none(); @@ -106,21 +115,21 @@ public final class TestPackage { */ @Test public void openSave() throws IOException, InvalidFormatException { - String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); - File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageOpenSaveTMP.docx"); + String originalFile = getSampleFileName("TestPackageCommon.docx"); + File targetFile = getOutputFile("TestPackageOpenSaveTMP.docx"); - @SuppressWarnings("resource") - OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); - try { - p.save(targetFile.getAbsoluteFile()); + try (OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE)) { + try { + p.save(targetFile.getAbsoluteFile()); - // Compare the original and newly saved document - assertTrue(targetFile.exists()); - ZipFileAssert.assertEquals(new File(originalFile), targetFile); - assertTrue(targetFile.delete()); - } finally { - // use revert to not re-write the input file - p.revert(); + // Compare the original and newly saved document + assertTrue(targetFile.exists()); + ZipFileAssert.assertEquals(new File(originalFile), targetFile); + assertTrue(targetFile.delete()); + } finally { + // use revert to not re-write the input file + p.revert(); + } } } @@ -131,37 +140,24 @@ public final class TestPackage { @Test public void createGetsContentTypes() throws IOException, InvalidFormatException, SecurityException, IllegalArgumentException { - File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx"); + File targetFile = getOutputFile("TestCreatePackageTMP.docx"); // Zap the target file, in case of an earlier run if(targetFile.exists()) { assertTrue(targetFile.delete()); } - @SuppressWarnings("resource") - OPCPackage pkg = OPCPackage.create(targetFile); - - // Check it has content types for rels and xml - ContentTypeManager ctm = getContentTypeManager(pkg); - assertEquals( - "application/xml", - ctm.getContentType( - PackagingURIHelper.createPartName("/foo.xml") - ) - ); - assertEquals( - ContentTypes.RELATIONSHIPS_PART, - ctm.getContentType( - PackagingURIHelper.createPartName("/foo.rels") - ) - ); - assertNull( - ctm.getContentType( - PackagingURIHelper.createPartName("/foo.txt") - ) - ); - - pkg.revert(); + try (OPCPackage pkg = OPCPackage.create(targetFile)) { + try { + // Check it has content types for rels and xml + ContentTypeManager ctm = getContentTypeManager(pkg); + assertEquals("application/xml", ctm.getContentType(createPartName("/foo.xml"))); + assertEquals(ContentTypes.RELATIONSHIPS_PART, ctm.getContentType(createPartName("/foo.rels"))); + assertNull(ctm.getContentType(createPartName("/foo.txt"))); + } finally { + pkg.revert(); + } + } } /** @@ -169,9 +165,9 @@ public final class TestPackage { */ @Test public void createPackageAddPart() throws IOException, InvalidFormatException { - File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx"); + File targetFile = getOutputFile("TestCreatePackageTMP.docx"); - File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestCreatePackageOUTPUT.docx"); + File expectedFile = getSampleFile("TestCreatePackageOUTPUT.docx"); // Zap the target file, in case of an earlier run if(targetFile.exists()) { @@ -180,27 +176,23 @@ public final class TestPackage { // Create a package OPCPackage pkg = OPCPackage.create(targetFile); - PackagePartName corePartName = PackagingURIHelper - .createPartName("/word/document.xml"); + PackagePartName corePartName = createPartName("/word/document.xml"); pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT, "rId1"); - PackagePart corePart = pkg - .createPart( - corePartName, - "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"); + PackagePart corePart = pkg.createPart(corePartName, XWPFRelation.DOCUMENT.getContentType()); Document doc = DocumentHelper.createDocument(); - Element elDocument = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:document"); + Element elDocument = doc.createElementNS(NS_OOXML_WP_MAIN, "w:document"); doc.appendChild(elDocument); - Element elBody = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:body"); + Element elBody = doc.createElementNS(NS_OOXML_WP_MAIN, "w:body"); elDocument.appendChild(elBody); - Element elParagraph = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:p"); + Element elParagraph = doc.createElementNS(NS_OOXML_WP_MAIN, "w:p"); elBody.appendChild(elParagraph); - Element elRun = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:r"); + Element elRun = doc.createElementNS(NS_OOXML_WP_MAIN, "w:r"); elParagraph.appendChild(elRun); - Element elText = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:t"); + Element elText = doc.createElementNS(NS_OOXML_WP_MAIN, "w:t"); elRun.appendChild(elText); elText.setTextContent("Hello Open XML !"); @@ -219,82 +211,79 @@ public final class TestPackage { @Test public void createPackageWithCoreDocument() throws IOException, InvalidFormatException, URISyntaxException, SAXException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - OPCPackage pkg = OPCPackage.create(baos); + try (OPCPackage pkg = OPCPackage.create(baos)) { - // Add a core document - PackagePartName corePartName = PackagingURIHelper.createPartName("/xl/workbook.xml"); - // Create main part relationship - pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT, "rId1"); - // Create main document part - PackagePart corePart = pkg.createPart(corePartName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"); - // Put in some dummy content - OutputStream coreOut = corePart.getOutputStream(); - coreOut.write("".getBytes(StandardCharsets.UTF_8)); - coreOut.close(); + // Add a core document + PackagePartName corePartName = createPartName("/xl/workbook.xml"); + // Create main part relationship + pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT, "rId1"); + // Create main document part + PackagePart corePart = pkg.createPart(corePartName, XSSFRelation.WORKBOOK.getContentType()); + // Put in some dummy content + try (OutputStream coreOut = corePart.getOutputStream()) { + coreOut.write("".getBytes(StandardCharsets.UTF_8)); + } - // And another bit - PackagePartName sheetPartName = PackagingURIHelper.createPartName("/xl/worksheets/sheet1.xml"); - PackageRelationship rel = - corePart.addRelationship(sheetPartName, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet", "rSheet1"); - assertNotNull(rel); + // And another bit + PackagePartName sheetPartName = createPartName("/xl/worksheets/sheet1.xml"); + PackageRelationship rel = corePart.addRelationship( + sheetPartName, TargetMode.INTERNAL, XSSFRelation.WORKSHEET.getRelation(), "rSheet1"); + assertNotNull(rel); - PackagePart part = pkg.createPart(sheetPartName, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"); - assertNotNull(part); + PackagePart part = pkg.createPart(sheetPartName, XSSFRelation.WORKSHEET.getContentType()); + assertNotNull(part); - // Dummy content again - coreOut = corePart.getOutputStream(); - coreOut.write("".getBytes(StandardCharsets.UTF_8)); - coreOut.close(); + // Dummy content again + try (OutputStream coreOut = corePart.getOutputStream()) { + coreOut.write("".getBytes(StandardCharsets.UTF_8)); + } - //add a relationship with internal target: "#Sheet1!A1" - corePart.addRelationship(new URI("#Sheet1!A1"), TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", "rId2"); + //add a relationship with internal target: "#Sheet1!A1" + corePart.addRelationship(new URI("#Sheet1!A1"), TargetMode.INTERNAL, PackageRelationshipTypes.HYPERLINK_PART, "rId2"); - // Check things are as expected - PackageRelationshipCollection coreRels = - pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT); - assertEquals(1, coreRels.size()); - PackageRelationship coreRel = coreRels.getRelationship(0); - assertNotNull(coreRel); - assertEquals("/", coreRel.getSourceURI().toString()); - assertEquals("/xl/workbook.xml", coreRel.getTargetURI().toString()); - assertNotNull(pkg.getPart(coreRel)); + // Check things are as expected + PackageRelationshipCollection coreRels = + pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT); + assertEquals(1, coreRels.size()); + PackageRelationship coreRel = coreRels.getRelationship(0); + assertNotNull(coreRel); + assertEquals("/", coreRel.getSourceURI().toString()); + assertEquals("/xl/workbook.xml", coreRel.getTargetURI().toString()); + assertNotNull(pkg.getPart(coreRel)); + } - // Save and re-load - pkg.close(); + // Save and re-load File tmp = TempFile.createTempFile("testCreatePackageWithCoreDocument", ".zip"); try (OutputStream fout = new FileOutputStream(tmp)) { - fout.write(baos.toByteArray()); + baos.writeTo(fout); + fout.flush(); } - pkg = OPCPackage.open(tmp.getPath()); - //tmp.delete(); - try { + try (OPCPackage pkg = OPCPackage.open(tmp.getPath())) { // Check still right - coreRels = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT); + PackageRelationshipCollection coreRels = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT); assertEquals(1, coreRels.size()); - coreRel = coreRels.getRelationship(0); + PackageRelationship coreRel = coreRels.getRelationship(0); assertNotNull(coreRel); assertEquals("/", coreRel.getSourceURI().toString()); assertEquals("/xl/workbook.xml", coreRel.getTargetURI().toString()); - corePart = pkg.getPart(coreRel); + PackagePart corePart = pkg.getPart(coreRel); assertNotNull(corePart); - PackageRelationshipCollection rels = corePart.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"); + PackageRelationshipCollection rels = corePart.getRelationshipsByType(PackageRelationshipTypes.HYPERLINK_PART); assertEquals(1, rels.size()); - rel = rels.getRelationship(0); + PackageRelationship rel = rels.getRelationship(0); assertNotNull(rel); assertEquals("Sheet1!A1", rel.getTargetURI().getRawFragment()); assertMSCompatibility(pkg); - } finally { - pkg.close(); } } private void assertMSCompatibility(OPCPackage pkg) throws IOException, InvalidFormatException, SAXException { - PackagePartName relName = PackagingURIHelper.createPartName(PackageRelationship.getContainerPartRelationship()); + PackagePartName relName = createPartName(PackageRelationship.getContainerPartRelationship()); PackagePart relPart = pkg.getPart(relName); Document xmlRelationshipsDoc = DocumentHelper.readDocument(relPart.getInputStream()); @@ -315,11 +304,11 @@ public final class TestPackage { */ @Test public void openPackage() throws IOException, InvalidFormatException { - File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestOpenPackageTMP.docx"); + File targetFile = getOutputFile("TestOpenPackageTMP.docx"); - File inputFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageINPUT.docx"); + File inputFile = getSampleFile("TestOpenPackageINPUT.docx"); - File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageOUTPUT.docx"); + File expectedFile = getSampleFile("TestOpenPackageOUTPUT.docx"); // Copy the input file in the output directory FileHelper.copyFile(inputFile, targetFile); @@ -328,30 +317,29 @@ public final class TestPackage { OPCPackage pkg = OPCPackage.open(targetFile.getAbsolutePath()); // Modify core part - PackagePartName corePartName = PackagingURIHelper - .createPartName("/word/document.xml"); + PackagePartName corePartName = createPartName("/word/document.xml"); PackagePart corePart = pkg.getPart(corePartName); // Delete some part to have a valid document for (PackageRelationship rel : corePart.getRelationships()) { corePart.removeRelationship(rel.getId()); - pkg.removePart(PackagingURIHelper.createPartName(PackagingURIHelper + pkg.removePart(createPartName(PackagingURIHelper .resolvePartUri(corePart.getPartName().getURI(), rel .getTargetURI()))); } // Create a content Document doc = DocumentHelper.createDocument(); - Element elDocument = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:document"); + Element elDocument = doc.createElementNS(NS_OOXML_WP_MAIN, "w:document"); doc.appendChild(elDocument); - Element elBody = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:body"); + Element elBody = doc.createElementNS(NS_OOXML_WP_MAIN, "w:body"); elDocument.appendChild(elBody); - Element elParagraph = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:p"); + Element elParagraph = doc.createElementNS(NS_OOXML_WP_MAIN, "w:p"); elBody.appendChild(elParagraph); - Element elRun = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:r"); + Element elRun = doc.createElementNS(NS_OOXML_WP_MAIN, "w:r"); elParagraph.appendChild(elRun); - Element elText = doc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:t"); + Element elText = doc.createElementNS(NS_OOXML_WP_MAIN, "w:t"); elRun.appendChild(elText); elText.setTextContent("Hello Open XML !"); @@ -375,23 +363,23 @@ public final class TestPackage { */ @Test public void saveToOutputStream() throws IOException, InvalidFormatException { - String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); - File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageOpenSaveTMP.docx"); + String originalFile = getSampleFileName("TestPackageCommon.docx"); + File targetFile = getOutputFile("TestPackageOpenSaveTMP.docx"); - @SuppressWarnings("resource") - OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); - try { - try (FileOutputStream fout = new FileOutputStream(targetFile)) { - p.save(fout); + try (OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE)) { + try { + try (FileOutputStream fout = new FileOutputStream(targetFile)) { + p.save(fout); + } + + // Compare the original and newly saved document + assertTrue(targetFile.exists()); + ZipFileAssert.assertEquals(new File(originalFile), targetFile); + assertTrue(targetFile.delete()); + } finally { + // use revert to not re-write the input file + p.revert(); } - - // Compare the original and newly saved document - assertTrue(targetFile.exists()); - ZipFileAssert.assertEquals(new File(originalFile), targetFile); - assertTrue(targetFile.delete()); - } finally { - // use revert to not re-write the input file - p.revert(); } } @@ -402,12 +390,10 @@ public final class TestPackage { */ @Test public void openFromInputStream() throws IOException, InvalidFormatException { - String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); - - try (FileInputStream finp = new FileInputStream(originalFile)) { - @SuppressWarnings("resource") - OPCPackage p = OPCPackage.open(finp); + String originalFile = getSampleFileName("TestPackageCommon.docx"); + try (FileInputStream finp = new FileInputStream(originalFile); + OPCPackage p = OPCPackage.open(finp)) { try { assertNotNull(p); assertNotNull(p.getRelationships()); @@ -415,7 +401,7 @@ public final class TestPackage { // Check it has the usual bits assertTrue(p.hasRelationships()); - assertTrue(p.containPart(PackagingURIHelper.createPartName("/_rels/.rels"))); + assertTrue(p.containPart(createPartName("/_rels/.rels"))); } finally { p.revert(); } @@ -428,123 +414,94 @@ public final class TestPackage { @Test @Ignore public void removePartRecursive() throws IOException, InvalidFormatException, URISyntaxException { - String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); - File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageRemovePartRecursiveOUTPUT.docx"); - File tempFile = OpenXML4JTestDataSamples.getOutputFile("TestPackageRemovePartRecursiveTMP.docx"); + String originalFile = getSampleFileName("TestPackageCommon.docx"); + File targetFile = getOutputFile("TestPackageRemovePartRecursiveOUTPUT.docx"); + File tempFile = getOutputFile("TestPackageRemovePartRecursiveTMP.docx"); - @SuppressWarnings("resource") - OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE); - try { - p.removePartRecursive(PackagingURIHelper.createPartName(new URI( - "/word/document.xml"))); + try (OPCPackage p = OPCPackage.open(originalFile, PackageAccess.READ_WRITE)) { + p.removePartRecursive(createPartName(new URI("/word/document.xml"))); p.save(tempFile.getAbsoluteFile()); // Compare the original and newly saved document assertTrue(targetFile.exists()); ZipFileAssert.assertEquals(targetFile, tempFile); assertTrue(targetFile.delete()); - } finally { p.revert(); } } @Test - public void deletePart() throws InvalidFormatException { - TreeMap expectedValues; - TreeMap values; - - values = new TreeMap<>(); + public void deletePart() throws InvalidFormatException, IOException { + final TreeMap expectedValues = new TreeMap<>(); + final TreeMap values = new TreeMap<>(); // Expected values - expectedValues = new TreeMap<>(); - expectedValues.put(PackagingURIHelper.createPartName("/_rels/.rels"), - "application/vnd.openxmlformats-package.relationships+xml"); + expectedValues.put(createPartName("/_rels/.rels"), ContentTypes.RELATIONSHIPS_PART); + expectedValues.put(createPartName("/docProps/app.xml"), CONTENT_EXT_PROPS); + expectedValues.put(createPartName("/docProps/core.xml"), ContentTypes.CORE_PROPERTIES_PART); + expectedValues.put(createPartName("/word/fontTable.xml"), XWPFRelation.FONT_TABLE.getContentType()); + expectedValues.put(createPartName("/word/media/image1.gif"), XWPFRelation.IMAGE_GIF.getContentType()); + expectedValues.put(createPartName("/word/settings.xml"), XWPFRelation.SETTINGS.getContentType()); + expectedValues.put(createPartName("/word/styles.xml"), XWPFRelation.STYLES.getContentType()); + expectedValues.put(createPartName("/word/theme/theme1.xml"), XWPFRelation.THEME.getContentType()); + expectedValues.put(createPartName("/word/webSettings.xml"), XWPFRelation.WEB_SETTINGS.getContentType()); - expectedValues - .put(PackagingURIHelper.createPartName("/docProps/app.xml"), - "application/vnd.openxmlformats-officedocument.extended-properties+xml"); - expectedValues.put(PackagingURIHelper - .createPartName("/docProps/core.xml"), - "application/vnd.openxmlformats-package.core-properties+xml"); - expectedValues - .put(PackagingURIHelper.createPartName("/word/fontTable.xml"), - "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"); - expectedValues.put(PackagingURIHelper - .createPartName("/word/media/image1.gif"), "image/gif"); - expectedValues - .put(PackagingURIHelper.createPartName("/word/settings.xml"), - "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"); - expectedValues - .put(PackagingURIHelper.createPartName("/word/styles.xml"), - "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"); - expectedValues.put(PackagingURIHelper - .createPartName("/word/theme/theme1.xml"), - "application/vnd.openxmlformats-officedocument.theme+xml"); - expectedValues - .put( - PackagingURIHelper - .createPartName("/word/webSettings.xml"), - "application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"); + String filepath = getSampleFileName("sample.docx"); - String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); + try (OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE)) { + try { + // Remove the core part + p.deletePart(createPartName("/word/document.xml")); - @SuppressWarnings("resource") - OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE); - // Remove the core part - p.deletePart(PackagingURIHelper.createPartName("/word/document.xml")); + for (PackagePart part : p.getParts()) { + values.put(part.getPartName(), part.getContentType()); + logger.log(POILogger.DEBUG, part.getPartName()); + } - for (PackagePart part : p.getParts()) { - values.put(part.getPartName(), part.getContentType()); - logger.log(POILogger.DEBUG, part.getPartName()); + // Compare expected values with values return by the package + for (PackagePartName partName : expectedValues.keySet()) { + assertNotNull(values.get(partName)); + assertEquals(expectedValues.get(partName), values.get(partName)); + } + } finally { + // Don't save modifications + p.revert(); + } } - - // Compare expected values with values return by the package - for (PackagePartName partName : expectedValues.keySet()) { - assertNotNull(values.get(partName)); - assertEquals(expectedValues.get(partName), values.get(partName)); - } - // Don't save modifications - p.revert(); } @Test - public void deletePartRecursive() throws InvalidFormatException { - TreeMap expectedValues; - TreeMap values; - - values = new TreeMap<>(); + public void deletePartRecursive() throws InvalidFormatException, IOException { + final TreeMap expectedValues = new TreeMap<>(); + final TreeMap values = new TreeMap<>(); // Expected values - expectedValues = new TreeMap<>(); - expectedValues.put(PackagingURIHelper.createPartName("/_rels/.rels"), - "application/vnd.openxmlformats-package.relationships+xml"); + expectedValues.put(createPartName("/_rels/.rels"), ContentTypes.RELATIONSHIPS_PART); + expectedValues.put(createPartName("/docProps/app.xml"), CONTENT_EXT_PROPS); + expectedValues.put(createPartName("/docProps/core.xml"), ContentTypes.CORE_PROPERTIES_PART); - expectedValues - .put(PackagingURIHelper.createPartName("/docProps/app.xml"), - "application/vnd.openxmlformats-officedocument.extended-properties+xml"); - expectedValues.put(PackagingURIHelper - .createPartName("/docProps/core.xml"), - "application/vnd.openxmlformats-package.core-properties+xml"); + String filepath = getSampleFileName("sample.docx"); - String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); + try (OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE)) { + try { + // Remove the core part + p.deletePartRecursive(createPartName("/word/document.xml")); - @SuppressWarnings("resource") - OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ_WRITE); - // Remove the core part - p.deletePartRecursive(PackagingURIHelper.createPartName("/word/document.xml")); + for (PackagePart part : p.getParts()) { + values.put(part.getPartName(), part.getContentType()); + logger.log(POILogger.DEBUG, part.getPartName()); + } - for (PackagePart part : p.getParts()) { - values.put(part.getPartName(), part.getContentType()); - logger.log(POILogger.DEBUG, part.getPartName()); + // Compare expected values with values return by the package + for (PackagePartName partName : expectedValues.keySet()) { + assertNotNull(values.get(partName)); + assertEquals(expectedValues.get(partName), values.get(partName)); + } + } finally { + // Don't save modifications + p.revert(); + } } - - // Compare expected values with values return by the package - for (PackagePartName partName : expectedValues.keySet()) { - assertNotNull(values.get(partName)); - assertEquals(expectedValues.get(partName), values.get(partName)); - } - // Don't save modifications - p.revert(); } /** @@ -554,37 +511,36 @@ public final class TestPackage { @Test public void openFileThenOverwrite() throws IOException, InvalidFormatException { File tempFile = TempFile.createTempFile("poiTesting","tmp"); - File origFile = OpenXML4JTestDataSamples.getSampleFile("TestPackageCommon.docx"); + File origFile = getSampleFile("TestPackageCommon.docx"); FileHelper.copyFile(origFile, tempFile); - // Open the temp file - OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE); - // Close it - p.close(); + // Open and close the temp file + try (OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE)) { + assertNotNull(p); + } // Delete it assertTrue(tempFile.delete()); // Reset FileHelper.copyFile(origFile, tempFile); - p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE); - - // Save it to the same file - not allowed - try { - p.save(tempFile); - fail("You shouldn't be able to call save(File) to overwrite the current file"); - } catch(InvalidOperationException e) { - // expected here + try (OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ_WRITE)) { + // Save it to the same file - not allowed + try { + p.save(tempFile); + fail("You shouldn't be able to call save(File) to overwrite the current file"); + } catch(InvalidOperationException e) { + // expected here + } } - - p.close(); // Delete it assertTrue(tempFile.delete()); // Open it read only, then close and delete - allowed FileHelper.copyFile(origFile, tempFile); - p = OPCPackage.open(tempFile.toString(), PackageAccess.READ); - p.close(); + try (OPCPackage p = OPCPackage.open(tempFile.toString(), PackageAccess.READ)) { + assertNotNull(p); + } assertTrue(tempFile.delete()); } @@ -596,7 +552,7 @@ public final class TestPackage { public void openFileThenSaveDelete() throws IOException, InvalidFormatException { File tempFile = TempFile.createTempFile("poiTesting","tmp"); File tempFile2 = TempFile.createTempFile("poiTesting","tmp"); - File origFile = OpenXML4JTestDataSamples.getSampleFile("TestPackageCommon.docx"); + File origFile = getSampleFile("TestPackageCommon.docx"); FileHelper.copyFile(origFile, tempFile); // Open the temp file @@ -615,34 +571,34 @@ public final class TestPackage { } @Test - public void getPartsByName() throws InvalidFormatException { - String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); + public void getPartsByName() throws InvalidFormatException, IOException { + String filepath = getSampleFileName("sample.docx"); - @SuppressWarnings("resource") - OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ_WRITE); - try { - List rs = pkg.getPartsByName(Pattern.compile("/word/.*?\\.xml")); - HashMap selected = new HashMap<>(); + try (OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ_WRITE)) { + try { + List rs = pkg.getPartsByName(Pattern.compile("/word/.*?\\.xml")); + HashMap selected = new HashMap<>(); - for(PackagePart p : rs) - selected.put(p.getPartName().getName(), p); + for (PackagePart p : rs) + selected.put(p.getPartName().getName(), p); - assertEquals(6, selected.size()); - assertTrue(selected.containsKey("/word/document.xml")); - assertTrue(selected.containsKey("/word/fontTable.xml")); - assertTrue(selected.containsKey("/word/settings.xml")); - assertTrue(selected.containsKey("/word/styles.xml")); - assertTrue(selected.containsKey("/word/theme/theme1.xml")); - assertTrue(selected.containsKey("/word/webSettings.xml")); - } finally { - // use revert to not re-write the input file - pkg.revert(); - } + assertEquals(6, selected.size()); + assertTrue(selected.containsKey("/word/document.xml")); + assertTrue(selected.containsKey("/word/fontTable.xml")); + assertTrue(selected.containsKey("/word/settings.xml")); + assertTrue(selected.containsKey("/word/styles.xml")); + assertTrue(selected.containsKey("/word/theme/theme1.xml")); + assertTrue(selected.containsKey("/word/webSettings.xml")); + } finally { + // use revert to not re-write the input file + pkg.revert(); + } + } } @Test public void getPartSize() throws IOException, InvalidFormatException { - String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx"); + String filepath = getSampleFileName("sample.docx"); try (OPCPackage pkg = OPCPackage.open(filepath, PackageAccess.READ)) { int checked = 0; for (PackagePart part : pkg.getParts()) { @@ -671,113 +627,133 @@ public final class TestPackage { } @Test - public void replaceContentType() - throws IOException, InvalidFormatException, SecurityException, IllegalArgumentException { - InputStream is = OpenXML4JTestDataSamples.openSampleStream("sample.xlsx"); - @SuppressWarnings("resource") - OPCPackage p = OPCPackage.open(is); + public void replaceContentType() throws IOException, InvalidFormatException { + try (InputStream is = openSampleStream("sample.xlsx"); + OPCPackage p = OPCPackage.open(is)) { + try { + ContentTypeManager mgr = getContentTypeManager(p); - ContentTypeManager mgr = getContentTypeManager(p); + assertTrue(mgr.isContentTypeRegister(XSSFRelation.WORKBOOK.getContentType())); + assertFalse(mgr.isContentTypeRegister(XSSFRelation.MACROS_WORKBOOK.getContentType())); + assertTrue(p.replaceContentType(XSSFRelation.WORKBOOK.getContentType(), XSSFRelation.MACROS_WORKBOOK.getContentType())); - assertTrue(mgr.isContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")); - assertFalse(mgr.isContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml")); - - assertTrue( - p.replaceContentType( - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", - "application/vnd.ms-excel.sheet.macroEnabled.main+xml") - ); - - assertFalse(mgr.isContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")); - assertTrue(mgr.isContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml")); - p.revert(); - is.close(); + assertFalse(mgr.isContentTypeRegister(XSSFRelation.WORKBOOK.getContentType())); + assertTrue(mgr.isContentTypeRegister(XSSFRelation.MACROS_WORKBOOK.getContentType())); + } finally { + p.revert(); + } + } } - /** - * Verify we give helpful exceptions (or as best we can) when - * supplied with non-OOXML file types (eg OLE2, ODF) - */ - @Test - public void NonOOXMLFileTypes() throws Exception { - // Spreadsheet has a good mix of alternate file types - POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); - // OLE2 - Stream - try { - try (InputStream stream = files.openResourceAsStream("SampleSS.xls")) { - OPCPackage.open(stream); - } - fail("Shouldn't be able to open OLE2"); - } catch (OLE2NotOfficeXmlFileException e) { - assertTrue(e.getMessage().contains("The supplied data appears to be in the OLE2 Format")); - assertTrue(e.getMessage().contains("You are calling the part of POI that deals with OOXML")); - } - // OLE2 - File - try { - OPCPackage.open(files.getFile("SampleSS.xls")); - fail("Shouldn't be able to open OLE2"); - } catch (OLE2NotOfficeXmlFileException e) { - assertTrue(e.getMessage().contains("The supplied data appears to be in the OLE2 Format")); - assertTrue(e.getMessage().contains("You are calling the part of POI that deals with OOXML")); - } + @Test + public void NonOOXML_OLE2Stream() throws Exception { + expectedEx.expect(OLE2NotOfficeXmlFileException.class); + expectedEx.expectMessage(AllOf.allOf( + containsString("The supplied data appears to be in the OLE2 Format"), + containsString("You are calling the part of POI that deals with OOXML") + )); + try (InputStream stream = xlsSamples.openResourceAsStream("SampleSS.xls"); + OPCPackage p = OPCPackage.open(stream)) { + assertNotNull(p); + fail("Shouldn't be able to open OLE2"); + } + } - // Raw XML - Stream - try { - try (InputStream stream = files.openResourceAsStream("SampleSS.xml")) { - OPCPackage.open(stream); - } - fail("Shouldn't be able to open XML"); - } catch (NotOfficeXmlFileException e) { - assertTrue(e.getMessage().contains("The supplied data appears to be a raw XML file")); - assertTrue(e.getMessage().contains("Formats such as Office 2003 XML")); - } - // Raw XML - File - try { - OPCPackage.open(files.getFile("SampleSS.xml")); - fail("Shouldn't be able to open XML"); - } catch (NotOfficeXmlFileException e) { - assertTrue(e.getMessage().contains("The supplied data appears to be a raw XML file")); - assertTrue(e.getMessage().contains("Formats such as Office 2003 XML")); - } + @Test + public void NonOOXML_OLE2File() throws Exception { + expectedEx.expect(OLE2NotOfficeXmlFileException.class); + expectedEx.expectMessage(AllOf.allOf( + containsString("The supplied data appears to be in the OLE2 Format"), + containsString("You are calling the part of POI that deals with OOXML") + )); + try (OPCPackage p = OPCPackage.open(xlsSamples.getFile("SampleSS.xls"))) { + assertNotNull(p); + fail("Shouldn't be able to open OLE2"); + } + } - // ODF / ODS - Stream - try { - try (InputStream stream = files.openResourceAsStream("SampleSS.ods")) { - OPCPackage.open(stream); - } - fail("Shouldn't be able to open ODS"); - } catch (ODFNotOfficeXmlFileException e) { - assertTrue(e.toString().contains("The supplied data appears to be in ODF")); - assertTrue(e.toString().contains("Formats like these (eg ODS")); - } - // ODF / ODS - File - try { - OPCPackage.open(files.getFile("SampleSS.ods")); - fail("Shouldn't be able to open ODS"); - } catch (ODFNotOfficeXmlFileException e) { - assertTrue(e.toString().contains("The supplied data appears to be in ODF")); - assertTrue(e.toString().contains("Formats like these (eg ODS")); - } + @Test + public void NonOOXML_RawXmlStream() throws Exception { + expectedEx.expect(NotOfficeXmlFileException.class); + expectedEx.expectMessage(AllOf.allOf( + containsString("The supplied data appears to be a raw XML file"), + containsString("Formats such as Office 2003 XML") + )); + try (InputStream stream = xlsSamples.openResourceAsStream("SampleSS.xml"); + OPCPackage p = OPCPackage.open(stream)) { + assertNotNull(p); + fail("Shouldn't be able to open XML"); + } + } - // Plain Text - Stream - try { - try (InputStream stream = files.openResourceAsStream("SampleSS.txt")) { - OPCPackage.open(stream); - } - fail("Shouldn't be able to open Plain Text"); - } catch (NotOfficeXmlFileException e) { - assertTrue(e.getMessage().contains("No valid entries or contents found")); - assertTrue(e.getMessage().contains("not a valid OOXML")); - } - // Plain Text - File - try { - OPCPackage.open(files.getFile("SampleSS.txt")); - fail("Shouldn't be able to open Plain Text"); - } catch (UnsupportedFileFormatException e) { - // Unhelpful low-level error, sorry - } - } + @Test + public void NonOOXML_RawXmlFile() throws Exception { + expectedEx.expect(NotOfficeXmlFileException.class); + expectedEx.expectMessage(AllOf.allOf( + containsString("The supplied data appears to be a raw XML file"), + containsString("Formats such as Office 2003 XML") + )); + try (OPCPackage p = OPCPackage.open(xlsSamples.getFile("SampleSS.xml"))) { + assertNotNull(p); + fail("Shouldn't be able to open XML"); + } + } + + @Test + public void NonOOXML_ODFStream() throws Exception { + expectedEx.expect(ODFNotOfficeXmlFileException.class); + expectedEx.expectMessage(AllOf.allOf( + containsString("The supplied data appears to be in ODF"), + containsString("Formats like these (eg ODS") + )); + try (InputStream stream = xlsSamples.openResourceAsStream("SampleSS.ods"); + OPCPackage p = OPCPackage.open(stream)) { + assertNotNull(p); + fail("Shouldn't be able to open ODS"); + } + } + + @Test + public void NonOOXML_ODFFile() throws Exception { + expectedEx.expect(ODFNotOfficeXmlFileException.class); + expectedEx.expectMessage(AllOf.allOf( + containsString("The supplied data appears to be in ODF"), + containsString("Formats like these (eg ODS") + )); + try (OPCPackage p = OPCPackage.open(xlsSamples.getFile("SampleSS.ods"))) { + assertNotNull(p); + fail("Shouldn't be able to open ODS"); + } + } + + @Test + public void NonOOXML_TextStream() throws Exception { + expectedEx.expect(NotOfficeXmlFileException.class); + expectedEx.expectMessage(AllOf.allOf( + containsString("No valid entries or contents found"), + containsString("not a valid OOXML") + )); + try (InputStream stream = xlsSamples.openResourceAsStream("SampleSS.txt"); + OPCPackage p = OPCPackage.open(stream)) { + assertNotNull(p); + fail("Shouldn't be able to open Plain Text"); + } + } + + @Test + public void NonOOXML_TextFile() throws Exception { + // Unhelpful low-level error, sorry + expectedEx.expect(UnsupportedFileFormatException.class); + expectedEx.expectMessage(AllOf.allOf( + containsString("No valid entries or contents found"), + containsString("not a valid OOXML") + )); + try (OPCPackage p = OPCPackage.open(xlsSamples.getFile("SampleSS.txt"))) { + assertNotNull(p); + fail("Shouldn't be able to open Plain Text"); + } + } /** * Zip bomb handling test @@ -789,7 +765,7 @@ public final class TestPackage { throws IOException, EncryptedDocumentException { ByteArrayOutputStream bos = new ByteArrayOutputStream(2500000); - try (ZipFile zipFile = ZipHelper.openZipFile(OpenXML4JTestDataSamples.getSampleFile("sample.xlsx")); + try (ZipFile zipFile = ZipHelper.openZipFile(getSampleFile("sample.xlsx")); ZipArchiveOutputStream append = new ZipArchiveOutputStream(bos)) { assertNotNull(zipFile); @@ -952,14 +928,14 @@ public final class TestPackage { @Test public void testConstructors() throws IOException { // verify the various ways to construct a ZipSecureFile - File file = OpenXML4JTestDataSamples.getSampleFile("sample.xlsx"); - ZipSecureFile zipFile = new ZipSecureFile(file); - assertNotNull(zipFile.getName()); - zipFile.close(); + File file = getSampleFile("sample.xlsx"); + try (ZipSecureFile zipFile = new ZipSecureFile(file)) { + assertNotNull(zipFile.getName()); + } - zipFile = new ZipSecureFile(file.getAbsolutePath()); - assertNotNull(zipFile.getName()); - zipFile.close(); + try (ZipSecureFile zipFile = new ZipSecureFile(file.getAbsolutePath())) { + assertNotNull(zipFile.getName()); + } } @Test @@ -976,83 +952,73 @@ public final class TestPackage { // bug 60128 @Test(expected=NotOfficeXmlFileException.class) public void testCorruptFile() throws InvalidFormatException { - File file = OpenXML4JTestDataSamples.getSampleFile("invalid.xlsx"); + File file = getSampleFile("invalid.xlsx"); OPCPackage.open(file, PackageAccess.READ); } - // bug 61381 + private interface CountingStream { + InputStream create(InputStream is, int length); + } + + // bug 61381 @Test public void testTooShortFilterStreams() throws IOException { - File xssf = OpenXML4JTestDataSamples.getSampleFile("sample.xlsx"); - File hssf = POIDataSamples.getSpreadSheetInstance().getFile("SampleSS.xls"); - - InputStream[] isList = { - new PushbackInputStream(new FileInputStream(xssf), 2), - new BufferedInputStream(new FileInputStream(xssf), 2), - new PushbackInputStream(new FileInputStream(hssf), 2), - new BufferedInputStream(new FileInputStream(hssf), 2), - }; - - try { - for (InputStream is : isList) { - WorkbookFactory.create(is).close(); - } - } finally { - for (InputStream is : isList) { - IOUtils.closeQuietly(is); - } - } + for (String file : new String[]{"sample.xlsx","SampleSS.xls"}) { + for (CountingStream cs : new CountingStream[]{PushbackInputStream::new, BufferedInputStream::new}) { + try (InputStream is = cs.create(xlsSamples.openResourceAsStream(file), 2); + Workbook wb = WorkbookFactory.create(is)) { + assertEquals(3, wb.getNumberOfSheets()); + } + } + } } @Test public void testBug56479() throws Exception { - InputStream is = OpenXML4JTestDataSamples.openSampleStream("dcterms_bug_56479.zip"); - OPCPackage p = OPCPackage.open(is); + try (InputStream is = 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 (final PackagePart part : p.getParts()) { - final String partName = part.getPartName().toString(); - final String contentType = part.getContentType(); - if ("/docProps/core.xml".equals(partName)) { - assertEquals(ContentTypes.CORE_PROPERTIES_PART, contentType); - foundCoreProps = true; - } - if ("/word/document.xml".equals(partName)) { - assertEquals(XWPFRelation.DOCUMENT.getContentType(), contentType); - foundDocument = true; - } - if ("/word/theme/theme1.xml".equals(partName)) { - assertEquals(XWPFRelation.THEME.getContentType(), contentType); - foundTheme1 = true; + // Check we found the contents of it + boolean foundCoreProps = false, foundDocument = false, foundTheme1 = false; + for (final PackagePart part : p.getParts()) { + final String partName = part.getPartName().toString(); + final String contentType = part.getContentType(); + switch (partName) { + case "/docProps/core.xml": + assertEquals(ContentTypes.CORE_PROPERTIES_PART, contentType); + foundCoreProps = true; + break; + case "/word/document.xml": + assertEquals(XWPFRelation.DOCUMENT.getContentType(), contentType); + foundDocument = true; + break; + case "/word/theme/theme1.xml": + assertEquals(XWPFRelation.THEME.getContentType(), contentType); + foundTheme1 = true; + break; + } } + 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); } - 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); - p.close(); - is.close(); } @Test public void unparseableCentralDirectory() throws IOException { - File f = OpenXML4JTestDataSamples.getSampleFile("at.pzp.www_uploads_media_PP_Scheinecker-jdk6error.pptx"); - SlideShow ppt = SlideShowFactory.create(f, null, true); - ppt.close(); + File f = getSampleFile("at.pzp.www_uploads_media_PP_Scheinecker-jdk6error.pptx"); + SlideShowFactory.create(f, null, true).close(); } @Test public void testClosingStreamOnException() throws IOException { - InputStream is = OpenXML4JTestDataSamples.openSampleStream("dcterms_bug_56479.zip"); File tmp = File.createTempFile("poi-test-truncated-zip", ""); + // create a corrupted zip file by truncating a valid zip file to the first 100 bytes - OutputStream os = new FileOutputStream(tmp); - for (int i = 0; i < 100; i++) { - os.write(is.read()); + try (InputStream is = openSampleStream("dcterms_bug_56479.zip"); + OutputStream os = new FileOutputStream(tmp)) { + IOUtils.copy(is, os, 100); } - os.flush(); - os.close(); - is.close(); // feed the corrupted zip file to OPCPackage try { @@ -1094,14 +1060,14 @@ public final class TestPackage { @Test(expected = InvalidFormatException.class) public void testBug62592() throws Exception { - InputStream is = OpenXML4JTestDataSamples.openSampleStream("62592.thmx"); + InputStream is = openSampleStream("62592.thmx"); /*OPCPackage p =*/ OPCPackage.open(is); } @Test public void testBug62592SequentialCallsToGetParts() throws Exception { //make absolutely certain that sequential calls don't throw InvalidFormatExceptions - String originalFile = OpenXML4JTestDataSamples.getSampleFileName("TestPackageCommon.docx"); + String originalFile = getSampleFileName("TestPackageCommon.docx"); try (OPCPackage p2 = OPCPackage.open(originalFile, PackageAccess.READ)) { p2.getParts(); p2.getParts(); @@ -1138,11 +1104,9 @@ public final class TestPackage { private static void openInvalidFile(final String name, final boolean useStream) throws IOException, InvalidFormatException { - // Spreadsheet has a good mix of alternate file types - final POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); ZipPackage pkgTest = null; - try (final InputStream is = (useStream) ? files.openResourceAsStream(name) : null) { - try (final ZipPackage pkg = (useStream) ? new ZipPackage(is, PackageAccess.READ) : new ZipPackage(files.getFile(name), PackageAccess.READ)) { + try (final InputStream is = (useStream) ? xlsSamples.openResourceAsStream(name) : null) { + try (final ZipPackage pkg = (useStream) ? new ZipPackage(is, PackageAccess.READ) : new ZipPackage(xlsSamples.getFile(name), PackageAccess.READ)) { pkgTest = pkg; assertNotNull(pkg.getZipArchive()); assertFalse(pkg.getZipArchive().isClosed()); @@ -1196,8 +1160,8 @@ public final class TestPackage { @SuppressWarnings("UnstableApiUsage") @Test public void testBug63029() throws Exception { - File testFile = OpenXML4JTestDataSamples.getSampleFile("sample.docx"); - File tmpFile = OpenXML4JTestDataSamples.getOutputFile("Bug63029.docx"); + File testFile = getSampleFile("sample.docx"); + File tmpFile = getOutputFile("Bug63029.docx"); Files.copy(testFile, tmpFile); int numPartsBefore = 0; @@ -1212,7 +1176,7 @@ public final class TestPackage { pkg.addMarshaller("poi/junit", (part, out) -> { throw new RuntimeException("Bugzilla 63029"); }); - pkg.createPart(PackagingURIHelper.createPartName("/poi/test.xml"), "poi/junit"); + pkg.createPart(createPartName("/poi/test.xml"), "poi/junit"); } catch (RuntimeException e){ ex = e; } diff --git a/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java b/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java index 607954233b..2ddbb2470e 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java +++ b/src/ooxml/testcases/org/apache/poi/ss/format/TestCellFormatPart.java @@ -16,74 +16,95 @@ ==================================================================== */ package org.apache.poi.ss.format; +import static java.awt.Color.ORANGE; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.awt.Color; import java.io.IOException; import java.util.Locale; import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; + +import javax.swing.JLabel; import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.LocaleUtil; import org.apache.poi.xssf.XSSFITestDataProvider; import org.junit.AfterClass; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -/** Test the individual CellFormatPart types. */ -public class TestCellFormatPart extends CellFormatTestBase { - +/** + * Class for spreadsheet-based tests, such as are used for cell formatting. + * This reads tests from the spreadsheet, as well as reading + * flags that can be used to paramterize these tests. + *

+ * Each test has four parts: The expected result (column A), the format string + * (column B), the value to format (column C), and a comma-separated list of + * categores that this test falls in. Normally all tests are run, but if the + * flag "Categories" is not empty, only tests that have at least one category + * listed in "Categories" are run. + */ +public class TestCellFormatPart { + private static final Pattern NUMBER_EXTRACT_FMT = Pattern.compile( + "([-+]?[0-9]+)(\\.[0-9]+)?.*(?:(e).*?([+-]?[0-9]+))", + Pattern.CASE_INSENSITIVE); + private static final Color TEST_COLOR = ORANGE.darker(); private static Locale userLocale; - + + + @BeforeClass public static void setLocale() { userLocale = LocaleUtil.getUserLocale(); LocaleUtil.setUserLocale(Locale.UK); } - + @AfterClass public static void unsetLocale() { LocaleUtil.setUserLocale(userLocale); } - - private static final Pattern NUMBER_EXTRACT_FMT = Pattern.compile( - "([-+]?[0-9]+)(\\.[0-9]+)?.*(?:(e).*?([+-]?[0-9]+))", - Pattern.CASE_INSENSITIVE); - public TestCellFormatPart() { - super(XSSFITestDataProvider.instance); + private final ITestDataProvider _testDataProvider = XSSFITestDataProvider.instance; + + private interface CellValue { + Object getValue(Cell cell); + + default void equivalent(String expected, String actual, CellFormatPart format) { + assertEquals("format \"" + format + "\"", '"' + expected + '"', + '"' + actual + '"'); + } } @Test public void testGeneralFormat() throws IOException { - runFormatTests("GeneralFormatTests.xlsx", new CellValue() { - @Override - public Object getValue(Cell cell) { - switch (CellFormat.ultimateType(cell)) { - case BOOLEAN: - return cell.getBooleanCellValue(); - case NUMERIC: - return cell.getNumericCellValue(); - default: - return cell.getStringCellValue(); - } + runFormatTests("GeneralFormatTests.xlsx", cell -> { + assertNotNull(cell); + switch (CellFormat.ultimateType(cell)) { + case BOOLEAN: + return cell.getBooleanCellValue(); + case NUMERIC: + return cell.getNumericCellValue(); + default: + return cell.getStringCellValue(); } }); } @Test public void testNumberFormat() throws IOException { - runFormatTests("NumberFormatTests.xlsx", new CellValue() { - @Override - public Object getValue(Cell cell) { - return cell.getNumericCellValue(); - } - }); + runFormatTests("NumberFormatTests.xlsx", Cell::getNumericCellValue); } @Test @@ -95,7 +116,7 @@ public class TestCellFormatPart extends CellFormatTestBase { } @Override - void equivalent(String expected, String actual, + public void equivalent(String expected, String actual, CellFormatPart format) { double expectedVal = extractNumber(expected); double actualVal = extractNumber(actual); @@ -112,12 +133,7 @@ public class TestCellFormatPart extends CellFormatTestBase { TimeZone tz = LocaleUtil.getUserTimeZone(); LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET")); try { - runFormatTests("DateFormatTests.xlsx", new CellValue() { - @Override - public Object getValue(Cell cell) { - return cell.getDateCellValue(); - } - }); + runFormatTests("DateFormatTests.xlsx", Cell::getDateCellValue); } finally { LocaleUtil.setUserTimeZone(tz); } @@ -125,55 +141,34 @@ public class TestCellFormatPart extends CellFormatTestBase { @Test public void testElapsedFormat() throws IOException { - runFormatTests("ElapsedFormatTests.xlsx", new CellValue() { - @Override - public Object getValue(Cell cell) { - return cell.getNumericCellValue(); - } - }); + runFormatTests("ElapsedFormatTests.xlsx", Cell::getNumericCellValue); } @Test public void testTextFormat() throws IOException { - runFormatTests("TextFormatTests.xlsx", new CellValue() { - @Override - public Object getValue(Cell cell) { - if (CellFormat.ultimateType(cell) == CellType.BOOLEAN) { - return cell.getBooleanCellValue(); - } - return cell.getStringCellValue(); - } - }); + runFormatTests("TextFormatTests.xlsx", cell -> + (CellFormat.ultimateType(cell) == CellType.BOOLEAN) ? cell.getBooleanCellValue() : cell.getStringCellValue() + ); } @Test public void testConditions() throws IOException { - runFormatTests("FormatConditionTests.xlsx", new CellValue() { - @Override - Object getValue(Cell cell) { - return cell.getNumericCellValue(); - } - }); + runFormatTests("FormatConditionTests.xlsx", Cell::getNumericCellValue); } @Test public void testNamedColors() { assertTrue(CellFormatPart.NAMED_COLORS.size() >= HSSFColor.HSSFColorPredefined.values().length); - assertNotNull(CellFormatPart.NAMED_COLORS.get("GREEN")); - assertNotNull(CellFormatPart.NAMED_COLORS.get("Green")); - assertNotNull(CellFormatPart.NAMED_COLORS.get("RED")); - assertNotNull(CellFormatPart.NAMED_COLORS.get("Red")); - assertNotNull(CellFormatPart.NAMED_COLORS.get("BLUE")); - assertNotNull(CellFormatPart.NAMED_COLORS.get("Blue")); - assertNotNull(CellFormatPart.NAMED_COLORS.get("YELLOW")); - assertNotNull(CellFormatPart.NAMED_COLORS.get("Yellow")); + Stream.of("GREEN","Green","RED","Red","BLUE","Blue","YELLOW","Yellow") + .map(CellFormatPart.NAMED_COLORS::get) + .forEach(Assert::assertNotNull); } private double extractNumber(String str) { Matcher m = NUMBER_EXTRACT_FMT.matcher(str); - if (!m.find()) - throw new IllegalArgumentException( - "Cannot find number in \"" + str + "\""); + if (!m.find()) { + throw new IllegalArgumentException("Cannot find number in \"" + str + "\""); + } StringBuilder sb = new StringBuilder(); // The groups in the pattern are the parts of the number @@ -182,6 +177,45 @@ public class TestCellFormatPart extends CellFormatTestBase { if (part != null) sb.append(part); } - return Double.valueOf(sb.toString()); + return Double.parseDouble(sb.toString()); + } + + + protected void runFormatTests(String workbookName, CellValue valueGetter) throws IOException { + try (Workbook workbook = _testDataProvider.openSampleWorkbook(workbookName)) { + workbook.setMissingCellPolicy(Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); + + Sheet sheet = workbook.getSheet("Tests"); + boolean isHeader = true; + for (Row row : sheet) { + // Skip the header row + if (isHeader || row == null) { + isHeader = false; + continue; + } + String expectedText = row.getCell(0).getStringCellValue(); + String format = row.getCell(1).getStringCellValue(); + Cell value = row.getCell(2); + + if (expectedText.isEmpty() && format.isEmpty()) { + continue; + } + + Object objVal = valueGetter.getValue(value); + JLabel label = new JLabel(); + label.setForeground(TEST_COLOR); + label.setText("xyzzy"); + + Color origColor = label.getForeground(); + CellFormatPart cellFormatPart = new CellFormatPart(format); + // If this doesn't apply, no color change is expected + Color expectedColor = cellFormatPart.apply(label, objVal).applies ? TEST_COLOR : origColor; + + String actualText = label.getText(); + Color actualColor = label.getForeground(); + valueGetter.equivalent(expectedText, actualText, cellFormatPart); + assertEquals("no color", expectedColor, actualColor); + } + } } } diff --git a/src/testcases/org/apache/poi/ss/format/CellFormatTestBase.java b/src/testcases/org/apache/poi/ss/format/CellFormatTestBase.java deleted file mode 100644 index c6abb30117..0000000000 --- a/src/testcases/org/apache/poi/ss/format/CellFormatTestBase.java +++ /dev/null @@ -1,309 +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.ss.format; - -import static java.awt.Color.BLACK; -import static java.awt.Color.BLUE; -import static java.awt.Color.CYAN; -import static java.awt.Color.GREEN; -import static java.awt.Color.MAGENTA; -import static java.awt.Color.ORANGE; -import static java.awt.Color.RED; -import static java.awt.Color.WHITE; -import static java.awt.Color.YELLOW; -import static org.junit.Assert.assertEquals; - -import java.awt.Color; -import java.io.IOException; -import java.util.Arrays; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; - -import javax.swing.JLabel; - -import org.apache.poi.ss.ITestDataProvider; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.util.LocaleUtil; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; - -/** - * This class is a base class for spreadsheet-based tests, such as are used for - * cell formatting. This reads tests from the spreadsheet, as well as reading - * flags that can be used to paramterize these tests. - *

- * Each test has four parts: The expected result (column A), the format string - * (column B), the value to format (column C), and a comma-separated list of - * categores that this test falls in. Normally all tests are run, but if the - * flag "Categories" is not empty, only tests that have at least one category - * listed in "Categories" are run. - */ -public class CellFormatTestBase { - private static final POILogger logger = POILogFactory.getLogger(CellFormatTestBase.class); - - private final ITestDataProvider _testDataProvider; - - protected Workbook workbook; - - private String testFile; - private Map testFlags; - private boolean tryAllColors; - private JLabel label; - - private static final String[] COLOR_NAMES = - {"Black", "Red", "Green", "Blue", "Yellow", "Cyan", "Magenta", - "White"}; - private static final Color[] COLORS = - {BLACK, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE}; - - public static final Color TEST_COLOR = ORANGE.darker(); - - protected CellFormatTestBase(ITestDataProvider testDataProvider) { - _testDataProvider = testDataProvider; - } - - abstract static class CellValue { - abstract Object getValue(Cell cell); - - Color getColor(Cell cell) { - return TEST_COLOR; - } - - void equivalent(String expected, String actual, CellFormatPart format) { - assertEquals("format \"" + format + "\"", '"' + expected + '"', - '"' + actual + '"'); - } - } - - protected void runFormatTests(String workbookName, CellValue valueGetter) throws IOException { - - openWorkbook(workbookName); - - readFlags(workbook); - - Set runCategories = new TreeSet<>( - String.CASE_INSENSITIVE_ORDER); - String runCategoryList = flagString("Categories", ""); - if (runCategoryList != null) { - runCategories.addAll(Arrays.asList(runCategoryList.split( - "\\s*,\\s*"))); - runCategories.remove(""); // this can be found and means nothing - } - - Sheet sheet = workbook.getSheet("Tests"); - Iterator rowIter = sheet.rowIterator(); - // Skip the header row - rowIter.next(); - while (rowIter.hasNext()) { - Row row = rowIter.next(); - if (row == null) continue; - String expectedText = row.getCell(0).getStringCellValue(); - String format = row.getCell(1).getStringCellValue(); - Cell value = row.getCell(2); - String testCategoryList = row.getCell(3).getStringCellValue(); - boolean byCategory = runByCategory(runCategories, testCategoryList); - if ((expectedText.length() > 0 || format.length() > 0) && byCategory) { - tryFormat(row.getRowNum(), expectedText, format, valueGetter, value); - } - } - - workbook.close(); - } - - /** - * Open a given workbook. - * - * @param workbookName The workbook name. This is presumed to live in the - * "spreadsheets" directory under the directory named in - * the Java property "POI.testdata.path". - */ - protected void openWorkbook(String workbookName) { - workbook = _testDataProvider.openSampleWorkbook(workbookName); - workbook.setMissingCellPolicy(MissingCellPolicy.CREATE_NULL_AS_BLANK); - testFile = workbookName; - } - - /** - * Read the flags from the workbook. Flags are on the sheet named "Flags", - * and consist of names in column A and values in column B. These are put - * into a map that can be queried later. - * - * @param wb The workbook to look in. - */ - private void readFlags(Workbook wb) { - Sheet flagSheet = wb.getSheet("Flags"); - testFlags = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); - if (flagSheet != null) { - int end = flagSheet.getLastRowNum(); - // Skip the header row, therefore "+ 1" - for (int r = flagSheet.getFirstRowNum() + 1; r <= end; r++) { - Row row = flagSheet.getRow(r); - if (row == null) - continue; - String flagName = row.getCell(0).getStringCellValue(); - String flagValue = row.getCell(1).getStringCellValue(); - if (flagName.length() > 0) { - testFlags.put(flagName, flagValue); - } - } - } - - tryAllColors = flagBoolean("AllColors", true); - } - - /** - * Returns true if any of the categories for this run are contained - * in the test's listed categories. - * - * @param categories The categories of tests to be run. If this is - * empty, then all tests will be run. - * @param testCategories The categories that this test is in. This is a - * comma-separated list. If any tests in - * this list are in categories, the test will - * be run. - * - * @return true if the test should be run. - */ - private boolean runByCategory(Set categories, - String testCategories) { - - if (categories.isEmpty()) - return true; - // If there are specified categories, find out if this has one of them - for (String category : testCategories.split("\\s*,\\s*")) { - if (categories.contains(category)) { - return true; - } - } - return false; - } - - private void tryFormat(int row, String expectedText, String desc, - CellValue getter, Cell cell) { - - Object value = getter.getValue(cell); - Color testColor = getter.getColor(cell); - if (testColor == null) - testColor = TEST_COLOR; - - if (label == null) - label = new JLabel(); - label.setForeground(testColor); - label.setText("xyzzy"); - - logger.log(POILogger.INFO, String.format(LocaleUtil.getUserLocale(), - "Row %d: \"%s\" -> \"%s\": expected \"%s\"", - row + 1, String.valueOf(value), desc, expectedText)); - String actualText = tryColor(desc, null, getter, value, expectedText, - testColor); - logger.log(POILogger.INFO, String.format(LocaleUtil.getUserLocale(), - ", actual \"%s\")%n", actualText)); - - if (tryAllColors && testColor != TEST_COLOR) { - for (int i = 0; i < COLOR_NAMES.length; i++) { - tryColor(desc, COLOR_NAMES[i], getter, value, expectedText, COLORS[i]); - } - } - } - - private String tryColor(String desc, String cname, CellValue getter, - Object value, String expectedText, Color expectedColor) { - - if (cname != null) - desc = "[" + cname + "]" + desc; - Color origColor = label.getForeground(); - CellFormatPart format = new CellFormatPart(desc); - if (!format.apply(label, value).applies) { - // If this doesn't apply, no color change is expected - expectedColor = origColor; - } - - String actualText = label.getText(); - Color actualColor = label.getForeground(); - getter.equivalent(expectedText, actualText, format); - assertEquals(cname == null ? "no color" : "color " + cname, - expectedColor, actualColor); - return actualText; - } - - /** - * Returns the value for the given flag. The flag has the value of - * true if the text value is "true", "yes", or - * "on" (ignoring case). - * - * @param flagName The name of the flag to fetch. - * @param expected The value for the flag that is expected when the tests - * are run for a full test. If the current value is not the - * expected one, you will get a warning in the test output. - * This is so that you do not accidentally leave a flag set - * to a value that prevents running some tests, thereby - * letting you accidentally release code that is not fully - * tested. - * - * @return The value for the flag. - */ - protected boolean flagBoolean(String flagName, boolean expected) { - String value = testFlags.get(flagName); - boolean isSet; - if (value == null) - isSet = false; - else { - isSet = value.equalsIgnoreCase("true") || value.equalsIgnoreCase( - "yes") || value.equalsIgnoreCase("on"); - } - warnIfUnexpected(flagName, expected, isSet); - return isSet; - } - - /** - * Returns the value for the given flag. - * - * @param flagName The name of the flag to fetch. - * @param expected The value for the flag that is expected when the tests - * are run for a full test. If the current value is not the - * expected one, you will get a warning in the test output. - * This is so that you do not accidentally leave a flag set - * to a value that prevents running some tests, thereby - * letting you accidentally release code that is not fully - * tested. - * - * @return The value for the flag. - */ - protected String flagString(String flagName, String expected) { - String value = testFlags.get(flagName); - if (value == null) - value = ""; - warnIfUnexpected(flagName, expected, value); - return value; - } - - private void warnIfUnexpected(String flagName, Object expected, - Object actual) { - if (!actual.equals(expected)) { - System.err.println( - "WARNING: " + testFile + ": " + "Flag " + flagName + - " = \"" + actual + "\" [not \"" + expected + "\"]"); - } - } -}