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.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1612075 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
de67dec077
commit
3fa5334ac0
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.JavaVersion.JAVA_1_5;
|
|||
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"));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue