From af4d1e9f1a5e5b78c242fefa5aae25c22c68b0f4 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Fri, 29 Jul 2022 17:07:19 +0000 Subject: [PATCH] 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 --- .../test/java/org/apache/poi/POITestCase.java | 10 ++++++++++ .../org/apache/poi/poifs/dev/TestPOIFSDump.java | 4 ++-- .../poi/ss/usermodel/TestDataFormatter.java | 16 +++++++++++++--- .../usermodel/TestExcelStyleDateFormatter.java | 4 ++-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/poi/src/test/java/org/apache/poi/POITestCase.java b/poi/src/test/java/org/apache/poi/POITestCase.java index 4f43cb216c..d4c99bebdb 100644 --- a/poi/src/test/java/org/apache/poi/POITestCase.java +++ b/poi/src/test/java/org/apache/poi/POITestCase.java @@ -157,4 +157,14 @@ public final class POITestCase { } 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")); + } } diff --git a/poi/src/test/java/org/apache/poi/poifs/dev/TestPOIFSDump.java b/poi/src/test/java/org/apache/poi/poifs/dev/TestPOIFSDump.java index 3a8455e3e0..ad58573764 100644 --- a/poi/src/test/java/org/apache/poi/poifs/dev/TestPOIFSDump.java +++ b/poi/src/test/java/org/apache/poi/poifs/dev/TestPOIFSDump.java @@ -30,6 +30,7 @@ import java.io.UnsupportedEncodingException; import java.security.Permission; import org.apache.commons.io.output.NullPrintStream; +import org.apache.poi.POITestCase; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.poifs.filesystem.NotOLE2FileException; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; @@ -168,8 +169,7 @@ public class TestPOIFSDump { @Test @SuppressForbidden("tests java.security features deprecated in java 17 - no other option though") void testMainNoArgs() { - final String javaVersion = System.getProperty("java.version"); - Assumptions.assumeFalse(javaVersion.startsWith("18") || javaVersion.startsWith("19") || javaVersion.startsWith("2"), + Assumptions.assumeFalse(POITestCase.getJDKVersion() >= 18, "SecurityManager does not work any more since JDK 18"); SecurityManager sm = System.getSecurityManager(); diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java index 8d420462c1..0d6c1af5b9 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -35,6 +35,7 @@ import java.util.Locale; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; +import org.apache.poi.POITestCase; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter; @@ -774,13 +775,14 @@ class TestDataFormatter { assertTrue(DateUtil.isADateFormat(-1, "[h]")); } - @Test void testLargeNumbersAndENotation() throws IOException{ assertFormatsTo("1E+86", 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999d); assertFormatsTo("1E-84", 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000001d); // 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" // https://support.microsoft.com/en-us/kb/65903 @@ -821,10 +823,18 @@ class TestDataFormatter { DataFormat dataFormat = wb.createDataFormat(); newStyle.setDataFormat(dataFormat.getFormat("General")); 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 void testFormulaEvaluation() throws IOException { try (Workbook wb = HSSFTestDataSamples.openSampleWorkbook("FormulaEvalTestData.xls")) { diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java b/poi/src/test/java/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java index ad86341a26..0588c75a77 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Locale; import java.util.stream.Stream; +import org.apache.poi.POITestCase; import org.apache.poi.util.LocaleUtil; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -41,8 +42,7 @@ 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 static final int jreVersion = - Integer.parseInt(System.getProperty("java.version").replaceAll("^(?:1\\.)?(\\d+).*", "$1")); + private static final int jreVersion = POITestCase.getJDKVersion(); 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); -- 2.39.5