diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2021-01-11 00:34:21 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2021-01-11 00:34:21 +0000 |
commit | 22e67b0dc7fe2468980b227fa9ad2469bcac47e3 (patch) | |
tree | 50bea4f83343f345bdbfe0f07cdf1b10afe7bf46 | |
parent | 9daa77751a524300e70cee3a6a467cabcb980817 (diff) | |
download | poi-22e67b0dc7fe2468980b227fa9ad2469bcac47e3.tar.gz poi-22e67b0dc7fe2468980b227fa9ad2469bcac47e3.zip |
Sonar fixes
add asserts to tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885343 13f79535-47bb-0310-9956-ffa450edef68
12 files changed, 223 insertions, 252 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java index 4c89852f9e..70c78b3184 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java @@ -318,7 +318,6 @@ public final class TestXSSFReader { } } - @Test @Disabled("until we fix issue https://bz.apache.org/bugzilla/show_bug.cgi?id=61701") void test61701() throws Exception { try(Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("simple-table-named-range.xlsx")) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java index c61862cd5d..e89280c167 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java @@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; 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 java.io.File; import java.io.FileOutputStream; @@ -53,15 +52,15 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues { } // override some tests which do not work for SXSSF - @Override @Disabled("cloneSheet() not implemented") @Test + @Override @Disabled("cloneSheet() not implemented") protected void bug18800() { /* cloneSheet() not implemented */ } - @Override @Disabled("cloneSheet() not implemented") @Test + @Override @Disabled("cloneSheet() not implemented") protected void bug22720() { /* cloneSheet() not implemented */ } - @Override @Disabled("Evaluation is not fully supported") @Test + @Override @Disabled("Evaluation is not fully supported") protected void bug47815() { /* Evaluation is not supported */ } - @Override @Disabled("Evaluation is not fully supported") @Test + @Override @Disabled("Evaluation is not fully supported") protected void bug46729_testMaxFunctionArguments() { /* Evaluation is not supported */ } - @Override @Disabled("Reading data is not supported") @Test + @Override @Disabled("Reading data is not supported") protected void bug57798() { /* Reading data is not supported */ } /** @@ -106,19 +105,14 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues { @Test @Override protected void bug60197_NamedRangesReferToCorrectSheetWhenSheetOrderIsChanged() throws Exception { - try { - super.bug60197_NamedRangesReferToCorrectSheetWhenSheetOrderIsChanged(); - } catch (final RuntimeException e) { - final Throwable cause = e.getCause(); - //noinspection StatementWithEmptyBody - if (cause instanceof IOException && cause.getMessage().equals("Stream closed")) { - // expected on the second time that _testDataProvider.writeOutAndReadBack(SXSSFWorkbook) is called - // if the test makes it this far, then we know that XSSFName sheet indices are updated when sheet - // order is changed, which is the purpose of this test. Therefore, consider this a passing test. - } else { - throw e; - } - } + // expected on the second time that _testDataProvider.writeOutAndReadBack(SXSSFWorkbook) is called + // if the test makes it this far, then we know that XSSFName sheet indices are updated when sheet + // order is changed, which is the purpose of this test. Therefore, consider this a passing test. + RuntimeException e = + assertThrows(RuntimeException.class, () -> super.bug60197_NamedRangesReferToCorrectSheetWhenSheetOrderIsChanged()); + Throwable cause = e.getCause(); + assertTrue(cause instanceof IOException); + assertEquals("Stream closed", cause.getMessage()); } @Test @@ -176,7 +170,6 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues { sheet.setArrayFormula(col1Value, range); } - @Test @Disabled("takes too long for the normal test run") void test62872() throws Exception { final int COLUMN_COUNT = 300; @@ -204,8 +197,6 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues { cell.setCellValue(new Date(i*TEN_MINUTES+(j*TEN_MINUTES)/COLUMN_COUNT)); } i++; - // if (i % 1000 == 0) - // logger.info("Created Row " + i); } try (FileOutputStream out = new FileOutputStream(File.createTempFile("test62872", ".xlsx"))) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index 746efffc88..f677b5cc0f 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -57,7 +57,11 @@ import org.apache.poi.xssf.SXSSFITestDataProvider; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.model.SharedStringsTable; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.EnumSource; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType; @@ -75,23 +79,29 @@ public final class TestXSSFCell extends BaseTestXCell { */ @Test void test47026_1() throws IOException { - Workbook wb = _testDataProvider.openSampleWorkbook("47026.xlsm"); - Sheet sheet = wb.getSheetAt(0); - Row row = sheet.getRow(0); - Cell cell = row.getCell(0); - cell.setCellValue("456"); - wb.close(); + try (Workbook wb = _testDataProvider.openSampleWorkbook("47026.xlsm")) { + Sheet sheet = wb.getSheetAt(0); + Row row = sheet.getRow(0); + Cell cell = row.getCell(0); + assertEquals(CellType.NUMERIC, cell.getCachedFormulaResultType()); + cell.setCellValue("456"); + assertEquals(CellType.STRING, cell.getCachedFormulaResultType()); + } } @Test void test47026_2() throws IOException { - Workbook wb = _testDataProvider.openSampleWorkbook("47026.xlsm"); - Sheet sheet = wb.getSheetAt(0); - Row row = sheet.getRow(0); - Cell cell = row.getCell(0); - cell.setCellFormula(null); - cell.setCellValue("456"); - wb.close(); + try (Workbook wb = _testDataProvider.openSampleWorkbook("47026.xlsm")) { + Sheet sheet = wb.getSheetAt(0); + Row row = sheet.getRow(0); + Cell cell = row.getCell(0); + assertEquals(CellType.NUMERIC, cell.getCachedFormulaResultType()); + cell.setCellFormula(null); + IllegalStateException e = assertThrows(IllegalStateException.class, cell::getCachedFormulaResultType); + assertEquals("Only formula cells have cached results", e.getMessage()); + cell.setCellValue("456"); + assertEquals(CellType.STRING, cell.getCellType()); + } } /** @@ -441,33 +451,35 @@ public final class TestXSSFCell extends BaseTestXCell { } } - @Test - void testBug56644ReturnNull() throws IOException { + @ParameterizedTest + @EnumSource(value = MissingCellPolicy.class) + void testBug56644ReturnNull(MissingCellPolicy policy) throws IOException { try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx")) { - wb.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL); + wb.setMissingCellPolicy(policy); Sheet sheet = wb.getSheet("samplelist"); Row row = sheet.getRow(20); - row.createCell(2); - } - } - - @Test - void testBug56644ReturnBlank() throws IOException { - try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx")) { - wb.setMissingCellPolicy(MissingCellPolicy.RETURN_NULL_AND_BLANK); - Sheet sheet = wb.getSheet("samplelist"); - Row row = sheet.getRow(20); - row.createCell(2); - } - } - - @Test - void testBug56644CreateBlank() throws IOException { - try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx")) { - wb.setMissingCellPolicy(MissingCellPolicy.CREATE_NULL_AS_BLANK); - Sheet sheet = wb.getSheet("samplelist"); - Row row = sheet.getRow(20); - row.createCell(2); + switch (policy) { + case CREATE_NULL_AS_BLANK: { + Cell cell = row.getCell(2); + assertNotNull(cell); + assertEquals(CellType.STRING, cell.getCellType()); + break; + } + case RETURN_BLANK_AS_NULL: { + Cell cell = row.getCell(2); + assertNotNull(cell); + assertEquals(CellType.STRING, cell.getCellType()); + cell.setBlank(); + cell = row.getCell(2); + assertNull(cell); + break; + } + case RETURN_NULL_AND_BLANK: { + Cell cell = row.getCell(2); + assertNotNull(cell); + break; + } + } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/util/MemoryUsage.java b/src/ooxml/testcases/org/apache/poi/xssf/util/MemoryUsage.java index d34957475d..e7ba523ded 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/util/MemoryUsage.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/util/MemoryUsage.java @@ -17,17 +17,22 @@ package org.apache.poi.xssf.util; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.util.CellReference; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData; @@ -37,6 +42,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; /** * Mixed utilities for testing memory usage in XSSF */ +@Disabled("only for manual tests") @SuppressWarnings("InfiniteLoopStatement") public class MemoryUsage { private static final int NUM_COLUMNS = 255; @@ -46,78 +52,6 @@ public class MemoryUsage { } /** - * Generate a spreadsheet until OutOfMemoryError - * <p> - * cells in even columns are numbers, cells in odd columns are strings - * </p> - * - * @param wb the workbook to write to - * @param numCols the number of columns in a row - */ - public static void mixedSpreadsheet(Workbook wb, int numCols) { - System.out.println(); - System.out.println("Testing " + wb.getClass().getName() + " mixed"); - printMemoryUsage("before"); - int i=0, cnt=0; - try { - Sheet sh = wb.createSheet(); - for(i=0; ; i++) { - Row row = sh.createRow(i); - for(int j=0; j < numCols; j++) { - Cell cell = row.createCell(j); - if(j % 2 == 0) { - cell.setCellValue(j); - } else { - cell.setCellValue(new CellReference(j, i).formatAsString()); - } - cnt++; - } - } - } catch (OutOfMemoryError er) { - System.out.println("Failed at row=" + i + ", objects : " + cnt); - } catch (final Exception e) { - System.out.println("Unable to reach an OutOfMemoryError"); - System.out.println(e.getClass().getName() + ": " + e.getMessage()); - } - printMemoryUsage("after"); - } - - /** - * Generate a spreadsheet who's all cell values are numbers. - * The data is generated until OutOfMemoryError. - * <p> - * as compared to {@link #mixedSpreadsheet(org.apache.poi.ss.usermodel.Workbook, int)}, - * this method does not set string values and, hence, does not involve the Shared Strings Table. - * </p> - * - * @param wb the workbook to write to - * @param numCols the number of columns in a row - */ - public static void numberSpreadsheet(Workbook wb, int numCols) { - System.out.println(); - System.out.println("Testing " + wb.getClass().getName() + " numbers"); - printMemoryUsage("before"); - int i=0, cnt=0; - try { - Sheet sh = wb.createSheet(); - for(i=0; ; i++) { - Row row = sh.createRow(i); - for(int j=0; j < numCols; j++) { - Cell cell = row.createCell(j); - cell.setCellValue(j); - cnt++; - } - } - } catch (OutOfMemoryError er) { - System.out.println("Failed at row=" + i + ", objects : " + cnt); - } catch (final Exception e) { - System.out.println("Unable to reach an OutOfMemoryError"); - System.out.println(e.getClass().getName() + ": " + e.getMessage()); - } - printMemoryUsage("after"); - } - - /** * Generate a spreadsheet until OutOfMemoryError using low-level OOXML XmlBeans. * Similar to {@link #numberSpreadsheet(org.apache.poi.ss.usermodel.Workbook, int)} * @@ -144,11 +78,9 @@ public class MemoryUsage { } } catch (OutOfMemoryError er) { System.out.println("Failed at row=" + i + ", objects: " + cnt); - } catch (final Exception e) { - System.out.println("Unable to reach an OutOfMemoryError"); - System.out.println(e.getClass().getName() + ": " + e.getMessage()); + } finally { + printMemoryUsage("after"); } - printMemoryUsage("after"); } /** @@ -158,25 +90,21 @@ public class MemoryUsage { */ @Test void testXmlDetached() { - System.out.println(); - System.out.println("Testing detached"); - List<CTRow> rows = new ArrayList<>(); int i = 0; try { for(;;) { //create a standalone CTRow bean CTRow r = CTRow.Factory.newInstance(); + assertNotNull(r); r.setR(++i); rows.add(r); } } catch (OutOfMemoryError er) { System.out.println("Failed at row=" + i + " from " + rows.size() + " kept."); - } catch (final Exception e) { - System.out.println("Unable to reach an OutOfMemoryError"); - System.out.println(e.getClass().getName() + ": " + e.getMessage()); + } finally { + printMemoryUsage("after"); } - printMemoryUsage("after"); } /** @@ -187,8 +115,6 @@ public class MemoryUsage { */ @Test void testXmlAttached() { - System.out.println(); - System.out.println("Testing attached"); printMemoryUsage("before"); List<CTRow> rows = new ArrayList<>(); int i = 0; @@ -199,35 +125,79 @@ public class MemoryUsage { for(;;) { //create CTRow attached to the parent object CTRow r = data.addNewRow(); + assertNotNull(r); r.setR(++i); rows.add(r); } } catch (OutOfMemoryError er) { System.out.println("Failed at row=" + i + " from " + rows.size() + " kept."); - } catch (final Exception e) { - System.out.println("Unable to reach an OutOfMemoryError"); - System.out.println(e.getClass().getName() + ": " + e.getMessage()); + } finally { + printMemoryUsage("after"); } - printMemoryUsage("after"); - } - - @Test - void testMixedHSSF() { - mixedSpreadsheet(new HSSFWorkbook(), NUM_COLUMNS); - } - - @Test - void testMixedXSSF() { - mixedSpreadsheet(new XSSFWorkbook(), NUM_COLUMNS); } - @Test - void testNumberHSSF() { - numberSpreadsheet(new HSSFWorkbook(), NUM_COLUMNS); + /** + * Generate a spreadsheet until OutOfMemoryError + * cells in even columns are numbers, cells in odd columns are strings + */ + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void testMixed(boolean useXSSF) throws IOException { + int i=0, cnt=0; + try (Workbook wb = WorkbookFactory.create(useXSSF)) { + printMemoryUsage("before"); + Sheet sh = wb.createSheet(); + for(i=0; ; i++) { + Row row = sh.createRow(i); + for(int j=0; j < NUM_COLUMNS; j++) { + Cell cell = row.createCell(j); + assertNotNull(cell); + if(j % 2 == 0) { + cell.setCellValue(j); + } else { + cell.setCellValue(new CellReference(j, i).formatAsString()); + } + cnt++; + } + } + } catch (OutOfMemoryError er) { + System.out.println("Failed at row=" + i + ", objects : " + cnt); + } finally { + printMemoryUsage("after"); + } } - @Test - void testNumberXSSF() { - numberSpreadsheet(new XSSFWorkbook(), NUM_COLUMNS); + /** + * Generate a spreadsheet who's all cell values are numbers. + * The data is generated until OutOfMemoryError. + * <p> + * as compared to {@link #mixedSpreadsheet(org.apache.poi.ss.usermodel.Workbook, int)}, + * this method does not set string values and, hence, does not involve the Shared Strings Table. + * </p> + * + * @param wb the workbook to write to + * @param numCols the number of columns in a row + */ + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void testNumberHSSF(boolean useXSSF) throws IOException { + int i=0, cnt=0; + try (Workbook wb = WorkbookFactory.create(useXSSF)) { + printMemoryUsage("before"); + Sheet sh = wb.createSheet(); + for(i=0; ; i++) { + Row row = sh.createRow(i); + assertNotNull(row); + for(int j=0; j < NUM_COLUMNS; j++) { + Cell cell = row.createCell(j); + cell.setCellValue(j); + cnt++; + } + } + } catch (OutOfMemoryError er) { + System.out.println("Failed at row=" + i + ", objects : " + cnt); + } finally { + printMemoryUsage("after"); + } } }
\ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java index c3505d42fb..4ad0d80a8c 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java @@ -172,11 +172,13 @@ class TestXWPFBugs { doc.write(out); out.close(); - XWPFDocument doc2 = new XWPFDocument(new ByteArrayInputStream(out.toByteArray())); - doc2.close(); + try (XWPFDocument doc2 = new XWPFDocument(new ByteArrayInputStream(out.toByteArray()))) { + assertNotNull(doc2); + } - XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc); - docBack.close(); + try (XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc)) { + assertNotNull(docBack); + } } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java index 0f044a98f7..73d2eebe20 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java @@ -31,7 +31,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; -public final class TestXWPFHeader { +final class TestXWPFHeader { @Test void testSimpleHeader() throws IOException { @@ -143,7 +143,7 @@ public final class TestXWPFHeader { String fText2 = "More Text!"; headerF.createParagraph().insertNewRun(0).setText(fText1); headerF.createParagraph().insertNewRun(0).setText(fText2); -// headerF.getParagraphs().get(0).insertNewRun(0).setText(fText1); + // headerF.getParagraphs().get(0).insertNewRun(0).setText(fText1); // Check it assertEquals(tText, headerD.getParagraphs().get(0).getText()); @@ -206,15 +206,15 @@ public final class TestXWPFHeader { } } + @Test void testSetWatermarkOnEmptyDoc() throws IOException { try (XWPFDocument sampleDoc = new XWPFDocument()) { // No header is set (yet) XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); - assertNull(policy.getDefaultHeader()); - assertNull(policy.getFirstPageHeader()); - assertNull(policy.getDefaultFooter()); + assertNull(policy); + policy = sampleDoc.createHeaderFooterPolicy(); policy.createWatermark("DRAFT"); assertNotNull(policy.getDefaultHeader()); @@ -232,25 +232,21 @@ public final class TestXWPFHeader { } @Disabled - @Test void testAddPictureData() { // TODO } @Disabled - @Test void testGetAllPictures() { // TODO } @Disabled - @Test void testGetAllPackagePictures() { // TODO } @Disabled - @Test void testGetPictureDataById() { // TODO } diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java b/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java index b44b5d5d53..73c0423859 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java @@ -18,61 +18,58 @@ 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.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.stream.Stream; import org.apache.poi.util.IOUtils; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; final class TestEmptyDocument { private static final POILogger LOG = POILogFactory.getLogger(TestEmptyDocument.class); - @Test - void testSingleEmptyDocument() throws IOException { - POIFSFileSystem fs = new POIFSFileSystem(); - DirectoryEntry dir = fs.getRoot(); - dir.createDocument("Foo", new ByteArrayInputStream(new byte[] {})); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - fs.writeFilesystem(out); - new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray())).close(); - fs.close(); + private interface EmptyDoc { + void handle(DirectoryEntry dir) throws IOException; } - @Test - void testSingleEmptyDocumentEvent() throws IOException { - POIFSFileSystem fs = new POIFSFileSystem(); - DirectoryEntry dir = fs.getRoot(); - dir.createDocument("Foo", 0, event -> LOG.log(POILogger.WARN, "written")); + public static Stream<Arguments> emptySupplier() { + return Stream.of( + Arguments.of("SingleEmptyDocument", (EmptyDoc)TestEmptyDocument::SingleEmptyDocument), + Arguments.of("SingleEmptyDocumentEvent", (EmptyDoc)TestEmptyDocument::SingleEmptyDocumentEvent), + Arguments.of("EmptyDocumentWithFriend", (EmptyDoc)TestEmptyDocument::EmptyDocumentWithFriend), + Arguments.of("EmptyDocumentEventWithFriend", (EmptyDoc)TestEmptyDocument::EmptyDocumentEventWithFriend) + ); + } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - fs.writeFilesystem(out); - new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray())).close(); - fs.close(); + private static void SingleEmptyDocument(DirectoryEntry dir) throws IOException { + dir.createDocument("Foo", new ByteArrayInputStream(new byte[]{})); } - @Test - void testEmptyDocumentWithFriend() throws IOException { - POIFSFileSystem fs = new POIFSFileSystem(); - DirectoryEntry dir = fs.getRoot(); - dir.createDocument("Bar", new ByteArrayInputStream(new byte[] { 0 })); - dir.createDocument("Foo", new ByteArrayInputStream(new byte[] {})); + private static void SingleEmptyDocumentEvent(DirectoryEntry dir) throws IOException { + dir.createDocument("Foo", 0, event -> LOG.log(POILogger.WARN, "written")); + } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - fs.writeFilesystem(out); - new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray())).close(); - fs.close(); + private static void EmptyDocumentWithFriend(DirectoryEntry dir) throws IOException { + dir.createDocument("Bar", new ByteArrayInputStream(new byte[]{0})); + dir.createDocument("Foo", new ByteArrayInputStream(new byte[]{})); } - @Test - void testEmptyDocumentEventWithFriend() throws IOException { - POIFSFileSystem fs = new POIFSFileSystem(); - DirectoryEntry dir = fs.getRoot(); + private static void EmptyDocumentEventWithFriend(DirectoryEntry dir) throws IOException { dir.createDocument("Bar", 1, event -> { try { event.getStream().write(0); @@ -81,38 +78,45 @@ final class TestEmptyDocument { } }); dir.createDocument("Foo", 0, event -> {}); + } - ByteArrayOutputStream out = new ByteArrayOutputStream(); - fs.writeFilesystem(out); - new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray())).close(); - fs.close(); + + @ParameterizedTest(name = "{index} {0}") + @MethodSource("emptySupplier") + void testFoo(String testName, EmptyDoc emptyDoc) throws IOException { + try (POIFSFileSystem fs = new POIFSFileSystem()) { + DirectoryEntry dir = fs.getRoot(); + emptyDoc.handle(dir); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + fs.writeFilesystem(out); + assertDoesNotThrow(() -> new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()))); + } } @Test void testEmptyDocumentBug11744() throws Exception { byte[] testData = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - POIFSFileSystem fs = new POIFSFileSystem(); - fs.createDocument(new ByteArrayInputStream(new byte[0]), "Empty"); - fs.createDocument(new ByteArrayInputStream(testData), "NotEmpty"); ByteArrayOutputStream out = new ByteArrayOutputStream(); - fs.writeFilesystem(out); - out.toByteArray(); - fs.close(); + try (POIFSFileSystem fs = new POIFSFileSystem()) { + fs.createDocument(new ByteArrayInputStream(new byte[0]), "Empty"); + fs.createDocument(new ByteArrayInputStream(testData), "NotEmpty"); + fs.writeFilesystem(out); + } // This line caused the error. - fs = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray())); - - DocumentEntry entry = (DocumentEntry) fs.getRoot().getEntry("Empty"); - assertEquals(0, entry.getSize(), "Expected zero size"); - byte[] actualReadbackData; - actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry)); - assertEquals(0, actualReadbackData.length, "Expected zero read from stream"); - - entry = (DocumentEntry) fs.getRoot().getEntry("NotEmpty"); - actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry)); - assertEquals(testData.length, entry.getSize(), "Expected size was wrong"); - assertArrayEquals(testData, actualReadbackData, "Expected same data read from stream"); - fs.close(); + try (POIFSFileSystem fs = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()))) { + DocumentEntry entry = (DocumentEntry) fs.getRoot().getEntry("Empty"); + assertEquals(0, entry.getSize(), "Expected zero size"); + byte[] actualReadbackData; + actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry)); + assertEquals(0, actualReadbackData.length, "Expected zero read from stream"); + + entry = (DocumentEntry) fs.getRoot().getEntry("NotEmpty"); + actualReadbackData = IOUtils.toByteArray(new DocumentInputStream(entry)); + assertEquals(testData.length, entry.getSize(), "Expected size was wrong"); + assertArrayEquals(testData, actualReadbackData, "Expected same data read from stream"); + } } } diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java index 8682bad46b..c644fb1c60 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java @@ -42,6 +42,9 @@ import org.apache.poi.poifs.storage.BATBlock; import org.apache.poi.poifs.storage.HeaderBlock; import org.apache.poi.util.IOUtils; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; /** * Tests for the older OPOIFS-based POIFSFileSystem @@ -137,23 +140,18 @@ final class TestPOIFSFileSystem { * The other is to fix the handling of the last block in * POIFS, since it seems to be slight wrong */ - @Test - void testShortLastBlock() throws Exception { - String[] files = new String[] { - "ShortLastBlock.qwp", "ShortLastBlock.wps" - }; - - for (String file : files) { - // Open the file up - POIFSFileSystem fs = new POIFSFileSystem( - _samples.openResourceAsStream(file) - ); + @ParameterizedTest + @CsvSource({ "ShortLastBlock.qwp, 1303681", "ShortLastBlock.wps, 140787" }) + void testShortLastBlock(String file, int size) throws Exception { + // Open the file up + try (POIFSFileSystem fs = new POIFSFileSystem(_samples.openResourceAsStream(file))) { // Write it into a temp output array ByteArrayOutputStream baos = new ByteArrayOutputStream(); fs.writeFilesystem(baos); // Check sizes + assertEquals(size, baos.size()); } } diff --git a/src/testcases/org/apache/poi/sl/draw/geom/TestPresetGeometries.java b/src/testcases/org/apache/poi/sl/draw/geom/TestPresetGeometries.java index 4d0061673e..28849f77e9 100644 --- a/src/testcases/org/apache/poi/sl/draw/geom/TestPresetGeometries.java +++ b/src/testcases/org/apache/poi/sl/draw/geom/TestPresetGeometries.java @@ -51,7 +51,6 @@ class TestPresetGeometries { } @Disabled("problem solved? Turn back on if this debugging is still in process.") - @Test void testCheckXMLParser() throws Exception{ // Gump reports a strange error because of an unavailable XML Parser, let's try to find out where // this comes from diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestLeftRight.java b/src/testcases/org/apache/poi/ss/formula/functions/TestLeftRight.java index f6f0d1be19..4bf92c7196 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestLeftRight.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestLeftRight.java @@ -50,8 +50,8 @@ class TestLeftRight { @Test void testLeftRight_bug49841() { - invokeLeft(ANY_STRING_VALUE, NEGATIVE_OPERAND); - invokeRight(ANY_STRING_VALUE, NEGATIVE_OPERAND); + assertEquals(ErrorEval.VALUE_INVALID, invokeLeft(ANY_STRING_VALUE, NEGATIVE_OPERAND)); + assertEquals(ErrorEval.VALUE_INVALID, invokeRight(ANY_STRING_VALUE, NEGATIVE_OPERAND)); } @Test diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestStatsLib.java b/src/testcases/org/apache/poi/ss/formula/functions/TestStatsLib.java index 70e83bd9af..9fb322f978 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestStatsLib.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestStatsLib.java @@ -104,7 +104,6 @@ class TestStatsLib extends BaseTestNumeric { assertDouble("kthLargest ", x, d); } - @Test @Disabled("... implement ...") void testKthSmallest() { } diff --git a/src/testcases/org/apache/poi/ss/formula/ptg/TestReferencePtg.java b/src/testcases/org/apache/poi/ss/formula/ptg/TestReferencePtg.java index cc67d4304a..f4170286ca 100644 --- a/src/testcases/org/apache/poi/ss/formula/ptg/TestReferencePtg.java +++ b/src/testcases/org/apache/poi/ss/formula/ptg/TestReferencePtg.java @@ -18,6 +18,7 @@ package org.apache.poi.ss.formula.ptg; import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -71,7 +72,7 @@ final class TestReferencePtg { @Test void testBug44921() throws IOException { try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex44921-21902.xls")) { - HSSFTestDataSamples.writeOutAndReadBack(wb).close(); + assertDoesNotThrow(() -> HSSFTestDataSamples.writeOutAndReadBack(wb)); } } |