Fix class so IS_JAVA_* and IS_OS_* constants work correctly.

bug 21767, from Tetsuya Kaneuchi


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137485 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-21 22:57:51 +00:00
parent 6a5bdacba3
commit 6a0ce20988
2 changed files with 284 additions and 236 deletions

View File

@ -66,12 +66,16 @@
* @author <a href="mailto:sdowney@panix.com">Steve Downey</a> * @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Michael Becke * @author Michael Becke
* @author Tetsuya Kaneuchi
* @since 1.0 * @since 1.0
* @version $Id: SystemUtils.java,v 1.18 2003/07/18 16:48:25 ggregory Exp $ * @version $Id: SystemUtils.java,v 1.19 2003/07/21 22:57:50 scolebourne Exp $
*/ */
public class SystemUtils { public class SystemUtils {
// System property constants
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// These MUST be declared first. Other constants depend on this.
/** /**
* <p>The <code>file.encoding</code> System Property.</p> * <p>The <code>file.encoding</code> System Property.</p>
* <p>File encoding, such as <code>Cp1252</code>.</p> * <p>File encoding, such as <code>Cp1252</code>.</p>
@ -94,185 +98,6 @@ public class SystemUtils {
*/ */
public static final String FILE_SEPARATOR = getSystemProperty("file.separator"); public static final String FILE_SEPARATOR = getSystemProperty("file.separator");
//-----------------------------------------------------------------------
/**
* <p>Is <code>true</code> if this is Java version 1.1 (also 1.1.x versions).</p>
*
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");
/**
* <p>Is <code>true</code> if this is Java version 1.2 (also 1.2.x versions).</p>
*
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");
/**
* <p>Is <code>true</code> if this is Java version 1.3 (also 1.3.x versions).</p>
*
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");
/**
* <p>Is <code>true</code> if this is Java version 1.4 (also 1.4.x versions).</p>
*
* <p>The field will <code>false</code> false if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
/**
* <p>Is <code>true</code> if this is Java version 1.5 (also 1.5.x versions).</p>
*
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");
/**
* <p>Is <code>true</code> if this is AIX.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_AIX = getOSMatches("AIX");
/**
* <p>Is <code>true</code> if this is HP-UX.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX");
/**
* <p>Is <code>true</code> if this is Irix.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_IRIX = getOSMatches("Irix");
/**
* <p>Is <code>true</code> if this is Linux.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_LINUX = getOSMatches("Linux") || getOSMatches("LINUX");
// Windows XP returns 'Windows 2000' just for fun...
/**
* <p>Is <code>true</code> if this is Mac.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_MAC = getOSMatches("Mac");
/**
* <p>Is <code>true</code> if this is Mac.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X");
/**
* <p>Is <code>true</code> if this is OS/2.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_OS2 = getOSMatches("OS/2");
/**
* <p>Is <code>true</code> if this is Solaris.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris");
/**
* <p>Is <code>true</code> if this is SunOS.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS");
//-----------------------------------------------------------------------
// OS names from http://www.vamphq.com/os.html
// Selected ones included - please advise commons-dev@jakarta.apache.org
// if you want another added or a mistake corrected
/**
* <p>Is <code>true</code> if this is Windows.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS = getOSMatches("Windows");
/**
* <p>Is <code>true</code> if this is Windows 2000.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_2000 = getOSMatches("Windows", "5.0");
/**
* <p>Is <code>true</code> if this is Windows 95.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_95 = getOSMatches("Windows 9", "4.0");
// JDK 1.2 running on Windows98 returns 'Windows 95', hence the above
/**
* <p>Is <code>true</code> if this is Windows 98.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_98 = getOSMatches("Windows 9", "4.1");
// JDK 1.2 running on Windows98 returns 'Windows 95', hence the above
/**
* <p>Is <code>true</code> if this is Windows ME.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_ME = getOSMatches("Windows", "4.9");
// JDK 1.2 running on WindowsME may return 'Windows 95', hence the above
/**
* <p>Is <code>true</code> if this is Windows NT.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_NT = getOSMatches("Windows NT");
// Windows 2000 returns 'Windows 2000' but may suffer from same JDK1.2 problem
/**
* <p>Is <code>true</code> if this is Windows XP.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_XP = getOSMatches("Windows", "5.1");
/** /**
* <p>The <code>java.class.path</code> System Property. Java class path.</p> * <p>The <code>java.class.path</code> System Property. Java class path.</p>
* *
@ -432,33 +257,6 @@ public class SystemUtils {
*/ */
public static final String JAVA_VERSION = getSystemProperty("java.version"); public static final String JAVA_VERSION = getSystemProperty("java.version");
//-----------------------------------------------------------------------
/**
* <p>Gets the Java version 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
* </ul>
*
* <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
*/
public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat();
/**
* <p>Gets the Java version 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
* </ul>
*
* <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
*/
public static final int JAVA_VERSION_INT = getJavaVersionAsInt();
/** /**
* <p>The <code>java.vm.info</code> System Property. Java Virtual Machine implementation * <p>The <code>java.vm.info</code> System Property. Java Virtual Machine implementation
* info.</p> * info.</p>
@ -644,6 +442,235 @@ public class SystemUtils {
*/ */
public static final String USER_NAME = getSystemProperty("user.name"); public static final String USER_NAME = getSystemProperty("user.name");
// Java version
//-----------------------------------------------------------------------
// These MUST be declared after those above as they depend on the
// values being set up
/**
* <p>Gets the Java version 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
* </ul>
*
* <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
*/
public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat();
/**
* <p>Gets the Java version 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
* </ul>
*
* <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
*/
public static final int JAVA_VERSION_INT = getJavaVersionAsInt();
// Java version checks
//-----------------------------------------------------------------------
// These MUST be declared after those above as they depend on the
// values being set up
/**
* <p>Is <code>true</code> if this is Java version 1.1 (also 1.1.x versions).</p>
*
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");
/**
* <p>Is <code>true</code> if this is Java version 1.2 (also 1.2.x versions).</p>
*
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");
/**
* <p>Is <code>true</code> if this is Java version 1.3 (also 1.3.x versions).</p>
*
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");
/**
* <p>Is <code>true</code> if this is Java version 1.4 (also 1.4.x versions).</p>
*
* <p>The field will <code>false</code> false if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
/**
* <p>Is <code>true</code> if this is Java version 1.5 (also 1.5.x versions).</p>
*
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
* <code>null</code>.</p>
*/
public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");
// Operating system checks
//-----------------------------------------------------------------------
// These MUST be declared after those above as they depend on the
// values being set up
// OS names from http://www.vamphq.com/os.html
// Selected ones included - please advise commons-dev@jakarta.apache.org
// if you want another added or a mistake corrected
/**
* <p>Is <code>true</code> if this is AIX.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_AIX = getOSMatches("AIX");
/**
* <p>Is <code>true</code> if this is HP-UX.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX");
/**
* <p>Is <code>true</code> if this is Irix.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_IRIX = getOSMatches("Irix");
/**
* <p>Is <code>true</code> if this is Linux.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_LINUX = getOSMatches("Linux") || getOSMatches("LINUX");
/**
* <p>Is <code>true</code> if this is Mac.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_MAC = getOSMatches("Mac");
/**
* <p>Is <code>true</code> if this is Mac.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X");
/**
* <p>Is <code>true</code> if this is OS/2.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_OS2 = getOSMatches("OS/2");
/**
* <p>Is <code>true</code> if this is Solaris.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris");
/**
* <p>Is <code>true</code> if this is SunOS.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS");
/**
* <p>Is <code>true</code> if this is Windows.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS = getOSMatches("Windows");
/**
* <p>Is <code>true</code> if this is Windows 2000.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_2000 = getOSMatches("Windows", "5.0");
/**
* <p>Is <code>true</code> if this is Windows 95.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_95 = getOSMatches("Windows 9", "4.0");
// JDK 1.2 running on Windows98 returns 'Windows 95', hence the above
/**
* <p>Is <code>true</code> if this is Windows 98.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_98 = getOSMatches("Windows 9", "4.1");
// JDK 1.2 running on Windows98 returns 'Windows 95', hence the above
/**
* <p>Is <code>true</code> if this is Windows ME.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_ME = getOSMatches("Windows", "4.9");
// JDK 1.2 running on WindowsME may return 'Windows 95', hence the above
/**
* <p>Is <code>true</code> if this is Windows NT.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_NT = getOSMatches("Windows NT");
// Windows 2000 returns 'Windows 2000' but may suffer from same JDK1.2 problem
/**
* <p>Is <code>true</code> if this is Windows XP.</p>
*
* <p>The field will return <code>false</code> if <code>OS_NAME</code> is
* <code>null</code>.</p>
*/
public static final boolean IS_OS_WINDOWS_XP = getOSMatches("Windows", "5.1");
// Windows XP returns 'Windows 2000' just for fun...
//-----------------------------------------------------------------------
/**
* <p>SystemUtils instances should NOT be constructed in standard
* programming. Instead, the class should be used as
* <code>SystemUtils.FILE_SEPARATOR</code>.</p>
*
* <p>This constructor is public to permit tools that require a JavaBean
* instance to operate.</p>
*/
public SystemUtils() {
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* <p>Gets the Java version number as a <code>float</code>.</p> * <p>Gets the Java version number as a <code>float</code>.</p>
@ -654,9 +681,9 @@ public class SystemUtils {
* <li><code>1.31f</code> for JDK 1.3.1 * <li><code>1.31f</code> for JDK 1.3.1
* </ul> * </ul>
* *
* @return the version, for example 1.31f for JDK 1.3.1
* @deprecated Use {@link #JAVA_VERSION_FLOAT} instead. * @deprecated Use {@link #JAVA_VERSION_FLOAT} instead.
* Method will be removed in Commons Lang 3.0. * Method will be removed in Commons Lang 3.0.
* @return the version, for example 1.31f for JDK 1.3.1
*/ */
public static float getJavaVersion() { public static float getJavaVersion() {
return JAVA_VERSION_FLOAT; return JAVA_VERSION_FLOAT;
@ -813,15 +840,4 @@ public static boolean isJavaVersionAtLeast(int requiredVersion) {
return (JAVA_VERSION_INT >= requiredVersion); return (JAVA_VERSION_INT >= requiredVersion);
} }
/**
* <p>SystemUtils instances should NOT be constructed in standard
* programming. Instead, the class should be used as
* <code>SystemUtils.FILE_SEPARATOR</code>.</p>
*
* <p>This constructor is public to permit tools that require a JavaBean
* instance to operate.</p>
*/
public SystemUtils() {
}
} }

View File

@ -64,7 +64,8 @@
* Only limited testing can be performed. * Only limited testing can be performed.
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @version $Id: SystemUtilsTest.java,v 1.2 2003/05/24 14:34:14 scolebourne Exp $ * @author Tetsuya Kaneuchi
* @version $Id: SystemUtilsTest.java,v 1.3 2003/07/21 22:57:51 scolebourne Exp $
*/ */
public class SystemUtilsTest extends TestCase { public class SystemUtilsTest extends TestCase {
@ -74,22 +75,6 @@ public SystemUtilsTest(String name) {
public static void main(String[] args) { public static void main(String[] args) {
TestRunner.run(suite()); TestRunner.run(suite());
// SystemUtils s = new SystemUtils();
// System.out.println(s.IS_OS_WINDOWS);
// System.out.println(s.IS_OS_WINDOWS_95);
// System.out.println(s.IS_OS_WINDOWS_98);
// java.util.Properties p = System.getProperties();
// java.util.Enumeration keys = p.keys();
// java.util.List list = new java.util.ArrayList();
// while( keys.hasMoreElements() ) {
// list.add(keys.nextElement());
// }
// java.util.Collections.sort(list);
// for (java.util.Iterator it = list.iterator(); it.hasNext();) {
// String key = (String) it.next();
// System.out.println(key + " " + p.getProperty(key));
// }
} }
public static Test suite() { public static Test suite() {
@ -356,4 +341,51 @@ public void testOSMatches2() {
assertEquals(false, getOSMatches("Windows 9", "4.1")); assertEquals(false, getOSMatches("Windows 9", "4.1"));
} }
public void testIS_OS() {
String osName = System.getProperty("os.name");
if (osName == null) {
assertEquals(false, SystemUtils.IS_OS_WINDOWS);
assertEquals(false, SystemUtils.IS_OS_SOLARIS);
assertEquals(false, SystemUtils.IS_OS_LINUX);
assertEquals(false, SystemUtils.IS_OS_MAC_OSX);
} else if (osName.startsWith("Windows")) {
assertTrue(SystemUtils.IS_OS_WINDOWS);
} else if (osName.startsWith("Solaris")) {
assertTrue(SystemUtils.IS_OS_SOLARIS);
} else if (osName.toLowerCase().startsWith("linux")) {
assertTrue(SystemUtils.IS_OS_LINUX);
} else if (osName.startsWith("Mac OS X")) {
assertTrue(SystemUtils.IS_OS_MAC_OSX);
} else if (osName.startsWith("OS/2")) {
assertTrue(SystemUtils.IS_OS_OS2);
} else if (osName.startsWith("SunOS")) {
assertTrue(SystemUtils.IS_OS_SUN_OS);
} else {
System.out.println("Can't test IS_OS value");
}
}
public void testIS_JAVA() {
String javaVersion = System.getProperty("java.version");
if (javaVersion == null) {
assertEquals(false, SystemUtils.IS_JAVA_1_1);
assertEquals(false, SystemUtils.IS_JAVA_1_2);
assertEquals(false, SystemUtils.IS_JAVA_1_3);
assertEquals(false, SystemUtils.IS_JAVA_1_4);
assertEquals(false, SystemUtils.IS_JAVA_1_5);
} else if (javaVersion.startsWith("1.1")) {
assertTrue(SystemUtils.IS_JAVA_1_1);
} else if (javaVersion.startsWith("1.2")) {
assertTrue(SystemUtils.IS_JAVA_1_2);
} else if (javaVersion.startsWith("1.3")) {
assertTrue(SystemUtils.IS_JAVA_1_3);
} else if (javaVersion.startsWith("1.4")) {
assertTrue(SystemUtils.IS_JAVA_1_4);
} else if (javaVersion.startsWith("1.5")) {
assertTrue(SystemUtils.IS_JAVA_1_5);
} else {
System.out.println("Can't test IS_JAVA value");
}
}
} }