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 Tetsuya Kaneuchi
|
||||||
* @author Rafal Krupinski
|
* @author Rafal Krupinski
|
||||||
* @since 1.0
|
* @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 {
|
public class SystemUtils {
|
||||||
|
|
||||||
|
@ -146,6 +146,27 @@ public class SystemUtils {
|
||||||
*/
|
*/
|
||||||
public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv");
|
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>
|
* <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>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>
|
* <code>null</code>.</p>
|
||||||
*/
|
*/
|
||||||
public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
|
public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
|
||||||
|
@ -1166,7 +1187,7 @@ private static String getSystemProperty(String property) {
|
||||||
);
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Is the Java version at least the requested version.</p>
|
* <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) {
|
public static boolean isJavaVersionAtLeast(int requiredVersion) {
|
||||||
return (JAVA_VERSION_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>
|
* <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
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.apache.commons.lang;
|
package org.apache.commons.lang;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
@ -29,14 +29,13 @@
|
||||||
* Unit tests {@link org.apache.commons.lang.SystemUtils}.
|
* Unit tests {@link org.apache.commons.lang.SystemUtils}.
|
||||||
*
|
*
|
||||||
* Only limited testing can be performed.
|
* Only limited testing can be performed.
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @author Tetsuya Kaneuchi
|
* @author Tetsuya Kaneuchi
|
||||||
* @author Gary D. Gregory
|
* @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 class SystemUtilsTest extends TestCase {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
TestRunner.run(suite());
|
TestRunner.run(suite());
|
||||||
}
|
}
|
||||||
|
@ -46,12 +45,14 @@ public static Test suite() {
|
||||||
suite.setName("SystemUtils Tests");
|
suite.setName("SystemUtils Tests");
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// COPIED FROM SystemUtils
|
// COPIED FROM SystemUtils
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
private String JAVA_VERSION;
|
private String JAVA_VERSION;
|
||||||
|
|
||||||
private String OS_NAME;
|
private String OS_NAME;
|
||||||
|
|
||||||
private String OS_VERSION;
|
private String OS_VERSION;
|
||||||
|
|
||||||
public SystemUtilsTest(String name) {
|
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>
|
||||||
*
|
* Get the Java version number as a <code>float</code>.
|
||||||
* <p>Example output:</p>
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Example output:
|
||||||
|
* </p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li><code>1.2f</code> for JDK 1.2
|
* <li><code>1.2f</code> for JDK 1.2
|
||||||
* <li><code>1.31f</code> for JDK 1.3.1
|
* <li><code>1.31f</code> for JDK 1.3.1
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* <p>Patch releases are not reported.
|
* <p>
|
||||||
* Zero is returned if JAVA_VERSION is <code>null</code>.</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
|
* @return the version, for example 1.31f for JDK 1.3.1
|
||||||
*/
|
*/
|
||||||
|
@ -82,18 +88,23 @@ private float getJavaVersionAsFloat() {
|
||||||
}
|
}
|
||||||
return Float.parseFloat(str);
|
return Float.parseFloat(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Get the Java version number as an <code>int</code>.</p>
|
* <p>
|
||||||
*
|
* Get the Java version number as an <code>int</code>.
|
||||||
* <p>Example output:</p>
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Example output:
|
||||||
|
* </p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li><code>120</code> for JDK 1.2
|
* <li><code>120</code> for JDK 1.2
|
||||||
* <li><code>131</code> for JDK 1.3.1
|
* <li><code>131</code> for JDK 1.3.1
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* <p>Patch releases are not reported.
|
* <p>
|
||||||
* Zero is returned if JAVA_VERSION is <code>null</code>.</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
|
* @return the version, for example 131 for JDK 1.3.1
|
||||||
*/
|
*/
|
||||||
|
@ -110,11 +121,12 @@ private int getJavaVersionAsInt() {
|
||||||
}
|
}
|
||||||
return Integer.parseInt(str);
|
return Integer.parseInt(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decides if the java version matches.
|
* 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
|
* @return true if matches, or false if not or can't determine
|
||||||
*/
|
*/
|
||||||
private boolean getJavaVersionMatches(String versionPrefix) {
|
private boolean getJavaVersionMatches(String versionPrefix) {
|
||||||
|
@ -122,12 +134,13 @@ private boolean getJavaVersionMatches(String versionPrefix) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return JAVA_VERSION.startsWith(versionPrefix);
|
return JAVA_VERSION.startsWith(versionPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decides if the operating system matches.
|
* 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
|
* @return true if matches, or false if not or can't determine
|
||||||
*/
|
*/
|
||||||
private boolean getOSMatches(String osNamePrefix) {
|
private boolean getOSMatches(String osNamePrefix) {
|
||||||
|
@ -135,13 +148,15 @@ private boolean getOSMatches(String osNamePrefix) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return OS_NAME.startsWith(osNamePrefix);
|
return OS_NAME.startsWith(osNamePrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decides if the operating system matches.
|
* Decides if the operating system matches.
|
||||||
*
|
*
|
||||||
* @param osNamePrefix the prefix for the os name
|
* @param osNamePrefix
|
||||||
* @param osVersionPrefix the prefix for the version
|
* 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
|
* @return true if matches, or false if not or can't determine
|
||||||
*/
|
*/
|
||||||
private boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
|
private boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
|
||||||
|
@ -149,7 +164,7 @@ private boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix);
|
return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
@ -168,7 +183,7 @@ public void testConstructor() {
|
||||||
assertEquals(true, Modifier.isPublic(SystemUtils.class.getModifiers()));
|
assertEquals(true, Modifier.isPublic(SystemUtils.class.getModifiers()));
|
||||||
assertEquals(false, Modifier.isFinal(SystemUtils.class.getModifiers()));
|
assertEquals(false, Modifier.isFinal(SystemUtils.class.getModifiers()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assums no security manager exists.
|
* Assums no security manager exists.
|
||||||
*/
|
*/
|
||||||
|
@ -268,7 +283,7 @@ public void testIS_OS() {
|
||||||
public void testJavaVersion() {
|
public void testJavaVersion() {
|
||||||
assertEquals(SystemUtils.JAVA_VERSION_FLOAT, SystemUtils.getJavaVersion(), 0f);
|
assertEquals(SystemUtils.JAVA_VERSION_FLOAT, SystemUtils.getJavaVersion(), 0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testJavaVersionAsFloat() {
|
public void testJavaVersionAsFloat() {
|
||||||
JAVA_VERSION = null;
|
JAVA_VERSION = null;
|
||||||
assertEquals(0f, getJavaVersionAsFloat(), 0.000001f);
|
assertEquals(0f, getJavaVersionAsFloat(), 0.000001f);
|
||||||
|
@ -289,7 +304,7 @@ public void testJavaVersionAsFloat() {
|
||||||
JAVA_VERSION = "1.6.0";
|
JAVA_VERSION = "1.6.0";
|
||||||
assertEquals(1.6f, getJavaVersionAsFloat(), 0.000001f);
|
assertEquals(1.6f, getJavaVersionAsFloat(), 0.000001f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testJavaVersionAsInt() {
|
public void testJavaVersionAsInt() {
|
||||||
JAVA_VERSION = null;
|
JAVA_VERSION = null;
|
||||||
assertEquals(0, getJavaVersionAsInt());
|
assertEquals(0, getJavaVersionAsInt());
|
||||||
|
@ -310,7 +325,7 @@ public void testJavaVersionAsInt() {
|
||||||
JAVA_VERSION = "1.6.0";
|
JAVA_VERSION = "1.6.0";
|
||||||
assertEquals(160, getJavaVersionAsInt());
|
assertEquals(160, getJavaVersionAsInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testJavaVersionAtLeastFloat() {
|
public void testJavaVersionAtLeastFloat() {
|
||||||
float version = SystemUtils.JAVA_VERSION_FLOAT;
|
float version = SystemUtils.JAVA_VERSION_FLOAT;
|
||||||
assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
|
assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
|
||||||
|
@ -319,7 +334,7 @@ public void testJavaVersionAtLeastFloat() {
|
||||||
version += 0.2f;
|
version += 0.2f;
|
||||||
assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
|
assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testJavaVersionAtLeastInt() {
|
public void testJavaVersionAtLeastInt() {
|
||||||
int version = SystemUtils.JAVA_VERSION_INT;
|
int version = SystemUtils.JAVA_VERSION_INT;
|
||||||
assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
|
assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
|
||||||
|
@ -328,7 +343,7 @@ public void testJavaVersionAtLeastInt() {
|
||||||
version += 20;
|
version += 20;
|
||||||
assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
|
assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
public void testJavaVersionMatches() {
|
public void testJavaVersionMatches() {
|
||||||
JAVA_VERSION = null;
|
JAVA_VERSION = null;
|
||||||
|
@ -386,7 +401,7 @@ public void testJavaVersionMatches() {
|
||||||
assertEquals(false, getJavaVersionMatches("1.4"));
|
assertEquals(false, getJavaVersionMatches("1.4"));
|
||||||
assertEquals(false, getJavaVersionMatches("1.5"));
|
assertEquals(false, getJavaVersionMatches("1.5"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testOSMatches() {
|
public void testOSMatches() {
|
||||||
OS_NAME = null;
|
OS_NAME = null;
|
||||||
assertEquals(false, getOSMatches("Windows"));
|
assertEquals(false, getOSMatches("Windows"));
|
||||||
|
@ -397,7 +412,7 @@ public void testOSMatches() {
|
||||||
OS_NAME = "OS/2";
|
OS_NAME = "OS/2";
|
||||||
assertEquals(false, getOSMatches("Windows"));
|
assertEquals(false, getOSMatches("Windows"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testOSMatches2() {
|
public void testOSMatches2() {
|
||||||
OS_NAME = null;
|
OS_NAME = null;
|
||||||
OS_VERSION = null;
|
OS_VERSION = null;
|
||||||
|
@ -418,4 +433,23 @@ public void testOSMatches2() {
|
||||||
OS_VERSION = "4.0";
|
OS_VERSION = "4.0";
|
||||||
assertEquals(false, getOSMatches("Windows 9", "4.1"));
|
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