diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2021-01-17 00:50:47 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2021-01-17 00:50:47 +0000 |
commit | 020f33a940112c44e570eb42cc78f9e6d5ffb7f4 (patch) | |
tree | 2633365345fca5f1fef56a11c39b07f262b42b8e /src/testcases | |
parent | 44efecf42ed62aba1d6c9e149c3683bfd19f6453 (diff) | |
download | poi-020f33a940112c44e570eb42cc78f9e6d5ffb7f4.tar.gz poi-020f33a940112c44e570eb42cc78f9e6d5ffb7f4.zip |
Sonar fixes
add asserts to tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885585 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
9 files changed, 180 insertions, 229 deletions
diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java index 4e93a62156..67d351bffc 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java @@ -19,6 +19,7 @@ package org.apache.poi.ss.usermodel; import static org.apache.poi.POITestCase.skipTest; import static org.apache.poi.POITestCase.testPassesNow; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -124,12 +125,12 @@ public abstract class BaseTestSheetShiftRows { */ @Test public final void testShiftRow() throws IOException { - Workbook wb = _testDataProvider.createWorkbook(); - Sheet s = wb.createSheet(); - s.createRow(0).createCell(0).setCellValue("TEST1"); - s.createRow(3).createCell(0).setCellValue("TEST2"); - s.shiftRows(0,4,1); - wb.close(); + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet s = wb.createSheet(); + s.createRow(0).createCell(0).setCellValue("TEST1"); + s.createRow(3).createCell(0).setCellValue("TEST2"); + assertDoesNotThrow(() -> s.shiftRows(0, 4, 1)); + } } /** @@ -491,13 +492,12 @@ public abstract class BaseTestSheetShiftRows { @Test void test47169() throws IOException { - Workbook wb = _testDataProvider.createWorkbook(); - Sheet sheet = wb.createSheet(); - sheet.createRow(30); - sheet.shiftRows(29, 29, 1, true, true); - sheet.createRow(30); - - wb.close(); + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet sheet = wb.createSheet(); + sheet.createRow(30); + sheet.shiftRows(29, 29, 1, true, true); + assertDoesNotThrow(() -> sheet.createRow(30)); + } } /** diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index 6de40bb4b4..11bb3c9357 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -18,6 +18,7 @@ package org.apache.poi.ss.usermodel; 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.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -765,8 +766,7 @@ public abstract class BaseTestWorkbook { @Test void test58499() throws IOException { - try (Workbook workbook = _testDataProvider.createWorkbook(); - OutputStream os = new NullOutputStream()) { + try (Workbook workbook = _testDataProvider.createWorkbook()) { Sheet sheet = workbook.createSheet(); for (int i = 0; i < 900; i++) { Row r = sheet.createRow(i); @@ -775,7 +775,7 @@ public abstract class BaseTestWorkbook { c.setCellStyle(cs); c.setCellValue("AAA"); } - workbook.write(os); + assertDoesNotThrow(() -> workbook.write(new NullOutputStream())); } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java index fd904ab1b5..e27557e9b9 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java @@ -17,6 +17,7 @@ package org.apache.poi.ss.usermodel; +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.assertTrue; @@ -25,6 +26,7 @@ import java.text.DateFormatSymbols; import java.text.FieldPosition; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -35,111 +37,88 @@ import java.util.stream.Stream; import org.apache.poi.util.LocaleUtil; 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; class TestExcelStyleDateFormatter { private static final String EXCEL_DATE_FORMAT = "MMMMM"; private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT); - private final int jreVersion; - - public TestExcelStyleDateFormatter() { - jreVersion = Integer.parseInt(System.getProperty("java.version") - .replace("1.8", "8").replaceAll("(\\d+).*", "$1")); - } + private static final int jreVersion = + Integer.parseInt(System.getProperty("java.version").replaceAll("^(?:1\\.)?(\\d+).*", "$1")); + private static final String provider = System.getProperty("java.locale.providers"); + private static final FieldPosition fp = new FieldPosition(java.text.DateFormat.MONTH_FIELD); + private static final ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT); /** * [Bug 60369] Month format 'MMMMM' issue with TEXT-formula and Java 8 */ - @Test - void test60369() { - Map<Locale, String> testMap = initializeLocales(); - - // We have to set up dates as well. - List<Date> testDates = Stream.of("1980-01-12", "1995-02-11", "2045-03-10", "2016-04-09", "2017-05-08", - "1945-06-07", "1998-07-06", "2099-08-05", "1988-09-04", "2023-10-03", "1978-11-02", "1890-12-01") - .map(this::parseDate).collect(Collectors.toList()); - + @ParameterizedTest + @MethodSource("initializeLocales") + void test60369(Locale locale, String expected, Date d, int month) { // Let's iterate over the test setup. - final String provider = System.getProperty("java.locale.providers"); - final FieldPosition fp = new FieldPosition(java.text.DateFormat.MONTH_FIELD); - final ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT); final StringBuffer sb = new StringBuffer(); - for (Map.Entry<Locale,String> me : testMap.entrySet()) { - final Locale locale = me.getKey(); - final String expected = me.getValue(); - formatter.setDateFormatSymbols(DateFormatSymbols.getInstance(locale)); - int month = 0; - for (Date d : testDates) { - sb.setLength(0); - String result = formatter.format(d, sb, fp).toString(); - String msg = "Failed testDates for locale " + locale + ", provider: " + provider + - " and date " + d + ", having: " + result; - - int actIdx = localeIndex(locale); - - assertNotNull(result, msg); - assertTrue(result.length() > actIdx, msg); - assertEquals(expected.charAt(month), result.charAt(actIdx), msg); - month++; - } - } + formatter.setDateFormatSymbols(DateFormatSymbols.getInstance(locale)); + String result = formatter.format(d, sb, fp).toString(); + String msg = "Failed testDates for locale " + locale + ", provider: " + provider + + " and date " + d + ", having: " + result; + + int actIdx = localeIndex(locale); + + assertNotNull(result, msg); + assertTrue(result.length() > actIdx, msg); + assertEquals(expected.charAt(month), result.charAt(actIdx), msg); } /** * Depending on the JRE version, the provider setting and the locale, a different result * is expected and selected via an index */ - private int localeIndex(Locale locale) { - final String provider = System.getProperty("java.locale.providers"); + private static int localeIndex(Locale locale) { return jreVersion < 9 || !locale.equals (Locale.CHINESE) || (provider != null && (provider.startsWith("JRE") || provider.startsWith("COMPAT"))) ? 0 : 1; } - private Date parseDate(String dateStr) { - try { - return DATE_FORMAT.parse(dateStr); - } catch (ParseException e) { - return new Date(0); - } - } - /** * Setting up the locale to be tested together with a list of asserted * unicode-formatted results and put them in a map. */ - private Map<Locale, String> initializeLocales() { - Map<Locale, String> testMap = new HashMap<>(); - - testMap.put(Locale.GERMAN, "JFMAMJJASOND"); - testMap.put(new Locale("de", "AT"), "JFMAMJJASOND"); - testMap.put(Locale.UK, "JFMAMJJASOND"); - testMap.put(new Locale("en", "IN"), "JFMAMJJASOND"); - testMap.put(new Locale("in", "ID"), "JFMAMJJASOND"); - testMap.put(Locale.FRENCH, "jfmamjjasond"); - - testMap.put(new Locale("ru", "RU"), - "\u044f\u0444\u043c\u0430\u043c\u0438\u0438\u0430\u0441\u043e\u043d\u0434"); - - testMap.put(Locale.CHINESE, new String[]{ - "\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341\u5341\u5341", - "123456789111" - }[localeIndex(Locale.CHINESE)]); - - testMap.put(new Locale("tr", "TR"), - "\u004f\u015e\u004d\u004e\u004d\u0048\u0054\u0041\u0045\u0045\u004b\u0041"); - - testMap.put(new Locale("hu", "HU"), - "\u006a\u0066\u006d\u00e1\u006d\u006a\u006a\u0061\u0073\u006f\u006e\u0064"); - - return testMap; + public static Stream<Arguments> initializeLocales() throws ParseException { + Object[][] locExps = { + { Locale.GERMAN, "JFMAMJJASOND" }, + { new Locale("de", "AT"), "JFMAMJJASOND" }, + { Locale.UK, "JFMAMJJASOND" }, + { new Locale("en", "IN"), "JFMAMJJASOND" }, + { new Locale("in", "ID"), "JFMAMJJASOND" }, + { Locale.FRENCH, "jfmamjjasond" }, + { new Locale("ru", "RU"), "\u044f\u0444\u043c\u0430\u043c\u0438\u0438\u0430\u0441\u043e\u043d\u0434" }, + { Locale.CHINESE, localeIndex(Locale.CHINESE) == 0 ? "\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341\u5341\u5341" : "123456789111" }, + { new Locale("tr", "TR"), "\u004f\u015e\u004d\u004e\u004d\u0048\u0054\u0041\u0045\u0045\u004b\u0041" }, + { new Locale("hu", "HU"), "\u006a\u0066\u006d\u00e1\u006d\u006a\u006a\u0061\u0073\u006f\u006e\u0064" } + }; + + String[] dates = { + "1980-01-12", "1995-02-11", "2045-03-10", "2016-04-09", "2017-05-08", + "1945-06-07", "1998-07-06", "2099-08-05", "1988-09-04", "2023-10-03", "1978-11-02", "1890-12-01" + }; + + List<Arguments> list = new ArrayList<>(locExps.length * dates.length); + for (Object[] locExp : locExps) { + int month = 0; + for (String date : dates) { + list.add(Arguments.of(locExp[0], locExp[1], DATE_FORMAT.parse(date), month++)); + } + } + return list.stream(); } @Test void testConstruct() { - new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT, LocaleUtil.getUserLocale()); - new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT); + assertDoesNotThrow(() -> new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT, LocaleUtil.getUserLocale())); + assertDoesNotThrow(() -> new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT)); } @Test diff --git a/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java b/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java index 3113bc95e9..00697bbdb4 100644 --- a/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java +++ b/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java @@ -19,6 +19,9 @@ package org.apache.poi.ss.util; +import static java.text.DateFormat.getDateInstance; +import static java.text.DateFormat.getDateTimeInstance; +import static java.text.DateFormat.getTimeInstance; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -45,22 +48,34 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.LocaleID; +import org.apache.poi.util.NullOutputStream; import org.apache.poi.util.TempFile; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; final class TestDateFormatConverter { - private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception { + @ParameterizedTest + @CsvSource({ + "true, false, " + DateFormat.DEFAULT + ", Default", + "true, false, " + DateFormat.SHORT + ", Short", + "true, false, " + DateFormat.MEDIUM + ", Medium", + "true, false, " + DateFormat.LONG + ", Long", + "true, false, " + DateFormat.FULL + ", Full", + "true, true, " + DateFormat.DEFAULT + ", Default", + "true, true, " + DateFormat.SHORT + ", Short", + "true, true, " + DateFormat.MEDIUM + ", Medium", + "true, true, " + DateFormat.LONG + ", Long", + "true, true, " + DateFormat.FULL + ", Full", + "false, true, " + DateFormat.DEFAULT + ", Default", + "false, true, " + DateFormat.SHORT + ", Short", + "false, true, " + DateFormat.MEDIUM + ", Medium", + "false, true, " + DateFormat.LONG + ", Long", + "false, true, " + DateFormat.FULL + ", Full" + }) + void testJavaDateFormatsInExcel(boolean dates, boolean times, int style, String styleName ) throws Exception { try (Workbook workbook = new HSSFWorkbook()) { - String sheetName; - if (dates) { - if (times) { - sheetName = "DateTimes"; - } else { - sheetName = "Dates"; - } - } else { - sheetName = "Times"; - } + String sheetName = (dates) ? ((times) ? "DateTimes" : "Dates") : "Times"; Sheet sheet = workbook.createSheet(sheetName); Row header = sheet.createRow(0); header.createCell(0).setCellValue("locale"); @@ -73,90 +88,51 @@ final class TestDateFormatConverter { int rowNum = 1; for (Locale locale : DateFormat.getAvailableLocales()) { - try { - Row row = sheet.createRow(rowNum++); - - row.createCell(0).setCellValue(locale.toString()); - row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT)); - - DateFormat dateFormat; - if (dates) { - if (times) { - dateFormat = DateFormat.getDateTimeInstance(style, style, locale); - } else { - dateFormat = DateFormat.getDateInstance(style, locale); - } - } else { - dateFormat = DateFormat.getTimeInstance(style, locale); - } - - Cell cell = row.createCell(2); - - cell.setCellValue(date); - CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); - - String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern(); - String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); - - DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); - cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern)); - row.createCell(3).setCellValue(dateFormat.format(date)); - - cell.setCellStyle(cellStyle); - - // the formula returns TRUE is the formatted date in column C equals to the string in column D - row.createCell(4).setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum); - row.createCell(5).setCellValue(javaDateFormatPattern); - row.createCell(6).setCellValue(excelFormatPattern); - } catch (Exception e) { - throw new RuntimeException( - "Failed for locale: " + locale + " and style " + style + "\n" + - "Having locales: " + Arrays.toString(DateFormat.getAvailableLocales()), e); - } - } + Row row = sheet.createRow(rowNum++); + + row.createCell(0).setCellValue(locale.toString()); + row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT)); + + DateFormat dateFormat = (dates) + ? (times ? getDateTimeInstance(style, style, locale) : getDateInstance(style, locale)) + : getTimeInstance(style, locale); + + Cell cell = row.createCell(2); + Date date = new Date(); + cell.setCellValue(date); + CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); - File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx"); - try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { - workbook.write(outputStream); + String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern(); + String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); + + DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); + cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern)); + row.createCell(3).setCellValue(dateFormat.format(date)); + + cell.setCellStyle(cellStyle); + + // the formula returns TRUE is the formatted date in column C equals to the string in column D + row.createCell(4).setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum); + row.createCell(5).setCellValue(javaDateFormatPattern); + row.createCell(6).setCellValue(excelFormatPattern); } - //System.out.println("Open " + outputFile.getAbsolutePath() + " in Excel"); + workbook.write(new NullOutputStream()); } } @Test - void testJavaDateFormatsInExcel() throws Exception { - Date date = new Date(); - - outputLocaleDataFormats(date, true, false, DateFormat.DEFAULT, "Default" ); - outputLocaleDataFormats(date, true, false, DateFormat.SHORT, "Short" ); - outputLocaleDataFormats(date, true, false, DateFormat.MEDIUM, "Medium" ); - outputLocaleDataFormats(date, true, false, DateFormat.LONG, "Long" ); - outputLocaleDataFormats(date, true, false, DateFormat.FULL, "Full" ); - - outputLocaleDataFormats(date, true, true, DateFormat.DEFAULT, "Default" ); - outputLocaleDataFormats(date, true, true, DateFormat.SHORT, "Short" ); - outputLocaleDataFormats(date, true, true, DateFormat.MEDIUM, "Medium" ); - outputLocaleDataFormats(date, true, true, DateFormat.LONG, "Long" ); - outputLocaleDataFormats(date, true, true, DateFormat.FULL, "Full" ); - - outputLocaleDataFormats(date, false, true, DateFormat.DEFAULT, "Default" ); - outputLocaleDataFormats(date, false, true, DateFormat.SHORT, "Short" ); - outputLocaleDataFormats(date, false, true, DateFormat.MEDIUM, "Medium" ); - outputLocaleDataFormats(date, false, true, DateFormat.LONG, "Long" ); - outputLocaleDataFormats(date, false, true, DateFormat.FULL, "Full" ); - } - - @Test void testJDK8EmptyLocale() { // JDK 8 seems to add an empty locale-string to the list returned via DateFormat.getAvailableLocales() // therefore we now cater for this special locale as well - DateFormatConverter.getPrefixForLocale(new Locale("")); + String prefix = DateFormatConverter.getPrefixForLocale(new Locale("")); + assertEquals("", prefix); } @Test void testJDK11MyLocale() { - DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.forLanguageTag("my")); + DateFormat df = getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.forLanguageTag("my")); + assertNotNull(df); } @Test diff --git a/src/testcases/org/apache/poi/ss/util/TestExpandedDouble.java b/src/testcases/org/apache/poi/ss/util/TestExpandedDouble.java index 30f84a3eb7..9465619b79 100644 --- a/src/testcases/org/apache/poi/ss/util/TestExpandedDouble.java +++ b/src/testcases/org/apache/poi/ss/util/TestExpandedDouble.java @@ -24,6 +24,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigInteger; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; /** * Tests for {@link ExpandedDouble} @@ -54,33 +56,27 @@ final class TestExpandedDouble { /** * Tests specific values for conversion from {@link ExpandedDouble} to {@link NormalisedDecimal} and back */ - @Test - void testRoundTripShifting() { - long[] rawValues = { - 0x4010000000000004L, - 0x7010000000000004L, - 0x1010000000000004L, - 0x0010000000000001L, // near lowest normal number - 0x0010000000000000L, // lowest normal number - 0x000FFFFFFFFFFFFFL, // highest subnormal number - 0x0008000000000000L, // subnormal number - - 0xC010000000000004L, - 0xE230100010001004L, - 0x403CE0FFFFFFFFF2L, - 0x0000000000000001L, // smallest non-zero number (subnormal) - 0x6230100010000FFEL, - 0x6230100010000FFFL, - 0x6230100010001000L, - 0x403CE0FFFFFFFFF0L, // has single digit round trip error - 0x2B2BFFFF10001079L, - }; - for (int i = 0; i < rawValues.length; i++) { - confirmRoundTrip(i, rawValues[i]); - } - } - - public static void confirmRoundTrip(int i, long rawBitsA) { + @ParameterizedTest + @ValueSource(longs = { + 0x4010000000000004L, + 0x7010000000000004L, + 0x1010000000000004L, + 0x0010000000000001L, // near lowest normal number + 0x0010000000000000L, // lowest normal number + 0x000FFFFFFFFFFFFFL, // highest subnormal number + 0x0008000000000000L, // subnormal number + + 0xC010000000000004L, + 0xE230100010001004L, + 0x403CE0FFFFFFFFF2L, + 0x0000000000000001L, // smallest non-zero number (subnormal) + 0x6230100010000FFEL, + 0x6230100010000FFFL, + 0x6230100010001000L, + 0x403CE0FFFFFFFFF0L, // has single digit round trip error + 0x2B2BFFFF10001079L, + }) + void confirmRoundTrip(long rawBitsA) { double a = Double.longBitsToDouble(rawBitsA); if (a == 0.0) { // Can't represent 0.0 or -0.0 with NormalisedDecimal diff --git a/src/testcases/org/apache/poi/ss/util/TestNumberComparer.java b/src/testcases/org/apache/poi/ss/util/TestNumberComparer.java index 42a0edd841..225d9f835c 100644 --- a/src/testcases/org/apache/poi/ss/util/TestNumberComparer.java +++ b/src/testcases/org/apache/poi/ss/util/TestNumberComparer.java @@ -17,6 +17,7 @@ package org.apache.poi.ss.util; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -24,6 +25,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.poi.ss.util.NumberComparisonExamples.ComparisonExample; import org.apache.poi.util.HexDump; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests for {@link NumberComparer} @@ -46,22 +49,15 @@ final class TestNumberComparer { assertTrue(success, "One or more cases failed. See stderr"); } - @Test - void testRoundTripOnComparisonExamples() { - ComparisonExample[] examples = NumberComparisonExamples.getComparisonExamples(); - for(int i=0;i<examples.length; i++) { - ComparisonExample ce = examples[i]; - confirmRoundTrip(i, ce.getA()); - confirmRoundTrip(i, ce.getNegA()); - confirmRoundTrip(i, ce.getB()); - confirmRoundTrip(i, ce.getNegB()); + @ParameterizedTest + @MethodSource("org.apache.poi.ss.util.NumberComparisonExamples#getComparisonExamples") + void testRoundTripOnComparisonExamples(ComparisonExample ce) { + double[] vals = { ce.getA(), ce.getNegA(), ce.getB(), ce.getNegB() }; + for (double a : vals) { + assertDoesNotThrow(() -> new TestExpandedDouble().confirmRoundTrip(Double.doubleToLongBits(a))); } } - private void confirmRoundTrip(int i, double a) { - TestExpandedDouble.confirmRoundTrip(i, Double.doubleToLongBits(a)); - } - /** * The actual example from bug 47598 */ diff --git a/src/testcases/org/apache/poi/ss/util/TestSheetUtil.java b/src/testcases/org/apache/poi/ss/util/TestSheetUtil.java index c61ba56401..1731d46371 100644 --- a/src/testcases/org/apache/poi/ss/util/TestSheetUtil.java +++ b/src/testcases/org/apache/poi/ss/util/TestSheetUtil.java @@ -17,6 +17,7 @@ package org.apache.poi.ss.util; +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.assertNull; @@ -93,7 +94,7 @@ final class TestSheetUtil { void testCanComputeWidthHSSF() throws IOException { try (Workbook wb = new HSSFWorkbook()) { // cannot check on result because on some machines we get back false here! - SheetUtil.canComputeColumnWidth(wb.getFontAt(0)); + assertDoesNotThrow(() -> SheetUtil.canComputeColumnWidth(wb.getFontAt(0))); } } diff --git a/src/testcases/org/apache/poi/util/TestHexDump.java b/src/testcases/org/apache/poi/util/TestHexDump.java index 4b87b976ae..4a465cb4be 100644 --- a/src/testcases/org/apache/poi/util/TestHexDump.java +++ b/src/testcases/org/apache/poi/util/TestHexDump.java @@ -30,6 +30,8 @@ import java.io.UnsupportedEncodingException; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; class TestHexDump { @@ -168,21 +170,18 @@ class TestHexDump { assertTrue(dump.contains("123456789:;<=>?@"), "Had: \n" + dump); } - @Test - void testDumpToStringOutOfIndex1() { - assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, -1)); - assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, 2)); - assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, 1)); + @ParameterizedTest + @ValueSource(ints = {-1, 2, 1}) + void testDumpToStringOutOfIndex1(int index) { + assertThrows(ArrayIndexOutOfBoundsException.class, () -> + HexDump.dump(new byte[1], 0, index)); } - @Test - void testDumpToStringNoDataEOL1() { - HexDump.dump(new byte[0], 0, 1); - } - - @Test - void testDumpToStringNoDataEOL2() { - HexDump.dump(new byte[0], 0, 0); + @ParameterizedTest + @ValueSource(ints = {0, 1}) + void testDumpToStringNoDataEOL(int index) { + String s = HexDump.dump(new byte[0], 0, index); + assertEquals("No Data", s.trim()); } private static byte[] testArray() { diff --git a/src/testcases/org/apache/poi/util/TestIOUtils.java b/src/testcases/org/apache/poi/util/TestIOUtils.java index 196fcf401b..c39efacfe2 100644 --- a/src/testcases/org/apache/poi/util/TestIOUtils.java +++ b/src/testcases/org/apache/poi/util/TestIOUtils.java @@ -181,7 +181,8 @@ final class TestIOUtils { @Test void testSkipFullyBug61294() throws IOException { - IOUtils.skipFully(new ByteArrayInputStream(new byte[0]), 1); + long skipped = IOUtils.skipFully(new ByteArrayInputStream(new byte[0]), 1); + assertEquals(-1L, skipped); } @Test @@ -215,9 +216,12 @@ final class TestIOUtils { @Test void testMaxLengthIgnored() throws IOException { try (InputStream is = new FileInputStream(TMP)) { - IOUtils.toByteArray(is, 90, Integer.MAX_VALUE); - IOUtils.toByteArray(is, 90, 100); - IOUtils.toByteArray(is, Integer.MAX_VALUE, Integer.MAX_VALUE); + int len = IOUtils.toByteArray(is, 90, Integer.MAX_VALUE).length; + assertEquals(90, len); + len = IOUtils.toByteArray(is, 90, 100).length; + assertEquals(90, len); + len = IOUtils.toByteArray(is, Integer.MAX_VALUE, Integer.MAX_VALUE).length; + assertTrue(len > 300-2*90); } } |