Adjust some tests for Java 19 and Java 20

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903105 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2022-07-29 17:07:19 +00:00
parent 4df10cf9dd
commit af4d1e9f1a
4 changed files with 27 additions and 7 deletions

View File

@ -157,4 +157,14 @@ public final class POITestCase {
} }
ImageIO.setCacheDirectory(tmpDir); ImageIO.setCacheDirectory(tmpDir);
} }
/**
* Returns the major version of Java as simple integer, i.e.
* "8" for JDK 1.8, "11" for JDK 11, "12" for JDK 12 and so on.
*
* @return The major version of Java
*/
public static int getJDKVersion() {
return Integer.parseInt(System.getProperty("java.version").replaceAll("^(?:1\\.)?(\\d+).*", "$1"));
}
} }

View File

@ -30,6 +30,7 @@ import java.io.UnsupportedEncodingException;
import java.security.Permission; import java.security.Permission;
import org.apache.commons.io.output.NullPrintStream; import org.apache.commons.io.output.NullPrintStream;
import org.apache.poi.POITestCase;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.filesystem.NotOLE2FileException; import org.apache.poi.poifs.filesystem.NotOLE2FileException;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
@ -168,8 +169,7 @@ public class TestPOIFSDump {
@Test @Test
@SuppressForbidden("tests java.security features deprecated in java 17 - no other option though") @SuppressForbidden("tests java.security features deprecated in java 17 - no other option though")
void testMainNoArgs() { void testMainNoArgs() {
final String javaVersion = System.getProperty("java.version"); Assumptions.assumeFalse(POITestCase.getJDKVersion() >= 18,
Assumptions.assumeFalse(javaVersion.startsWith("18") || javaVersion.startsWith("19") || javaVersion.startsWith("2"),
"SecurityManager does not work any more since JDK 18"); "SecurityManager does not work any more since JDK 18");
SecurityManager sm = System.getSecurityManager(); SecurityManager sm = System.getSecurityManager();

View File

@ -35,6 +35,7 @@ import java.util.Locale;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.poi.POITestCase;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter; import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter;
@ -774,13 +775,14 @@ class TestDataFormatter {
assertTrue(DateUtil.isADateFormat(-1, "[h]")); assertTrue(DateUtil.isADateFormat(-1, "[h]"));
} }
@Test @Test
void testLargeNumbersAndENotation() throws IOException{ void testLargeNumbersAndENotation() throws IOException{
assertFormatsTo("1E+86", 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999d); assertFormatsTo("1E+86", 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999d);
assertFormatsTo("1E-84", 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000001d); assertFormatsTo("1E-84", 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000001d);
// Smallest double // Smallest double
assertFormatsTo("1E-323", 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001d); // See https://bugs.openjdk.org/browse/JDK-8291240
assertFormatsTo(POITestCase.getJDKVersion() >= 19 ? "9.9E-324" : "1E-323",
0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001d);
// "up to 11 numeric characters, with the decimal point counting as a numeric character" // "up to 11 numeric characters, with the decimal point counting as a numeric character"
// https://support.microsoft.com/en-us/kb/65903 // https://support.microsoft.com/en-us/kb/65903
@ -821,10 +823,18 @@ class TestDataFormatter {
DataFormat dataFormat = wb.createDataFormat(); DataFormat dataFormat = wb.createDataFormat();
newStyle.setDataFormat(dataFormat.getFormat("General")); newStyle.setDataFormat(dataFormat.getFormat("General"));
String actual = new DataFormatter().formatCellValue(rawValue); String actual = new DataFormatter().formatCellValue(rawValue);
assertEquals(expected, actual); assertEquals(expected, actual,
"Failed for input " + input);
} }
} }
@Test
public void testJDK19() {
// See https://bugs.openjdk.org/browse/JDK-8291240
assertEquals(POITestCase.getJDKVersion() >= 19 ? "9.9E-324" : "1.0E-323",
Double.toString(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001d));
}
@Test @Test
void testFormulaEvaluation() throws IOException { void testFormulaEvaluation() throws IOException {
try (Workbook wb = HSSFTestDataSamples.openSampleWorkbook("FormulaEvalTestData.xls")) { try (Workbook wb = HSSFTestDataSamples.openSampleWorkbook("FormulaEvalTestData.xls")) {

View File

@ -32,6 +32,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.poi.POITestCase;
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.ParameterizedTest;
@ -41,8 +42,7 @@ 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 static final int jreVersion = private static final int jreVersion = POITestCase.getJDKVersion();
Integer.parseInt(System.getProperty("java.version").replaceAll("^(?:1\\.)?(\\d+).*", "$1"));
private static final String provider = System.getProperty("java.locale.providers"); 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 FieldPosition fp = new FieldPosition(java.text.DateFormat.MONTH_FIELD);
private static final ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT); private static final ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT);