Add support for java.awt.headless (Java 1.4.)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137817 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a076adf1c1
commit
c03bc5f6ce
|
@ -33,7 +33,7 @@
|
|||
* @author Tetsuya Kaneuchi
|
||||
* @author Rafal Krupinski
|
||||
* @since 1.0
|
||||
* @version $Id: SystemUtils.java,v 1.32 2004/02/18 22:59:49 ggregory Exp $
|
||||
* @version $Id: SystemUtils.java,v 1.33 2004/02/25 00:25:29 ggregory Exp $
|
||||
*/
|
||||
public class SystemUtils {
|
||||
|
||||
|
@ -146,6 +146,27 @@ public class SystemUtils {
|
|||
*/
|
||||
public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv");
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The <code>java.awt.headless</code> System Property.
|
||||
* The value of this property is the String <code>"true"</code> or <code>"false"</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>Defaults to <code>null</code> if the runtime does not have
|
||||
* security access to read this property or the property does not exist.</p>
|
||||
*
|
||||
* <p>
|
||||
* This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
|
||||
* or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
|
||||
* will be out of sync with that System property.
|
||||
* </p>
|
||||
*
|
||||
* @see #isJavaAwtHeadless()
|
||||
* @since 2.1
|
||||
* @since Java 1.4
|
||||
*/
|
||||
public static final String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless");
|
||||
|
||||
/**
|
||||
* <p>The <code>java.awt.printerjob</code> System Property.</p>
|
||||
*
|
||||
|
@ -821,7 +842,7 @@ public class SystemUtils {
|
|||
/**
|
||||
* <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
|
||||
* <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
|
||||
* <code>null</code>.</p>
|
||||
*/
|
||||
public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
|
||||
|
@ -1166,7 +1187,7 @@ private static String getSystemProperty(String property) {
|
|||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Is the Java version at least the requested version.</p>
|
||||
|
@ -1202,7 +1223,20 @@ public static boolean isJavaVersionAtLeast(float requiredVersion) {
|
|||
public static boolean isJavaVersionAtLeast(int requiredVersion) {
|
||||
return (JAVA_VERSION_INT >= requiredVersion);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the {@link #JAVA_AWT_HEADLESS} value is <code>true</code>.
|
||||
*
|
||||
* @return <code>true</code> if <code>JAVA_AWT_HEADLESS</code> is <code>"true"</code>,
|
||||
* <code>false</code> otherwise.
|
||||
*
|
||||
* @see #JAVA_AWT_HEADLESS
|
||||
* @since 2.1
|
||||
* @since Java 1.4
|
||||
*/
|
||||
public static boolean isJavaAwtHeadless() {
|
||||
return JAVA_AWT_HEADLESS != null ? JAVA_AWT_HEADLESS.equals(Boolean.TRUE.toString()) : false;
|
||||
}
|
||||
/**
|
||||
* <p>Gets the Java home directory as a <code>File</code>.</p>
|
||||
*
|
||||
|
|
|
@ -13,12 +13,12 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.lang;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
@ -29,14 +29,13 @@
|
|||
* Unit tests {@link org.apache.commons.lang.SystemUtils}.
|
||||
*
|
||||
* Only limited testing can be performed.
|
||||
*
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
* @author Tetsuya Kaneuchi
|
||||
* @author Gary D. Gregory
|
||||
* @version $Id: SystemUtilsTest.java,v 1.9 2004/02/18 23:06:19 ggregory Exp $
|
||||
* @version $Id: SystemUtilsTest.java,v 1.10 2004/02/25 00:25:29 ggregory Exp $
|
||||
*/
|
||||
public class SystemUtilsTest extends TestCase {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TestRunner.run(suite());
|
||||
}
|
||||
|
@ -46,12 +45,14 @@ public static Test suite() {
|
|||
suite.setName("SystemUtils Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// COPIED FROM SystemUtils
|
||||
//-----------------------------------------------------------------------
|
||||
private String JAVA_VERSION;
|
||||
|
||||
private String OS_NAME;
|
||||
|
||||
private String OS_VERSION;
|
||||
|
||||
public SystemUtilsTest(String name) {
|
||||
|
@ -59,16 +60,21 @@ public SystemUtilsTest(String name) {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Get the Java version number as a <code>float</code>.</p>
|
||||
*
|
||||
* <p>Example output:</p>
|
||||
* <p>
|
||||
* Get the Java version number as a <code>float</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Example output:
|
||||
* </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 JAVA_VERSION is <code>null</code>.
|
||||
* </p>
|
||||
*
|
||||
* @return the version, for example 1.31f for JDK 1.3.1
|
||||
*/
|
||||
|
@ -82,18 +88,23 @@ private float getJavaVersionAsFloat() {
|
|||
}
|
||||
return Float.parseFloat(str);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>Get the Java version number as an <code>int</code>.</p>
|
||||
*
|
||||
* <p>Example output:</p>
|
||||
* <p>
|
||||
* Get the Java version number as an <code>int</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Example output:
|
||||
* </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 JAVA_VERSION is <code>null</code>.
|
||||
* </p>
|
||||
*
|
||||
* @return the version, for example 131 for JDK 1.3.1
|
||||
*/
|
||||
|
@ -110,11 +121,12 @@ private int getJavaVersionAsInt() {
|
|||
}
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decides if the java version matches.
|
||||
*
|
||||
* @param versionPrefix the prefix for the java version
|
||||
* @param versionPrefix
|
||||
* the prefix for the java version
|
||||
* @return true if matches, or false if not or can't determine
|
||||
*/
|
||||
private boolean getJavaVersionMatches(String versionPrefix) {
|
||||
|
@ -122,12 +134,13 @@ private boolean getJavaVersionMatches(String versionPrefix) {
|
|||
return false;
|
||||
}
|
||||
return JAVA_VERSION.startsWith(versionPrefix);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides if the operating system matches.
|
||||
*
|
||||
* @param osNamePrefix the prefix for the os name
|
||||
* @param osNamePrefix
|
||||
* the prefix for the os name
|
||||
* @return true if matches, or false if not or can't determine
|
||||
*/
|
||||
private boolean getOSMatches(String osNamePrefix) {
|
||||
|
@ -135,13 +148,15 @@ private boolean getOSMatches(String osNamePrefix) {
|
|||
return false;
|
||||
}
|
||||
return OS_NAME.startsWith(osNamePrefix);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides if the operating system matches.
|
||||
*
|
||||
* @param osNamePrefix the prefix for the os name
|
||||
* @param osVersionPrefix the prefix for the version
|
||||
* @param osNamePrefix
|
||||
* the prefix for the os name
|
||||
* @param osVersionPrefix
|
||||
* the prefix for the version
|
||||
* @return true if matches, or false if not or can't determine
|
||||
*/
|
||||
private boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
|
||||
|
@ -149,7 +164,7 @@ private boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
|
|||
return false;
|
||||
}
|
||||
return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix);
|
||||
}
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
@ -168,7 +183,7 @@ public void testConstructor() {
|
|||
assertEquals(true, Modifier.isPublic(SystemUtils.class.getModifiers()));
|
||||
assertEquals(false, Modifier.isFinal(SystemUtils.class.getModifiers()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Assums no security manager exists.
|
||||
*/
|
||||
|
@ -268,7 +283,7 @@ public void testIS_OS() {
|
|||
public void testJavaVersion() {
|
||||
assertEquals(SystemUtils.JAVA_VERSION_FLOAT, SystemUtils.getJavaVersion(), 0f);
|
||||
}
|
||||
|
||||
|
||||
public void testJavaVersionAsFloat() {
|
||||
JAVA_VERSION = null;
|
||||
assertEquals(0f, getJavaVersionAsFloat(), 0.000001f);
|
||||
|
@ -289,7 +304,7 @@ public void testJavaVersionAsFloat() {
|
|||
JAVA_VERSION = "1.6.0";
|
||||
assertEquals(1.6f, getJavaVersionAsFloat(), 0.000001f);
|
||||
}
|
||||
|
||||
|
||||
public void testJavaVersionAsInt() {
|
||||
JAVA_VERSION = null;
|
||||
assertEquals(0, getJavaVersionAsInt());
|
||||
|
@ -310,7 +325,7 @@ public void testJavaVersionAsInt() {
|
|||
JAVA_VERSION = "1.6.0";
|
||||
assertEquals(160, getJavaVersionAsInt());
|
||||
}
|
||||
|
||||
|
||||
public void testJavaVersionAtLeastFloat() {
|
||||
float version = SystemUtils.JAVA_VERSION_FLOAT;
|
||||
assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
|
||||
|
@ -319,7 +334,7 @@ public void testJavaVersionAtLeastFloat() {
|
|||
version += 0.2f;
|
||||
assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
|
||||
}
|
||||
|
||||
|
||||
public void testJavaVersionAtLeastInt() {
|
||||
int version = SystemUtils.JAVA_VERSION_INT;
|
||||
assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
|
||||
|
@ -328,7 +343,7 @@ public void testJavaVersionAtLeastInt() {
|
|||
version += 20;
|
||||
assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testJavaVersionMatches() {
|
||||
JAVA_VERSION = null;
|
||||
|
@ -386,7 +401,7 @@ public void testJavaVersionMatches() {
|
|||
assertEquals(false, getJavaVersionMatches("1.4"));
|
||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||
}
|
||||
|
||||
|
||||
public void testOSMatches() {
|
||||
OS_NAME = null;
|
||||
assertEquals(false, getOSMatches("Windows"));
|
||||
|
@ -397,7 +412,7 @@ public void testOSMatches() {
|
|||
OS_NAME = "OS/2";
|
||||
assertEquals(false, getOSMatches("Windows"));
|
||||
}
|
||||
|
||||
|
||||
public void testOSMatches2() {
|
||||
OS_NAME = null;
|
||||
OS_VERSION = null;
|
||||
|
@ -418,4 +433,23 @@ public void testOSMatches2() {
|
|||
OS_VERSION = "4.0";
|
||||
assertEquals(false, getOSMatches("Windows 9", "4.1"));
|
||||
}
|
||||
|
||||
public void testJavaAwtHeadless() {
|
||||
boolean atLeastJava14 = SystemUtils.isJavaVersionAtLeast(140);
|
||||
String expectedStringValue = System.getProperty("java.awt.headless");
|
||||
String expectedStringValueWithDefault = System.getProperty("java.awt.headless", "false");
|
||||
assertNotNull(expectedStringValueWithDefault);
|
||||
if (atLeastJava14) {
|
||||
boolean expectedValue = Boolean.valueOf(expectedStringValue).booleanValue();
|
||||
if (expectedStringValue != null) {
|
||||
assertEquals(expectedStringValue, SystemUtils.JAVA_AWT_HEADLESS);
|
||||
}
|
||||
assertEquals(expectedValue, SystemUtils.isJavaAwtHeadless());
|
||||
} else {
|
||||
assertNull(expectedStringValue);
|
||||
assertNull(SystemUtils.JAVA_AWT_HEADLESS);
|
||||
assertEquals(expectedStringValueWithDefault, "" + SystemUtils.isJavaAwtHeadless());
|
||||
}
|
||||
assertEquals(expectedStringValueWithDefault, "" + SystemUtils.isJavaAwtHeadless());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue