From 6e797a40a7fb7d0e5abf555c0d179dd3937bdecb Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Fri, 5 Apr 2019 18:33:35 +0200 Subject: [PATCH] LANG-1443: Add more SystemUtils.IS_JAVA_XX variants (#415) --- src/changes/changes.xml | 3 +- .../org/apache/commons/lang3/JavaVersion.java | 18 +++ .../org/apache/commons/lang3/SystemUtils.java | 24 ++++ .../apache/commons/lang3/JavaVersionTest.java | 19 +-- .../apache/commons/lang3/SystemUtilsTest.java | 114 ++++++++++++++++++ 5 files changed, 170 insertions(+), 8 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ed0a6355e..fa8864e67 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,7 +46,8 @@ The type attribute can be add,update,fix,remove. - Adding the Functions class. + Add more SystemUtils.IS_JAVA_XX variants. + Adding the Functions class. Update to JUnit 5 Add @FunctionalInterface to ThreadPredicate and ThreadGroupPredicate Update Java Language requirement to 1.8 diff --git a/src/main/java/org/apache/commons/lang3/JavaVersion.java b/src/main/java/org/apache/commons/lang3/JavaVersion.java index aacd3a16e..c860519a8 100644 --- a/src/main/java/org/apache/commons/lang3/JavaVersion.java +++ b/src/main/java/org/apache/commons/lang3/JavaVersion.java @@ -101,6 +101,20 @@ public enum JavaVersion { */ JAVA_11(11.0f, "11"), + /** + * Java 12 + * + * @since 3.9 + */ + JAVA_12(12.0f, "12"), + + /** + * Java 13 + * + * @since 3.9 + */ + JAVA_13(13.0f, "13"), + /** * The most recent java version. Mainly introduced to avoid to break when a new version of Java is used. */ @@ -204,6 +218,10 @@ public enum JavaVersion { return JAVA_10; } else if ("11".equals(nom)) { return JAVA_11; + } else if ("12".equals(nom)) { + return JAVA_12; + } else if ("13".equals(nom)) { + return JAVA_13; } if (nom == null) { return null; diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java b/src/main/java/org/apache/commons/lang3/SystemUtils.java index cc51f1196..f5085cf4b 100644 --- a/src/main/java/org/apache/commons/lang3/SystemUtils.java +++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java @@ -997,6 +997,30 @@ public class SystemUtils { */ public static final boolean IS_JAVA_11 = getJavaVersionMatches("11"); + /** + *

+ * Is {@code true} if this is Java version 12 (also 12.x versions). + *

+ *

+ * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}. + *

