Handle Java version with text prefix, as found on HP-UX
bug 32123, from Jason Gritman git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137994 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bf0e5af79d
commit
167c031371
|
@ -221,6 +221,9 @@ limitations under the License.
|
|||
<contributor>
|
||||
<name>Pete Gieser</name>
|
||||
</contributor>
|
||||
<contributor>
|
||||
<name>Jason Gritman</name>
|
||||
</contributor>
|
||||
<contributor>
|
||||
<name>Matthew Hawthorne</name>
|
||||
</contributor>
|
||||
|
|
|
@ -32,8 +32,9 @@ import java.io.File;
|
|||
* @author Michael Becke
|
||||
* @author Tetsuya Kaneuchi
|
||||
* @author Rafal Krupinski
|
||||
* @author Jason Gritman
|
||||
* @since 1.0
|
||||
* @version $Id: SystemUtils.java,v 1.37 2004/11/06 01:27:59 ggregory Exp $
|
||||
* @version $Id: SystemUtils.java,v 1.38 2004/11/12 00:48:41 scolebourne Exp $
|
||||
*/
|
||||
public class SystemUtils {
|
||||
|
||||
|
@ -782,9 +783,23 @@ public class SystemUtils {
|
|||
|
||||
// Java version
|
||||
//-----------------------------------------------------------------------
|
||||
// These MUST be declared after those above as they depend on the
|
||||
// This MUST be declared after those above as it depends on the
|
||||
// values being set up
|
||||
|
||||
|
||||
/**
|
||||
* <p>Gets the Java version as a <code>String</code> trimming leading letters.</p>
|
||||
*
|
||||
* <p>The field will return <code>null</code> if {@link #JAVA_VERSION} is <code>null</code>.</p>
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
public static final String JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
|
||||
// Java version values
|
||||
//-----------------------------------------------------------------------
|
||||
// These MUST be declared after the trim above as they depend on the
|
||||
// value being set up
|
||||
|
||||
/**
|
||||
* <p>Gets the Java version as a <code>float</code>.</p>
|
||||
*
|
||||
|
@ -1086,17 +1101,17 @@ public class SystemUtils {
|
|||
* </ul>
|
||||
*
|
||||
* <p>Patch releases are not reported.
|
||||
* Zero is returned if {@link #JAVA_VERSION} is <code>null</code>.</p>
|
||||
* Zero is returned if {@link #JAVA_VERSION_TRIMMED} is <code>null</code>.</p>
|
||||
*
|
||||
* @return the version, for example 1.31f for JDK 1.3.1
|
||||
*/
|
||||
private static float getJavaVersionAsFloat() {
|
||||
if (JAVA_VERSION == null) {
|
||||
if (JAVA_VERSION_TRIMMED == null) {
|
||||
return 0f;
|
||||
}
|
||||
String str = JAVA_VERSION.substring(0, 3);
|
||||
if (JAVA_VERSION.length() >= 5) {
|
||||
str = str + JAVA_VERSION.substring(4, 5);
|
||||
String str = JAVA_VERSION_TRIMMED.substring(0, 3);
|
||||
if (JAVA_VERSION_TRIMMED.length() >= 5) {
|
||||
str = str + JAVA_VERSION_TRIMMED.substring(4, 5);
|
||||
}
|
||||
return Float.parseFloat(str);
|
||||
}
|
||||
|
@ -1111,24 +1126,41 @@ public class SystemUtils {
|
|||
* </ul>
|
||||
*
|
||||
* <p>Patch releases are not reported.
|
||||
* Zero is returned if {@link #JAVA_VERSION} is <code>null</code>.</p>
|
||||
* Zero is returned if {@link #JAVA_VERSION_TRIMMED} is <code>null</code>.</p>
|
||||
*
|
||||
* @return the version, for example 131 for JDK 1.3.1
|
||||
*/
|
||||
private static int getJavaVersionAsInt() {
|
||||
if (JAVA_VERSION == null) {
|
||||
if (JAVA_VERSION_TRIMMED == null) {
|
||||
return 0;
|
||||
}
|
||||
String str = JAVA_VERSION.substring(0, 1);
|
||||
str = str + JAVA_VERSION.substring(2, 3);
|
||||
if (JAVA_VERSION.length() >= 5) {
|
||||
str = str + JAVA_VERSION.substring(4, 5);
|
||||
String str = JAVA_VERSION_TRIMMED.substring(0, 1);
|
||||
str = str + JAVA_VERSION_TRIMMED.substring(2, 3);
|
||||
if (JAVA_VERSION_TRIMMED.length() >= 5) {
|
||||
str = str + JAVA_VERSION_TRIMMED.substring(4, 5);
|
||||
} else {
|
||||
str = str + "0";
|
||||
}
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the text of the java version to start with numbers.
|
||||
*
|
||||
* @return the trimmed java version
|
||||
*/
|
||||
private static String getJavaVersionTrimmed() {
|
||||
if (JAVA_VERSION != null) {
|
||||
for (int i = 0; i < JAVA_VERSION.length(); i++) {
|
||||
char ch = JAVA_VERSION.charAt(i);
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
return JAVA_VERSION.substring(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Decides if the java version matches.</p>
|
||||
*
|
||||
|
@ -1136,10 +1168,10 @@ public class SystemUtils {
|
|||
* @return true if matches, or false if not or can't determine
|
||||
*/
|
||||
private static boolean getJavaVersionMatches(String versionPrefix) {
|
||||
if (JAVA_VERSION == null) {
|
||||
if (JAVA_VERSION_TRIMMED == null) {
|
||||
return false;
|
||||
}
|
||||
return JAVA_VERSION.startsWith(versionPrefix);
|
||||
return JAVA_VERSION_TRIMMED.startsWith(versionPrefix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -33,7 +33,7 @@ import junit.textui.TestRunner;
|
|||
* @author Stephen Colebourne
|
||||
* @author Tetsuya Kaneuchi
|
||||
* @author Gary D. Gregory
|
||||
* @version $Id: SystemUtilsTest.java,v 1.10 2004/02/25 00:25:29 ggregory Exp $
|
||||
* @version $Id: SystemUtilsTest.java,v 1.11 2004/11/12 00:48:41 scolebourne Exp $
|
||||
*/
|
||||
public class SystemUtilsTest extends TestCase {
|
||||
public static void main(String[] args) {
|
||||
|
@ -51,6 +51,8 @@ public class SystemUtilsTest extends TestCase {
|
|||
//-----------------------------------------------------------------------
|
||||
private String JAVA_VERSION;
|
||||
|
||||
private String JAVA_VERSION_TRIMMED;
|
||||
|
||||
private String OS_NAME;
|
||||
|
||||
private String OS_VERSION;
|
||||
|
@ -60,51 +62,41 @@ public class SystemUtilsTest extends TestCase {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Get the Java version number as a <code>float</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Example output:
|
||||
* </p>
|
||||
* <p>Gets the Java version number as a <code>float</code>.</p>
|
||||
*
|
||||
* <p>Example return values:</p>
|
||||
* <ul>
|
||||
* <li><code>1.2f</code> for JDK 1.2
|
||||
* <li><code>1.31f</code> for JDK 1.3.1
|
||||
* <li><code>1.2f</code> for JDK 1.2
|
||||
* <li><code>1.31f</code> for JDK 1.3.1
|
||||
* </ul>
|
||||
*
|
||||
* <p>
|
||||
* Patch releases are not reported. Zero is returned if JAVA_VERSION is <code>null</code>.
|
||||
* </p>
|
||||
* <p>Patch releases are not reported.
|
||||
* Zero is returned if {@link #JAVA_VERSION_TRIMMED} is <code>null</code>.</p>
|
||||
*
|
||||
* @return the version, for example 1.31f for JDK 1.3.1
|
||||
*/
|
||||
private float getJavaVersionAsFloat() {
|
||||
if (JAVA_VERSION == null) {
|
||||
if (JAVA_VERSION_TRIMMED == null) {
|
||||
return 0f;
|
||||
}
|
||||
String str = JAVA_VERSION.substring(0, 3);
|
||||
if (JAVA_VERSION.length() >= 5) {
|
||||
str = str + JAVA_VERSION.substring(4, 5);
|
||||
String str = JAVA_VERSION_TRIMMED.substring(0, 3);
|
||||
if (JAVA_VERSION_TRIMMED.length() >= 5) {
|
||||
str = str + JAVA_VERSION_TRIMMED.substring(4, 5);
|
||||
}
|
||||
return Float.parseFloat(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Get the Java version number as an <code>int</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Example output:
|
||||
* </p>
|
||||
* <p>Gets the Java version number as an <code>int</code>.</p>
|
||||
*
|
||||
* <p>Example return values:</p>
|
||||
* <ul>
|
||||
* <li><code>120</code> for JDK 1.2
|
||||
* <li><code>131</code> for JDK 1.3.1
|
||||
* <li><code>120</code> for JDK 1.2
|
||||
* <li><code>131</code> for JDK 1.3.1
|
||||
* </ul>
|
||||
*
|
||||
* <p>
|
||||
* Patch releases are not reported. Zero is returned if JAVA_VERSION is <code>null</code>.
|
||||
* </p>
|
||||
* <p>Patch releases are not reported.
|
||||
* Zero is returned if {@link #JAVA_VERSION_TRIMMED} is <code>null</code>.</p>
|
||||
*
|
||||
* @return the version, for example 131 for JDK 1.3.1
|
||||
*/
|
||||
|
@ -112,16 +104,33 @@ public class SystemUtilsTest extends TestCase {
|
|||
if (JAVA_VERSION == null) {
|
||||
return 0;
|
||||
}
|
||||
String str = JAVA_VERSION.substring(0, 1);
|
||||
str = str + JAVA_VERSION.substring(2, 3);
|
||||
if (JAVA_VERSION.length() >= 5) {
|
||||
str = str + JAVA_VERSION.substring(4, 5);
|
||||
String str = JAVA_VERSION_TRIMMED.substring(0, 1);
|
||||
str = str + JAVA_VERSION_TRIMMED.substring(2, 3);
|
||||
if (JAVA_VERSION_TRIMMED.length() >= 5) {
|
||||
str = str + JAVA_VERSION_TRIMMED.substring(4, 5);
|
||||
} else {
|
||||
str = str + "0";
|
||||
}
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the text of the java version to start with numbers.
|
||||
*
|
||||
* @return the trimmed java version
|
||||
*/
|
||||
private String getJavaVersionTrimmed() {
|
||||
if (JAVA_VERSION != null) {
|
||||
for (int i = 0; i < JAVA_VERSION.length(); i++) {
|
||||
char ch = JAVA_VERSION.charAt(i);
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
return JAVA_VERSION.substring(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides if the java version matches.
|
||||
*
|
||||
|
@ -130,10 +139,10 @@ public class SystemUtilsTest extends TestCase {
|
|||
* @return true if matches, or false if not or can't determine
|
||||
*/
|
||||
private boolean getJavaVersionMatches(String versionPrefix) {
|
||||
if (JAVA_VERSION == null) {
|
||||
if (JAVA_VERSION_TRIMMED == null) {
|
||||
return false;
|
||||
}
|
||||
return JAVA_VERSION.startsWith(versionPrefix);
|
||||
return JAVA_VERSION_TRIMMED.startsWith(versionPrefix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -286,44 +295,68 @@ public class SystemUtilsTest extends TestCase {
|
|||
|
||||
public void testJavaVersionAsFloat() {
|
||||
JAVA_VERSION = null;
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(0f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "1.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.1f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "1.2";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.2f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "1.3.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.3f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "1.3.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.31f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "1.4.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.4f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "1.4.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.41f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "1.5.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.5f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "1.6.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.6f, getJavaVersionAsFloat(), 0.000001f);
|
||||
JAVA_VERSION = "JavaVM-1.3.1"; //HP-UX
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(1.31f, getJavaVersionAsFloat(), 0.000001f);
|
||||
}
|
||||
|
||||
public void testJavaVersionAsInt() {
|
||||
JAVA_VERSION = null;
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(0, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "1.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(110, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "1.2";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(120, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "1.3.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(130, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "1.3.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(131, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "1.4.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(140, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "1.4.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(141, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "1.5.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(150, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "1.6.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(160, getJavaVersionAsInt());
|
||||
JAVA_VERSION = "JavaVM-1.3.1"; //HP-UX
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(131, getJavaVersionAsInt());
|
||||
}
|
||||
|
||||
public void testJavaVersionAtLeastFloat() {
|
||||
|
@ -347,54 +380,63 @@ public class SystemUtilsTest extends TestCase {
|
|||
//-----------------------------------------------------------------------
|
||||
public void testJavaVersionMatches() {
|
||||
JAVA_VERSION = null;
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(false, getJavaVersionMatches("1.1"));
|
||||
assertEquals(false, getJavaVersionMatches("1.2"));
|
||||
assertEquals(false, getJavaVersionMatches("1.3"));
|
||||
assertEquals(false, getJavaVersionMatches("1.4"));
|
||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||
JAVA_VERSION = "1.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(true, getJavaVersionMatches("1.1"));
|
||||
assertEquals(false, getJavaVersionMatches("1.2"));
|
||||
assertEquals(false, getJavaVersionMatches("1.3"));
|
||||
assertEquals(false, getJavaVersionMatches("1.4"));
|
||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||
JAVA_VERSION = "1.2";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(false, getJavaVersionMatches("1.1"));
|
||||
assertEquals(true, getJavaVersionMatches("1.2"));
|
||||
assertEquals(false, getJavaVersionMatches("1.3"));
|
||||
assertEquals(false, getJavaVersionMatches("1.4"));
|
||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||
JAVA_VERSION = "1.3.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(false, getJavaVersionMatches("1.1"));
|
||||
assertEquals(false, getJavaVersionMatches("1.2"));
|
||||
assertEquals(true, getJavaVersionMatches("1.3"));
|
||||
assertEquals(false, getJavaVersionMatches("1.4"));
|
||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||
JAVA_VERSION = "1.3.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(false, getJavaVersionMatches("1.1"));
|
||||
assertEquals(false, getJavaVersionMatches("1.2"));
|
||||
assertEquals(true, getJavaVersionMatches("1.3"));
|
||||
assertEquals(false, getJavaVersionMatches("1.4"));
|
||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||
JAVA_VERSION = "1.4.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(false, getJavaVersionMatches("1.1"));
|
||||
assertEquals(false, getJavaVersionMatches("1.2"));
|
||||
assertEquals(false, getJavaVersionMatches("1.3"));
|
||||
assertEquals(true, getJavaVersionMatches("1.4"));
|
||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||
JAVA_VERSION = "1.4.1";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(false, getJavaVersionMatches("1.1"));
|
||||
assertEquals(false, getJavaVersionMatches("1.2"));
|
||||
assertEquals(false, getJavaVersionMatches("1.3"));
|
||||
assertEquals(true, getJavaVersionMatches("1.4"));
|
||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||
JAVA_VERSION = "1.5.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(false, getJavaVersionMatches("1.1"));
|
||||
assertEquals(false, getJavaVersionMatches("1.2"));
|
||||
assertEquals(false, getJavaVersionMatches("1.3"));
|
||||
assertEquals(false, getJavaVersionMatches("1.4"));
|
||||
assertEquals(true, getJavaVersionMatches("1.5"));
|
||||
JAVA_VERSION = "1.6.0";
|
||||
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
|
||||
assertEquals(false, getJavaVersionMatches("1.1"));
|
||||
assertEquals(false, getJavaVersionMatches("1.2"));
|
||||
assertEquals(false, getJavaVersionMatches("1.3"));
|
||||
|
|
Loading…
Reference in New Issue