Sonar fixes

add asserts to tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885585 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2021-01-17 00:50:47 +00:00
parent 44efecf42e
commit 020f33a940
9 changed files with 169 additions and 218 deletions

View File

@ -19,6 +19,7 @@ package org.apache.poi.ss.usermodel;
import static org.apache.poi.POITestCase.skipTest; import static org.apache.poi.POITestCase.skipTest;
import static org.apache.poi.POITestCase.testPassesNow; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -124,12 +125,12 @@ public abstract class BaseTestSheetShiftRows {
*/ */
@Test @Test
public final void testShiftRow() throws IOException { public final void testShiftRow() throws IOException {
Workbook wb = _testDataProvider.createWorkbook(); try (Workbook wb = _testDataProvider.createWorkbook()) {
Sheet s = wb.createSheet(); Sheet s = wb.createSheet();
s.createRow(0).createCell(0).setCellValue("TEST1"); s.createRow(0).createCell(0).setCellValue("TEST1");
s.createRow(3).createCell(0).setCellValue("TEST2"); s.createRow(3).createCell(0).setCellValue("TEST2");
s.shiftRows(0,4,1); assertDoesNotThrow(() -> s.shiftRows(0, 4, 1));
wb.close(); }
} }
/** /**
@ -491,13 +492,12 @@ public abstract class BaseTestSheetShiftRows {
@Test @Test
void test47169() throws IOException { void test47169() throws IOException {
Workbook wb = _testDataProvider.createWorkbook(); try (Workbook wb = _testDataProvider.createWorkbook()) {
Sheet sheet = wb.createSheet(); Sheet sheet = wb.createSheet();
sheet.createRow(30); sheet.createRow(30);
sheet.shiftRows(29, 29, 1, true, true); sheet.shiftRows(29, 29, 1, true, true);
sheet.createRow(30); assertDoesNotThrow(() -> sheet.createRow(30));
}
wb.close();
} }
/** /**

View File

@ -18,6 +18,7 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -765,8 +766,7 @@ public abstract class BaseTestWorkbook {
@Test @Test
void test58499() throws IOException { void test58499() throws IOException {
try (Workbook workbook = _testDataProvider.createWorkbook(); try (Workbook workbook = _testDataProvider.createWorkbook()) {
OutputStream os = new NullOutputStream()) {
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
for (int i = 0; i < 900; i++) { for (int i = 0; i < 900; i++) {
Row r = sheet.createRow(i); Row r = sheet.createRow(i);
@ -775,7 +775,7 @@ public abstract class BaseTestWorkbook {
c.setCellStyle(cs); c.setCellStyle(cs);
c.setCellValue("AAA"); c.setCellValue("AAA");
} }
workbook.write(os); assertDoesNotThrow(() -> workbook.write(new NullOutputStream()));
} }
} }

View File

@ -17,6 +17,7 @@
package org.apache.poi.ss.usermodel; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -25,6 +26,7 @@ import java.text.DateFormatSymbols;
import java.text.FieldPosition; import java.text.FieldPosition;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -35,111 +37,88 @@ import java.util.stream.Stream;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.jupiter.api.Test; 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 { class TestExcelStyleDateFormatter {
private static final String EXCEL_DATE_FORMAT = "MMMMM"; private static final String EXCEL_DATE_FORMAT = "MMMMM";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT); private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
private final int jreVersion; private static final int jreVersion =
Integer.parseInt(System.getProperty("java.version").replaceAll("^(?:1\\.)?(\\d+).*", "$1"));
public TestExcelStyleDateFormatter() { private static final String provider = System.getProperty("java.locale.providers");
jreVersion = Integer.parseInt(System.getProperty("java.version") private static final FieldPosition fp = new FieldPosition(java.text.DateFormat.MONTH_FIELD);
.replace("1.8", "8").replaceAll("(\\d+).*", "$1")); private static final ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT);
}
/** /**
* [Bug 60369] Month format 'MMMMM' issue with TEXT-formula and Java 8 * [Bug 60369] Month format 'MMMMM' issue with TEXT-formula and Java 8
*/ */
@Test @ParameterizedTest
void test60369() { @MethodSource("initializeLocales")
Map<Locale, String> testMap = initializeLocales(); void test60369(Locale locale, String expected, Date d, int month) {
// 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());
// Let's iterate over the test setup. // 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(); final StringBuffer sb = new StringBuffer();
for (Map.Entry<Locale,String> me : testMap.entrySet()) { formatter.setDateFormatSymbols(DateFormatSymbols.getInstance(locale));
final Locale locale = me.getKey(); String result = formatter.format(d, sb, fp).toString();
final String expected = me.getValue(); String msg = "Failed testDates for locale " + locale + ", provider: " + provider +
formatter.setDateFormatSymbols(DateFormatSymbols.getInstance(locale)); " and date " + d + ", having: " + result;
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); int actIdx = localeIndex(locale);
assertNotNull(result, msg); assertNotNull(result, msg);
assertTrue(result.length() > actIdx, msg); assertTrue(result.length() > actIdx, msg);
assertEquals(expected.charAt(month), result.charAt(actIdx), msg); assertEquals(expected.charAt(month), result.charAt(actIdx), msg);
month++;
}
}
} }
/** /**
* Depending on the JRE version, the provider setting and the locale, a different result * Depending on the JRE version, the provider setting and the locale, a different result
* is expected and selected via an index * is expected and selected via an index
*/ */
private int localeIndex(Locale locale) { private static int localeIndex(Locale locale) {
final String provider = System.getProperty("java.locale.providers");
return jreVersion < 9 || return jreVersion < 9 ||
!locale.equals (Locale.CHINESE) || !locale.equals (Locale.CHINESE) ||
(provider != null && (provider.startsWith("JRE") || provider.startsWith("COMPAT"))) (provider != null && (provider.startsWith("JRE") || provider.startsWith("COMPAT")))
? 0 : 1; ? 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 * Setting up the locale to be tested together with a list of asserted
* unicode-formatted results and put them in a map. * unicode-formatted results and put them in a map.
*/ */
private Map<Locale, String> initializeLocales() { public static Stream<Arguments> initializeLocales() throws ParseException {
Map<Locale, String> testMap = new HashMap<>(); 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" }
};
testMap.put(Locale.GERMAN, "JFMAMJJASOND"); String[] dates = {
testMap.put(new Locale("de", "AT"), "JFMAMJJASOND"); "1980-01-12", "1995-02-11", "2045-03-10", "2016-04-09", "2017-05-08",
testMap.put(Locale.UK, "JFMAMJJASOND"); "1945-06-07", "1998-07-06", "2099-08-05", "1988-09-04", "2023-10-03", "1978-11-02", "1890-12-01"
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"), List<Arguments> list = new ArrayList<>(locExps.length * dates.length);
"\u044f\u0444\u043c\u0430\u043c\u0438\u0438\u0430\u0441\u043e\u043d\u0434"); for (Object[] locExp : locExps) {
int month = 0;
testMap.put(Locale.CHINESE, new String[]{ for (String date : dates) {
"\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341\u5341\u5341", list.add(Arguments.of(locExp[0], locExp[1], DATE_FORMAT.parse(date), month++));
"123456789111" }
}[localeIndex(Locale.CHINESE)]); }
return list.stream();
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;
} }
@Test @Test
void testConstruct() { void testConstruct() {
new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT, LocaleUtil.getUserLocale()); assertDoesNotThrow(() -> new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT, LocaleUtil.getUserLocale()));
new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT); assertDoesNotThrow(() -> new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT));
} }
@Test @Test

View File

@ -19,6 +19,9 @@
package org.apache.poi.ss.util; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; 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.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.LocaleID; import org.apache.poi.util.LocaleID;
import org.apache.poi.util.NullOutputStream;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
final class TestDateFormatConverter { 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()) { try (Workbook workbook = new HSSFWorkbook()) {
String sheetName; String sheetName = (dates) ? ((times) ? "DateTimes" : "Dates") : "Times";
if (dates) {
if (times) {
sheetName = "DateTimes";
} else {
sheetName = "Dates";
}
} else {
sheetName = "Times";
}
Sheet sheet = workbook.createSheet(sheetName); Sheet sheet = workbook.createSheet(sheetName);
Row header = sheet.createRow(0); Row header = sheet.createRow(0);
header.createCell(0).setCellValue("locale"); header.createCell(0).setCellValue("locale");
@ -73,90 +88,51 @@ final class TestDateFormatConverter {
int rowNum = 1; int rowNum = 1;
for (Locale locale : DateFormat.getAvailableLocales()) { for (Locale locale : DateFormat.getAvailableLocales()) {
try { Row row = sheet.createRow(rowNum++);
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(locale.toString()); row.createCell(0).setCellValue(locale.toString());
row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT)); row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT));
DateFormat dateFormat; DateFormat dateFormat = (dates)
if (dates) { ? (times ? getDateTimeInstance(style, style, locale) : getDateInstance(style, locale))
if (times) { : getTimeInstance(style, locale);
dateFormat = DateFormat.getDateTimeInstance(style, style, locale);
} else {
dateFormat = DateFormat.getDateInstance(style, locale);
}
} else {
dateFormat = DateFormat.getTimeInstance(style, locale);
}
Cell cell = row.createCell(2); Cell cell = row.createCell(2);
Date date = new Date();
cell.setCellValue(date);
CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
cell.setCellValue(date); String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern(); DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
row.createCell(3).setCellValue(dateFormat.format(date));
DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); cell.setCellStyle(cellStyle);
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);
// the formula returns TRUE is the formatted date in column C equals to the string in column D row.createCell(5).setCellValue(javaDateFormatPattern);
row.createCell(4).setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum); row.createCell(6).setCellValue(excelFormatPattern);
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);
}
} }
File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx"); workbook.write(new NullOutputStream());
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
workbook.write(outputStream);
}
//System.out.println("Open " + outputFile.getAbsolutePath() + " in Excel");
} }
} }
@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 @Test
void testJDK8EmptyLocale() { void testJDK8EmptyLocale() {
// JDK 8 seems to add an empty locale-string to the list returned via DateFormat.getAvailableLocales() // 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 // therefore we now cater for this special locale as well
DateFormatConverter.getPrefixForLocale(new Locale("")); String prefix = DateFormatConverter.getPrefixForLocale(new Locale(""));
assertEquals("", prefix);
} }
@Test @Test
void testJDK11MyLocale() { void testJDK11MyLocale() {
DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.forLanguageTag("my")); DateFormat df = getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.forLanguageTag("my"));
assertNotNull(df);
} }
@Test @Test

