Handle badly formatted java version numbers without exceptions

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@389911 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-03-29 22:22:32 +00:00
parent a9f5a6b188
commit d7db8448a1
2 changed files with 28 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 The Apache Software Foundation.
* Copyright 2002-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -1114,7 +1114,11 @@ private static float getJavaVersionAsFloat() {
if (JAVA_VERSION_TRIMMED.length() >= 5) {
str = str + JAVA_VERSION_TRIMMED.substring(4, 5);
}
try {
return Float.parseFloat(str);
} catch (Exception ex) {
return 0;
}
}
/**
@ -1142,7 +1146,11 @@ private static int getJavaVersionAsInt() {
} else {
str = str + "0";
}
try {
return Integer.parseInt(str);
} catch (Exception ex) {
return 0;
}
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright 2003,2004 The Apache Software Foundation.
* Copyright 2003,2004,2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -83,7 +83,11 @@ private float getJavaVersionAsFloat() {
if (JAVA_VERSION_TRIMMED.length() >= 5) {
str = str + JAVA_VERSION_TRIMMED.substring(4, 5);
}
try {
return Float.parseFloat(str);
} catch (Exception ex) {
return 0;
}
}
/**
@ -111,7 +115,11 @@ private int getJavaVersionAsInt() {
} else {
str = str + "0";
}
try {
return Integer.parseInt(str);
} catch (Exception ex) {
return 0;
}
}
/**
@ -324,6 +332,9 @@ public void testJavaVersionAsFloat() {
JAVA_VERSION = "JavaVM-1.3.1"; //HP-UX
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
assertEquals(1.31f, getJavaVersionAsFloat(), 0.000001f);
JAVA_VERSION = "XXX-1.3.x"; //error
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
assertEquals(0.0f, getJavaVersionAsFloat(), 0.000001f);
}
public void testJavaVersionAsInt() {
@ -357,6 +368,9 @@ public void testJavaVersionAsInt() {
JAVA_VERSION = "JavaVM-1.3.1"; //HP-UX
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
assertEquals(131, getJavaVersionAsInt());
JAVA_VERSION = "XXX-1.3.x"; //error
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
assertEquals(0, getJavaVersionAsInt());
}
public void testJavaVersionAtLeastFloat() {