+ * + * @since 3.9 + */ + public static final boolean IS_JAVA_12 = getJavaVersionMatches("12"); + + /** + *

+ * Is {@code true} if this is Java version 13 (also 13.x versions). + *

+ *

+ * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}. + *

+ * + * @since 3.9 + */ + public static final boolean IS_JAVA_13 = getJavaVersionMatches("13"); + // Operating system checks // ----------------------------------------------------------------------- // These MUST be declared after those above as they depend on the diff --git a/src/test/java/org/apache/commons/lang3/JavaVersionTest.java b/src/test/java/org/apache/commons/lang3/JavaVersionTest.java index 2bbb3c62e..64d8f820a 100644 --- a/src/test/java/org/apache/commons/lang3/JavaVersionTest.java +++ b/src/test/java/org/apache/commons/lang3/JavaVersionTest.java @@ -20,11 +20,11 @@ package org.apache.commons.lang3; import org.junit.jupiter.api.Test; -import static org.apache.commons.lang3.JavaVersion.JAVA_RECENT; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.apache.commons.lang3.JavaVersion.JAVA_0_9; +import static org.apache.commons.lang3.JavaVersion.JAVA_10; +import static org.apache.commons.lang3.JavaVersion.JAVA_11; +import static org.apache.commons.lang3.JavaVersion.JAVA_12; +import static org.apache.commons.lang3.JavaVersion.JAVA_13; import static org.apache.commons.lang3.JavaVersion.JAVA_1_1; import static org.apache.commons.lang3.JavaVersion.JAVA_1_2; import static org.apache.commons.lang3.JavaVersion.JAVA_1_3; @@ -34,9 +34,12 @@ import static org.apache.commons.lang3.JavaVersion.JAVA_1_6; import static org.apache.commons.lang3.JavaVersion.JAVA_1_7; import static org.apache.commons.lang3.JavaVersion.JAVA_1_8; import static org.apache.commons.lang3.JavaVersion.JAVA_9; -import static org.apache.commons.lang3.JavaVersion.JAVA_10; +import static org.apache.commons.lang3.JavaVersion.JAVA_RECENT; import static org.apache.commons.lang3.JavaVersion.get; import static org.apache.commons.lang3.JavaVersion.getJavaVersion; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Unit tests {@link org.apache.commons.lang3.JavaVersion}. @@ -56,11 +59,13 @@ public class JavaVersionTest { assertEquals(JAVA_1_8, get("1.8"), "1.8 failed"); assertEquals(JAVA_9, get("9"), "9 failed"); assertEquals(JAVA_10, get("10"), "10 failed"); - assertEquals(JavaVersion.JAVA_11, get("11"), "11 failed"); + assertEquals(JAVA_11, get("11"), "11 failed"); + assertEquals(JAVA_12, get("12"), "12 failed"); + assertEquals(JAVA_13, get("13"), "13 failed"); assertEquals(JAVA_RECENT, get("1.10"), "1.10 failed"); // assertNull("2.10 unexpectedly worked", get("2.10")); assertEquals(get("1.5"), getJavaVersion("1.5"), "Wrapper method failed"); - assertEquals(JAVA_RECENT, get("12"), "Unhandled"); // LANG-1384 + assertEquals(JAVA_RECENT, get("14"), "Unhandled"); // LANG-1384 } @Test diff --git a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java index 53b46b3be..d05053253 100644 --- a/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java @@ -30,6 +30,8 @@ import static org.apache.commons.lang3.JavaVersion.JAVA_1_8; import static org.apache.commons.lang3.JavaVersion.JAVA_9; import static org.apache.commons.lang3.JavaVersion.JAVA_10; import static org.apache.commons.lang3.JavaVersion.JAVA_11; +import static org.apache.commons.lang3.JavaVersion.JAVA_12; +import static org.apache.commons.lang3.JavaVersion.JAVA_13; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -141,6 +143,8 @@ public class SystemUtilsTest { assertFalse(SystemUtils.IS_JAVA_9); assertFalse(SystemUtils.IS_JAVA_10); assertFalse(SystemUtils.IS_JAVA_11); + assertFalse(SystemUtils.IS_JAVA_12); + assertFalse(SystemUtils.IS_JAVA_13); } else if (javaVersion.startsWith("1.8")) { assertFalse(SystemUtils.IS_JAVA_1_1); assertFalse(SystemUtils.IS_JAVA_1_2); @@ -154,6 +158,8 @@ public class SystemUtilsTest { assertFalse(SystemUtils.IS_JAVA_9); assertFalse(SystemUtils.IS_JAVA_10); assertFalse(SystemUtils.IS_JAVA_11); + assertFalse(SystemUtils.IS_JAVA_12); + assertFalse(SystemUtils.IS_JAVA_13); } else if (javaVersion.startsWith("9")) { assertFalse(SystemUtils.IS_JAVA_1_1); assertFalse(SystemUtils.IS_JAVA_1_2); @@ -167,6 +173,8 @@ public class SystemUtilsTest { assertTrue(SystemUtils.IS_JAVA_9); assertFalse(SystemUtils.IS_JAVA_10); assertFalse(SystemUtils.IS_JAVA_11); + assertFalse(SystemUtils.IS_JAVA_12); + assertFalse(SystemUtils.IS_JAVA_13); } else if (javaVersion.startsWith("10")) { assertFalse(SystemUtils.IS_JAVA_1_1); assertFalse(SystemUtils.IS_JAVA_1_2); @@ -180,6 +188,8 @@ public class SystemUtilsTest { assertFalse(SystemUtils.IS_JAVA_9); assertTrue(SystemUtils.IS_JAVA_10); assertFalse(SystemUtils.IS_JAVA_11); + assertFalse(SystemUtils.IS_JAVA_12); + assertFalse(SystemUtils.IS_JAVA_13); } else if (javaVersion.startsWith("11")) { assertFalse(SystemUtils.IS_JAVA_1_1); assertFalse(SystemUtils.IS_JAVA_1_2); @@ -193,6 +203,38 @@ public class SystemUtilsTest { assertFalse(SystemUtils.IS_JAVA_9); assertFalse(SystemUtils.IS_JAVA_10); assertTrue(SystemUtils.IS_JAVA_11); + assertFalse(SystemUtils.IS_JAVA_12); + assertFalse(SystemUtils.IS_JAVA_13); + } else if (javaVersion.startsWith("12")) { + assertFalse(SystemUtils.IS_JAVA_1_1); + assertFalse(SystemUtils.IS_JAVA_1_2); + assertFalse(SystemUtils.IS_JAVA_1_3); + assertFalse(SystemUtils.IS_JAVA_1_4); + assertFalse(SystemUtils.IS_JAVA_1_5); + assertFalse(SystemUtils.IS_JAVA_1_6); + assertFalse(SystemUtils.IS_JAVA_1_7); + assertFalse(SystemUtils.IS_JAVA_1_8); + assertFalse(SystemUtils.IS_JAVA_1_9); + assertFalse(SystemUtils.IS_JAVA_9); + assertFalse(SystemUtils.IS_JAVA_10); + assertFalse(SystemUtils.IS_JAVA_11); + assertTrue(SystemUtils.IS_JAVA_12); + assertFalse(SystemUtils.IS_JAVA_13); + } else if (javaVersion.startsWith("13")) { + assertFalse(SystemUtils.IS_JAVA_1_1); + assertFalse(SystemUtils.IS_JAVA_1_2); + assertFalse(SystemUtils.IS_JAVA_1_3); + assertFalse(SystemUtils.IS_JAVA_1_4); + assertFalse(SystemUtils.IS_JAVA_1_5); + assertFalse(SystemUtils.IS_JAVA_1_6); + assertFalse(SystemUtils.IS_JAVA_1_7); + assertFalse(SystemUtils.IS_JAVA_1_8); + assertFalse(SystemUtils.IS_JAVA_1_9); + assertFalse(SystemUtils.IS_JAVA_9); + assertFalse(SystemUtils.IS_JAVA_10); + assertFalse(SystemUtils.IS_JAVA_11); + assertFalse(SystemUtils.IS_JAVA_12); + assertTrue(SystemUtils.IS_JAVA_13); } else { System.out.println("Can't test IS_JAVA value: " + javaVersion); } @@ -433,6 +475,8 @@ public class SystemUtilsTest { assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_9)); assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_10)); assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_11)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_12)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_13)); } else if (SystemUtils.IS_JAVA_9) { assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_1)); assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_2)); @@ -445,6 +489,8 @@ public class SystemUtilsTest { assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_9)); assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_10)); assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_11)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_12)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_13)); } else if (SystemUtils.IS_JAVA_10) { assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_1)); assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_2)); @@ -457,6 +503,8 @@ public class SystemUtilsTest { assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_9)); assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_10)); assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_11)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_12)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_13)); } else if (SystemUtils.IS_JAVA_11) { assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_1)); assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_2)); @@ -469,6 +517,36 @@ public class SystemUtilsTest { assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_9)); assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_10)); assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_11)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_12)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_13)); + } else if (SystemUtils.IS_JAVA_12) { + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_1)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_2)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_3)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_4)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_5)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_6)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_7)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_8)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_9)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_10)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_11)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_12)); + assertFalse(SystemUtils.isJavaVersionAtLeast(JAVA_13)); + } else if (SystemUtils.IS_JAVA_13) { + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_1)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_2)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_3)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_4)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_5)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_6)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_7)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_1_8)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_9)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_10)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_11)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_12)); + assertTrue(SystemUtils.isJavaVersionAtLeast(JAVA_13)); } } @@ -486,6 +564,8 @@ public class SystemUtilsTest { assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_9)); assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_10)); assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_11)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_12)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_13)); } else if (SystemUtils.IS_JAVA_9) { assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_1)); assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_2)); @@ -498,6 +578,8 @@ public class SystemUtilsTest { assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_9)); assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_10)); assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_11)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_12)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_13)); } else if (SystemUtils.IS_JAVA_10) { assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_1)); assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_2)); @@ -510,6 +592,8 @@ public class SystemUtilsTest { assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_9)); assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_10)); assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_11)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_12)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_13)); } else if (SystemUtils.IS_JAVA_11) { assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_1)); assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_2)); @@ -522,6 +606,36 @@ public class SystemUtilsTest { assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_9)); assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_10)); assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_11)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_12)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_13)); + } else if (SystemUtils.IS_JAVA_12) { + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_1)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_2)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_3)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_4)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_5)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_6)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_7)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_8)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_9)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_10)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_11)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_12)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_13)); + } else if (SystemUtils.IS_JAVA_13) { + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_1)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_2)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_3)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_4)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_5)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_6)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_7)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_1_8)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_9)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_10)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_11)); + assertFalse(SystemUtils.isJavaVersionAtMost(JAVA_12)); + assertTrue(SystemUtils.isJavaVersionAtMost(JAVA_13)); } }