commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang SystemUtils.java
Date Wed, 25 Feb 2004 00:25:29 GMT
ggregory    2004/02/24 16:25:29

  Modified:    lang/src/test/org/apache/commons/lang SystemUtilsTest.java
               lang/src/java/org/apache/commons/lang SystemUtils.java
  Log:
  
  Add support for java.awt.headless (Java 1.4.)
  
  Revision  Changes    Path
  1.10      +70 -36    jakarta-commons/lang/src/test/org/apache/commons/lang/SystemUtilsTest.java
  
  Index: SystemUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/SystemUtilsTest.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SystemUtilsTest.java	18 Feb 2004 23:06:19 -0000	1.9
  +++ SystemUtilsTest.java	25 Feb 2004 00:25:29 -0000	1.10
  @@ -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$
    */
   public class SystemUtilsTest extends TestCase {
  -
       public static void main(String[] args) {
           TestRunner.run(suite());
       }
  @@ -46,12 +45,14 @@
           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 @@
       }
   
       /**
  -     * <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 @@
           }
           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 @@
           }
           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 @@
               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 @@
               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 @@
               return false;
           }
           return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix);
  -    }    
  +    }
   
       protected void setUp() throws Exception {
           super.setUp();
  @@ -168,7 +183,7 @@
           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 testJavaVersion() {
           assertEquals(SystemUtils.JAVA_VERSION_FLOAT, SystemUtils.getJavaVersion(), 0f);
       }
  -   
  +
       public void testJavaVersionAsFloat() {
           JAVA_VERSION = null;
           assertEquals(0f, getJavaVersionAsFloat(), 0.000001f);
  @@ -289,7 +304,7 @@
           JAVA_VERSION = "1.6.0";
           assertEquals(1.6f, getJavaVersionAsFloat(), 0.000001f);
       }
  -    
  +
       public void testJavaVersionAsInt() {
           JAVA_VERSION = null;
           assertEquals(0, getJavaVersionAsInt());
  @@ -310,7 +325,7 @@
           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 @@
           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 @@
           version += 20;
           assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
       }
  -    
  +
       //-----------------------------------------------------------------------
       public void testJavaVersionMatches() {
           JAVA_VERSION = null;
  @@ -386,7 +401,7 @@
           assertEquals(false, getJavaVersionMatches("1.4"));
           assertEquals(false, getJavaVersionMatches("1.5"));
       }
  -    
  +
       public void testOSMatches() {
           OS_NAME = null;
           assertEquals(false, getOSMatches("Windows"));
  @@ -397,7 +412,7 @@
           OS_NAME = "OS/2";
           assertEquals(false, getOSMatches("Windows"));
       }
  -    
  +
       public void testOSMatches2() {
           OS_NAME = null;
           OS_VERSION = null;
  @@ -417,5 +432,24 @@
           OS_NAME = "OS/2";
           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());
       }
   }
  
  
  
  1.33      +38 -4     jakarta-commons/lang/src/java/org/apache/commons/lang/SystemUtils.java
  
  Index: SystemUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/SystemUtils.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- SystemUtils.java	18 Feb 2004 22:59:49 -0000	1.32
  +++ SystemUtils.java	25 Feb 2004 00:25:29 -0000	1.33
  @@ -147,6 +147,27 @@
       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>Defaults to <code>null</code> if the runtime does not have
  @@ -821,7 +842,7 @@
       /**
        * <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 @@
               );
               return null;
           }
  -    }    
  +    }
       
       /**
        * <p>Is the Java version at least the requested version.</p>
  @@ -1202,7 +1223,20 @@
       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>
        * 
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message