commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1612075 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/JavaVersion.java test/java/org/apache/commons/lang3/JavaVersionTest.java
Date Sun, 20 Jul 2014 11:31:18 GMT
Author: rmannibucau
Date: Sun Jul 20 11:31:17 2014
New Revision: 1612075

URL: http://svn.apache.org/r1612075
Log:
LANG-1027 don't fail if java version is unknown, lang is too much used to make apps and libs
failling cause of an unknown java version.

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/JavaVersion.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/JavaVersionTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/JavaVersion.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/JavaVersion.java?rev=1612075&r1=1612074&r2=1612075&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/JavaVersion.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/JavaVersion.java Sun
Jul 20 11:31:17 2014
@@ -69,7 +69,17 @@ public enum JavaVersion {
     /**
      * Java 1.8.
      */
-    JAVA_1_8(1.8f, "1.8");
+    JAVA_1_8(1.8f, "1.8"),
+
+    /**
+     * Java 1.9.
+     */
+    JAVA_1_9(1.9f, "1.9"),
+
+    /**
+     * Java 1.x, x > 9. Mainly introduced to avoid to break when a new version of Java
is used.
+     */
+    JAVA_RECENT(maxVersion(), Float.toString(maxVersion()));
 
     /**
      * The float value.
@@ -147,9 +157,21 @@ public enum JavaVersion {
             return JAVA_1_7;
         } else if ("1.8".equals(nom)) {
             return JAVA_1_8;
-        } else {
+        } else if ("1.9".equals(nom)) {
+            return JAVA_1_9;
+        }
+        if (nom == null) {
             return null;
         }
+        final float v = toFloatVersion(nom);
+        if ((v - 1.) < 1.) { // then we need to check decimals > .9
+            final int firstComma = Math.max(nom.indexOf('.'), nom.indexOf(','));
+            final int end = Math.max(nom.length(), nom.indexOf(',', firstComma));
+            if (Float.parseFloat(nom.substring(firstComma + 1, end)) > .9f) {
+                return JAVA_RECENT;
+            }
+        }
+        return null;
     }
 
     //-----------------------------------------------------------------------
@@ -165,4 +187,24 @@ public enum JavaVersion {
         return name;
     }
 
+    // upper bound of java version considering 2. or current is the higher
+    private static float maxVersion() {
+        final float v = toFloatVersion(System.getProperty("java.version", "2.0"));
+        if (v > 0) {
+            return v;
+        }
+        return 2f;
+    }
+
+    private static float toFloatVersion(final String name) {
+        final String[] toParse = name.split("\\.");
+        if (toParse.length >= 2) {
+            try {
+                return Float.parseFloat(toParse[0] + '.' + toParse[1]);
+            } catch (final NumberFormatException nfe) {
+                // no-op, let use default
+            }
+        }
+        return -1;
+    }
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/JavaVersionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/JavaVersionTest.java?rev=1612075&r1=1612074&r2=1612075&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/JavaVersionTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/JavaVersionTest.java
Sun Jul 20 11:31:17 2014
@@ -19,6 +19,8 @@
 package org.apache.commons.lang3;
 
 import org.junit.Test;
+
+import static org.apache.commons.lang3.JavaVersion.JAVA_RECENT;
 import static org.junit.Assert.*;
 import static org.apache.commons.lang3.JavaVersion.JAVA_0_9;
 import static org.apache.commons.lang3.JavaVersion.JAVA_1_1;
@@ -29,6 +31,7 @@ import static org.apache.commons.lang3.J
 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_1_9;
 import static org.apache.commons.lang3.JavaVersion.get;
 import static org.apache.commons.lang3.JavaVersion.getJavaVersion;
 
@@ -50,7 +53,9 @@ public class JavaVersionTest {
         assertEquals("1.6 failed", JAVA_1_6, get("1.6"));
         assertEquals("1.7 failed", JAVA_1_7, get("1.7"));
         assertEquals("1.8 failed", JAVA_1_8, get("1.8"));
-        assertNull("1.9 unexpectedly worked", get("1.9"));
+        assertEquals("1.9 failed", JAVA_1_9, get("1.9"));
+        assertEquals("1.10 failed", JAVA_RECENT, get("1.10"));
+        // assertNull("2.10 unexpectedly worked", get("2.10"));
         assertEquals("Wrapper method failed", get("1.5"), getJavaVersion("1.5"));
     }
 



Mime
View raw message