From: Dominik Stadler Date: Sun, 3 Mar 2019 14:09:50 +0000 (+0000) Subject: Close more file-handles in tests X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9ccd9934f6db346766189c99d17271c6b37ffafa;p=poi.git Close more file-handles in tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1854718 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFBEventBasedExcelExtractor.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFBEventBasedExcelExtractor.java index e173876188..226a76ead6 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFBEventBasedExcelExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFBEventBasedExcelExtractor.java @@ -34,8 +34,6 @@ import java.nio.file.Files; * Tests for {@link XSSFBEventBasedExcelExtractor} */ public class TestXSSFBEventBasedExcelExtractor { - - protected XSSFEventBasedExcelExtractor getExtractor(String sampleName) throws Exception { return new XSSFBEventBasedExcelExtractor(XSSFTestDataSamples. openSamplePackage(sampleName)); @@ -47,88 +45,83 @@ public class TestXSSFBEventBasedExcelExtractor { @Test public void testGetSimpleText() throws Exception { // a very simple file - XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsb"); - extractor.setIncludeCellComments(true); - extractor.getText(); - - String text = extractor.getText(); - assertTrue(text.length() > 0); - - // Check sheet names - assertStartsWith(text, "Sheet1"); - assertEndsWith(text, "Sheet3\n"); - - // Now without, will have text - extractor.setIncludeSheetNames(false); - text = extractor.getText(); - String CHUNK1 = - "Lorem\t111\n" + - "ipsum\t222\n" + - "dolor\t333\n" + - "sit\t444\n" + - "amet\t555\n" + - "consectetuer\t666\n" + - "adipiscing\t777\n" + - "elit\t888\n" + - "Nunc\t999\n"; - String CHUNK2 = - "The quick brown fox jumps over the lazy dog\n" + - "hello, xssf hello, xssf\n" + - "hello, xssf hello, xssf\n" + - "hello, xssf hello, xssf\n" + - "hello, xssf hello, xssf\n"; - assertEquals( - CHUNK1 + - "at\t4995\n" + - CHUNK2 - , text); - + try (XSSFEventBasedExcelExtractor extractor = getExtractor("sample.xlsb")) { + extractor.setIncludeCellComments(true); + extractor.getText(); + + String text = extractor.getText(); + assertTrue(text.length() > 0); + + // Check sheet names + assertStartsWith(text, "Sheet1"); + assertEndsWith(text, "Sheet3\n"); + + // Now without, will have text + extractor.setIncludeSheetNames(false); + text = extractor.getText(); + String CHUNK1 = + "Lorem\t111\n" + + "ipsum\t222\n" + + "dolor\t333\n" + + "sit\t444\n" + + "amet\t555\n" + + "consectetuer\t666\n" + + "adipiscing\t777\n" + + "elit\t888\n" + + "Nunc\t999\n"; + String CHUNK2 = + "The quick brown fox jumps over the lazy dog\n" + + "hello, xssf hello, xssf\n" + + "hello, xssf hello, xssf\n" + + "hello, xssf hello, xssf\n" + + "hello, xssf hello, xssf\n"; + assertEquals( + CHUNK1 + + "at\t4995\n" + + CHUNK2 + , text); + } } - /** * Test text extraction from text box using getShapes() - * - * @throws Exception */ @Test public void testShapes() throws Exception { - XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsb"); - - try { + try (XSSFEventBasedExcelExtractor ooxmlExtractor = getExtractor("WithTextBox.xlsb")) { String text = ooxmlExtractor.getText(); assertContains(text, "Line 1"); assertContains(text, "Line 2"); assertContains(text, "Line 3"); - } finally { - ooxmlExtractor.close(); } } @Test public void testBeta() throws Exception { - XSSFEventBasedExcelExtractor extractor = getExtractor("Simple.xlsb"); - extractor.setIncludeCellComments(true); - String text = extractor.getText(); - assertContains(text, - "This is an example spreadsheet created with Microsoft Excel 2007 Beta 2."); + try (XSSFEventBasedExcelExtractor extractor = getExtractor("Simple.xlsb")) { + extractor.setIncludeCellComments(true); + String text = extractor.getText(); + assertContains(text, + "This is an example spreadsheet created with Microsoft Excel 2007 Beta 2."); + } } @Test public void test62815() throws Exception { //test file based on http://oss.sheetjs.com/test_files/RkNumber.xlsb - XSSFEventBasedExcelExtractor extractor = getExtractor("62815.xlsb"); - extractor.setIncludeCellComments(true); - String[] rows = extractor.getText().split("[\r\n]+"); - assertEquals(283, rows.length); - BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(), - StandardCharsets.UTF_8); - String line = reader.readLine(); - for (int i = 0; i < rows.length; i++) { - assertEquals(line, rows[i]); - line = reader.readLine(); - while (line != null && line.startsWith("#")) { + try (XSSFEventBasedExcelExtractor extractor = getExtractor("62815.xlsb")) { + extractor.setIncludeCellComments(true); + String[] rows = extractor.getText().split("[\r\n]+"); + assertEquals(283, rows.length); + BufferedReader reader = Files.newBufferedReader(XSSFTestDataSamples.getSampleFile("62815.xlsb.txt").toPath(), + StandardCharsets.UTF_8); + String line = reader.readLine(); + for (String row : rows) { + assertEquals(line, row); line = reader.readLine(); + while (line != null && line.startsWith("#")) { + line = reader.readLine(); + } } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java index 25af5f6935..e1c58b00cb 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Date; import java.util.regex.Matcher; @@ -58,444 +59,449 @@ public final class TestXSSFExportToXML { @Test public void testExportToXML() throws Exception { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx")) { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx"); - - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { - - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; - - XSSFMap map = mapInfo.getXSSFMapById(1); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xml = os.toString("UTF-8"); - - assertNotNull(xml); - assertFalse(xml.isEmpty()); - - String docente = xml.split("")[1].split("")[0].trim(); - String nome = xml.split("")[1].split("")[0].trim(); - String tutor = xml.split("")[1].split("")[0].trim(); - String cdl = xml.split("")[1].split("")[0].trim(); - String durata = xml.split("")[1].split("")[0].trim(); - String argomento = xml.split("")[1].split("")[0].trim(); - String progetto = xml.split("")[1].split("")[0].trim(); - String crediti = xml.split("")[1].split("")[0].trim(); - - assertEquals("ro", docente); - assertEquals("ro", nome); - assertEquals("ds", tutor); - assertEquals("gs", cdl); - assertEquals("g", durata); - assertEquals("gvvv", argomento); - assertEquals("aaaa", progetto); - assertEquals("aa", crediti); - - parseXML(xml); - - found = true; - } - assertTrue(found); + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; + + XSSFMap map = mapInfo.getXSSFMapById(1); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xml = os.toString("UTF-8"); + + assertNotNull(xml); + assertFalse(xml.isEmpty()); + + String docente = xml.split("")[1].split("")[0].trim(); + String nome = xml.split("")[1].split("")[0].trim(); + String tutor = xml.split("")[1].split("")[0].trim(); + String cdl = xml.split("")[1].split("")[0].trim(); + String durata = xml.split("")[1].split("")[0].trim(); + String argomento = xml.split("")[1].split("")[0].trim(); + String progetto = xml.split("")[1].split("")[0].trim(); + String crediti = xml.split("")[1].split("")[0].trim(); + + assertEquals("ro", docente); + assertEquals("ro", nome); + assertEquals("ds", tutor); + assertEquals("gs", cdl); + assertEquals("g", durata); + assertEquals("gvvv", argomento); + assertEquals("aaaa", progetto); + assertEquals("aa", crediti); + + parseXML(xml); + + found = true; + } + assertTrue(found); + } } @Test public void testExportToXMLInverseOrder() throws Exception { + try (XSSFWorkbook wb = XSSFTestDataSamples + .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx")) { - XSSFWorkbook wb = XSSFTestDataSamples - .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); - - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { - - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; - - XSSFMap map = mapInfo.getXSSFMapById(1); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xml = os.toString("UTF-8"); - - assertNotNull(xml); - assertFalse(xml.isEmpty()); - - String docente = xml.split("")[1].split("")[0].trim(); - String nome = xml.split("")[1].split("")[0].trim(); - String tutor = xml.split("")[1].split("")[0].trim(); - String cdl = xml.split("")[1].split("")[0].trim(); - String durata = xml.split("")[1].split("")[0].trim(); - String argomento = xml.split("")[1].split("")[0].trim(); - String progetto = xml.split("")[1].split("")[0].trim(); - String crediti = xml.split("")[1].split("")[0].trim(); - - assertEquals("aa", nome); - assertEquals("aaaa", docente); - assertEquals("gvvv", tutor); - assertEquals("g", cdl); - assertEquals("gs", durata); - assertEquals("ds", argomento); - assertEquals("ro", progetto); - assertEquals("ro", crediti); - - parseXML(xml); - - found = true; - } - assertTrue(found); + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; + + XSSFMap map = mapInfo.getXSSFMapById(1); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xml = os.toString("UTF-8"); + + assertNotNull(xml); + assertFalse(xml.isEmpty()); + + String docente = xml.split("")[1].split("")[0].trim(); + String nome = xml.split("")[1].split("")[0].trim(); + String tutor = xml.split("")[1].split("")[0].trim(); + String cdl = xml.split("")[1].split("")[0].trim(); + String durata = xml.split("")[1].split("")[0].trim(); + String argomento = xml.split("")[1].split("")[0].trim(); + String progetto = xml.split("")[1].split("")[0].trim(); + String crediti = xml.split("")[1].split("")[0].trim(); + + assertEquals("aa", nome); + assertEquals("aaaa", docente); + assertEquals("gvvv", tutor); + assertEquals("g", cdl); + assertEquals("gs", durata); + assertEquals("ds", argomento); + assertEquals("ro", progetto); + assertEquals("ro", crediti); + + parseXML(xml); + + found = true; + } + assertTrue(found); + } } @Test - public void testXPathOrdering() { + public void testXPathOrdering() throws IOException { + try (XSSFWorkbook wb = XSSFTestDataSamples + .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx")) { - XSSFWorkbook wb = XSSFTestDataSamples - .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + if (p instanceof MapInfo) { + MapInfo mapInfo = (MapInfo) p; - if (p instanceof MapInfo) { - MapInfo mapInfo = (MapInfo) p; + XSSFMap map = mapInfo.getXSSFMapById(1); + XSSFExportToXml exporter = new XSSFExportToXml(map); - XSSFMap map = mapInfo.getXSSFMapById(1); - XSSFExportToXml exporter = new XSSFExportToXml(map); + assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME")); + assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE")); + } - assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME")); - assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE")); - } - - found = true; - } - assertTrue(found); + found = true; + } + assertTrue(found); + } } @Test public void testMultiTable() throws Exception { + try (XSSFWorkbook wb = XSSFTestDataSamples + .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx")) { - XSSFWorkbook wb = XSSFTestDataSamples - .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx"); + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + if (p instanceof MapInfo) { + MapInfo mapInfo = (MapInfo) p; - if (p instanceof MapInfo) { - MapInfo mapInfo = (MapInfo) p; + XSSFMap map = mapInfo.getXSSFMapById(2); - XSSFMap map = mapInfo.getXSSFMapById(2); + assertNotNull(map); - assertNotNull(map); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xml = os.toString("UTF-8"); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xml = os.toString("UTF-8"); + assertNotNull(xml); - assertNotNull(xml); + String[] regexConditions = { + "", + "", + "", + "DataBinding", + "Map Append=\"false\" AutoFit=\"false\" ID=\"1\"", + "Map Append=\"false\" AutoFit=\"false\" ID=\"5\"", + }; - String[] regexConditions = { - "", - "", - "", - "DataBinding", - "Map Append=\"false\" AutoFit=\"false\" ID=\"1\"", - "Map Append=\"false\" AutoFit=\"false\" ID=\"5\"", - }; + for (String condition : regexConditions) { + Pattern pattern = Pattern.compile(condition); + Matcher matcher = pattern.matcher(xml); + assertTrue(matcher.find()); + } + } - for (String condition : regexConditions) { - Pattern pattern = Pattern.compile(condition); - Matcher matcher = pattern.matcher(xml); - assertTrue(matcher.find()); - } - } - - found = true; - } - assertTrue(found); + found = true; + } + assertTrue(found); + } } @Test @Ignore(value="Fails, but I don't know if it is ok or not...") public void testExportToXMLSingleAttributeNamespace() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx"); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx")) { - for (XSSFMap map : wb.getCustomXMLMappings()) { - XSSFExportToXml exporter = new XSSFExportToXml(map); + for (XSSFMap map : wb.getCustomXMLMappings()) { + XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + } } } @Test public void test55850ComplexXmlExport() throws Exception { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55850.xlsx")) { - XSSFWorkbook wb = XSSFTestDataSamples - .openSampleWorkbook("55850.xlsx"); + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; + XSSFMap map = mapInfo.getXSSFMapById(2); + + assertNotNull("XSSFMap is null", map); - XSSFMap map = mapInfo.getXSSFMapById(2); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); - assertNotNull("XSSFMap is null", map); + assertNotNull(xmlData); + assertFalse(xmlData.isEmpty()); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); + String a = xmlData.split("")[1].split("")[0].trim(); + String b = a.split("")[1].split("")[0].trim(); + String c = b.split("")[1].split("")[0].trim(); + String d = c.split("")[1].split("")[0].trim(); + String e = d.split("")[1].split("")[0].trim(); - assertNotNull(xmlData); - assertFalse(xmlData.isEmpty()); + String euro = e.split("")[1].split("")[0].trim(); + String chf = e.split("")[1].split("")[0].trim(); - String a = xmlData.split("")[1].split("")[0].trim(); - String b = a.split("")[1].split("")[0].trim(); - String c = b.split("")[1].split("")[0].trim(); - String d = c.split("")[1].split("")[0].trim(); - String e = d.split("")[1].split("")[0].trim(); + assertEquals("15", euro); + assertEquals("19", chf); - String euro = e.split("")[1].split("")[0].trim(); - String chf = e.split("")[1].split("")[0].trim(); + parseXML(xmlData); - assertEquals("15", euro); - assertEquals("19", chf); - - parseXML(xmlData); - - found = true; + found = true; + } + assertTrue(found); } - assertTrue(found); } @Test public void testFormulaCells_Bugzilla_55927() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx"); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx")) { - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; - XSSFMap map = mapInfo.getXSSFMapById(1); + XSSFMap map = mapInfo.getXSSFMapById(1); - assertNotNull("XSSFMap is null", map); + assertNotNull("XSSFMap is null", map); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); - assertNotNull(xmlData); - assertFalse(xmlData.isEmpty()); + assertNotNull(xmlData); + assertFalse(xmlData.isEmpty()); - assertEquals("2012-01-13", xmlData.split("")[1].split("")[0].trim()); - assertEquals("2012-02-16", xmlData.split("")[1].split("")[0].trim()); + assertEquals("2012-01-13", xmlData.split("")[1].split("")[0].trim()); + assertEquals("2012-02-16", xmlData.split("")[1].split("")[0].trim()); - parseXML(xmlData); + parseXML(xmlData); - found = true; + found = true; + } + assertTrue(found); } - assertTrue(found); } @Test public void testFormulaCells_Bugzilla_55926() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx"); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx")) { - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; - XSSFMap map = mapInfo.getXSSFMapById(1); + XSSFMap map = mapInfo.getXSSFMapById(1); - assertNotNull("XSSFMap is null", map); + assertNotNull("XSSFMap is null", map); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); - assertNotNull(xmlData); - assertFalse(xmlData.isEmpty()); + assertNotNull(xmlData); + assertFalse(xmlData.isEmpty()); - String a = xmlData.split("")[1].split("")[0].trim(); - String doubleValue = a.split("")[1].split("")[0].trim(); - String stringValue = a.split("")[1].split("")[0].trim(); + String a = xmlData.split("")[1].split("")[0].trim(); + String doubleValue = a.split("")[1].split("")[0].trim(); + String stringValue = a.split("")[1].split("")[0].trim(); - assertEquals("Hello World", stringValue); - assertEquals("5.1", doubleValue); + assertEquals("Hello World", stringValue); + assertEquals("5.1", doubleValue); - parseXML(xmlData); + parseXML(xmlData); - found = true; + found = true; + } + assertTrue(found); } - assertTrue(found); } @Test public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx")) { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx"); + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; + XSSFMap map = mapInfo.getXSSFMapById(1); + + assertNotNull("XSSFMap is null", map); - XSSFMap map = mapInfo.getXSSFMapById(1); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); - assertNotNull("XSSFMap is null", map); + assertNotNull(xmlData); + assertFalse(xmlData.isEmpty()); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); + String a = xmlData.split("")[1].split("")[0].trim(); + String euro = a.split("")[1].split("")[0].trim(); + assertEquals("1", euro); - assertNotNull(xmlData); - assertFalse(xmlData.isEmpty()); + parseXML(xmlData); - String a = xmlData.split("")[1].split("")[0].trim(); - String euro = a.split("")[1].split("")[0].trim(); - assertEquals("1",euro); - - parseXML(xmlData); - - found = true; + found = true; + } + assertTrue(found); } - assertTrue(found); } @Test public void testXmlExportSchemaWithXSAllTag_Bugzilla_56169() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx"); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx")) { - for (XSSFMap map : wb.getCustomXMLMappings()) { - XSSFExportToXml exporter = new XSSFExportToXml(map); + for (XSSFMap map : wb.getCustomXMLMappings()) { + XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); - assertNotNull(xmlData); - assertTrue(!xmlData.isEmpty()); + assertNotNull(xmlData); + assertTrue(!xmlData.isEmpty()); - String a = xmlData.split("")[1].split("")[0].trim(); - String a_b = a.split("")[1].split("")[0].trim(); - String a_b_c = a_b.split("")[1].split("")[0].trim(); - String a_b_c_e = a_b_c.split("")[1].split("")[0].trim(); - String a_b_c_e_euro = a_b_c_e.split("")[1].split("")[0].trim(); - String a_b_c_e_chf = a_b_c_e.split("")[1].split("")[0].trim(); + String a = xmlData.split("")[1].split("")[0].trim(); + String a_b = a.split("")[1].split("")[0].trim(); + String a_b_c = a_b.split("")[1].split("")[0].trim(); + String a_b_c_e = a_b_c.split("")[1].split("")[0].trim(); + String a_b_c_e_euro = a_b_c_e.split("")[1].split("")[0].trim(); + String a_b_c_e_chf = a_b_c_e.split("")[1].split("")[0].trim(); - assertEquals("1", a_b_c_e_euro); - assertEquals("2", a_b_c_e_chf); + assertEquals("1", a_b_c_e_euro); + assertEquals("2", a_b_c_e_chf); - String a_b_d = a_b.split("")[1].split("")[0].trim(); - String a_b_d_e = a_b_d.split("")[1].split("")[0].trim(); + String a_b_d = a_b.split("")[1].split("")[0].trim(); + String a_b_d_e = a_b_d.split("")[1].split("")[0].trim(); - String a_b_d_e_euro = a_b_d_e.split("")[1].split("")[0].trim(); - String a_b_d_e_chf = a_b_d_e.split("")[1].split("")[0].trim(); + String a_b_d_e_euro = a_b_d_e.split("")[1].split("")[0].trim(); + String a_b_d_e_chf = a_b_d_e.split("")[1].split("")[0].trim(); - assertEquals("3", a_b_d_e_euro); - assertEquals("4", a_b_d_e_chf); + assertEquals("3", a_b_d_e_euro); + assertEquals("4", a_b_d_e_chf); + } } } @Test public void testXmlExportCompare_Bug_55923() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) { + + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + XSSFMap map = mapInfo.getXSSFMapById(4); - if (!(p instanceof MapInfo)) { - continue; + assertNotNull("XSSFMap is null", map); + + XSSFExportToXml exporter = new XSSFExportToXml(map); + assertEquals(0, exporter.compare("", "")); + assertEquals(0, exporter.compare("/", "/")); + assertEquals(0, exporter.compare("//", "//")); + assertEquals(0, exporter.compare("/a/", "/b/")); + + assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR", + "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF")); + + found = true; } - MapInfo mapInfo = (MapInfo) p; - - XSSFMap map = mapInfo.getXSSFMapById(4); - - assertNotNull("XSSFMap is null", map); - - XSSFExportToXml exporter = new XSSFExportToXml(map); - assertEquals(0, exporter.compare("", "")); - assertEquals(0, exporter.compare("/", "/")); - assertEquals(0, exporter.compare("//", "//")); - assertEquals(0, exporter.compare("/a/", "/b/")); - - assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR", - "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF")); - - found = true; + assertTrue(found); } - assertTrue(found); } @Test public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) { + + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; + + XSSFMap map = mapInfo.getXSSFMapById(4); + + assertNotNull("XSSFMap is null", map); + + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); + + assertNotNull(xmlData); + assertFalse(xmlData.isEmpty()); - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + String a = xmlData.split("")[1].split("")[0].trim(); + String a_b = a.split("")[1].split("")[0].trim(); + String a_b_c = a_b.split("")[1].split("")[0].trim(); + String a_b_c_e = a_b_c.split("")[1].split("")[0].trim(); + String a_b_c_e_euro = a_b_c_e.split("")[1].split("")[0].trim(); + String a_b_c_e_chf = a_b_c_e.split("")[1].split("")[0].trim(); - if (!(p instanceof MapInfo)) { - continue; + assertEquals("1", a_b_c_e_euro); + assertEquals("2", a_b_c_e_chf); + + String a_b_d = a_b.split("")[1].split("")[0].trim(); + String a_b_d_e = a_b_d.split("")[1].split("")[0].trim(); + + String a_b_d_e_euro = a_b_d_e.split("")[1].split("")[0].trim(); + String a_b_d_e_chf = a_b_d_e.split("")[1].split("")[0].trim(); + + assertEquals("3", a_b_d_e_euro); + assertEquals("4", a_b_d_e_chf); + + found = true; } - MapInfo mapInfo = (MapInfo) p; - - XSSFMap map = mapInfo.getXSSFMapById(4); - - assertNotNull("XSSFMap is null", map); - - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); - - assertNotNull(xmlData); - assertFalse(xmlData.isEmpty()); - - String a = xmlData.split("")[1].split("")[0].trim(); - String a_b = a.split("")[1].split("")[0].trim(); - String a_b_c = a_b.split("")[1].split("")[0].trim(); - String a_b_c_e = a_b_c.split("")[1].split("")[0].trim(); - String a_b_c_e_euro = a_b_c_e.split("")[1].split("")[0].trim(); - String a_b_c_e_chf = a_b_c_e.split("")[1].split("")[0].trim(); - - assertEquals("1",a_b_c_e_euro); - assertEquals("2",a_b_c_e_chf); - - String a_b_d = a_b.split("")[1].split("")[0].trim(); - String a_b_d_e = a_b_d.split("")[1].split("")[0].trim(); - - String a_b_d_e_euro = a_b_d_e.split("")[1].split("")[0].trim(); - String a_b_d_e_chf = a_b_d_e.split("")[1].split("")[0].trim(); - - assertEquals("3",a_b_d_e_euro); - assertEquals("4",a_b_d_e_chf); - - found = true; + assertTrue(found); } - assertTrue(found); } private void parseXML(String xmlData) throws IOException, SAXException, ParserConfigurationException { @@ -505,163 +511,164 @@ public final class TestXSSFExportToXML { DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); docBuilder.setEntityResolver(new DummyEntityResolver()); - docBuilder.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8"))); + docBuilder.parse(new ByteArrayInputStream(xmlData.getBytes(StandardCharsets.UTF_8))); } - private static class DummyEntityResolver implements EntityResolver - { + private static class DummyEntityResolver implements EntityResolver { @Override - public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException - { + public InputSource resolveEntity(String publicId, String systemId) { return null; } } @Test public void testExportDataTypes() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); - - Sheet sheet = wb.getSheetAt(0); - Row row = sheet.getRow(0); - - Cell cString = row.createCell(0); - cString.setCellValue("somestring"); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) { - Cell cBoolean = row.createCell(1); - cBoolean.setCellValue(true); + Sheet sheet = wb.getSheetAt(0); + Row row = sheet.getRow(0); - Cell cError = row.createCell(2); - cError.setCellErrorValue(FormulaError.NUM.getCode()); - - Cell cFormulaString = row.createCell(3); - cFormulaString.setCellFormula("A1"); + Cell cString = row.createCell(0); + cString.setCellValue("somestring"); - Cell cFormulaNumeric = row.createCell(4); - cFormulaNumeric.setCellFormula("F1"); + Cell cBoolean = row.createCell(1); + cBoolean.setCellValue(true); - Cell cNumeric = row.createCell(5); - cNumeric.setCellValue(1.2); + Cell cError = row.createCell(2); + cError.setCellErrorValue(FormulaError.NUM.getCode()); - Cell cDate = row.createCell(6); - cDate.setCellValue(new Date()); + Cell cFormulaString = row.createCell(3); + cFormulaString.setCellFormula("A1"); - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + Cell cFormulaNumeric = row.createCell(4); + cFormulaNumeric.setCellFormula("F1"); - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; + Cell cNumeric = row.createCell(5); + cNumeric.setCellValue(1.2); + + Cell cDate = row.createCell(6); + cDate.setCellValue(new Date()); + + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; + + XSSFMap map = mapInfo.getXSSFMapById(4); - XSSFMap map = mapInfo.getXSSFMapById(4); + assertNotNull("XSSFMap is null", map); - assertNotNull("XSSFMap is null", map); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); + assertNotNull(xmlData); + assertFalse(xmlData.isEmpty()); - assertNotNull(xmlData); - assertFalse(xmlData.isEmpty()); - - parseXML(xmlData); - - found = true; + parseXML(xmlData); + + found = true; + } + assertTrue(found); } - assertTrue(found); } @Test public void testValidateFalse() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx")) { + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; + XSSFMap map = mapInfo.getXSSFMapById(4); - XSSFMap map = mapInfo.getXSSFMapById(4); + assertNotNull("XSSFMap is null", map); - assertNotNull("XSSFMap is null", map); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, false); + String xmlData = os.toString("UTF-8"); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, false); - String xmlData = os.toString("UTF-8"); + assertNotNull(xmlData); + assertFalse(xmlData.isEmpty()); - assertNotNull(xmlData); - assertFalse(xmlData.isEmpty()); - - parseXML(xmlData); - - found = true; + parseXML(xmlData); + + found = true; + } + assertTrue(found); } - assertTrue(found); } @Test public void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx"); - - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { - - if (!(p instanceof MapInfo)) { - continue; + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx")) { + + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; + + XSSFMap map = mapInfo.getXSSFMapById(1); + + assertNotNull("XSSFMap is null", map); + + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); + + assertNotNull(xmlData); + assertFalse(xmlData.isEmpty()); + + assertEquals("2014-12-31", xmlData.split("")[1].split("")[0].trim()); + assertEquals("12.5", xmlData.split("")[1].split("")[0].trim()); + + parseXML(xmlData); + + found = true; } - MapInfo mapInfo = (MapInfo) p; - - XSSFMap map = mapInfo.getXSSFMapById(1); - - assertNotNull("XSSFMap is null", map); - - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); - - assertNotNull(xmlData); - assertFalse(xmlData.isEmpty()); - - assertEquals("2014-12-31", xmlData.split("")[1].split("")[0].trim()); - assertEquals("12.5", xmlData.split("")[1].split("")[0].trim()); - - parseXML(xmlData); - - found = true; + assertTrue(found); } - assertTrue(found); } @Test public void testBug59026() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx"); - - Collection mappings = wb.getCustomXMLMappings(); - assertTrue(mappings.size() > 0); - for (XSSFMap map : mappings) { - XSSFExportToXml exporter = new XSSFExportToXml(map); - - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, false); - assertNotNull(os.toString("UTF-8")); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx")) { + Collection mappings = wb.getCustomXMLMappings(); + assertTrue(mappings.size() > 0); + for (XSSFMap map : mappings) { + XSSFExportToXml exporter = new XSSFExportToXml(map); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, false); + assertNotNull(os.toString("UTF-8")); + } } } @Test public void testExportTableWithNonMappedColumn_Bugzilla_61281() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61281.xlsx"); - for (XSSFMap map : wb.getCustomXMLMappings()) { - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - exporter.exportToXML(bos, true); - assertNotNull(DocumentHelper.readDocument(new ByteArrayInputStream(bos.toByteArray()))); - String exportedXml = bos.toString("UTF-8"); - assertEquals("1", exportedXml.replaceAll("\\s+", "")); + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61281.xlsx")) { + for (XSSFMap map : wb.getCustomXMLMappings()) { + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + exporter.exportToXML(bos, true); + assertNotNull(DocumentHelper.readDocument(new ByteArrayInputStream(bos.toByteArray()))); + String exportedXml = bos.toString("UTF-8"); + assertEquals("1", exportedXml.replaceAll("\\s+", "")); + } } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java index 366d853f73..dfd4ce5ca6 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java @@ -31,35 +31,36 @@ import org.apache.poi.POIDataSamples; public class TestLoadSaveXSSF extends TestCase { - private static final POIDataSamples _ssSampels = POIDataSamples.getSpreadSheetInstance(); + private static final POIDataSamples _ssSamples = POIDataSamples.getSpreadSheetInstance(); public void testLoadSample() throws Exception { - XSSFWorkbook workbook = new XSSFWorkbook(_ssSampels.openResourceAsStream("sample.xlsx")); - assertEquals(3, workbook.getNumberOfSheets()); - assertEquals("Sheet1", workbook.getSheetName(0)); - Sheet sheet = workbook.getSheetAt(0); - Row row = sheet.getRow(0); - Cell cell = row.getCell((short) 1); - assertNotNull(cell); - assertEquals(111.0, cell.getNumericCellValue(), 0.0); - cell = row.getCell((short) 0); - assertEquals("Lorem", cell.getRichStringCellValue().getString()); + try (XSSFWorkbook workbook = new XSSFWorkbook(_ssSamples.openResourceAsStream("sample.xlsx"))) { + assertEquals(3, workbook.getNumberOfSheets()); + assertEquals("Sheet1", workbook.getSheetName(0)); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + Cell cell = row.getCell((short) 1); + assertNotNull(cell); + assertEquals(111.0, cell.getNumericCellValue(), 0.0); + cell = row.getCell((short) 0); + assertEquals("Lorem", cell.getRichStringCellValue().getString()); + } } - // TODO filename string hard coded in XSSFWorkbook constructor in order to make ant test-ooxml target be successful. public void testLoadStyles() throws Exception { - XSSFWorkbook workbook = new XSSFWorkbook(_ssSampels.openResourceAsStream("styles.xlsx")); - Sheet sheet = workbook.getSheetAt(0); - Row row = sheet.getRow(0); - Cell cell = row.getCell((short) 0); - CellStyle style = cell.getCellStyle(); - // assertNotNull(style); + try (XSSFWorkbook workbook = new XSSFWorkbook(_ssSamples.openResourceAsStream("styles.xlsx"))) { + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + Cell cell = row.getCell((short) 0); + CellStyle style = cell.getCellStyle(); + assertNotNull(style); + } } - // TODO filename string hard coded in XSSFWorkbook constructor in order to make ant test-ooxml target be successful. public void testLoadPictures() throws Exception { - XSSFWorkbook workbook = new XSSFWorkbook(_ssSampels.openResourceAsStream("picture.xlsx")); - List pictures = workbook.getAllPictures(); - assertEquals(1, pictures.size()); + try (XSSFWorkbook workbook = new XSSFWorkbook(_ssSamples.openResourceAsStream("picture.xlsx"))) { + List pictures = workbook.getAllPictures(); + assertEquals(1, pictures.size()); + } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCalculationChain.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCalculationChain.java index 42cf2714a5..c4f0f5ef6b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCalculationChain.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCalculationChain.java @@ -26,37 +26,37 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell; import junit.framework.TestCase; +import java.io.IOException; public final class TestCalculationChain extends TestCase { - public void test46535() { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("46535.xlsx"); - - CalculationChain chain = wb.getCalculationChain(); - //the bean holding the reference to the formula to be deleted - CTCalcCell c = chain.getCTCalcChain().getCArray(0); - int cnt = chain.getCTCalcChain().sizeOfCArray(); - assertEquals(10, c.getI()); - assertEquals("E1", c.getR()); - - XSSFSheet sheet = wb.getSheet("Test"); - XSSFCell cell = sheet.getRow(0).getCell(4); - - assertEquals(CellType.FORMULA, cell.getCellType()); - cell.setCellFormula(null); - - //the count of items is less by one - c = chain.getCTCalcChain().getCArray(0); - int cnt2 = chain.getCTCalcChain().sizeOfCArray(); - assertEquals(cnt - 1, cnt2); - //the first item in the calculation chain is the former second one - assertEquals(10, c.getI()); - assertEquals("C1", c.getR()); - - assertEquals(CellType.STRING, cell.getCellType()); - cell.setCellValue("ABC"); - assertEquals(CellType.STRING, cell.getCellType()); + public void test46535() throws IOException { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("46535.xlsx")) { + + CalculationChain chain = wb.getCalculationChain(); + //the bean holding the reference to the formula to be deleted + CTCalcCell c = chain.getCTCalcChain().getCArray(0); + int cnt = chain.getCTCalcChain().sizeOfCArray(); + assertEquals(10, c.getI()); + assertEquals("E1", c.getR()); + + XSSFSheet sheet = wb.getSheet("Test"); + XSSFCell cell = sheet.getRow(0).getCell(4); + + assertEquals(CellType.FORMULA, cell.getCellType()); + cell.setCellFormula(null); + + //the count of items is less by one + c = chain.getCTCalcChain().getCArray(0); + int cnt2 = chain.getCTCalcChain().sizeOfCArray(); + assertEquals(cnt - 1, cnt2); + //the first item in the calculation chain is the former second one + assertEquals(10, c.getI()); + assertEquals("C1", c.getR()); + + assertEquals(CellType.STRING, cell.getCellType()); + cell.setCellValue("ABC"); + assertEquals(CellType.STRING, cell.getCellType()); + } } - - } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java index 9a1d1fdd83..b6df578c36 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java @@ -54,7 +54,7 @@ public class TestCommentsTable { private static final String TEST_AUTHOR = "test author"; @Test - public void findAuthor() throws Exception { + public void findAuthor() { CommentsTable sheetComments = new CommentsTable(); assertEquals(1, sheetComments.getNumberOfAuthors()); assertEquals(0, sheetComments.findAuthor("")); @@ -68,7 +68,7 @@ public class TestCommentsTable { } @Test - public void getCellComment() throws Exception { + public void getCellComment() { CommentsTable sheetComments = new CommentsTable(); CTComments comments = sheetComments.getCTComments(); @@ -94,123 +94,126 @@ public class TestCommentsTable { @Test - public void existing() { - Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx"); - Sheet sheet1 = workbook.getSheetAt(0); - Sheet sheet2 = workbook.getSheetAt(1); - - assertTrue( ((XSSFSheet)sheet1).hasComments() ); - assertFalse( ((XSSFSheet)sheet2).hasComments() ); - - // Comments should be in C5 and C7 - Row r5 = sheet1.getRow(4); - Row r7 = sheet1.getRow(6); - assertNotNull( r5.getCell(2).getCellComment() ); - assertNotNull( r7.getCell(2).getCellComment() ); - - // Check they have what we expect - // TODO: Rich text formatting - Comment cc5 = r5.getCell(2).getCellComment(); - Comment cc7 = r7.getCell(2).getCellComment(); - - assertEquals("Nick Burch", cc5.getAuthor()); - assertEquals("Nick Burch:\nThis is a comment", cc5.getString().getString()); - assertEquals(4, cc5.getRow()); - assertEquals(2, cc5.getColumn()); - - assertEquals("Nick Burch", cc7.getAuthor()); - assertEquals("Nick Burch:\nComment #1\n", cc7.getString().getString()); - assertEquals(6, cc7.getRow()); - assertEquals(2, cc7.getColumn()); + public void existing() throws IOException { + try (Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx")) { + Sheet sheet1 = workbook.getSheetAt(0); + Sheet sheet2 = workbook.getSheetAt(1); + + assertTrue(((XSSFSheet) sheet1).hasComments()); + assertFalse(((XSSFSheet) sheet2).hasComments()); + + // Comments should be in C5 and C7 + Row r5 = sheet1.getRow(4); + Row r7 = sheet1.getRow(6); + assertNotNull(r5.getCell(2).getCellComment()); + assertNotNull(r7.getCell(2).getCellComment()); + + // Check they have what we expect + // TODO: Rich text formatting + Comment cc5 = r5.getCell(2).getCellComment(); + Comment cc7 = r7.getCell(2).getCellComment(); + + assertEquals("Nick Burch", cc5.getAuthor()); + assertEquals("Nick Burch:\nThis is a comment", cc5.getString().getString()); + assertEquals(4, cc5.getRow()); + assertEquals(2, cc5.getColumn()); + + assertEquals("Nick Burch", cc7.getAuthor()); + assertEquals("Nick Burch:\nComment #1\n", cc7.getString().getString()); + assertEquals(6, cc7.getRow()); + assertEquals(2, cc7.getColumn()); + } } @Test - public void writeRead() { - XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx"); - XSSFSheet sheet1 = workbook.getSheetAt(0); - XSSFSheet sheet2 = workbook.getSheetAt(1); - - assertTrue( sheet1.hasComments() ); - assertFalse( sheet2.hasComments() ); - - // Change on comment on sheet 1, and add another into - // sheet 2 - Row r5 = sheet1.getRow(4); - Comment cc5 = r5.getCell(2).getCellComment(); - cc5.setAuthor("Apache POI"); - cc5.setString(new XSSFRichTextString("Hello!")); - - Row r2s2 = sheet2.createRow(2); - Cell c1r2s2 = r2s2.createCell(1); - assertNull(c1r2s2.getCellComment()); - - Drawing dg = sheet2.createDrawingPatriarch(); - Comment cc2 = dg.createCellComment(new XSSFClientAnchor()); - cc2.setAuthor("Also POI"); - cc2.setString(new XSSFRichTextString("A new comment")); - c1r2s2.setCellComment(cc2); - - - // Save, and re-load the file - workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); - - // Check we still have comments where we should do - sheet1 = workbook.getSheetAt(0); - sheet2 = workbook.getSheetAt(1); - assertNotNull(sheet1.getRow(4).getCell(2).getCellComment()); - assertNotNull(sheet1.getRow(6).getCell(2).getCellComment()); - assertNotNull(sheet2.getRow(2).getCell(1).getCellComment()); - - // And check they still have the contents they should do - assertEquals("Apache POI", - sheet1.getRow(4).getCell(2).getCellComment().getAuthor()); - assertEquals("Nick Burch", - sheet1.getRow(6).getCell(2).getCellComment().getAuthor()); - assertEquals("Also POI", - sheet2.getRow(2).getCell(1).getCellComment().getAuthor()); - - assertEquals("Hello!", - sheet1.getRow(4).getCell(2).getCellComment().getString().getString()); + public void writeRead() throws IOException { + try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx")) { + XSSFSheet sheet1 = workbook.getSheetAt(0); + XSSFSheet sheet2 = workbook.getSheetAt(1); + + assertTrue(sheet1.hasComments()); + assertFalse(sheet2.hasComments()); + + // Change on comment on sheet 1, and add another into + // sheet 2 + Row r5 = sheet1.getRow(4); + Comment cc5 = r5.getCell(2).getCellComment(); + cc5.setAuthor("Apache POI"); + cc5.setString(new XSSFRichTextString("Hello!")); + + Row r2s2 = sheet2.createRow(2); + Cell c1r2s2 = r2s2.createCell(1); + assertNull(c1r2s2.getCellComment()); + + Drawing dg = sheet2.createDrawingPatriarch(); + Comment cc2 = dg.createCellComment(new XSSFClientAnchor()); + cc2.setAuthor("Also POI"); + cc2.setString(new XSSFRichTextString("A new comment")); + c1r2s2.setCellComment(cc2); + + // Save, and re-load the file + try (XSSFWorkbook workbookBack = XSSFTestDataSamples.writeOutAndReadBack(workbook)) { + // Check we still have comments where we should do + sheet1 = workbookBack.getSheetAt(0); + sheet2 = workbookBack.getSheetAt(1); + assertNotNull(sheet1.getRow(4).getCell(2).getCellComment()); + assertNotNull(sheet1.getRow(6).getCell(2).getCellComment()); + assertNotNull(sheet2.getRow(2).getCell(1).getCellComment()); + + // And check they still have the contents they should do + assertEquals("Apache POI", + sheet1.getRow(4).getCell(2).getCellComment().getAuthor()); + assertEquals("Nick Burch", + sheet1.getRow(6).getCell(2).getCellComment().getAuthor()); + assertEquals("Also POI", + sheet2.getRow(2).getCell(1).getCellComment().getAuthor()); + + assertEquals("Hello!", + sheet1.getRow(4).getCell(2).getCellComment().getString().getString()); + } + } } @Test - public void readWriteMultipleAuthors() { - XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx"); - XSSFSheet sheet1 = workbook.getSheetAt(0); - XSSFSheet sheet2 = workbook.getSheetAt(1); - - assertTrue( sheet1.hasComments() ); - assertFalse( sheet2.hasComments() ); - - assertEquals("Nick Burch", - sheet1.getRow(4).getCell(2).getCellComment().getAuthor()); - assertEquals("Nick Burch", - sheet1.getRow(6).getCell(2).getCellComment().getAuthor()); - assertEquals("Torchbox", - sheet1.getRow(12).getCell(2).getCellComment().getAuthor()); - - // Save, and re-load the file - workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); - - // Check we still have comments where we should do - sheet1 = workbook.getSheetAt(0); - assertNotNull(sheet1.getRow(4).getCell(2).getCellComment()); - assertNotNull(sheet1.getRow(6).getCell(2).getCellComment()); - assertNotNull(sheet1.getRow(12).getCell(2).getCellComment()); - - // And check they still have the contents they should do - assertEquals("Nick Burch", - sheet1.getRow(4).getCell(2).getCellComment().getAuthor()); - assertEquals("Nick Burch", - sheet1.getRow(6).getCell(2).getCellComment().getAuthor()); - assertEquals("Torchbox", - sheet1.getRow(12).getCell(2).getCellComment().getAuthor()); - - // Todo - check text too, once bug fixed + public void readWriteMultipleAuthors() throws IOException { + try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx")) { + XSSFSheet sheet1 = workbook.getSheetAt(0); + XSSFSheet sheet2 = workbook.getSheetAt(1); + + assertTrue(sheet1.hasComments()); + assertFalse(sheet2.hasComments()); + + assertEquals("Nick Burch", + sheet1.getRow(4).getCell(2).getCellComment().getAuthor()); + assertEquals("Nick Burch", + sheet1.getRow(6).getCell(2).getCellComment().getAuthor()); + assertEquals("Torchbox", + sheet1.getRow(12).getCell(2).getCellComment().getAuthor()); + + // Save, and re-load the file + try (XSSFWorkbook workbookBack = XSSFTestDataSamples.writeOutAndReadBack(workbook)) { + + // Check we still have comments where we should do + sheet1 = workbookBack.getSheetAt(0); + assertNotNull(sheet1.getRow(4).getCell(2).getCellComment()); + assertNotNull(sheet1.getRow(6).getCell(2).getCellComment()); + assertNotNull(sheet1.getRow(12).getCell(2).getCellComment()); + + // And check they still have the contents they should do + assertEquals("Nick Burch", + sheet1.getRow(4).getCell(2).getCellComment().getAuthor()); + assertEquals("Nick Burch", + sheet1.getRow(6).getCell(2).getCellComment().getAuthor()); + assertEquals("Torchbox", + sheet1.getRow(12).getCell(2).getCellComment().getAuthor()); + + // Todo - check text too, once bug fixed + } + } } @Test - public void removeComment() throws Exception { + public void removeComment() { final CellAddress addrA1 = new CellAddress("A1"); final CellAddress addrA2 = new CellAddress("A2"); final CellAddress addrA3 = new CellAddress("A3"); @@ -257,7 +260,7 @@ public class TestCommentsTable { Cell A1 = getCell(sheet, 0, 0); //Cell A1 = getCell(sheet, 2, 2); Drawing drawing = sheet.createDrawingPatriarch(); - setComment(sheet, A1, drawing, "for A1", helper, anchor); + setComment(A1, drawing, "for A1", helper, anchor); // find comment in A1 before we set the comment in B2 Comment commentA1 = A1.getCellComment(); @@ -266,7 +269,7 @@ public class TestCommentsTable { // place comment in B2, according to Bug 54920 this removes the comment in A1! Cell B2 = getCell(sheet, 1, 1); - setComment(sheet, B2, drawing, "for B2", helper, anchor); + setComment(B2, drawing, "for B2", helper, anchor); // find comment in A1 Comment commentB2 = B2.getCellComment(); @@ -282,7 +285,7 @@ public class TestCommentsTable { // Set the comment on a sheet // - private static void setComment(Sheet sheet, Cell cell, Drawing drawing, String commentText, CreationHelper helper, ClientAnchor anchor) { + private static void setComment(Cell cell, Drawing drawing, String commentText, CreationHelper helper, ClientAnchor anchor) { anchor.setCol1(cell.getColumnIndex()); anchor.setCol2(cell.getColumnIndex()); anchor.setRow1(cell.getRowIndex()); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java index 4bf84a5534..c140652012 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java @@ -159,7 +159,7 @@ public class TestXWPFPictureData extends TestCase { } - public void testBug51770() throws InvalidFormatException, IOException { + public void testBug51770() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx"); XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy(); XWPFHeader header = policy.getDefaultHeader(); @@ -175,6 +175,5 @@ public class TestXWPFPictureData extends TestCase { } } } - } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index 41a2f5aa1c..ce0d97f2b8 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -1155,32 +1155,36 @@ public abstract class BaseTestCell { } @Test - public void getBooleanCellValue_returnsFalse_onABlankCell() { - Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0); - assertEquals(CellType.BLANK, cell.getCellType()); - boolean result = cell.getBooleanCellValue(); - assertFalse(result); + public void getBooleanCellValue_returnsFalse_onABlankCell() throws IOException { + try (Workbook workbook = _testDataProvider.createWorkbook()) { + Cell cell = workbook.createSheet().createRow(0).createCell(0); + assertEquals(CellType.BLANK, cell.getCellType()); + boolean result = cell.getBooleanCellValue(); + assertFalse(result); + } } @Test - public void setStringCellValue_ifThrows_shallNotChangeCell() { - Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0); + public void setStringCellValue_ifThrows_shallNotChangeCell() throws IOException { + try (Workbook workbook = _testDataProvider.createWorkbook()) { + Cell cell = workbook.createSheet().createRow(0).createCell(0); - final double value = 2.78; - cell.setCellValue(value); - assertEquals(CellType.NUMERIC, cell.getCellType()); + final double value = 2.78; + cell.setCellValue(value); + assertEquals(CellType.NUMERIC, cell.getCellType()); - int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1; - String badStringValue = new String(new byte[badLength], StandardCharsets.UTF_8); + int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1; + String badStringValue = new String(new byte[badLength], StandardCharsets.UTF_8); - try { - cell.setCellValue(badStringValue); - } catch (IllegalArgumentException e) { - // no-op, expected to throw but we need to assert something more - } + try { + cell.setCellValue(badStringValue); + } catch (IllegalArgumentException e) { + // no-op, expected to throw but we need to assert something more + } - assertEquals(CellType.NUMERIC, cell.getCellType()); - assertEquals(value, cell.getNumericCellValue(), 0); + assertEquals(CellType.NUMERIC, cell.getCellType()); + assertEquals(value, cell.getNumericCellValue(), 0); + } } @Test @@ -1359,22 +1363,30 @@ public abstract class BaseTestCell { } @Test(expected = IllegalArgumentException.class) - public void setCellType_FORMULA_onANonFormulaCell_throwsIllegalArgumentException() { + public void setCellType_FORMULA_onANonFormulaCell_throwsIllegalArgumentException() throws IOException { Cell cell = getInstance(); - cell.setCellType(CellType.FORMULA); + try { + cell.setCellType(CellType.FORMULA); + } finally { + cell.getSheet().getWorkbook().close(); + } } @Test - public void setCellType_FORMULA_onAFormulaCell_doesNothing() { + public void setCellType_FORMULA_onAFormulaCell_doesNothing() throws IOException { Cell cell = getInstance(); - cell.setCellFormula("3"); - cell.setCellValue("foo"); - - cell.setCellType(CellType.FORMULA); - - assertEquals(CellType.FORMULA, cell.getCellType()); - assertEquals(CellType.STRING, cell.getCachedFormulaResultType()); - assertEquals("foo", cell.getStringCellValue()); + try { + cell.setCellFormula("3"); + cell.setCellValue("foo"); + + cell.setCellType(CellType.FORMULA); + + assertEquals(CellType.FORMULA, cell.getCellType()); + assertEquals(CellType.STRING, cell.getCachedFormulaResultType()); + assertEquals("foo", cell.getStringCellValue()); + } finally { + cell.getSheet().getWorkbook().close(); + } } @Test @@ -1397,7 +1409,7 @@ public abstract class BaseTestCell { cell.setBlank(); - verify(cell).setCellType(CellType.BLANK); + verify(cell).setBlank(); } private Cell getInstance() {