diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2021-10-20 22:50:35 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2021-10-20 22:50:35 +0000 |
commit | bf360400be4b1c38323605afc996609dc311e067 (patch) | |
tree | 9e01d82d0e1bac510b089a0f86b60fbf2cdd50fb | |
parent | f09e50305efcb86c93850013948bbbe9000ecd7a (diff) | |
download | poi-bf360400be4b1c38323605afc996609dc311e067.tar.gz poi-bf360400be4b1c38323605afc996609dc311e067.zip |
sonar fixes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894426 13f79535-47bb-0310-9956-ffa450edef68
35 files changed, 915 insertions, 1064 deletions
diff --git a/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java b/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java index 57306d1637..c55ad25de4 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java @@ -368,29 +368,6 @@ public final class TestPOIXMLProperties { assertNotNull(wbBack.getProperties(), "Third writeOutAndReadBack"); assertEquals(propValue, wbBack.getProperties().getCustomProperties().getProperty(propName).getLpwstr(), "Third prop check"); assertEquals(propValue, wbBack.getProperties().getCustomProperties().getProperty(propName + "1").getLpwstr(), "Third prop check1"); - - /* Manual test to write out the file more than once: - File test1 = File.createTempFile("test1", ".xlsx", new File("C:\\temp")); - File test2 = File.createTempFile("test2", ".xlsx", new File("C:\\temp")); - try (final java.io.FileOutputStream fs = new java.io.FileOutputStream(test1)) { - workbook.write(fs); - } - try (final XSSFWorkbook wb = new XSSFWorkbook(test1)) { - assertNotNull(wb.getProperties()); - } catch (InvalidFormatException e) { - fail("Test1 copy failed: " + e.getMessage()); - } - - try (final java.io.FileOutputStream fs = new java.io.FileOutputStream(test2)) { - workbook.write(fs); - } - - try (final XSSFWorkbook wb = new XSSFWorkbook(test2)) { - assertNotNull(wb.getProperties()); - } catch (InvalidFormatException e) { - fail("Test2 copy failed: " + e.getMessage()); - } - */ } } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 4e06e60161..0762dbbe8f 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -2035,9 +2035,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { // Try to write-out and read again, should only work // in read-write mode, not read-only mode try (XSSFWorkbook wb2 = writeOutAndReadBack(wb1)) { - if (access == PackageAccess.READ) { - fail("Shouln't be able to write from read-only mode"); - } + assertNotEquals(PackageAccess.READ, access, "Shouln't be able to write from read-only mode"); // Check again s = wb2.getSheetAt(0); @@ -3552,20 +3550,20 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFCell v16 = sheet.getRow(15).getCell(21); XSSFCell v17 = sheet.getRow(16).getCell(21); assertEquals("U15/R15", v15.getCellFormula()); - assertEquals(STCellFormulaType.SHARED, v15.getCTCell().getF().getT()); + assertSame(STCellFormulaType.SHARED, v15.getCTCell().getF().getT()); assertEquals("U16/R16", v16.getCellFormula()); - assertEquals(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT()); //anomaly in original file + assertSame(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT()); //anomaly in original file assertEquals("U17/R17", v17.getCellFormula()); - assertEquals(STCellFormulaType.SHARED, v17.getCTCell().getF().getT()); + assertSame(STCellFormulaType.SHARED, v17.getCTCell().getF().getT()); int calcChainSize = wb.getCalculationChain().getCTCalcChain().sizeOfCArray(); v15.removeFormula(); assertEquals(CellType.NUMERIC, v15.getCellType(), "V15 is no longer a function"); assertNull(v15.getCTCell().getF(), "V15 xmlbeans function removed"); assertEquals("U16/R16", v16.getCellFormula()); - assertEquals(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT()); + assertSame(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT()); assertEquals("U17/R17", v17.getCellFormula()); - assertEquals(STCellFormulaType.SHARED, v17.getCTCell().getF().getT()); + assertSame(STCellFormulaType.SHARED, v17.getCTCell().getF().getT()); assertEquals(calcChainSize - 1, wb.getCalculationChain().getCTCalcChain().sizeOfCArray()); } } @@ -3623,21 +3621,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { void testBug65452() throws IOException { File file = XSSFTestDataSamples.getSampleFile("workbook.xml"); try (FileInputStream fis = new FileInputStream(file)) { - try { - Workbook wb = WorkbookFactory.create(fis); - if (wb != null) wb.close(); - fail("WorkbookFactory.create should have failed"); - } catch (IOException ie) { - assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage()); - } - } - try { - Workbook wb = WorkbookFactory.create(file); - if (wb != null) wb.close(); - fail("WorkbookFactory.create should have failed"); - } catch (IOException ie) { + IOException ie = assertThrows(IOException.class, () -> WorkbookFactory.create(fis)); assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage()); } + IOException ie = assertThrows(IOException.class, () -> WorkbookFactory.create(file)); + assertEquals("Can't open workbook - unsupported file type: XML", ie.getMessage()); } @Test diff --git a/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingShapes.java b/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingShapes.java index 185c257383..81673da773 100644 --- a/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingShapes.java +++ b/poi/src/test/java/org/apache/poi/hssf/model/TestDrawingShapes.java @@ -228,7 +228,7 @@ class TestDrawingShapes { assertEquals(1, drawing.getChildren().size()); rectangle2 = (HSSFSimpleShape) drawing.getChildren().get(0); assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, rectangle2.getShapeType()); - assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_BY_POINTS); + assertEquals(HSSFSimpleShape.WRAP_BY_POINTS, rectangle.getWrapText()); assertEquals(77, rectangle2.getLineWidth()); assertEquals(9, rectangle2.getLineStyle()); assertEquals(4444, rectangle2.getLineStyleColor()); diff --git a/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java b/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java index 379eb45694..6c55d18224 100644 --- a/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java @@ -17,15 +17,8 @@ package org.apache.poi.hssf.model; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.apache.poi.hssf.model.HSSFFormulaParser.parse; +import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.util.Locale; @@ -64,7 +57,7 @@ final class TestFormulaParser { * @return parsed token array already confirmed not {@code null} */ /* package */ static Ptg[] parseFormula(String formula) { - Ptg[] result = HSSFFormulaParser.parse(formula, null); + Ptg[] result = parse(formula, null); assertNotNull(result, "Ptg array should not be null"); return result; } @@ -172,17 +165,6 @@ final class TestFormulaParser { HSSFName yourFunc = wb2.getName("yourFunc"); assertNotNull(yourFunc); assertEqualsIgnoreCase("yourFunc", yourFunc.getNameName()); - - // Manually check to make sure file isn't corrupted - // TODO: develop a process for occasionally manually reviewing workbooks - // to verify workbooks are not corrupted - /* - final File fileIn = HSSFTestDataSamples.getSampleFile(testFile); - final File reSavedFile = new File(fileIn.getParentFile(), fileIn.getName().replace(".xls", "-saved.xls")); - FileOutputStream fos = new FileOutputStream(reSavedFile); - wb2.write(fos); - fos.close(); - */ } } } @@ -910,7 +892,7 @@ final class TestFormulaParser { private static void confirmArgCountMsg(String formula, String expectedMessage) throws IOException { try (HSSFWorkbook book = new HSSFWorkbook()) { - FormulaParseException e = assertThrows(FormulaParseException.class, () -> HSSFFormulaParser.parse(formula, book)); + FormulaParseException e = assertThrows(FormulaParseException.class, () -> parse(formula, book)); confirmParseException(e, expectedMessage); } } @@ -954,7 +936,7 @@ final class TestFormulaParser { Ptg[] ptgs; try { - ptgs = HSSFFormulaParser.parse("count(pfy1)", wb); + ptgs = parse("count(pfy1)", wb); } catch (IllegalArgumentException e) { if (e.getMessage().equals("Specified colIx (1012) is out of range")) { fail("Identified bug 45354"); @@ -980,12 +962,12 @@ final class TestFormulaParser { HSSFWorkbook book = new HSSFWorkbook(); book.createSheet("Sheet1"); - ptgs = HSSFFormulaParser.parse("Sheet1!A10:A40000", book); + ptgs = parse("Sheet1!A10:A40000", book); aptg = (AreaI) ptgs[0]; assertNotEquals(-25537, aptg.getLastRow(), "Identified bug 45358"); assertEquals(39999, aptg.getLastRow()); - ptgs = HSSFFormulaParser.parse("Sheet1!A10:A65536", book); + ptgs = parse("Sheet1!A10:A65536", book); aptg = (AreaI) ptgs[0]; assertEquals(65535, aptg.getLastRow()); @@ -1108,11 +1090,11 @@ final class TestFormulaParser { confirmSingle3DRef(expectedPtgs, 1); // now try (re-)parsing the formula - Ptg[] actualPtgs = HSSFFormulaParser.parse("[multibookFormulaB.xls]BSheet1!B1", wbA); + Ptg[] actualPtgs = parse("[multibookFormulaB.xls]BSheet1!B1", wbA); confirmSingle3DRef(actualPtgs, 1); // externalSheetIndex 1 -> BSheet1 // try parsing a formula pointing to a different external sheet - Ptg[] otherPtgs = HSSFFormulaParser.parse("[multibookFormulaB.xls]AnotherSheet!B1", wbA); + Ptg[] otherPtgs = parse("[multibookFormulaB.xls]AnotherSheet!B1", wbA); confirmSingle3DRef(otherPtgs, 0); // externalSheetIndex 0 -> AnotherSheet // try setting the same formula in a cell @@ -1303,17 +1285,12 @@ final class TestFormulaParser { */ @Test void testParseAbnormalSheetNamesAndRanges_bug42448() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - wb.createSheet("A"); - try { - HSSFFormulaParser.parse("SUM(A!C7:A!C67)", wb); - } catch (StringIndexOutOfBoundsException e) { - fail("Identified bug 42448"); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + wb.createSheet("A"); + assertDoesNotThrow(() -> parse("SUM(A!C7:A!C67)", wb), "Identified bug 42448"); + // the exact example from the bugzilla description: + parse("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69", wb); } - // the exact example from the bugzilla description: - HSSFFormulaParser.parse("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69", wb); - - wb.close(); } @Test @@ -1321,7 +1298,7 @@ final class TestFormulaParser { try (HSSFWorkbook wb = new HSSFWorkbook()) { Ptg[] ptgs; try { - ptgs = HSSFFormulaParser.parse("SUM(C1:OFFSET(C1,0,B1))", wb); + ptgs = parse("SUM(C1:OFFSET(C1,0,B1))", wb); } catch (RuntimeException e) { if (e.getMessage().equals("Specified named range 'OFFSET' does not exist in the current workbook.")) { fail("Identified bug 46951"); @@ -1360,14 +1337,14 @@ final class TestFormulaParser { HSSFWorkbook wb = new HSSFWorkbook(); wb.createSheet("Sheet1"); - ptgs = HSSFFormulaParser.parse("Sheet1!$A:$A,Sheet1!$1:$4", wb); + ptgs = parse("Sheet1!$A:$A,Sheet1!$1:$4", wb); confirmTokenClasses(ptgs, MemFuncPtg.class, Area3DPtg.class, Area3DPtg.class, UnionPtg.class ); - ptgs = HSSFFormulaParser.parse("'Sheet1'!$A:$A,'Sheet1'!$1:$4", wb); + ptgs = parse("'Sheet1'!$A:$A,'Sheet1'!$1:$4", wb); confirmTokenClasses(ptgs, MemFuncPtg.class, Area3DPtg.class, @@ -1382,7 +1359,7 @@ final class TestFormulaParser { void testExplicitRangeWithTwoSheetNames() throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); wb.createSheet("Sheet1"); - Ptg[] ptgs = HSSFFormulaParser.parse("Sheet1!F1:Sheet1!G2", wb); + Ptg[] ptgs = parse("Sheet1!F1:Sheet1!G2", wb); confirmTokenClasses(ptgs, MemFuncPtg.class, Ref3DPtg.class, @@ -1470,7 +1447,7 @@ final class TestFormulaParser { } private static void confirmParseError(HSSFWorkbook wb, String formula, String expectedMessage) { - FormulaParseException e = assertThrows(FormulaParseException.class, () -> HSSFFormulaParser.parse(formula, wb)); + FormulaParseException e = assertThrows(FormulaParseException.class, () -> parse(formula, wb)); confirmParseException(e, expectedMessage); } @@ -1494,7 +1471,7 @@ final class TestFormulaParser { Ptg[] result; try { - result = HSSFFormulaParser.parse("1+foo", wb); + result = parse("1+foo", wb); } catch (FormulaParseException e) { if (e.getMessage().equals("Specified name 'foo' is not a range as expected.")) { fail("Identified bug 47078c"); @@ -1518,13 +1495,13 @@ final class TestFormulaParser { String leadingZeroCellRef = "B000001"; // this should get parsed as "B1" HSSFWorkbook wb = new HSSFWorkbook(); - FormulaParseException e = assertThrows(FormulaParseException.class, () -> HSSFFormulaParser.parse(badCellRef, wb), + FormulaParseException e = assertThrows(FormulaParseException.class, () -> parse(badCellRef, wb), "Identified bug 47312b - Shouldn't be able to parse cell ref '" + badCellRef + "'."); confirmParseException(e, "Specified named range '" + badCellRef + "' does not exist in the current workbook."); Ptg[] ptgs; try { - ptgs = HSSFFormulaParser.parse(leadingZeroCellRef, wb); + ptgs = parse(leadingZeroCellRef, wb); assertEquals("B1", ptgs[0].toFormulaString()); } catch (FormulaParseException e2) { confirmParseException(e2, "Specified named range '" + leadingZeroCellRef + "' does not exist in the current workbook."); @@ -1536,7 +1513,7 @@ final class TestFormulaParser { Name n = wb.createName(); n.setNameName("B0"); n.setRefersToFormula("1+1"); - ptgs = HSSFFormulaParser.parse("B0", wb); + ptgs = parse("B0", wb); confirmTokenClasses(ptgs, NamePtg.class); wb.close(); @@ -1549,7 +1526,7 @@ final class TestFormulaParser { @Test void test57196_Formula() throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); - Ptg[] ptgs = HSSFFormulaParser.parse("DEC2HEX(HEX2DEC(O8)-O2+D2)", wb, FormulaType.CELL, -1); + Ptg[] ptgs = parse("DEC2HEX(HEX2DEC(O8)-O2+D2)", wb, FormulaType.CELL, -1); assertNotNull(ptgs, "Ptg array should not be null"); confirmTokenClasses(ptgs, diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java index 0ddc016d29..703d88d1f5 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java @@ -18,8 +18,10 @@ package org.apache.poi.hssf.usermodel; import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -213,8 +215,8 @@ final class TestFormulaEvaluatorBugs { FormulaRecordAggregate frec = (FormulaRecordAggregate) cellSUM.getCellValueRecord(); Ptg[] ops = frec.getFormulaRecord().getParsedExpression(); assertEquals(2, ops.length); - assertEquals(AreaPtg.class, ops[0].getClass()); - assertEquals(FuncVarPtg.class, ops[1].getClass()); + assertSame(AreaPtg.class, ops[0].getClass()); + assertSame(FuncVarPtg.class, ops[1].getClass()); // Actually stored as C1 to C65536 // (last row is -1 === 65535) @@ -254,23 +256,18 @@ final class TestFormulaEvaluatorBugs { */ @Test void testEvaluateBooleanInCell_bug44508() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - wb.setSheetName(0, "Sheet1"); - HSSFRow row = sheet.createRow(0); - HSSFCell cell = row.createCell(0); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + wb.setSheetName(0, "Sheet1"); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); - cell.setCellFormula("1=1"); + cell.setCellFormula("1=1"); - HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); - try { - fe.evaluateInCell(cell); - } catch (NumberFormatException e) { - fail("Identified bug 44508"); + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + assertDoesNotThrow(() -> fe.evaluateInCell(cell), "Identified bug 44508"); + assertTrue(cell.getBooleanCellValue()); } - assertTrue(cell.getBooleanCellValue()); - - wb.close(); } @Test @@ -496,7 +493,7 @@ final class TestFormulaEvaluatorBugs { cell = row.getCell(i); Ptg[] ptgs = getPtgs(cell); assertEquals(4, ptgs.length); - assertEquals(FuncPtg.class, ptgs[3].getClass()); + assertSame(FuncPtg.class, ptgs[3].getClass()); assertEquals("MID", ((FuncPtg)ptgs[3]).getName()); assertRefPtgA1('V', ptgs, 0); } @@ -563,14 +560,14 @@ final class TestFormulaEvaluatorBugs { } private Ptg[] getPtgs(HSSFCell cell) { assertEquals(CellType.FORMULA, cell.getCellType()); - assertEquals(FormulaRecordAggregate.class, cell.getCellValueRecord().getClass()); + assertSame(FormulaRecordAggregate.class, cell.getCellValueRecord().getClass()); FormulaRecordAggregate agg = (FormulaRecordAggregate)cell.getCellValueRecord(); FormulaRecord rec = agg.getFormulaRecord(); return rec.getParsedExpression(); } private void assertRefPtgA1(char rv, Ptg[] ptgs, int at) { Ptg ptg = ptgs[at]; - assertEquals(RefPtg.class, ptg.getClass()); + assertSame(RefPtg.class, ptg.getClass()); assertEquals(0, ((RefPtg)ptg).getRow()); assertEquals(0, ((RefPtg)ptg).getColumn()); assertEquals(rv, ptg.getRVAType()); diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFComment.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFComment.java index d84663db2b..5644733467 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFComment.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFComment.java @@ -288,13 +288,13 @@ final class TestHSSFComment extends BaseTestCellComment { comment = patriarch.createCellComment(new HSSFClientAnchor()); comment.setString(new HSSFRichTextString("comment3")); - assertEquals(patriarch.getChildren().size(), 3); + assertEquals(3, patriarch.getChildren().size()); try (HSSFWorkbook wbBack2 = HSSFTestDataSamples.writeOutAndReadBack(wbBack)) { sh = wbBack2.getSheetAt(0); patriarch = sh.getDrawingPatriarch(); comment = (HSSFComment) patriarch.getChildren().get(1); - assertEquals(comment.getBackgroundImageId(), 0); - assertEquals(patriarch.getChildren().size(), 3); + assertEquals(0, comment.getBackgroundImageId()); + assertEquals(3, patriarch.getChildren().size()); assertEquals("comment1", ((HSSFComment) patriarch.getChildren().get(0)).getString().getString()); assertEquals("comment2", ((HSSFComment) patriarch.getChildren().get(1)).getString().getString()); assertEquals("comment3", ((HSSFComment) patriarch.getChildren().get(2)).getString().getString()); diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java index 4e2843ad22..9e9846e1ef 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java @@ -241,7 +241,7 @@ public final class TestHSSFDataFormatter { // should not be equal to "555.555" assertTrue( DateUtil.isCellDateFormatted(cell) ); - assertNotEquals(fmtval, "555.555"); + assertNotEquals("555.555", fmtval); String fmt = cell.getCellStyle().getDataFormatString(); @@ -271,7 +271,7 @@ public final class TestHSSFDataFormatter { // should not be equal to "555.47431" assertTrue( DateUtil.isCellDateFormatted(cell) ); - assertNotEquals(fmtval, "555.47431"); + assertNotEquals("555.47431", fmtval); // check we found the time properly assertTrue(fmtval.contains("11:23"), @@ -325,10 +325,10 @@ public final class TestHSSFDataFormatter { assertEquals("444-55-1234", formatter.formatCellValue(cell)); // null test-- null cell should result in empty String - assertEquals(formatter.formatCellValue(null), ""); + assertEquals("", formatter.formatCellValue(null)); // null test-- null cell should result in empty String - assertEquals(formatter.formatCellValue(null), ""); + assertEquals("", formatter.formatCellValue(null)); } @Test diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFFormulaEvaluator.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFFormulaEvaluator.java index 10d2ad8650..dec43bd1d9 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFFormulaEvaluator.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFFormulaEvaluator.java @@ -71,30 +71,28 @@ final class TestHSSFFormulaEvaluator extends BaseTestFormulaEvaluator { @Test void testDefinedNameWithComplexFlag_bug47048() throws IOException { // Mock up a spreadsheet to match the critical details of the sample - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("Input"); - HSSFName definedName = wb.createName(); - definedName.setNameName("Is_Multicar_Vehicle"); - definedName.setRefersToFormula("Input!$B$17:$G$17"); - - // Set up some data and the formula - HSSFRow row17 = sheet.createRow(16); - row17.createCell(0).setCellValue(25.0); - row17.createCell(1).setCellValue(1.33); - row17.createCell(2).setCellValue(4.0); - - HSSFRow row = sheet.createRow(0); - HSSFCell cellA1 = row.createCell(0); - cellA1.setCellFormula("SUM(Is_Multicar_Vehicle)"); - - // Set the complex flag - POI doesn't usually manipulate this flag - NameRecord nameRec = TestHSSFName.getNameRecord(definedName); - nameRec.setOptionFlag((short) 0x10); // 0x10 -> complex - - HSSFFormulaEvaluator hsf = new HSSFFormulaEvaluator(wb); - CellValue value; - try { - value = hsf.evaluate(cellA1); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet("Input"); + HSSFName definedName = wb.createName(); + definedName.setNameName("Is_Multicar_Vehicle"); + definedName.setRefersToFormula("Input!$B$17:$G$17"); + + // Set up some data and the formula + HSSFRow row17 = sheet.createRow(16); + row17.createCell(0).setCellValue(25.0); + row17.createCell(1).setCellValue(1.33); + row17.createCell(2).setCellValue(4.0); + + HSSFRow row = sheet.createRow(0); + HSSFCell cellA1 = row.createCell(0); + cellA1.setCellFormula("SUM(Is_Multicar_Vehicle)"); + + // Set the complex flag - POI doesn't usually manipulate this flag + NameRecord nameRec = TestHSSFName.getNameRecord(definedName); + nameRec.setOptionFlag((short) 0x10); // 0x10 -> complex + + HSSFFormulaEvaluator hsf = new HSSFFormulaEvaluator(wb); + CellValue value = hsf.evaluate(cellA1); assertEquals(CellType.NUMERIC, value.getCellType()); assertEquals(5.33, value.getNumberValue(), 0.0); @@ -104,8 +102,6 @@ final class TestHSSFFormulaEvaluator extends BaseTestFormulaEvaluator { fail("Identified bug 47048a"); } throw e; - } finally { - wb.close(); } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java index b703390585..c8a5265a63 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFHeaderFooter.java @@ -122,9 +122,9 @@ final class TestHSSFHeaderFooter { try (HSSFWorkbook b2 = HSSFTestDataSamples.writeOutAndReadBack(b)) { HSSFHeader h2 = b2.getSheet("Test").getHeader(); - assertEquals(h2.getLeft(), "\u0391"); - assertEquals(h2.getCenter(), "\u0392"); - assertEquals(h2.getRight(), "\u0393"); + assertEquals("\u0391", h2.getLeft()); + assertEquals("\u0392", h2.getCenter()); + assertEquals("\u0393", h2.getRight()); } } } @@ -144,9 +144,9 @@ final class TestHSSFHeaderFooter { try (HSSFWorkbook b2 = HSSFTestDataSamples.writeOutAndReadBack(b)) { HSSFFooter f2 = b2.getSheet("Test").getFooter(); - assertEquals(f2.getLeft(), "\u0391"); - assertEquals(f2.getCenter(), "\u0392"); - assertEquals(f2.getRight(), "\u0393"); + assertEquals("\u0391", f2.getLeft()); + assertEquals("\u0392", f2.getCenter()); + assertEquals("\u0393", f2.getRight()); } } } @@ -156,14 +156,14 @@ final class TestHSSFHeaderFooter { try (HSSFWorkbook wb = openSampleWorkbook("DBCSHeader.xls")) { HSSFSheet s = wb.getSheetAt(0); HSSFHeader h = s.getHeader(); - assertEquals(h.getLeft(), "\u090f\u0915", "Header Left"); - assertEquals(h.getCenter(), "\u0939\u094b\u0917\u093e", "Header Center"); - assertEquals(h.getRight(), "\u091c\u093e", "Header Right"); + assertEquals("\u090f\u0915", h.getLeft(), "Header Left"); + assertEquals("\u0939\u094b\u0917\u093e", h.getCenter(), "Header Center"); + assertEquals("\u091c\u093e", h.getRight(), "Header Right"); HSSFFooter f = s.getFooter(); - assertEquals(f.getLeft(), "\u091c\u093e", "Footer Left"); - assertEquals(f.getCenter(), "\u091c\u093e", "Footer Center"); - assertEquals(f.getRight(), "\u091c\u093e", "Footer Right"); + assertEquals("\u091c\u093e", f.getLeft(), "Footer Left"); + assertEquals("\u091c\u093e", f.getCenter(), "Footer Center"); + assertEquals("\u091c\u093e", f.getRight(), "Footer Right"); } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java index 95364a33c2..e51f4b7a54 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFOptimiser.java @@ -19,6 +19,8 @@ package org.apache.poi.hssf.usermodel; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.io.IOException; + import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FillPatternType; @@ -30,141 +32,143 @@ import org.junit.jupiter.api.Test; final class TestHSSFOptimiser { @Test - void testDoesNoHarmIfNothingToDo() { - HSSFWorkbook wb = new HSSFWorkbook(); + void testDoesNoHarmIfNothingToDo() throws IOException { + try (HSSFWorkbook wb = new HSSFWorkbook()) { - // New files start with 4 built in fonts, and 21 built in styles - assertEquals(4, wb.getNumberOfFonts()); - assertEquals(21, wb.getNumCellStyles()); + // New files start with 4 built in fonts, and 21 built in styles + assertEquals(4, wb.getNumberOfFonts()); + assertEquals(21, wb.getNumCellStyles()); - // Create a test font and style, and use them - HSSFFont f = wb.createFont(); - f.setFontName("Testing"); - HSSFCellStyle s = wb.createCellStyle(); - s.setFont(f); + // Create a test font and style, and use them + HSSFFont f = wb.createFont(); + f.setFontName("Testing"); + HSSFCellStyle s = wb.createCellStyle(); + s.setFont(f); - HSSFSheet sheet = wb.createSheet(); - HSSFRow row = sheet.createRow(0); - row.createCell(0).setCellStyle(s); + HSSFSheet sheet = wb.createSheet(); + HSSFRow row = sheet.createRow(0); + row.createCell(0).setCellStyle(s); - // Should have one more than the default of each - assertEquals(5, wb.getNumberOfFonts()); - assertEquals(22, wb.getNumCellStyles()); + // Should have one more than the default of each + assertEquals(5, wb.getNumberOfFonts()); + assertEquals(22, wb.getNumCellStyles()); - // Optimise fonts - HSSFOptimiser.optimiseFonts(wb); + // Optimise fonts + HSSFOptimiser.optimiseFonts(wb); - assertEquals(5, wb.getNumberOfFonts()); - assertEquals(22, wb.getNumCellStyles()); + assertEquals(5, wb.getNumberOfFonts()); + assertEquals(22, wb.getNumCellStyles()); - assertEquals(f, s.getFont(wb)); + assertEquals(f, s.getFont(wb)); - // Optimise styles - HSSFOptimiser.optimiseCellStyles(wb); + // Optimise styles + HSSFOptimiser.optimiseCellStyles(wb); - assertEquals(5, wb.getNumberOfFonts()); - assertEquals(22, wb.getNumCellStyles()); + assertEquals(5, wb.getNumberOfFonts()); + assertEquals(22, wb.getNumCellStyles()); - assertEquals(f, s.getFont(wb)); + assertEquals(f, s.getFont(wb)); + } } @Test - void testOptimiseFonts() { - HSSFWorkbook wb = new HSSFWorkbook(); - - // Add 6 fonts, some duplicates - HSSFFont f1 = wb.createFont(); - f1.setFontHeight((short) 11); - f1.setFontName("Testing"); - - HSSFFont f2 = wb.createFont(); - f2.setFontHeight((short) 22); - f2.setFontName("Also Testing"); - - HSSFFont f3 = wb.createFont(); - f3.setFontHeight((short) 33); - f3.setFontName("Unique"); - - HSSFFont f4 = wb.createFont(); - f4.setFontHeight((short) 11); - f4.setFontName("Testing"); - - HSSFFont f5 = wb.createFont(); - f5.setFontHeight((short) 22); - f5.setFontName("Also Testing"); - - HSSFFont f6 = wb.createFont(); - f6.setFontHeight((short) 66); - f6.setFontName("Also Unique"); - - // Use all three of the four in cell styles - assertEquals(21, wb.getNumCellStyles()); - - HSSFCellStyle cs1 = wb.createCellStyle(); - cs1.setFont(f1); - assertEquals(5, cs1.getFontIndex()); - - HSSFCellStyle cs2 = wb.createCellStyle(); - cs2.setFont(f4); - assertEquals(8, cs2.getFontIndex()); - - HSSFCellStyle cs3 = wb.createCellStyle(); - cs3.setFont(f5); - assertEquals(9, cs3.getFontIndex()); - - HSSFCellStyle cs4 = wb.createCellStyle(); - cs4.setFont(f6); - assertEquals(10, cs4.getFontIndex()); - - assertEquals(25, wb.getNumCellStyles()); - - // And three in rich text - HSSFSheet s = wb.createSheet(); - HSSFRow r = s.createRow(0); - - HSSFRichTextString rtr1 = new HSSFRichTextString("Test"); - rtr1.applyFont(0, 2, f1); - rtr1.applyFont(3, 4, f2); - r.createCell(0).setCellValue(rtr1); - - HSSFRichTextString rtr2 = new HSSFRichTextString("AlsoTest"); - rtr2.applyFont(0, 2, f3); - rtr2.applyFont(3, 5, f5); - rtr2.applyFont(6, 8, f6); - r.createCell(1).setCellValue(rtr2); - - // Check what we have now - assertEquals(10, wb.getNumberOfFonts()); - assertEquals(25, wb.getNumCellStyles()); - - // Optimise - HSSFOptimiser.optimiseFonts(wb); - - // Check font count - assertEquals(8, wb.getNumberOfFonts()); - assertEquals(25, wb.getNumCellStyles()); - - // Check font use in cell styles - assertEquals(5, cs1.getFontIndex()); - assertEquals(5, cs2.getFontIndex()); // duplicate of 1 - assertEquals(6, cs3.getFontIndex()); // duplicate of 2 - assertEquals(8, cs4.getFontIndex()); // two have gone - - // And in rich text - - // RTR 1 had f1 and f2, unchanged - assertEquals(5, r.getCell(0).getRichStringCellValue().getFontAtIndex(0)); - assertEquals(5, r.getCell(0).getRichStringCellValue().getFontAtIndex(1)); - assertEquals(6, r.getCell(0).getRichStringCellValue().getFontAtIndex(3)); - assertEquals(6, r.getCell(0).getRichStringCellValue().getFontAtIndex(4)); - - // RTR 2 had f3 (unchanged), f5 (=f2) and f6 (moved down) - assertEquals(7, r.getCell(1).getRichStringCellValue().getFontAtIndex(0)); - assertEquals(7, r.getCell(1).getRichStringCellValue().getFontAtIndex(1)); - assertEquals(6, r.getCell(1).getRichStringCellValue().getFontAtIndex(3)); - assertEquals(6, r.getCell(1).getRichStringCellValue().getFontAtIndex(4)); - assertEquals(8, r.getCell(1).getRichStringCellValue().getFontAtIndex(6)); - assertEquals(8, r.getCell(1).getRichStringCellValue().getFontAtIndex(7)); + void testOptimiseFonts() throws IOException { + try (HSSFWorkbook wb = new HSSFWorkbook()) { + + // Add 6 fonts, some duplicates + HSSFFont f1 = wb.createFont(); + f1.setFontHeight((short) 11); + f1.setFontName("Testing"); + + HSSFFont f2 = wb.createFont(); + f2.setFontHeight((short) 22); + f2.setFontName("Also Testing"); + + HSSFFont f3 = wb.createFont(); + f3.setFontHeight((short) 33); + f3.setFontName("Unique"); + + HSSFFont f4 = wb.createFont(); + f4.setFontHeight((short) 11); + f4.setFontName("Testing"); + + HSSFFont f5 = wb.createFont(); + f5.setFontHeight((short) 22); + f5.setFontName("Also Testing"); + + HSSFFont f6 = wb.createFont(); + f6.setFontHeight((short) 66); + f6.setFontName("Also Unique"); + + // Use all three of the four in cell styles + assertEquals(21, wb.getNumCellStyles()); + + HSSFCellStyle cs1 = wb.createCellStyle(); + cs1.setFont(f1); + assertEquals(5, cs1.getFontIndex()); + + HSSFCellStyle cs2 = wb.createCellStyle(); + cs2.setFont(f4); + assertEquals(8, cs2.getFontIndex()); + + HSSFCellStyle cs3 = wb.createCellStyle(); + cs3.setFont(f5); + assertEquals(9, cs3.getFontIndex()); + + HSSFCellStyle cs4 = wb.createCellStyle(); + cs4.setFont(f6); + assertEquals(10, cs4.getFontIndex()); + + assertEquals(25, wb.getNumCellStyles()); + + // And three in rich text + HSSFSheet s = wb.createSheet(); + HSSFRow r = s.createRow(0); + + HSSFRichTextString rtr1 = new HSSFRichTextString("Test"); + rtr1.applyFont(0, 2, f1); + rtr1.applyFont(3, 4, f2); + r.createCell(0).setCellValue(rtr1); + + HSSFRichTextString rtr2 = new HSSFRichTextString("AlsoTest"); + rtr2.applyFont(0, 2, f3); + rtr2.applyFont(3, 5, f5); + rtr2.applyFont(6, 8, f6); + r.createCell(1).setCellValue(rtr2); + + // Check what we have now + assertEquals(10, wb.getNumberOfFonts()); + assertEquals(25, wb.getNumCellStyles()); + + // Optimise + HSSFOptimiser.optimiseFonts(wb); + + // Check font count + assertEquals(8, wb.getNumberOfFonts()); + assertEquals(25, wb.getNumCellStyles()); + + // Check font use in cell styles + assertEquals(5, cs1.getFontIndex()); + assertEquals(5, cs2.getFontIndex()); // duplicate of 1 + assertEquals(6, cs3.getFontIndex()); // duplicate of 2 + assertEquals(8, cs4.getFontIndex()); // two have gone + + // And in rich text + + // RTR 1 had f1 and f2, unchanged + assertEquals(5, r.getCell(0).getRichStringCellValue().getFontAtIndex(0)); + assertEquals(5, r.getCell(0).getRichStringCellValue().getFontAtIndex(1)); + assertEquals(6, r.getCell(0).getRichStringCellValue().getFontAtIndex(3)); + assertEquals(6, r.getCell(0).getRichStringCellValue().getFontAtIndex(4)); + + // RTR 2 had f3 (unchanged), f5 (=f2) and f6 (moved down) + assertEquals(7, r.getCell(1).getRichStringCellValue().getFontAtIndex(0)); + assertEquals(7, r.getCell(1).getRichStringCellValue().getFontAtIndex(1)); + assertEquals(6, r.getCell(1).getRichStringCellValue().getFontAtIndex(3)); + assertEquals(6, r.getCell(1).getRichStringCellValue().getFontAtIndex(4)); + assertEquals(8, r.getCell(1).getRichStringCellValue().getFontAtIndex(6)); + assertEquals(8, r.getCell(1).getRichStringCellValue().getFontAtIndex(7)); + } } @Test @@ -664,11 +668,11 @@ final class TestHSSFOptimiser { private void checkUserStyles(HSSFSheet sheet) { HSSFCellStyle parentStyle1 = sheet.getRow(1).getCell(0).getCellStyle().getParentStyle(); assertNotNull(parentStyle1); - assertEquals(parentStyle1.getUserStyleName(), "user define"); + assertEquals("user define", parentStyle1.getUserStyleName()); HSSFCellStyle parentStyle10 = sheet.getRow(10).getCell(0).getCellStyle().getParentStyle(); assertNotNull(parentStyle10); - assertEquals(parentStyle10.getUserStyleName(), "user define2"); + assertEquals("user define2", parentStyle10.getUserStyleName()); } private void checkColumnStyles(HSSFSheet sheet, int col1, int col2, boolean checkEquals) { diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFPicture.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFPicture.java index 58ba423f47..3ff14c846e 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFPicture.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFPicture.java @@ -185,13 +185,13 @@ final class TestHSSFPicture extends BaseTestPicture { EscherBSERecord bse = wb.getWorkbook().getBSERecord(idx1); - assertEquals(bse.getRef(), 1); + assertEquals(1, bse.getRef()); dr.createPicture(new HSSFClientAnchor(), idx1); - assertEquals(bse.getRef(), 2); + assertEquals(2, bse.getRef()); HSSFShapeGroup gr = dr.createGroup(new HSSFClientAnchor()); gr.createPicture(new HSSFChildAnchor(), idx1); - assertEquals(bse.getRef(), 3); + assertEquals(3, bse.getRef()); } } @@ -203,7 +203,7 @@ final class TestHSSFPicture extends BaseTestPicture { assertEquals(1, drawing.getChildren().size()); HSSFPicture picture = (HSSFPicture) drawing.getChildren().get(0); - assertEquals(picture.getFileName(), "test"); + assertEquals("test", picture.getFileName()); } } @@ -221,16 +221,16 @@ final class TestHSSFPicture extends BaseTestPicture { int idx1 = wb1.addPicture(data1, Workbook.PICTURE_TYPE_JPEG); HSSFPicture p1 = dr.createPicture(anchor, idx1); - assertEquals(p1.getFileName(), ""); + assertEquals("", p1.getFileName()); p1.setFileName("aaa"); - assertEquals(p1.getFileName(), "aaa"); + assertEquals("aaa", p1.getFileName()); try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { sh = wb2.getSheet("Pictures"); dr = sh.getDrawingPatriarch(); p1 = (HSSFPicture) dr.getChildren().get(0); - assertEquals(p1.getFileName(), "aaa"); + assertEquals("aaa", p1.getFileName()); } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index b6c9c918d2..da8fab94cb 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -523,8 +523,8 @@ final class TestHSSFSheet extends BaseTestSheet { void dbcsName () throws IOException { try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("DBCSSheetName.xls")) { wb.getSheetAt(1); - assertEquals(wb.getSheetName(1), "\u090f\u0915", "DBCS Sheet Name 2"); - assertEquals(wb.getSheetName(0), "\u091c\u093e", "DBCS Sheet Name 1"); + assertEquals("\u090f\u0915", wb.getSheetName(1), "DBCS Sheet Name 2"); + assertEquals("\u091c\u093e", wb.getSheetName(0), "DBCS Sheet Name 1"); } } @@ -781,7 +781,7 @@ final class TestHSSFSheet extends BaseTestSheet { assertNotEquals((short) 0, row.getHeight(), "Identified bug 41187 b"); - assertEquals(row.getCell(0).getRichStringCellValue().getString(), "Hi Excel!"); + assertEquals("Hi Excel!", row.getCell(0).getRichStringCellValue().getString()); // check row height for 'default' flag assertEquals((short) 0xFF, row.getHeight()); @@ -936,7 +936,7 @@ final class TestHSSFSheet extends BaseTestSheet { HSSFPatriarch dr = sh.getDrawingPatriarch(); assertNotNull(dr); HSSFSimpleShape comboBoxShape = (HSSFSimpleShape) dr.getChildren().get(0); - assertEquals(comboBoxShape.getShapeType(), HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX); + assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX, comboBoxShape.getShapeType()); assertNull(ish.findFirstRecordBySid(ObjRecord.sid)); // ObjRecord will appear after serializetion diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestPolygon.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestPolygon.java index 759e790a6f..597c464e0d 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestPolygon.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestPolygon.java @@ -34,250 +34,245 @@ import org.junit.jupiter.api.Test; class TestPolygon { @Test void testResultEqualsToAbstractShape() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sh = wb.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); - polygon.setPolygonDrawArea( 100, 100 ); - polygon.setPoints( new int[]{0, 90, 50}, new int[]{5, 5, 44} ); - polygon.setShapeId(1024); + HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); + polygon.setPolygonDrawArea(100, 100); + polygon.setPoints(new int[]{0, 90, 50}, new int[]{5, 5, 44}); + polygon.setShapeId(1024); - assertEquals(polygon.getEscherContainer().getChildCount(), 4); + assertEquals(4, polygon.getEscherContainer().getChildCount()); - //sp record - byte[] expected = decompress("H4sIAAAAAAAAAGNi4PrAwQAELEDMxcAAAAU6ZlwQAAAA"); - byte[] actual = polygon.getEscherContainer().getChild(0).serialize(); + //sp record + byte[] expected = decompress("H4sIAAAAAAAAAGNi4PrAwQAELEDMxcAAAAU6ZlwQAAAA"); + byte[] actual = polygon.getEscherContainer().getChild(0).serialize(); - assertEquals(expected.length, actual.length); - assertArrayEquals(expected, actual); + assertEquals(expected.length, actual.length); + assertArrayEquals(expected, actual); - expected = decompress("H4sIAAAAAAAAAGNgEPggxIANAABK4+laGgAAAA=="); - actual = polygon.getEscherContainer().getChild(2).serialize(); + expected = decompress("H4sIAAAAAAAAAGNgEPggxIANAABK4+laGgAAAA=="); + actual = polygon.getEscherContainer().getChild(2).serialize(); - assertEquals(expected.length, actual.length); - assertArrayEquals(expected, actual); + assertEquals(expected.length, actual.length); + assertArrayEquals(expected, actual); - expected = decompress("H4sIAAAAAAAAAGNgEPzAAAQACl6c5QgAAAA="); - actual = polygon.getEscherContainer().getChild(3).serialize(); + expected = decompress("H4sIAAAAAAAAAGNgEPzAAAQACl6c5QgAAAA="); + actual = polygon.getEscherContainer().getChild(3).serialize(); - assertEquals(expected.length, actual.length); - assertArrayEquals(expected, actual); + assertEquals(expected.length, actual.length); + assertArrayEquals(expected, actual); - ObjRecord obj = polygon.getObjRecord(); + ObjRecord obj = polygon.getObjRecord(); - expected = decompress("H4sIAAAAAAAAAItlkGIQZRBikGNgYBBMYEADAOAV/ZkeAAAA"); - actual = obj.serialize(); + expected = decompress("H4sIAAAAAAAAAItlkGIQZRBikGNgYBBMYEADAOAV/ZkeAAAA"); + actual = obj.serialize(); - assertEquals(expected.length, actual.length); - assertArrayEquals(expected, actual); - - wb.close(); + assertEquals(expected.length, actual.length); + assertArrayEquals(expected, actual); + } } @Test void testPolygonPoints() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - - HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); - polygon.setPolygonDrawArea( 100, 100 ); - polygon.setPoints( new int[]{0, 90, 50, 90}, new int[]{5, 5, 44, 88} ); - - EscherArrayProperty verticesProp1 = polygon.getOptRecord().lookup(EscherPropertyTypes.GEOMETRY__VERTICES); - - String expected = - "<record type=\"GEOMETRY__VERTICES\" id=\"-32443\" name=\"geometry.vertices\" propertyNumber=\"325\" propertySize=\"32\" numElements=\"5\" numElementsInMemory=\"5\" sizeOfElements=\"-16\">" + - "<flags flag=\"0x8145\" description=\"IS_COMPLEX\"/>" + - "<data>BQAFAPD/AAAFAFoABQAyACwAWgBYAAAABQA=</data>" + - "<elements>" + - "<item>AAAFAA==</item>" + - "<item>WgAFAA==</item>" + - "<item>MgAsAA==</item>" + - "<item>WgBYAA==</item>" + - "<item>AAAFAA==</item>" + - "</elements>" + - "</record>"; - String actual = verticesProp1.toXml("").replaceAll("[\r\n\t]",""); - - assertEquals(verticesProp1.getNumberOfElementsInArray(), 5); - assertEquals(expected, actual); - - polygon.setPoints(new int[]{1,2,3}, new int[] {4,5,6}); - assertArrayEquals(polygon.getXPoints(), new int[]{1, 2, 3}); - assertArrayEquals(polygon.getYPoints(), new int[]{4, 5, 6}); - - verticesProp1 = polygon.getOptRecord().lookup(EscherPropertyTypes.GEOMETRY__VERTICES); - - expected = - "<record type=\"GEOMETRY__VERTICES\" id=\"-32443\" name=\"geometry.vertices\" propertyNumber=\"325\" propertySize=\"28\" numElements=\"4\" numElementsInMemory=\"4\" sizeOfElements=\"-16\">" + - "<flags flag=\"0x8145\" description=\"IS_COMPLEX\"/>" + - "<data>BAAEAPD/AQAEAAIABQADAAYAAQAEAA==</data>" + - "<elements>" + - "<item>AQAEAA==</item>" + - "<item>AgAFAA==</item>" + - "<item>AwAGAA==</item>" + - "<item>AQAEAA==</item>" + - "</elements></record>"; - actual = verticesProp1.toXml("").replaceAll("[\r\n\t]",""); - - assertEquals(verticesProp1.getNumberOfElementsInArray(), 4); - assertEquals(expected, actual); - - wb.close(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sh = wb.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + + HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); + polygon.setPolygonDrawArea(100, 100); + polygon.setPoints(new int[]{0, 90, 50, 90}, new int[]{5, 5, 44, 88}); + + EscherArrayProperty verticesProp1 = polygon.getOptRecord().lookup(EscherPropertyTypes.GEOMETRY__VERTICES); + + String expected = + "<record type=\"GEOMETRY__VERTICES\" id=\"-32443\" name=\"geometry.vertices\" propertyNumber=\"325\" propertySize=\"32\" numElements=\"5\" numElementsInMemory=\"5\" sizeOfElements=\"-16\">" + + "<flags flag=\"0x8145\" description=\"IS_COMPLEX\"/>" + + "<data>BQAFAPD/AAAFAFoABQAyACwAWgBYAAAABQA=</data>" + + "<elements>" + + "<item>AAAFAA==</item>" + + "<item>WgAFAA==</item>" + + "<item>MgAsAA==</item>" + + "<item>WgBYAA==</item>" + + "<item>AAAFAA==</item>" + + "</elements>" + + "</record>"; + String actual = verticesProp1.toXml("").replaceAll("[\r\n\t]", ""); + + assertEquals(5, verticesProp1.getNumberOfElementsInArray()); + assertEquals(expected, actual); + + polygon.setPoints(new int[]{1, 2, 3}, new int[]{4, 5, 6}); + assertArrayEquals(new int[]{1, 2, 3}, polygon.getXPoints()); + assertArrayEquals(new int[]{4, 5, 6}, polygon.getYPoints()); + + verticesProp1 = polygon.getOptRecord().lookup(EscherPropertyTypes.GEOMETRY__VERTICES); + + expected = + "<record type=\"GEOMETRY__VERTICES\" id=\"-32443\" name=\"geometry.vertices\" propertyNumber=\"325\" propertySize=\"28\" numElements=\"4\" numElementsInMemory=\"4\" sizeOfElements=\"-16\">" + + "<flags flag=\"0x8145\" description=\"IS_COMPLEX\"/>" + + "<data>BAAEAPD/AQAEAAIABQADAAYAAQAEAA==</data>" + + "<elements>" + + "<item>AQAEAA==</item>" + + "<item>AgAFAA==</item>" + + "<item>AwAGAA==</item>" + + "<item>AQAEAA==</item>" + + "</elements></record>"; + actual = verticesProp1.toXml("").replaceAll("[\r\n\t]", ""); + + assertEquals(4, verticesProp1.getNumberOfElementsInArray()); + assertEquals(expected, actual); + } } @Test void testSetGetProperties() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sh = wb1.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - - HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); - polygon.setPolygonDrawArea( 102, 101 ); - polygon.setPoints( new int[]{1,2,3}, new int[]{4,5,6} ); - - assertArrayEquals(polygon.getXPoints(), new int[]{1,2,3}); - assertArrayEquals(polygon.getYPoints(), new int[]{4, 5, 6}); - assertEquals(polygon.getDrawAreaHeight(), 101); - assertEquals(polygon.getDrawAreaWidth(), 102); - - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sh = wb2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - polygon = (HSSFPolygon) patriarch.getChildren().get(0); - assertArrayEquals(polygon.getXPoints(), new int[]{1, 2, 3}); - assertArrayEquals(polygon.getYPoints(), new int[]{4, 5, 6}); - assertEquals(polygon.getDrawAreaHeight(), 101); - assertEquals(polygon.getDrawAreaWidth(), 102); - - polygon.setPolygonDrawArea( 1021, 1011 ); - polygon.setPoints( new int[]{11,21,31}, new int[]{41,51,61} ); - - assertArrayEquals(polygon.getXPoints(), new int[]{11, 21, 31}); - assertArrayEquals(polygon.getYPoints(), new int[]{41, 51, 61}); - assertEquals(polygon.getDrawAreaHeight(), 1011); - assertEquals(polygon.getDrawAreaWidth(), 1021); - - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sh = wb3.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - polygon = (HSSFPolygon) patriarch.getChildren().get(0); - - assertArrayEquals(polygon.getXPoints(), new int[]{11, 21, 31}); - assertArrayEquals(polygon.getYPoints(), new int[]{41, 51, 61}); - assertEquals(polygon.getDrawAreaHeight(), 1011); - assertEquals(polygon.getDrawAreaWidth(), 1021); - - wb3.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sh = wb1.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + + HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); + polygon.setPolygonDrawArea(102, 101); + polygon.setPoints(new int[]{1, 2, 3}, new int[]{4, 5, 6}); + + assertArrayEquals(new int[]{1, 2, 3}, polygon.getXPoints()); + assertArrayEquals(new int[]{4, 5, 6}, polygon.getYPoints()); + assertEquals(101, polygon.getDrawAreaHeight()); + assertEquals(102, polygon.getDrawAreaWidth()); + + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sh = wb2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + polygon = (HSSFPolygon) patriarch.getChildren().get(0); + assertArrayEquals(new int[]{1, 2, 3}, polygon.getXPoints()); + assertArrayEquals(new int[]{4, 5, 6}, polygon.getYPoints()); + assertEquals(101, polygon.getDrawAreaHeight()); + assertEquals(102, polygon.getDrawAreaWidth()); + + polygon.setPolygonDrawArea(1021, 1011); + polygon.setPoints(new int[]{11, 21, 31}, new int[]{41, 51, 61}); + + assertArrayEquals(new int[]{11, 21, 31}, polygon.getXPoints()); + assertArrayEquals(new int[]{41, 51, 61}, polygon.getYPoints()); + assertEquals(1011, polygon.getDrawAreaHeight()); + assertEquals(1021, polygon.getDrawAreaWidth()); + + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sh = wb3.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + polygon = (HSSFPolygon) patriarch.getChildren().get(0); + + assertArrayEquals(new int[]{11, 21, 31}, polygon.getXPoints()); + assertArrayEquals(new int[]{41, 51, 61}, polygon.getYPoints()); + assertEquals(1011, polygon.getDrawAreaHeight()); + assertEquals(1021, polygon.getDrawAreaWidth()); + } + } + } } @Test void testAddToExistingFile() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sh = wb1.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - - HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); - polygon.setPolygonDrawArea( 102, 101 ); - polygon.setPoints( new int[]{1,2,3}, new int[]{4,5,6} ); - - HSSFPolygon polygon1 = patriarch.createPolygon(new HSSFClientAnchor()); - polygon1.setPolygonDrawArea( 103, 104 ); - polygon1.setPoints( new int[]{11,12,13}, new int[]{14,15,16} ); - - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sh = wb2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - assertEquals(patriarch.getChildren().size(), 2); - - HSSFPolygon polygon2 = patriarch.createPolygon(new HSSFClientAnchor()); - polygon2.setPolygonDrawArea( 203, 204 ); - polygon2.setPoints( new int[]{21,22,23}, new int[]{24,25,26} ); - - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sh = wb3.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - assertEquals(patriarch.getChildren().size(), 3); - - polygon = (HSSFPolygon) patriarch.getChildren().get(0); - polygon1 = (HSSFPolygon) patriarch.getChildren().get(1); - polygon2 = (HSSFPolygon) patriarch.getChildren().get(2); - - assertArrayEquals(polygon.getXPoints(), new int[]{1, 2, 3}); - assertArrayEquals(polygon.getYPoints(), new int[]{4,5,6}); - assertEquals(polygon.getDrawAreaHeight(), 101); - assertEquals(polygon.getDrawAreaWidth(), 102); - - assertArrayEquals(polygon1.getXPoints(), new int[]{11,12,13}); - assertArrayEquals(polygon1.getYPoints(), new int[]{14,15,16}); - assertEquals(polygon1.getDrawAreaHeight(), 104); - assertEquals(polygon1.getDrawAreaWidth(), 103); - - assertArrayEquals(polygon2.getXPoints(), new int[]{21,22,23}); - assertArrayEquals(polygon2.getYPoints(), new int[]{24,25,26}); - assertEquals(polygon2.getDrawAreaHeight(), 204); - assertEquals(polygon2.getDrawAreaWidth(), 203); - - wb3.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sh = wb1.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + + HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); + polygon.setPolygonDrawArea(102, 101); + polygon.setPoints(new int[]{1, 2, 3}, new int[]{4, 5, 6}); + + HSSFPolygon polygon1 = patriarch.createPolygon(new HSSFClientAnchor()); + polygon1.setPolygonDrawArea(103, 104); + polygon1.setPoints(new int[]{11, 12, 13}, new int[]{14, 15, 16}); + + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sh = wb2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + assertEquals(2, patriarch.getChildren().size()); + + HSSFPolygon polygon2 = patriarch.createPolygon(new HSSFClientAnchor()); + polygon2.setPolygonDrawArea(203, 204); + polygon2.setPoints(new int[]{21, 22, 23}, new int[]{24, 25, 26}); + + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sh = wb3.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + assertEquals(3, patriarch.getChildren().size()); + + polygon = (HSSFPolygon) patriarch.getChildren().get(0); + polygon1 = (HSSFPolygon) patriarch.getChildren().get(1); + polygon2 = (HSSFPolygon) patriarch.getChildren().get(2); + + assertArrayEquals(new int[]{1, 2, 3}, polygon.getXPoints()); + assertArrayEquals(new int[]{4, 5, 6}, polygon.getYPoints()); + assertEquals(101, polygon.getDrawAreaHeight()); + assertEquals(102, polygon.getDrawAreaWidth()); + + assertArrayEquals(new int[]{11, 12, 13}, polygon1.getXPoints()); + assertArrayEquals(new int[]{14, 15, 16}, polygon1.getYPoints()); + assertEquals(104, polygon1.getDrawAreaHeight()); + assertEquals(103, polygon1.getDrawAreaWidth()); + + assertArrayEquals(new int[]{21, 22, 23}, polygon2.getXPoints()); + assertArrayEquals(new int[]{24, 25, 26}, polygon2.getYPoints()); + assertEquals(204, polygon2.getDrawAreaHeight()); + assertEquals(203, polygon2.getDrawAreaWidth()); + } + } + } } @Test void testExistingFile() throws IOException { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); - HSSFSheet sheet = wb.getSheet("polygon"); - HSSFPatriarch drawing = sheet.getDrawingPatriarch(); - assertEquals(1, drawing.getChildren().size()); - - HSSFPolygon polygon = (HSSFPolygon) drawing.getChildren().get(0); - assertEquals(polygon.getDrawAreaHeight(), 2466975); - assertEquals(polygon.getDrawAreaWidth(), 3686175); - assertArrayEquals(polygon.getXPoints(), new int[]{0, 0, 31479, 16159, 19676, 20502}); - assertArrayEquals(polygon.getYPoints(), new int[]{0, 0, 36, 56, 34, 18}); - - wb.close(); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { + HSSFSheet sheet = wb.getSheet("polygon"); + HSSFPatriarch drawing = sheet.getDrawingPatriarch(); + assertEquals(1, drawing.getChildren().size()); + + HSSFPolygon polygon = (HSSFPolygon) drawing.getChildren().get(0); + assertEquals(2466975, polygon.getDrawAreaHeight()); + assertEquals(3686175, polygon.getDrawAreaWidth()); + assertArrayEquals(new int[]{0, 0, 31479, 16159, 19676, 20502}, polygon.getXPoints()); + assertArrayEquals(new int[]{0, 0, 36, 56, 34, 18}, polygon.getYPoints()); + } } @Test void testPolygonType() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sh = wb1.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sh = wb1.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); - polygon.setPolygonDrawArea( 102, 101 ); - polygon.setPoints( new int[]{1,2,3}, new int[]{4,5,6} ); + HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); + polygon.setPolygonDrawArea(102, 101); + polygon.setPoints(new int[]{1, 2, 3}, new int[]{4, 5, 6}); - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sh = wb2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sh = wb2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); - HSSFPolygon polygon1 = patriarch.createPolygon(new HSSFClientAnchor()); - polygon1.setPolygonDrawArea( 102, 101 ); - polygon1.setPoints( new int[]{1,2,3}, new int[]{4,5,6} ); + HSSFPolygon polygon1 = patriarch.createPolygon(new HSSFClientAnchor()); + polygon1.setPolygonDrawArea(102, 101); + polygon1.setPoints(new int[]{1, 2, 3}, new int[]{4, 5, 6}); - EscherSpRecord spRecord = polygon1.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); + EscherSpRecord spRecord = polygon1.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); - spRecord.setShapeType((short)77/*RANDOM*/); + spRecord.setShapeType((short) 77/*RANDOM*/); - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sh = wb3.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sh = wb3.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); - assertEquals(patriarch.getChildren().size(), 2); - assertTrue(patriarch.getChildren().get(0) instanceof HSSFPolygon); - assertTrue(patriarch.getChildren().get(1) instanceof HSSFPolygon); - wb3.close(); + assertEquals(2, patriarch.getChildren().size()); + assertTrue(patriarch.getChildren().get(0) instanceof HSSFPolygon); + assertTrue(patriarch.getChildren().get(1) instanceof HSSFPolygon); + } + } + } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestRowStyle.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestRowStyle.java index 5bde759a5c..985a5736b4 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestRowStyle.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestRowStyle.java @@ -38,7 +38,7 @@ final class TestRowStyle { * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects * Last row, first row is tested against the correct values (99,0).<P> * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. - * HSSFSheet last row or first row is incorrect. <P> + * HSSFSheet last row or first row is incorrect. */ @Test void testWriteSheetFont() throws IOException { @@ -103,7 +103,7 @@ final class TestRowStyle { * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects * Last row, first row is tested against the correct values (99,0).<P> * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. - * HSSFSheet last row or first row is incorrect. <P> + * HSSFSheet last row or first row is incorrect. */ @Test void testWriteSheetStyle() throws IOException { @@ -165,8 +165,8 @@ final class TestRowStyle { assertNotNull(r, "Row is not null"); cs2 = r.getRowStyle(); assertNotNull(cs2); - assertEquals(cs2.getFillForegroundColor(), (short) 0x0, "FillForegroundColor for row:"); - assertEquals(cs2.getFillPattern(), FillPatternType.BRICKS, "FillPattern for row:"); + assertEquals((short) 0x0, cs2.getFillForegroundColor(), "FillForegroundColor for row:"); + assertEquals(FillPatternType.BRICKS, cs2.getFillPattern(), "FillPattern for row:"); } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestShapeGroup.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestShapeGroup.java index b02fa4fc7a..5e7b016e5d 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestShapeGroup.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestShapeGroup.java @@ -34,216 +34,210 @@ class TestShapeGroup { @Test void testSetGetCoordinates() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sh = wb1.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); - assertEquals(group.getX1(), 0); - assertEquals(group.getY1(), 0); - assertEquals(group.getX2(), 1023); - assertEquals(group.getY2(), 255); - - group.setCoordinates(1,2,3,4); - - assertEquals(group.getX1(), 1); - assertEquals(group.getY1(), 2); - assertEquals(group.getX2(), 3); - assertEquals(group.getY2(), 4); - - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sh = wb2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - group = (HSSFShapeGroup) patriarch.getChildren().get(0); - assertEquals(group.getX1(), 1); - assertEquals(group.getY1(), 2); - assertEquals(group.getX2(), 3); - assertEquals(group.getY2(), 4); - wb2.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sh = wb1.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); + assertEquals(0, group.getX1()); + assertEquals(0, group.getY1()); + assertEquals(1023, group.getX2()); + assertEquals(255, group.getY2()); + + group.setCoordinates(1, 2, 3, 4); + + assertEquals(1, group.getX1()); + assertEquals(2, group.getY1()); + assertEquals(3, group.getX2()); + assertEquals(4, group.getY2()); + + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sh = wb2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + group = (HSSFShapeGroup) patriarch.getChildren().get(0); + assertEquals(1, group.getX1()); + assertEquals(2, group.getY1()); + assertEquals(3, group.getX2()); + assertEquals(4, group.getY2()); + } + } } @Test void testAddToExistingFile() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sh = wb1.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - HSSFShapeGroup group1 = patriarch.createGroup(new HSSFClientAnchor()); - HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor()); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sh = wb1.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + HSSFShapeGroup group1 = patriarch.createGroup(new HSSFClientAnchor()); + HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor()); - group1.setCoordinates(1,2,3,4); - group2.setCoordinates(5,6,7,8); + group1.setCoordinates(1, 2, 3, 4); + group2.setCoordinates(5, 6, 7, 8); - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sh = wb2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sh = wb2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); - assertEquals(patriarch.getChildren().size(), 2); + assertEquals(2, patriarch.getChildren().size()); - HSSFShapeGroup group3 = patriarch.createGroup(new HSSFClientAnchor()); - group3.setCoordinates(9,10,11,12); + HSSFShapeGroup group3 = patriarch.createGroup(new HSSFClientAnchor()); + group3.setCoordinates(9, 10, 11, 12); - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sh = wb3.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sh = wb3.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); - assertEquals(patriarch.getChildren().size(), 3); - wb3.close(); + assertEquals(3, patriarch.getChildren().size()); + } + } + } } @Test void testModify() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - - // create a sheet with a text box - HSSFSheet sheet = wb1.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - - HSSFShapeGroup group1 = patriarch.createGroup(new - HSSFClientAnchor(0,0,0,0, - (short)0, 0, (short)15, 25)); - group1.setCoordinates(0, 0, 792, 612); - - HSSFTextbox textbox1 = group1.createTextbox(new - HSSFChildAnchor(100, 100, 300, 300)); - HSSFRichTextString rt1 = new HSSFRichTextString("Hello, World!"); - textbox1.setString(rt1); - - // write, read back and check that our text box is there - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet = wb2.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); - assertEquals(1, patriarch.getChildren().size()); - - group1 = (HSSFShapeGroup)patriarch.getChildren().get(0); - assertEquals(1, group1.getChildren().size()); - textbox1 = (HSSFTextbox)group1.getChildren().get(0); - assertEquals("Hello, World!", textbox1.getString().getString()); - - // modify anchor - assertEquals(new HSSFChildAnchor(100, 100, 300, 300), - textbox1.getAnchor()); - HSSFChildAnchor newAnchor = new HSSFChildAnchor(200,200, 400, 400); - textbox1.setAnchor(newAnchor); - // modify text - textbox1.setString(new HSSFRichTextString("Hello, World! (modified)")); - - // add a new text box - HSSFTextbox textbox2 = group1.createTextbox(new - HSSFChildAnchor(400, 400, 600, 600)); - HSSFRichTextString rt2 = new HSSFRichTextString("Hello, World-2"); - textbox2.setString(rt2); - assertEquals(2, group1.getChildren().size()); - - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sheet = wb3.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); - assertEquals(1, patriarch.getChildren().size()); - - group1 = (HSSFShapeGroup)patriarch.getChildren().get(0); - assertEquals(2, group1.getChildren().size()); - textbox1 = (HSSFTextbox)group1.getChildren().get(0); - assertEquals("Hello, World! (modified)", - textbox1.getString().getString()); - assertEquals(new HSSFChildAnchor(200,200, 400, 400), - textbox1.getAnchor()); - - textbox2 = (HSSFTextbox)group1.getChildren().get(1); - assertEquals("Hello, World-2", textbox2.getString().getString()); - assertEquals(new HSSFChildAnchor(400, 400, 600, 600), - textbox2.getAnchor()); - - HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3); - wb3.close(); - sheet = wb4.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); - group1 = (HSSFShapeGroup)patriarch.getChildren().get(0); - textbox1 = (HSSFTextbox)group1.getChildren().get(0); - textbox2 = (HSSFTextbox)group1.getChildren().get(1); - HSSFTextbox textbox3 = group1.createTextbox(new - HSSFChildAnchor(400,200, 600, 400)); - HSSFRichTextString rt3 = new HSSFRichTextString("Hello, World-3"); - textbox3.setString(rt3); - wb4.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + + // create a sheet with a text box + HSSFSheet sheet = wb1.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + HSSFShapeGroup group1 = patriarch.createGroup(new + HSSFClientAnchor(0, 0, 0, 0, + (short) 0, 0, (short) 15, 25)); + group1.setCoordinates(0, 0, 792, 612); + + HSSFTextbox textbox1 = group1.createTextbox(new + HSSFChildAnchor(100, 100, 300, 300)); + HSSFRichTextString rt1 = new HSSFRichTextString("Hello, World!"); + textbox1.setString(rt1); + + // write, read back and check that our text box is there + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet = wb2.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + assertEquals(1, patriarch.getChildren().size()); + + group1 = (HSSFShapeGroup) patriarch.getChildren().get(0); + assertEquals(1, group1.getChildren().size()); + textbox1 = (HSSFTextbox) group1.getChildren().get(0); + assertEquals("Hello, World!", textbox1.getString().getString()); + + // modify anchor + assertEquals(new HSSFChildAnchor(100, 100, 300, 300), textbox1.getAnchor()); + HSSFChildAnchor newAnchor = new HSSFChildAnchor(200, 200, 400, 400); + textbox1.setAnchor(newAnchor); + // modify text + textbox1.setString(new HSSFRichTextString("Hello, World! (modified)")); + + // add a new text box + HSSFTextbox textbox2 = group1.createTextbox(new HSSFChildAnchor(400, 400, 600, 600)); + HSSFRichTextString rt2 = new HSSFRichTextString("Hello, World-2"); + textbox2.setString(rt2); + assertEquals(2, group1.getChildren().size()); + + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sheet = wb3.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + assertEquals(1, patriarch.getChildren().size()); + + group1 = (HSSFShapeGroup) patriarch.getChildren().get(0); + assertEquals(2, group1.getChildren().size()); + textbox1 = (HSSFTextbox) group1.getChildren().get(0); + assertEquals("Hello, World! (modified)", textbox1.getString().getString()); + assertEquals(new HSSFChildAnchor(200, 200, 400, 400), textbox1.getAnchor()); + + textbox2 = (HSSFTextbox) group1.getChildren().get(1); + assertEquals("Hello, World-2", textbox2.getString().getString()); + assertEquals(new HSSFChildAnchor(400, 400, 600, 600), textbox2.getAnchor()); + + try (HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3)) { + sheet = wb4.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + group1 = (HSSFShapeGroup) patriarch.getChildren().get(0); + textbox1 = (HSSFTextbox) group1.getChildren().get(0); + textbox2 = (HSSFTextbox) group1.getChildren().get(1); + HSSFTextbox textbox3 = group1.createTextbox(new HSSFChildAnchor(400, 200, 600, 400)); + HSSFRichTextString rt3 = new HSSFRichTextString("Hello, World-3"); + textbox3.setString(rt3); + } + } + } + } } @Test void testAddShapesToGroup() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - - // create a sheet with a text box - HSSFSheet sheet = wb1.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - - HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); - int index = wb1.addPicture(new byte[]{1,2,3}, HSSFWorkbook.PICTURE_TYPE_JPEG); - group.createPicture(new HSSFChildAnchor(), index); - HSSFPolygon polygon = group.createPolygon(new HSSFChildAnchor()); - polygon.setPoints(new int[]{1,100, 1}, new int[]{1, 50, 100}); - group.createTextbox(new HSSFChildAnchor()); - group.createShape(new HSSFChildAnchor()); - - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet = wb2.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); - assertEquals(1, patriarch.getChildren().size()); - - assertTrue(patriarch.getChildren().get(0) instanceof HSSFShapeGroup); - group = (HSSFShapeGroup) patriarch.getChildren().get(0); - - assertEquals(group.getChildren().size(), 4); - - assertTrue(group.getChildren().get(0) instanceof HSSFPicture); - assertTrue(group.getChildren().get(1) instanceof HSSFPolygon); - assertTrue(group.getChildren().get(2) instanceof HSSFTextbox); - assertTrue(group.getChildren().get(3) instanceof HSSFSimpleShape); - - HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor()); - - index = wb2.addPicture(new byte[]{2,2,2}, HSSFWorkbook.PICTURE_TYPE_JPEG); - group2.createPicture(new HSSFChildAnchor(), index); - polygon = group2.createPolygon(new HSSFChildAnchor()); - polygon.setPoints(new int[]{1,100, 1}, new int[]{1, 50, 100}); - group2.createTextbox(new HSSFChildAnchor()); - group2.createShape(new HSSFChildAnchor()); - group2.createShape(new HSSFChildAnchor()); - - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sheet = wb3.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); - assertEquals(2, patriarch.getChildren().size()); - - group = (HSSFShapeGroup) patriarch.getChildren().get(1); - - assertEquals(group.getChildren().size(), 5); - - assertTrue(group.getChildren().get(0) instanceof HSSFPicture); - assertTrue(group.getChildren().get(1) instanceof HSSFPolygon); - assertTrue(group.getChildren().get(2) instanceof HSSFTextbox); - assertTrue(group.getChildren().get(3) instanceof HSSFSimpleShape); - assertTrue(group.getChildren().get(4) instanceof HSSFSimpleShape); - - group.getShapeId(); - wb3.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + + // create a sheet with a text box + HSSFSheet sheet = wb1.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); + int index = wb1.addPicture(new byte[]{1, 2, 3}, HSSFWorkbook.PICTURE_TYPE_JPEG); + group.createPicture(new HSSFChildAnchor(), index); + HSSFPolygon polygon = group.createPolygon(new HSSFChildAnchor()); + polygon.setPoints(new int[]{1, 100, 1}, new int[]{1, 50, 100}); + group.createTextbox(new HSSFChildAnchor()); + group.createShape(new HSSFChildAnchor()); + + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet = wb2.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + assertEquals(1, patriarch.getChildren().size()); + + assertTrue(patriarch.getChildren().get(0) instanceof HSSFShapeGroup); + group = (HSSFShapeGroup) patriarch.getChildren().get(0); + + assertEquals(4, group.getChildren().size()); + + assertTrue(group.getChildren().get(0) instanceof HSSFPicture); + assertTrue(group.getChildren().get(1) instanceof HSSFPolygon); + assertTrue(group.getChildren().get(2) instanceof HSSFTextbox); + assertTrue(group.getChildren().get(3) instanceof HSSFSimpleShape); + + HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor()); + + index = wb2.addPicture(new byte[]{2, 2, 2}, HSSFWorkbook.PICTURE_TYPE_JPEG); + group2.createPicture(new HSSFChildAnchor(), index); + polygon = group2.createPolygon(new HSSFChildAnchor()); + polygon.setPoints(new int[]{1, 100, 1}, new int[]{1, 50, 100}); + group2.createTextbox(new HSSFChildAnchor()); + group2.createShape(new HSSFChildAnchor()); + group2.createShape(new HSSFChildAnchor()); + + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sheet = wb3.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + assertEquals(2, patriarch.getChildren().size()); + + group = (HSSFShapeGroup) patriarch.getChildren().get(1); + + assertEquals(5, group.getChildren().size()); + + assertTrue(group.getChildren().get(0) instanceof HSSFPicture); + assertTrue(group.getChildren().get(1) instanceof HSSFPolygon); + assertTrue(group.getChildren().get(2) instanceof HSSFTextbox); + assertTrue(group.getChildren().get(3) instanceof HSSFSimpleShape); + assertTrue(group.getChildren().get(4) instanceof HSSFSimpleShape); + + group.getShapeId(); + } + } + } } @Test void testSpgrRecord() throws IOException { - HSSFWorkbook wb = new HSSFWorkbook(); + try (HSSFWorkbook wb = new HSSFWorkbook()) { - // create a sheet with a text box - HSSFSheet sheet = wb.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + // create a sheet with a text box + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); - assertSame(((EscherContainerRecord)group.getEscherContainer().getChild(0)).getChildById(EscherSpgrRecord.RECORD_ID), getSpgrRecord(group)); - wb.close(); + HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); + assertSame(((EscherContainerRecord) group.getEscherContainer().getChild(0)).getChildById(EscherSpgrRecord.RECORD_ID), getSpgrRecord(group)); + } } private static EscherSpgrRecord getSpgrRecord(HSSFShapeGroup group) { @@ -252,36 +246,36 @@ class TestShapeGroup { @Test void testClearShapes() throws IOException { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sheet = wb1.createSheet(); - HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); - HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sheet = wb1.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); - group.createShape(new HSSFChildAnchor()); - group.createShape(new HSSFChildAnchor()); + group.createShape(new HSSFChildAnchor()); + group.createShape(new HSSFChildAnchor()); - EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch); + EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch); - assertEquals(agg.getShapeToObjMapping().size(), 5); - assertEquals(agg.getTailRecords().size(), 0); - assertEquals(group.getChildren().size(), 2); + assertEquals(5, agg.getShapeToObjMapping().size()); + assertEquals(0, agg.getTailRecords().size()); + assertEquals(2, group.getChildren().size()); - group.clear(); + group.clear(); - assertEquals(agg.getShapeToObjMapping().size(), 1); - assertEquals(agg.getTailRecords().size(), 0); - assertEquals(group.getChildren().size(), 0); + assertEquals(1, agg.getShapeToObjMapping().size()); + assertEquals(0, agg.getTailRecords().size()); + assertEquals(0, group.getChildren().size()); - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sheet = wb2.getSheetAt(0); - patriarch = sheet.getDrawingPatriarch(); + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sheet = wb2.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); - group = (HSSFShapeGroup) patriarch.getChildren().get(0); + group = (HSSFShapeGroup) patriarch.getChildren().get(0); - assertEquals(agg.getShapeToObjMapping().size(), 1); - assertEquals(agg.getTailRecords().size(), 0); - assertEquals(group.getChildren().size(), 0); - wb2.close(); + assertEquals(1, agg.getShapeToObjMapping().size()); + assertEquals(0, agg.getTailRecords().size()); + assertEquals(0, group.getChildren().size()); + } + } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestText.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestText.java index 6cea76d5bf..b9f9340188 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestText.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestText.java @@ -37,7 +37,7 @@ class TestText { HSSFPatriarch patriarch = sh.createDrawingPatriarch(); HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); - assertEquals(textbox.getEscherContainer().getChildCount(), 5); + assertEquals(5, textbox.getEscherContainer().getChildCount()); //sp record byte[] expected = decompress("H4sIAAAAAAAAAFvEw/WBg4GBgZEFSHAxMAAA9gX7nhAAAAA="); @@ -85,126 +85,123 @@ class TestText { @Test void testAddTextToExistingFile() throws Exception { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sh = wb1.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); - textbox.setString(new HSSFRichTextString("just for test")); - HSSFTextbox textbox2 = patriarch.createTextbox(new HSSFClientAnchor()); - textbox2.setString(new HSSFRichTextString("just for test2")); - - assertEquals(patriarch.getChildren().size(), 2); - - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sh = wb2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - assertEquals(patriarch.getChildren().size(), 2); - HSSFTextbox text3 = patriarch.createTextbox(new HSSFClientAnchor()); - text3.setString(new HSSFRichTextString("text3")); - assertEquals(patriarch.getChildren().size(), 3); - - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sh = wb3.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - - assertEquals(patriarch.getChildren().size(), 3); - assertEquals(((HSSFTextbox) patriarch.getChildren().get(0)).getString().getString(), "just for test"); - assertEquals(((HSSFTextbox) patriarch.getChildren().get(1)).getString().getString(), "just for test2"); - assertEquals(((HSSFTextbox) patriarch.getChildren().get(2)).getString().getString(), "text3"); - - wb3.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sh = wb1.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); + textbox.setString(new HSSFRichTextString("just for test")); + HSSFTextbox textbox2 = patriarch.createTextbox(new HSSFClientAnchor()); + textbox2.setString(new HSSFRichTextString("just for test2")); + + assertEquals(2, patriarch.getChildren().size()); + + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sh = wb2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + assertEquals(2, patriarch.getChildren().size()); + HSSFTextbox text3 = patriarch.createTextbox(new HSSFClientAnchor()); + text3.setString(new HSSFRichTextString("text3")); + assertEquals(3, patriarch.getChildren().size()); + + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sh = wb3.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + + assertEquals(3, patriarch.getChildren().size()); + assertEquals("just for test", ((HSSFTextbox) patriarch.getChildren().get(0)).getString().getString()); + assertEquals("just for test2", ((HSSFTextbox) patriarch.getChildren().get(1)).getString().getString()); + assertEquals("text3", ((HSSFTextbox) patriarch.getChildren().get(2)).getString().getString()); + } + } + } } @Test void testSetGetProperties() throws Exception { - HSSFWorkbook wb1 = new HSSFWorkbook(); - HSSFSheet sh = wb1.createSheet(); - HSSFPatriarch patriarch = sh.createDrawingPatriarch(); - HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); - textbox.setString(new HSSFRichTextString("test")); - assertEquals(textbox.getString().getString(), "test"); - - textbox.setHorizontalAlignment((short) 5); - assertEquals(textbox.getHorizontalAlignment(), 5); - - textbox.setVerticalAlignment((short) 6); - assertEquals(textbox.getVerticalAlignment(), (short) 6); - - textbox.setMarginBottom(7); - assertEquals(textbox.getMarginBottom(), 7); - - textbox.setMarginLeft(8); - assertEquals(textbox.getMarginLeft(), 8); - - textbox.setMarginRight(9); - assertEquals(textbox.getMarginRight(), 9); - - textbox.setMarginTop(10); - assertEquals(textbox.getMarginTop(), 10); - - HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1); - wb1.close(); - sh = wb2.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - textbox = (HSSFTextbox) patriarch.getChildren().get(0); - assertEquals(textbox.getString().getString(), "test"); - assertEquals(textbox.getHorizontalAlignment(), 5); - assertEquals(textbox.getVerticalAlignment(), (short) 6); - assertEquals(textbox.getMarginBottom(), 7); - assertEquals(textbox.getMarginLeft(), 8); - assertEquals(textbox.getMarginRight(), 9); - assertEquals(textbox.getMarginTop(), 10); - - textbox.setString(new HSSFRichTextString("test1")); - textbox.setHorizontalAlignment(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED); - textbox.setVerticalAlignment(HSSFTextbox.VERTICAL_ALIGNMENT_TOP); - textbox.setMarginBottom(71); - textbox.setMarginLeft(81); - textbox.setMarginRight(91); - textbox.setMarginTop(101); - - assertEquals(textbox.getString().getString(), "test1"); - assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED); - assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP); - assertEquals(textbox.getMarginBottom(), 71); - assertEquals(textbox.getMarginLeft(), 81); - assertEquals(textbox.getMarginRight(), 91); - assertEquals(textbox.getMarginTop(), 101); - - HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); - wb2.close(); - sh = wb3.getSheetAt(0); - patriarch = sh.getDrawingPatriarch(); - textbox = (HSSFTextbox) patriarch.getChildren().get(0); - - assertEquals(textbox.getString().getString(), "test1"); - assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED); - assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP); - assertEquals(textbox.getMarginBottom(), 71); - assertEquals(textbox.getMarginLeft(), 81); - assertEquals(textbox.getMarginRight(), 91); - assertEquals(textbox.getMarginTop(), 101); - - wb3.close(); + try (HSSFWorkbook wb1 = new HSSFWorkbook()) { + HSSFSheet sh = wb1.createSheet(); + HSSFPatriarch patriarch = sh.createDrawingPatriarch(); + HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor()); + textbox.setString(new HSSFRichTextString("test")); + assertEquals("test", textbox.getString().getString()); + + textbox.setHorizontalAlignment((short) 5); + assertEquals(5, textbox.getHorizontalAlignment()); + + textbox.setVerticalAlignment((short) 6); + assertEquals((short) 6, textbox.getVerticalAlignment()); + + textbox.setMarginBottom(7); + assertEquals(7, textbox.getMarginBottom()); + + textbox.setMarginLeft(8); + assertEquals(8, textbox.getMarginLeft()); + + textbox.setMarginRight(9); + assertEquals(9, textbox.getMarginRight()); + + textbox.setMarginTop(10); + assertEquals(10, textbox.getMarginTop()); + + try (HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { + sh = wb2.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + textbox = (HSSFTextbox) patriarch.getChildren().get(0); + assertEquals("test", textbox.getString().getString()); + assertEquals(5, textbox.getHorizontalAlignment()); + assertEquals((short) 6, textbox.getVerticalAlignment()); + assertEquals(7, textbox.getMarginBottom()); + assertEquals(8, textbox.getMarginLeft()); + assertEquals(9, textbox.getMarginRight()); + assertEquals(10, textbox.getMarginTop()); + + textbox.setString(new HSSFRichTextString("test1")); + textbox.setHorizontalAlignment(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED); + textbox.setVerticalAlignment(HSSFTextbox.VERTICAL_ALIGNMENT_TOP); + textbox.setMarginBottom(71); + textbox.setMarginLeft(81); + textbox.setMarginRight(91); + textbox.setMarginTop(101); + + assertEquals("test1", textbox.getString().getString()); + assertEquals(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED, textbox.getHorizontalAlignment()); + assertEquals(HSSFTextbox.VERTICAL_ALIGNMENT_TOP, textbox.getVerticalAlignment()); + assertEquals(71, textbox.getMarginBottom()); + assertEquals(81, textbox.getMarginLeft()); + assertEquals(91, textbox.getMarginRight()); + assertEquals(101, textbox.getMarginTop()); + + try (HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2)) { + sh = wb3.getSheetAt(0); + patriarch = sh.getDrawingPatriarch(); + textbox = (HSSFTextbox) patriarch.getChildren().get(0); + + assertEquals("test1", textbox.getString().getString()); + assertEquals(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED, textbox.getHorizontalAlignment()); + assertEquals(HSSFTextbox.VERTICAL_ALIGNMENT_TOP, textbox.getVerticalAlignment()); + assertEquals(71, textbox.getMarginBottom()); + assertEquals(81, textbox.getMarginLeft()); + assertEquals(91, textbox.getMarginRight()); + assertEquals(101, textbox.getMarginTop()); + } + } + } } @Test void testExistingFileWithText() throws Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); - HSSFSheet sheet = wb.getSheet("text"); - HSSFPatriarch drawing = sheet.getDrawingPatriarch(); - assertEquals(1, drawing.getChildren().size()); - HSSFTextbox textbox = (HSSFTextbox) drawing.getChildren().get(0); - assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_LEFT); - assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP); - assertEquals(textbox.getMarginTop(), 0); - assertEquals(textbox.getMarginBottom(), 3600000); - assertEquals(textbox.getMarginLeft(), 3600000); - assertEquals(textbox.getMarginRight(), 0); - assertEquals(textbox.getString().getString(), "teeeeesssstttt"); - wb.close(); + try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls")) { + HSSFSheet sheet = wb.getSheet("text"); + HSSFPatriarch drawing = sheet.getDrawingPatriarch(); + assertEquals(1, drawing.getChildren().size()); + HSSFTextbox textbox = (HSSFTextbox) drawing.getChildren().get(0); + assertEquals(HSSFTextbox.VERTICAL_ALIGNMENT_TOP, textbox.getVerticalAlignment()); + assertEquals(0, textbox.getMarginTop()); + assertEquals(3600000, textbox.getMarginBottom()); + assertEquals(3600000, textbox.getMarginLeft()); + assertEquals(0, textbox.getMarginRight()); + assertEquals("teeeeesssstttt", textbox.getString().getString()); + } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java index 1ced954a27..5ae57250b3 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java @@ -44,7 +44,7 @@ final class TestUnicodeWorkbook { c.setCellValue(new HSSFRichTextString("\u00e4")); //Confirm that the sring will be compressed - assertEquals(c.getRichStringCellValue().getUnicodeString().getOptionFlags(), 0); + assertEquals(0, c.getRichStringCellValue().getUnicodeString().getOptionFlags()); try (HSSFWorkbook wb = HSSFTestDataSamples.writeOutAndReadBack(wb1)) { @@ -53,7 +53,7 @@ final class TestUnicodeWorkbook { assertNotNull(s); c = r.getCell(1); - assertEquals(c.getRichStringCellValue().getString(), "\u00e4"); + assertEquals("\u00e4", c.getRichStringCellValue().getString()); } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestWorkbook.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestWorkbook.java index 698e8704b8..ce83d5bbba 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestWorkbook.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestWorkbook.java @@ -20,6 +20,7 @@ package org.apache.poi.hssf.usermodel; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -66,7 +67,7 @@ final class TestWorkbook { * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects * Last row, first row is tested against the correct values (99,0).<P> * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. - * HSSFSheet last row or first row is incorrect. <P> + * HSSFSheet last row or first row is incorrect. */ @Test void testWriteSheetSimple() throws IOException { @@ -95,7 +96,7 @@ final class TestWorkbook { * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects * Last row, first row is tested against the correct values (74,25).<P> * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. - * HSSFSheet last row or first row is incorrect. <P> + * HSSFSheet last row or first row is incorrect. */ @Test void testWriteModifySheetSimple() throws IOException { @@ -133,7 +134,7 @@ final class TestWorkbook { * OBJECTIVE: Test that HSSF can read a simple spreadsheet (Simple.xls).<P> * SUCCESS: HSSF reads the sheet. Matches values in their particular positions.<P> * FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values - * in the sheet in their known positions.<P> + * in the sheet in their known positions. */ @Test void testReadSimple() throws IOException { @@ -150,7 +151,7 @@ final class TestWorkbook { * OBJECTIVE: Test that HSSF can read a simple spreadsheet (SimpleWithDataFormat.xls).<P> * SUCCESS: HSSF reads the sheet. Matches values in their particular positions and format is correct<P> * FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values - * in the sheet in their known positions.<P> + * in the sheet in their known positions. */ @Test void testReadSimpleWithDataFormat() throws IOException { @@ -161,7 +162,7 @@ final class TestWorkbook { assertEquals(1.25,cell.getNumericCellValue(), 1e-10); - assertEquals(format.getFormat(cell.getCellStyle().getDataFormat()), "0.0"); + assertEquals("0.0", format.getFormat(cell.getCellStyle().getDataFormat())); wb.close(); } @@ -171,7 +172,7 @@ final class TestWorkbook { * OBJECTIVE: Test that HSSF can write a sheet with custom data formats and then read it and get the proper formats.<P> * SUCCESS: HSSF reads the sheet. Matches values in their particular positions and format is correct<P> * FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values - * in the sheet in their known positions.<P> + * in the sheet in their known positions. */ @Test void testWriteDataFormat() throws IOException { @@ -196,9 +197,9 @@ final class TestWorkbook { assertEquals(1.25, c2.getNumericCellValue(), 1e-10); - assertEquals(format.getFormat(df), "0.0"); + assertEquals("0.0", format.getFormat(df)); - assertEquals(format, wb2.createDataFormat()); + assertSame(format, wb2.createDataFormat()); wb2.close(); wb1.close(); @@ -209,7 +210,7 @@ final class TestWorkbook { * OBJECTIVE: Test that HSSF can read a simple spreadsheet (Employee.xls).<P> * SUCCESS: HSSF reads the sheet. Matches values in their particular positions.<P> * FAILURE: HSSF does not read a sheet or excepts. HSSF cannot identify values - * in the sheet in their known positions.<P> + * in the sheet in their known positions. * */ @Test @@ -234,7 +235,7 @@ final class TestWorkbook { * has been properly replaced. <P> * FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts. * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value - * is incorrect or has not been replaced. <P> + * is incorrect or has not been replaced. */ @Test void testModifySimple() throws IOException { @@ -263,7 +264,7 @@ final class TestWorkbook { * FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts. * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value * is incorrect or has not been replaced or the incorrect cell has its value replaced - * or is incorrect. <P> + * or is incorrect. */ @Test void testModifySimpleWithSkip() throws IOException { @@ -301,7 +302,7 @@ final class TestWorkbook { * has been properly replaced. <P> * FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts. * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value - * is incorrect or has not been replaced. <P> + * is incorrect or has not been replaced. */ @Test void testModifySimpleWithStyling() throws IOException { @@ -334,7 +335,7 @@ final class TestWorkbook { * has been properly replaced. <P> * FAILURE: HSSF does not read a sheet or excepts. HSSF does not write the sheet or excepts. * HSSF does not re-read the sheet or excepts. Upon re-reading the sheet the value - * is incorrect or has not been replaced. <P> + * is incorrect or has not been replaced. */ @Test void testModifyEmployee() throws IOException { @@ -364,7 +365,7 @@ final class TestWorkbook { * OBJECTIVE: Test that HSSF can read a simple spreadsheet with and RKRecord and correctly * identify the cell as numeric and convert it to a NumberRecord. <P> * SUCCESS: HSSF reads a sheet. HSSF returns that the cell is a numeric type cell. <P> - * FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly identifies the cell<P> + * FAILURE: HSSF does not read a sheet or excepts. HSSF incorrectly identifies the cell */ @Test void testReadSheetWithRK() throws IOException { @@ -384,7 +385,7 @@ final class TestWorkbook { * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects * Last row, first row is tested against the correct values (74,25).<P> * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. - * HSSFSheet last row or first row is incorrect. <P> + * HSSFSheet last row or first row is incorrect. */ @Test void testWriteModifySheetMerged() throws IOException { diff --git a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java index 95133ff478..8674a41d15 100644 --- a/poi/src/test/java/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java +++ b/poi/src/test/java/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java @@ -19,8 +19,8 @@ package org.apache.poi.poifs.filesystem; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.util.Arrays; @@ -43,15 +43,7 @@ final class TestDocumentOutputStream { POIFSWriterListener l = (event) -> { DocumentOutputStream dstream = event.getStream(); - - try { - for (byte b : expected) { - dstream.write(b); - } - } catch (IOException ignored) { - fail("stream exhausted too early"); - } - + assertDoesNotThrow(() -> { for (byte b : expected) dstream.write(b); }, "stream exhausted too early"); assertThrows(IOException.class, () -> dstream.write(0)); }; @@ -67,13 +59,7 @@ final class TestDocumentOutputStream { POIFSWriterListener l = (event) -> { DocumentOutputStream dstream = event.getStream(); - - try { - dstream.write(expected); - } catch (IOException ignored) { - fail("stream exhausted too early"); - } - + assertDoesNotThrow(() -> dstream.write(expected), "stream exhausted too early"); assertThrows(IOException.class, () -> dstream.write(new byte[]{'7','7','7','7'})); }; @@ -90,11 +76,7 @@ final class TestDocumentOutputStream { POIFSWriterListener l = (event) -> { DocumentOutputStream dstream = event.getStream(); - try { - dstream.write(input, 1, 25); - } catch (IOException ignored) { - fail("stream exhausted too early"); - } + assertDoesNotThrow(() -> dstream.write(input, 1, 25), "stream exhausted too early"); assertThrows(IOException.class, () -> dstream.write(input, 0, 1)); }; diff --git a/poi/src/test/java/org/apache/poi/ss/formula/TestWorkbookEvaluator.java b/poi/src/test/java/org/apache/poi/ss/formula/TestWorkbookEvaluator.java index b0d6a019d4..d1be12ae67 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/TestWorkbookEvaluator.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/TestWorkbookEvaluator.java @@ -633,12 +633,12 @@ class TestWorkbookEvaluator { wb.getCreationHelper().createFormulaEvaluator().evaluateAll(); - assertEquals(cellA2.getStringCellValue(), "1"); - assertEquals(cellB2.getNumericCellValue(),0, 0.00001); - assertEquals(cellC2.getStringCellValue(),"3"); + assertEquals("1", cellA2.getStringCellValue()); + assertEquals(0,cellB2.getNumericCellValue(), 0.00001); + assertEquals("3",cellC2.getStringCellValue()); - assertEquals(cellA3.getStringCellValue(), "1"); - assertEquals(cellB3.getNumericCellValue(),0, 0.00001); - assertEquals(cellC3.getStringCellValue(),"3"); + assertEquals("1", cellA3.getStringCellValue()); + assertEquals(0,cellB3.getNumericCellValue(), 0.00001); + assertEquals("3",cellC3.getStringCellValue()); } } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/eval/BaseTestCircularReferences.java b/poi/src/test/java/org/apache/poi/ss/formula/eval/BaseTestCircularReferences.java index 74de1ca32c..01ee8426d7 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/eval/BaseTestCircularReferences.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/eval/BaseTestCircularReferences.java @@ -66,7 +66,7 @@ public abstract class BaseTestCircularReferences { * Makes sure that the specified evaluated cell value represents a circular reference error. */ private static void confirmCycleErrorCode(CellValue cellValue) { - assertSame(cellValue.getCellType(), CellType.ERROR); + assertSame(CellType.ERROR, cellValue.getCellType()); assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cellValue.getErrorValue()); } @@ -96,7 +96,7 @@ public abstract class BaseTestCircularReferences { CellValue cellValue = evaluateWithCycles(wb, testCell); assertNotNull(cellValue); - assertSame(cellValue.getCellType(), CellType.NUMERIC); + assertSame(CellType.NUMERIC, cellValue.getCellType()); assertEquals(2, cellValue.getNumberValue(), 0); } } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulaBugs.java b/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulaBugs.java index 64905dc47d..1e03fa8621 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulaBugs.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulaBugs.java @@ -17,9 +17,9 @@ package org.apache.poi.ss.formula.eval; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.io.InputStream; @@ -98,17 +98,6 @@ final class TestFormulaBugs { cell = row.createCell(3); // D5 cell.setCellFormula("IF(ISNUMBER(b1),b1,b2)"); -// if (false) { // set true to check excel file manually -// // bug report mentions 'Editing the formula in excel "fixes" the problem.' -// try { -// FileOutputStream fileOut = new FileOutputStream("27405output.xls"); -// wb.write(fileOut); -// fileOut.close(); -// } catch (IOException e) { -// throw new RuntimeException(e); -// } -// } - // use POI's evaluator as an extra sanity check FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); CellValue cv; @@ -124,7 +113,7 @@ final class TestFormulaBugs { } /** - * Bug 42448 - Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69 <p> + * Bug 42448 - Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69 */ @Test void test42448() throws IOException { @@ -142,11 +131,7 @@ final class TestFormulaBugs { // re-entered String inputFormula = "SUMPRODUCT(A!C7:A!C67, B8:B68) / B69"; // as per bug report - try { - cell.setCellFormula(inputFormula); - } catch (StringIndexOutOfBoundsException e) { - fail("Identified bug 42448"); - } + assertDoesNotThrow(() -> cell.setCellFormula(inputFormula), "Identified bug 42448"); assertEquals("SUMPRODUCT(A!C7:A!C67,B8:B68)/B69", cell.getCellFormula()); diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTFunc.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTFunc.java index 43bd6390ea..ac3805b30f 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTFunc.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTFunc.java @@ -117,7 +117,7 @@ final class TestTFunc { confirmString(eval, ""); eval = invokeTWithReference(ErrorEval.NAME_INVALID); - assertSame(eval, ErrorEval.NAME_INVALID); + assertSame(ErrorEval.NAME_INVALID, eval); } @Test diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java index 399a32eaea..ea5430b6e7 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWeekNumFunc.java @@ -17,17 +17,21 @@ package org.apache.poi.ss.formula.functions; -import org.apache.poi.ss.formula.OperationEvaluationContext; -import org.apache.poi.ss.formula.eval.*; -import org.apache.poi.ss.usermodel.DateUtil; -import org.apache.poi.util.StringUtil; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.time.LocalDate; import java.time.LocalDateTime; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import org.apache.poi.ss.formula.OperationEvaluationContext; +import org.apache.poi.ss.formula.eval.BlankEval; +import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.NumberEval; +import org.apache.poi.ss.formula.eval.StringEval; +import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.util.StringUtil; +import org.junit.jupiter.api.Test; /** * Tests WEEKNUM(date[, return_type]) excel function @@ -91,21 +95,15 @@ class TestWeekNumFunc { String formula = "WEEKNUM(" + dateValue + ")"; ValueEval[] args = new ValueEval[] { new NumberEval(dateValue) }; ValueEval result = WeekNum.instance.evaluate(args, DEFAULT_CONTEXT); - if (result instanceof NumberEval) { - assertEquals(expected, ((NumberEval)result).getNumberValue(), TOLERANCE, formula); - } else { - fail("unexpected eval result " + result); - } + assertTrue(result instanceof NumberEval); + assertEquals(expected, ((NumberEval)result).getNumberValue(), TOLERANCE, formula); } private void assertEvaluateEquals(double expected, double dateValue, double return_type) { String formula = "WEEKNUM(" + dateValue + ", " + return_type + ")"; ValueEval[] args = new ValueEval[] { new NumberEval(dateValue), new NumberEval(return_type) }; ValueEval result = WeekNum.instance.evaluate(args, DEFAULT_CONTEXT); - if (result instanceof NumberEval) { - assertEquals(expected, ((NumberEval)result).getNumberValue(), TOLERANCE, formula); - } else { - fail("unexpected eval result " + result); - } + assertTrue(result instanceof NumberEval); + assertEquals(expected, ((NumberEval)result).getNumberValue(), TOLERANCE, formula); } } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/ptg/TestArrayPtg.java b/poi/src/test/java/org/apache/poi/ss/formula/ptg/TestArrayPtg.java index d6f82f3cca..ffe072db74 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/ptg/TestArrayPtg.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/ptg/TestArrayPtg.java @@ -17,11 +17,13 @@ package org.apache.poi.ss.formula.ptg; +import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; -import org.apache.poi.hssf.HSSFTestDataSamples; +import java.io.IOException; + import org.apache.poi.hssf.record.TestcaseRecordInputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.util.LittleEndianByteArrayOutputStream; @@ -101,14 +103,15 @@ final class TestArrayPtg { * A spreadsheet was added to make the ordering clearer. */ @Test - void testElementOrderingInSpreadsheet() { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex42564-elementOrder.xls"); - - // The formula has an array with 3 rows and 5 columns - String formula = wb.getSheetAt(0).getRow(0).getCell(0).getCellFormula(); - - assertNotEquals("Identified bug 42564 b", "SUM({1,6,11;2,7,12;3,8,13;4,9,14;5,10,15})", formula); - assertEquals("SUM({1,2,3,4,5;6,7,8,9,10;11,12,13,14,15})", formula); + void testElementOrderingInSpreadsheet() throws IOException { + String formula; + try (HSSFWorkbook wb = openSampleWorkbook("ex42564-elementOrder.xls")) { + + // The formula has an array with 3 rows and 5 columns + formula = wb.getSheetAt(0).getRow(0).getCell(0).getCellFormula(); + assertNotEquals("SUM({1,6,11;2,7,12;3,8,13;4,9,14;5,10,15})", formula, "Identified bug 42564 b"); + assertEquals("SUM({1,2,3,4,5;6,7,8,9,10;11,12,13,14,15})", formula); + } } @Test diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java index 3edce0e0e4..94db023cc1 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java @@ -1539,13 +1539,13 @@ public abstract class BaseTestBugzillaIssues { // Verify that the changes were made assertNull(wb.getPrintArea(0), "Sheet0 before write"); - assertEquals(wb.getPrintArea(1), "Sheet1 before write", "Sheet1!$A$1:$A$1"); + assertEquals("Sheet1!$A$1:$A$1", wb.getPrintArea(1), "Sheet1 before write"); // Verify that the changes are non-volatile try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb)) { // CURRENTLY FAILS with "Sheet0!$A$1:$C$6" assertNull(wb2.getPrintArea(0), "Sheet0 after write"); - assertEquals(wb2.getPrintArea(1), "Sheet1 after write", "Sheet1!$A$1:$A$1"); + assertEquals("Sheet1!$A$1:$A$1", wb2.getPrintArea(1), "Sheet1 after write"); } } } diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheet.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheet.java index 8ed16c996b..2b4a8697a4 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheet.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheet.java @@ -184,7 +184,7 @@ public abstract class BaseTestSheet { Row clonedRow = clonedSheet.getRow(0); //Check for a good clone - assertEquals(clonedRow.getCell(0).getRichStringCellValue().getString(), "clone_test"); + assertEquals("clone_test", clonedRow.getCell(0).getRichStringCellValue().getString()); //Check that the cells are not somehow linked cell.setCellValue(factory.createRichTextString("Difference Check")); @@ -195,8 +195,8 @@ public abstract class BaseTestSheet { assertNotEquals("COS(2)", clonedRow.getCell(1).getCellFormula(), "formula cell not properly cloned"); - assertEquals(clonedRow.getCell(0).getRichStringCellValue().getString(), "clone_test"); - assertEquals(clonedRow.getCell(1).getCellFormula(), "SIN(1)"); + assertEquals("clone_test", clonedRow.getCell(0).getRichStringCellValue().getString()); + assertEquals("SIN(1)", clonedRow.getCell(1).getCellFormula()); } } @@ -641,9 +641,9 @@ public abstract class BaseTestSheet { Sheet s1 = wb.cloneSheet(0); r = s1.getRow(0); // sanity check - assertEquals(r.getCell(0).getNumericCellValue(), 1, 0, "double"); + assertEquals(1, r.getCell(0).getNumericCellValue(), 0, "double"); assertNotNull(r.getCell(1)); - assertEquals(r.getCell(1).getCellFormula(), "A1*2", "formula"); + assertEquals("A1*2", r.getCell(1).getCellFormula(), "formula"); } } diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java index 869b591eff..60ceb9b3cc 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java @@ -70,11 +70,11 @@ public abstract class BaseTestSheetShiftRows { // row (in original file each row was unique) s = wb2.getSheetAt(0); - assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 1); + assertEquals(1, s.getRow(0).getPhysicalNumberOfCells()); confirmEmptyRow(s, 1); - assertEquals(s.getRow(2).getPhysicalNumberOfCells(), 2); - assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 4); - assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5); + assertEquals(2, s.getRow(2).getPhysicalNumberOfCells()); + assertEquals(4, s.getRow(3).getPhysicalNumberOfCells()); + assertEquals(5, s.getRow(4).getPhysicalNumberOfCells()); // Shift rows 1-3 down 3 in the current one. This tests when // 1 row is blank. Write to a another temp file @@ -85,9 +85,9 @@ public abstract class BaseTestSheetShiftRows { confirmEmptyRow(s, 0); confirmEmptyRow(s, 1); confirmEmptyRow(s, 2); - assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 1); + assertEquals(1, s.getRow(3).getPhysicalNumberOfCells()); confirmEmptyRow(s, 4); - assertEquals(s.getRow(5).getPhysicalNumberOfCells(), 2); + assertEquals(2, s.getRow(5).getPhysicalNumberOfCells()); } } } @@ -100,11 +100,11 @@ public abstract class BaseTestSheetShiftRows { s.shiftRows(2, 3, -2); try (Workbook wb5 = _testDataProvider.writeOutAndReadBack(wb4)) { s = wb5.getSheetAt(0); - assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 3); - assertEquals(s.getRow(1).getPhysicalNumberOfCells(), 4); + assertEquals(3, s.getRow(0).getPhysicalNumberOfCells()); + assertEquals(4, s.getRow(1).getPhysicalNumberOfCells()); confirmEmptyRow(s, 2); confirmEmptyRow(s, 3); - assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5); + assertEquals(5, s.getRow(4).getPhysicalNumberOfCells()); } } } @@ -157,11 +157,11 @@ public abstract class BaseTestSheetShiftRows { assertNotNull(sheet.getCellComment(new CellAddress(3, 0))); String comment1 = sheet.getCellComment(new CellAddress(0, 0)).getString().getString(); - assertEquals(comment1, "comment top row1 (index0)\n"); + assertEquals("comment top row1 (index0)\n", comment1); String comment3 = sheet.getCellComment(new CellAddress(2, 0)).getString().getString(); - assertEquals(comment3, "comment top row3 (index2)\n"); + assertEquals("comment top row3 (index2)\n", comment3); String comment4 = sheet.getCellComment(new CellAddress(3, 0)).getString().getString(); - assertEquals(comment4, "comment top row4 (index3)\n"); + assertEquals("comment top row4 (index3)\n", comment4); //Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb); @@ -220,9 +220,9 @@ public abstract class BaseTestSheetShiftRows { } comment1 = sheet.getCellComment(new CellAddress(1, 0)).getString().getString(); - assertEquals(comment1, "comment top row3 (index2)\n"); + assertEquals("comment top row3 (index2)\n", comment1); String comment2 = sheet.getCellComment(new CellAddress(2, 0)).getString().getString(); - assertEquals(comment2, "comment top row4 (index3)\n"); + assertEquals("comment top row4 (index3)\n", comment2); } } } diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index 150622eccc..1c952d2a60 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -571,15 +571,15 @@ public abstract class BaseTestWorkbook { //Test the header h = s.getHeader(); - assertEquals(h.getCenter(), "\u20ac"); - assertEquals(h.getLeft(), "\u20ac"); - assertEquals(h.getRight(), "\u20ac"); + assertEquals("\u20ac", h.getCenter()); + assertEquals("\u20ac", h.getLeft()); + assertEquals("\u20ac", h.getRight()); //Test the footer f = s.getFooter(); - assertEquals(f.getCenter(), "\u20ac"); - assertEquals(f.getLeft(), "\u20ac"); - assertEquals(f.getRight(), "\u20ac"); + assertEquals("\u20ac", f.getCenter()); + assertEquals("\u20ac", f.getLeft()); + assertEquals("\u20ac", f.getRight()); //Test the dataformat r = s.getRow(0); @@ -590,7 +590,7 @@ public abstract class BaseTestWorkbook { //Test the cell string value /*c2 =*/ r.getCell(2); - assertEquals(c.getRichStringCellValue().getString(), "\u20ac"); + assertEquals("\u20ac", c.getRichStringCellValue().getString()); //Test the cell formula c3 = r.getCell(3); @@ -782,8 +782,8 @@ public abstract class BaseTestWorkbook { @Test void windowOneDefaults() throws IOException { try (Workbook b = _testDataProvider.createWorkbook()) { - assertEquals(b.getActiveSheetIndex(), 0); - assertEquals(b.getFirstVisibleTab(), 0); + assertEquals(0, b.getActiveSheetIndex()); + assertEquals(0, b.getFirstVisibleTab()); // throws NullPointerException when WindowOneRecord in Workbook is not probably initialized } } diff --git a/poi/src/test/java/org/apache/poi/ss/util/TestSheetBuilder.java b/poi/src/test/java/org/apache/poi/ss/util/TestSheetBuilder.java index e258895c8f..d1d1926724 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/TestSheetBuilder.java +++ b/poi/src/test/java/org/apache/poi/ss/util/TestSheetBuilder.java @@ -39,7 +39,7 @@ import org.junit.jupiter.api.Test; */ final class TestSheetBuilder { - private static Object[][] testData = new Object[][]{ + private static final Object[][] testData = { {1, 2, 3}, {new Date(), null, null}, {"one", "two", "=A1+B2"} @@ -50,12 +50,12 @@ final class TestSheetBuilder { try (Workbook wb = new HSSFWorkbook()) { Sheet sheet = new SheetBuilder(wb, testData).build(); - assertEquals(sheet.getPhysicalNumberOfRows(), 3); + assertEquals(3, sheet.getPhysicalNumberOfRows()); Row firstRow = sheet.getRow(0); Cell firstCell = firstRow.getCell(0); - assertEquals(firstCell.getCellType(), CellType.NUMERIC); + assertEquals(CellType.NUMERIC, firstCell.getCellType()); assertEquals(1.0, firstCell.getNumericCellValue(), 0.00001); diff --git a/poi/src/test/java/org/apache/poi/util/TestBitField.java b/poi/src/test/java/org/apache/poi/util/TestBitField.java index 55c30087fa..9b79fdac73 100644 --- a/poi/src/test/java/org/apache/poi/util/TestBitField.java +++ b/poi/src/test/java/org/apache/poi/util/TestBitField.java @@ -27,41 +27,39 @@ import org.junit.jupiter.api.Test; * Class to test BitField functionality */ final class TestBitField { - private static BitField bf_multi = BitFieldFactory.getInstance(0x3F80); - private static BitField bf_single = BitFieldFactory.getInstance(0x4000); + private static final BitField bf_multi = BitFieldFactory.getInstance(0x3F80); + private static final BitField bf_single = BitFieldFactory.getInstance(0x4000); @Test void testGetValue() { - assertEquals(bf_multi.getValue(-1), 127); - assertEquals(bf_multi.getValue(0), 0); - assertEquals(bf_single.getValue(-1), 1); - assertEquals(bf_single.getValue(0), 0); + assertEquals(127, bf_multi.getValue(-1)); + assertEquals(0, bf_multi.getValue(0)); + assertEquals(1, bf_single.getValue(-1)); + assertEquals(0, bf_single.getValue(0)); } @Test void testGetShortValue() { - assertEquals(bf_multi.getShortValue(( short ) -1), ( short ) 127); - assertEquals(bf_multi.getShortValue(( short ) 0), ( short ) 0); - assertEquals(bf_single.getShortValue(( short ) -1), ( short ) 1); - assertEquals(bf_single.getShortValue(( short ) 0), ( short ) 0); + assertEquals(( short ) 127, bf_multi.getShortValue(( short ) -1)); + assertEquals(( short ) 0, bf_multi.getShortValue(( short ) 0)); + assertEquals(( short ) 1, bf_single.getShortValue(( short ) -1)); + assertEquals(( short ) 0, bf_single.getShortValue(( short ) 0)); } @Test void testGetRawValue() { - assertEquals(bf_multi.getRawValue(-1), 0x3F80); - assertEquals(bf_multi.getRawValue(0), 0); - assertEquals(bf_single.getRawValue(-1), 0x4000); - assertEquals(bf_single.getRawValue(0), 0); + assertEquals(0x3F80, bf_multi.getRawValue(-1)); + assertEquals(0, bf_multi.getRawValue(0)); + assertEquals(0x4000, bf_single.getRawValue(-1)); + assertEquals(0, bf_single.getRawValue(0)); } @Test void testGetShortRawValue() { - assertEquals(bf_multi.getShortRawValue(( short ) -1), - ( short ) 0x3F80); - assertEquals(bf_multi.getShortRawValue(( short ) 0), ( short ) 0); - assertEquals(bf_single.getShortRawValue(( short ) -1), - ( short ) 0x4000); - assertEquals(bf_single.getShortRawValue(( short ) 0), ( short ) 0); + assertEquals(( short ) 0x3F80, bf_multi.getShortRawValue(( short ) -1)); + assertEquals(( short ) 0, bf_multi.getShortRawValue(( short ) 0)); + assertEquals(( short ) 0x4000, bf_single.getShortRawValue(( short ) -1)); + assertEquals(( short ) 0, bf_single.getShortRawValue(( short ) 0)); } @Test @@ -90,48 +88,39 @@ final class TestBitField { void testSetValue() { for (int j = 0; j < 128; j++) { - assertEquals(bf_multi.getValue(bf_multi.setValue(0, j)), j); - assertEquals(bf_multi.setValue(0, j), j << 7); + assertEquals(j, bf_multi.getValue(bf_multi.setValue(0, j))); + assertEquals(j << 7, bf_multi.setValue(0, j)); } // verify that excess bits are stripped off - assertEquals(bf_multi.setValue(0x3f80, 128), 0); + assertEquals(0, bf_multi.setValue(0x3f80, 128)); for (int j = 0; j < 2; j++) { - assertEquals(bf_single.getValue(bf_single.setValue(0, j)), j); - assertEquals(bf_single.setValue(0, j), j << 14); + assertEquals(j, bf_single.getValue(bf_single.setValue(0, j))); + assertEquals(j << 14, bf_single.setValue(0, j)); } // verify that excess bits are stripped off - assertEquals(bf_single.setValue(0x4000, 2), 0); + assertEquals(0, bf_single.setValue(0x4000, 2)); } @Test void testSetShortValue() { for (int j = 0; j < 128; j++) { - assertEquals(bf_multi - .getShortValue(bf_multi - .setShortValue(( short ) 0, ( short ) j)), ( short ) j); - assertEquals(bf_multi.setShortValue(( short ) 0, ( short ) j), - ( short ) (j << 7)); + assertEquals(( short ) j, bf_multi.getShortValue(bf_multi.setShortValue(( short ) 0, ( short ) j))); + assertEquals(( short ) (j << 7), bf_multi.setShortValue(( short ) 0, ( short ) j)); } // verify that excess bits are stripped off - assertEquals(bf_multi.setShortValue(( short ) 0x3f80, ( short ) 128), - ( short ) 0); - for (int j = 0; j < 2; j++) - { - assertEquals(bf_single - .getShortValue(bf_single - .setShortValue(( short ) 0, ( short ) j)), ( short ) j); - assertEquals(bf_single.setShortValue(( short ) 0, ( short ) j), - ( short ) (j << 14)); + assertEquals(( short ) 0, bf_multi.setShortValue(( short ) 0x3f80, ( short ) 128)); + for (int j = 0; j < 2; j++) { + assertEquals(( short ) j, bf_single.getShortValue(bf_single.setShortValue(( short ) 0, ( short ) j))); + assertEquals(( short ) (j << 14), bf_single.setShortValue(( short ) 0, ( short ) j)); } // verify that excess bits are stripped off - assertEquals(bf_single.setShortValue(( short ) 0x4000, ( short ) 2), - ( short ) 0); + assertEquals(( short ) 0, bf_single.setShortValue(( short ) 0x4000, ( short ) 2)); } @Test @@ -143,8 +132,7 @@ final class TestBitField { assertEquals(16, BitFieldFactory.getInstance(16).setByteBoolean(( byte ) 0, true)); assertEquals(32, BitFieldFactory.getInstance(32).setByteBoolean(( byte ) 0, true)); assertEquals(64, BitFieldFactory.getInstance(64).setByteBoolean(( byte ) 0, true)); - assertEquals(-128, - BitFieldFactory.getInstance(128).setByteBoolean(( byte ) 0, true)); + assertEquals(-128, BitFieldFactory.getInstance(128).setByteBoolean(( byte ) 0, true)); assertEquals(0, BitFieldFactory.getInstance(1).setByteBoolean(( byte ) 1, false)); assertEquals(0, BitFieldFactory.getInstance(2).setByteBoolean(( byte ) 2, false)); assertEquals(0, BitFieldFactory.getInstance(4).setByteBoolean(( byte ) 4, false)); @@ -163,26 +151,26 @@ final class TestBitField { @Test void testClear() { - assertEquals(bf_multi.clear(-1), 0xFFFFC07F); - assertEquals(bf_single.clear(-1), 0xFFFFBFFF); + assertEquals(0xFFFFC07F, bf_multi.clear(-1)); + assertEquals(0xFFFFBFFF, bf_single.clear(-1)); } @Test void testClearShort() { - assertEquals(bf_multi.clearShort(( short ) -1), ( short ) 0xC07F); - assertEquals(bf_single.clearShort(( short ) -1), ( short ) 0xBFFF); + assertEquals(( short ) 0xC07F, bf_multi.clearShort(( short ) -1)); + assertEquals(( short ) 0xBFFF, bf_single.clearShort(( short ) -1)); } @Test void testSet() { - assertEquals(bf_multi.set(0), 0x3F80); - assertEquals(bf_single.set(0), 0x4000); + assertEquals(0x3F80, bf_multi.set(0)); + assertEquals(0x4000, bf_single.set(0)); } @Test void testSetShort() { - assertEquals(bf_multi.setShort(( short ) 0), ( short ) 0x3F80); - assertEquals(bf_single.setShort(( short ) 0), ( short ) 0x4000); + assertEquals(( short ) 0x3F80, bf_multi.setShort(( short ) 0)); + assertEquals(( short ) 0x4000, bf_single.setShort(( short ) 0)); } @Test @@ -195,14 +183,10 @@ final class TestBitField { @Test void testSetShortBoolean() { - assertEquals(bf_multi.setShort(( short ) 0), - bf_multi.setShortBoolean(( short ) 0, true)); - assertEquals(bf_single.setShort(( short ) 0), - bf_single.setShortBoolean(( short ) 0, true)); - assertEquals(bf_multi.clearShort(( short ) -1), - bf_multi.setShortBoolean(( short ) -1, false)); - assertEquals(bf_single.clearShort(( short ) -1), - bf_single.setShortBoolean(( short ) -1, false)); + assertEquals(bf_multi.setShort(( short ) 0), bf_multi.setShortBoolean(( short ) 0, true)); + assertEquals(bf_single.setShort(( short ) 0), bf_single.setShortBoolean(( short ) 0, true)); + assertEquals(bf_multi.clearShort(( short ) -1), bf_multi.setShortBoolean(( short ) -1, false)); + assertEquals(bf_single.clearShort(( short ) -1), bf_single.setShortBoolean(( short ) -1, false)); } @Test diff --git a/poi/src/test/java/org/apache/poi/util/TestByteField.java b/poi/src/test/java/org/apache/poi/util/TestByteField.java index 8c37667fbc..4908dbcd36 100644 --- a/poi/src/test/java/org/apache/poi/util/TestByteField.java +++ b/poi/src/test/java/org/apache/poi/util/TestByteField.java @@ -19,7 +19,7 @@ package org.apache.poi.util; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -37,41 +37,24 @@ final class TestByteField { @Test void testConstructors() { - try { - new ByteField(-1); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new ByteField(-1)); ByteField field = new ByteField(2); assertEquals(( byte ) 0, field.get()); - try { - new ByteField(-1, ( byte ) 1); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new ByteField(-1, (byte)1)); + field = new ByteField(2, ( byte ) 3); assertEquals(( byte ) 3, field.get()); - byte[] array = new byte[ 3 ]; - try { - new ByteField(-1, ( byte ) 1, array); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new ByteField(-1, (byte)1, new byte[3])); + + byte[] array = new byte[ 3 ]; field = new ByteField(2, ( byte ) 4, array); assertEquals(( byte ) 4, field.get()); assertEquals(( byte ) 4, array[ 2 ]); - array = new byte[ 2 ]; - try { - new ByteField(2, ( byte ) 5, array); - fail("should have gotten ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new ByteField(2, (byte)5, new byte[2])); + for (byte b : _test_array) { array = new byte[ 1 ]; new ByteField(0, b, array); @@ -96,20 +79,16 @@ final class TestByteField { @Test void testReadFromBytes() { - ByteField field = new ByteField(1); + ByteField field1 = new ByteField(1); byte[] array = new byte[ 1 ]; - try { - field.readFromBytes(array); - fail("should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } - field = new ByteField(0); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> field1.readFromBytes(array)); + + ByteField field2 = new ByteField(0); for (int j = 0; j < _test_array.length; j++) { array[ 0 ] = _test_array[ j ]; - field.readFromBytes(array); - assertEquals(_test_array[ j ], field.get(), "testing " + j); + field2.readFromBytes(array); + assertEquals(_test_array[ j ], field2.get(), "testing " + j); } } diff --git a/poi/src/test/java/org/apache/poi/util/TestLittleEndian.java b/poi/src/test/java/org/apache/poi/util/TestLittleEndian.java index e75606b917..783dbe6854 100644 --- a/poi/src/test/java/org/apache/poi/util/TestLittleEndian.java +++ b/poi/src/test/java/org/apache/poi/util/TestLittleEndian.java @@ -17,6 +17,7 @@ package org.apache.poi.util; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -76,9 +77,9 @@ final class TestLittleEndian { byte[] testdata3 = new byte[ LittleEndianConsts.SHORT_SIZE + 1 ]; LittleEndian.putUShort(testdata3, 0, expected2); LittleEndian.putUShort(testdata3, 1, expected3); - assertEquals(testdata3[0], 0x0D); - assertEquals(testdata3[1], (byte)0x93); - assertEquals(testdata3[2], (byte)0xFF); + assertEquals(0x0D, testdata3[0]); + assertEquals((byte)0x93, testdata3[1]); + assertEquals((byte)0xFF, testdata3[2]); assertEquals(expected2, LittleEndian.getUShort(testdata3)); assertEquals(expected3, LittleEndian.getUShort(testdata3, 1)); @@ -107,9 +108,7 @@ final class TestLittleEndian { double nan = LittleEndian.getDouble(_nan_double_array, 0); byte[] data = new byte[8]; LittleEndian.putDouble(data, 0, nan); - for ( int i = 0; i < data.length; i++ ) { - assertEquals(data[i], _nan_double_array[i]); - } + assertArrayEquals(data, _nan_double_array); } /** diff --git a/poi/src/test/java/org/apache/poi/util/TestLongField.java b/poi/src/test/java/org/apache/poi/util/TestLongField.java index 8cd39aba92..1437afcb85 100644 --- a/poi/src/test/java/org/apache/poi/util/TestLongField.java +++ b/poi/src/test/java/org/apache/poi/util/TestLongField.java @@ -19,7 +19,6 @@ package org.apache.poi.util; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.fail; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -96,16 +95,12 @@ final class TestLongField { @Test void testReadFromBytes() { - LongField field = new LongField(1); + LongField field1 = new LongField(1); byte[] array = new byte[ 8 ]; - try { - field.readFromBytes(array); - fail("should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } - field = new LongField(0); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> field1.readFromBytes(array)); + + LongField field2 = new LongField(0); for (int j = 0; j < _test_array.length; j++) { array[ 0 ] = ( byte ) (_test_array[ j ] % 256); array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256); @@ -115,8 +110,8 @@ final class TestLongField { array[ 5 ] = ( byte ) ((_test_array[ j ] >> 40) % 256); array[ 6 ] = ( byte ) ((_test_array[ j ] >> 48) % 256); array[ 7 ] = ( byte ) ((_test_array[ j ] >> 56) % 256); - field.readFromBytes(array); - assertEquals(_test_array[ j ], field.get(), "testing " + j); + field2.readFromBytes(array); + assertEquals(_test_array[ j ], field2.get(), "testing " + j); } } diff --git a/poi/src/test/java9/module-info.class b/poi/src/test/java9/module-info.class Binary files differindex 028b943d0a..438e778ab5 100644 --- a/poi/src/test/java9/module-info.class +++ b/poi/src/test/java9/module-info.class |