View File

@ -24,6 +24,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigInteger; import java.math.BigInteger;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
/** /**
* Tests for {@link ExpandedDouble} * Tests for {@link ExpandedDouble}
@ -54,33 +56,27 @@ final class TestExpandedDouble {
/** /**
* Tests specific values for conversion from {@link ExpandedDouble} to {@link NormalisedDecimal} and back * Tests specific values for conversion from {@link ExpandedDouble} to {@link NormalisedDecimal} and back
*/ */
@Test @ParameterizedTest
void testRoundTripShifting() { @ValueSource(longs = {
long[] rawValues = { 0x4010000000000004L,
0x4010000000000004L, 0x7010000000000004L,
0x7010000000000004L, 0x1010000000000004L,
0x1010000000000004L, 0x0010000000000001L, // near lowest normal number
0x0010000000000001L, // near lowest normal number 0x0010000000000000L, // lowest normal number
0x0010000000000000L, // lowest normal number 0x000FFFFFFFFFFFFFL, // highest subnormal number
0x000FFFFFFFFFFFFFL, // highest subnormal number 0x0008000000000000L, // subnormal number
0x0008000000000000L, // subnormal number
0xC010000000000004L, 0xC010000000000004L,
0xE230100010001004L, 0xE230100010001004L,
0x403CE0FFFFFFFFF2L, 0x403CE0FFFFFFFFF2L,
0x0000000000000001L, // smallest non-zero number (subnormal) 0x0000000000000001L, // smallest non-zero number (subnormal)
0x6230100010000FFEL, 0x6230100010000FFEL,
0x6230100010000FFFL, 0x6230100010000FFFL,
0x6230100010001000L, 0x6230100010001000L,
0x403CE0FFFFFFFFF0L, // has single digit round trip error 0x403CE0FFFFFFFFF0L, // has single digit round trip error
0x2B2BFFFF10001079L, 0x2B2BFFFF10001079L,
}; })
for (int i = 0; i < rawValues.length; i++) { void confirmRoundTrip(long rawBitsA) {
confirmRoundTrip(i, rawValues[i]);
}
}
public static void confirmRoundTrip(int i, long rawBitsA) {
double a = Double.longBitsToDouble(rawBitsA); double a = Double.longBitsToDouble(rawBitsA);
if (a == 0.0) { if (a == 0.0) {
// Can't represent 0.0 or -0.0 with NormalisedDecimal // Can't represent 0.0 or -0.0 with NormalisedDecimal

View File

@ -17,6 +17,7 @@
package org.apache.poi.ss.util; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; 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.ss.util.NumberComparisonExamples.ComparisonExample;
import org.apache.poi.util.HexDump; import org.apache.poi.util.HexDump;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
/** /**
* Tests for {@link NumberComparer} * Tests for {@link NumberComparer}
@ -46,22 +49,15 @@ final class TestNumberComparer {
assertTrue(success, "One or more cases failed. See stderr"); assertTrue(success, "One or more cases failed. See stderr");
} }
@Test @ParameterizedTest
void testRoundTripOnComparisonExamples() { @MethodSource("org.apache.poi.ss.util.NumberComparisonExamples#getComparisonExamples")
ComparisonExample[] examples = NumberComparisonExamples.getComparisonExamples(); void testRoundTripOnComparisonExamples(ComparisonExample ce) {
for(int i=0;i<examples.length; i++) { double[] vals = { ce.getA(), ce.getNegA(), ce.getB(), ce.getNegB() };
ComparisonExample ce = examples[i]; for (double a : vals) {
confirmRoundTrip(i, ce.getA()); assertDoesNotThrow(() -> new TestExpandedDouble().confirmRoundTrip(Double.doubleToLongBits(a)));
confirmRoundTrip(i, ce.getNegA());
confirmRoundTrip(i, ce.getB());
confirmRoundTrip(i, ce.getNegB());
} }
} }
private void confirmRoundTrip(int i, double a) {
TestExpandedDouble.confirmRoundTrip(i, Double.doubleToLongBits(a));
}
/** /**
* The actual example from bug 47598 * The actual example from bug 47598
*/ */

View File

@ -17,6 +17,7 @@
package org.apache.poi.ss.util; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
@ -93,7 +94,7 @@ final class TestSheetUtil {
void testCanComputeWidthHSSF() throws IOException { void testCanComputeWidthHSSF() throws IOException {
try (Workbook wb = new HSSFWorkbook()) { try (Workbook wb = new HSSFWorkbook()) {
// cannot check on result because on some machines we get back false here! // cannot check on result because on some machines we get back false here!
SheetUtil.canComputeColumnWidth(wb.getFontAt(0)); assertDoesNotThrow(() -> SheetUtil.canComputeColumnWidth(wb.getFontAt(0)));
} }
} }

View File

@ -30,6 +30,8 @@ import java.io.UnsupportedEncodingException;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class TestHexDump { class TestHexDump {
@ -168,21 +170,18 @@ class TestHexDump {
assertTrue(dump.contains("123456789:;<=>?@"), "Had: \n" + dump); assertTrue(dump.contains("123456789:;<=>?@"), "Had: \n" + dump);
} }
@Test @ParameterizedTest
void testDumpToStringOutOfIndex1() { @ValueSource(ints = {-1, 2, 1})
assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, -1)); void testDumpToStringOutOfIndex1(int index) {
assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, 2)); assertThrows(ArrayIndexOutOfBoundsException.class, () ->
assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, 1)); HexDump.dump(new byte[1], 0, index));
} }
@Test @ParameterizedTest
void testDumpToStringNoDataEOL1() { @ValueSource(ints = {0, 1})
HexDump.dump(new byte[0], 0, 1); void testDumpToStringNoDataEOL(int index) {
} String s = HexDump.dump(new byte[0], 0, index);
assertEquals("No Data", s.trim());
@Test
void testDumpToStringNoDataEOL2() {
HexDump.dump(new byte[0], 0, 0);
} }
private static byte[] testArray() { private static byte[] testArray() {

View File

@ -181,7 +181,8 @@ final class TestIOUtils {
@Test @Test
void testSkipFullyBug61294() throws IOException { 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 @Test
@ -215,9 +216,12 @@ final class TestIOUtils {
@Test @Test
void testMaxLengthIgnored() throws IOException { void testMaxLengthIgnored() throws IOException {
try (InputStream is = new FileInputStream(TMP)) { try (InputStream is = new FileInputStream(TMP)) {
IOUtils.toByteArray(is, 90, Integer.MAX_VALUE); int len = IOUtils.toByteArray(is, 90, Integer.MAX_VALUE).length;
IOUtils.toByteArray(is, 90, 100); assertEquals(90, len);
IOUtils.toByteArray(is, Integer.MAX_VALUE, Integer.MAX_VALUE); 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);
} }
} }