KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > SystemUtilsTest


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.lang;
18
19 import java.io.File JavaDoc;
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.lang.reflect.Modifier JavaDoc;
22 import junit.framework.Assert;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27
28 /**
29  * Unit tests {@link org.apache.commons.lang.SystemUtils}.
30  *
31  * Only limited testing can be performed.
32  *
33  * @author Stephen Colebourne
34  * @author Tetsuya Kaneuchi
35  * @author Gary D. Gregory
36  * @version $Id: SystemUtilsTest.java 155423 2005-02-26 13:08:30Z dirkv $
37  */

38 public class SystemUtilsTest extends TestCase {
39     public static void main(String JavaDoc[] args) {
40         TestRunner.run(suite());
41     }
42
43     public static Test suite() {
44         TestSuite suite = new TestSuite(SystemUtilsTest.class);
45         suite.setName("SystemUtils Tests");
46         return suite;
47     }
48
49     //-----------------------------------------------------------------------
50
// COPIED FROM SystemUtils
51
//-----------------------------------------------------------------------
52
private String JavaDoc JAVA_VERSION;
53
54     private String JavaDoc JAVA_VERSION_TRIMMED;
55
56     private String JavaDoc OS_NAME;
57
58     private String JavaDoc OS_VERSION;
59
60     public SystemUtilsTest(String JavaDoc name) {
61         super(name);
62     }
63
64     /**
65      * <p>Gets the Java version number as a <code>float</code>.</p>
66      *
67      * <p>Example return values:</p>
68      * <ul>
69      * <li><code>1.2f</code> for JDK 1.2
70      * <li><code>1.31f</code> for JDK 1.3.1
71      * </ul>
72      *
73      * <p>Patch releases are not reported.
74      * Zero is returned if {@link #JAVA_VERSION_TRIMMED} is <code>null</code>.</p>
75      *
76      * @return the version, for example 1.31f for JDK 1.3.1
77      */

78     private float getJavaVersionAsFloat() {
79         if (JAVA_VERSION_TRIMMED == null) {
80             return 0f;
81         }
82         String JavaDoc str = JAVA_VERSION_TRIMMED.substring(0, 3);
83         if (JAVA_VERSION_TRIMMED.length() >= 5) {
84             str = str + JAVA_VERSION_TRIMMED.substring(4, 5);
85         }
86         return Float.parseFloat(str);
87     }
88
89     /**
90      * <p>Gets the Java version number as an <code>int</code>.</p>
91      *
92      * <p>Example return values:</p>
93      * <ul>
94      * <li><code>120</code> for JDK 1.2
95      * <li><code>131</code> for JDK 1.3.1
96      * </ul>
97      *
98      * <p>Patch releases are not reported.
99      * Zero is returned if {@link #JAVA_VERSION_TRIMMED} is <code>null</code>.</p>
100      *
101      * @return the version, for example 131 for JDK 1.3.1
102      */

103     private int getJavaVersionAsInt() {
104         if (JAVA_VERSION == null) {
105             return 0;
106         }
107         String JavaDoc str = JAVA_VERSION_TRIMMED.substring(0, 1);
108         str = str + JAVA_VERSION_TRIMMED.substring(2, 3);
109         if (JAVA_VERSION_TRIMMED.length() >= 5) {
110             str = str + JAVA_VERSION_TRIMMED.substring(4, 5);
111         } else {
112             str = str + "0";
113         }
114         return Integer.parseInt(str);
115     }
116
117     /**
118      * Trims the text of the java version to start with numbers.
119      *
120      * @return the trimmed java version
121      */

122     private String JavaDoc getJavaVersionTrimmed() {
123         if (JAVA_VERSION != null) {
124             for (int i = 0; i < JAVA_VERSION.length(); i++) {
125                 char ch = JAVA_VERSION.charAt(i);
126                 if (ch >= '0' && ch <= '9') {
127                     return JAVA_VERSION.substring(i);
128                 }
129             }
130         }
131         return null;
132     }
133
134     /**
135      * Decides if the java version matches.
136      *
137      * @param versionPrefix
138      * the prefix for the java version
139      * @return true if matches, or false if not or can't determine
140      */

141     private boolean getJavaVersionMatches(String JavaDoc versionPrefix) {
142         if (JAVA_VERSION_TRIMMED == null) {
143             return false;
144         }
145         return JAVA_VERSION_TRIMMED.startsWith(versionPrefix);
146     }
147
148     /**
149      * Decides if the operating system matches.
150      *
151      * @param osNamePrefix
152      * the prefix for the os name
153      * @return true if matches, or false if not or can't determine
154      */

155     private boolean getOSMatches(String JavaDoc osNamePrefix) {
156         if (OS_NAME == null) {
157             return false;
158         }
159         return OS_NAME.startsWith(osNamePrefix);
160     }
161
162     /**
163      * Decides if the operating system matches.
164      *
165      * @param osNamePrefix
166      * the prefix for the os name
167      * @param osVersionPrefix
168      * the prefix for the version
169      * @return true if matches, or false if not or can't determine
170      */

171     private boolean getOSMatches(String JavaDoc osNamePrefix, String JavaDoc osVersionPrefix) {
172         if (OS_NAME == null || OS_VERSION == null) {
173             return false;
174         }
175         return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix);
176     }
177
178     protected void setUp() throws Exception JavaDoc {
179         super.setUp();
180     }
181
182     protected void tearDown() throws Exception JavaDoc {
183         super.tearDown();
184     }
185
186     //-----------------------------------------------------------------------
187
public void testConstructor() {
188         assertNotNull(new SystemUtils());
189         Constructor JavaDoc[] cons = SystemUtils.class.getDeclaredConstructors();
190         assertEquals(1, cons.length);
191         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
192         assertEquals(true, Modifier.isPublic(SystemUtils.class.getModifiers()));
193         assertEquals(false, Modifier.isFinal(SystemUtils.class.getModifiers()));
194     }
195
196     /**
197      * Assums no security manager exists.
198      */

199     public void testGetJavaHome() {
200         File JavaDoc dir = SystemUtils.getJavaHome();
201         Assert.assertNotNull(dir);
202         Assert.assertTrue(dir.exists());
203     }
204
205     /**
206      * Assums no security manager exists.
207      */

208     public void testGetJavaIoTmpDir() {
209         File JavaDoc dir = SystemUtils.getJavaIoTmpDir();
210         Assert.assertNotNull(dir);
211         Assert.assertTrue(dir.exists());
212     }
213
214     /**
215      * Assums no security manager exists.
216      */

217     public void testGetUserDir() {
218         File JavaDoc dir = SystemUtils.getUserDir();
219         Assert.assertNotNull(dir);
220         Assert.assertTrue(dir.exists());
221     }
222
223     /**
224      * Assums no security manager exists.
225      */

226     public void testGetUserHome() {
227         File JavaDoc dir = SystemUtils.getUserHome();
228         Assert.assertNotNull(dir);
229         Assert.assertTrue(dir.exists());
230     }
231
232     public void testIS_JAVA() {
233         String JavaDoc javaVersion = System.getProperty("java.version");
234         if (javaVersion == null) {
235             assertEquals(false, SystemUtils.IS_JAVA_1_1);
236             assertEquals(false, SystemUtils.IS_JAVA_1_2);
237             assertEquals(false, SystemUtils.IS_JAVA_1_3);
238             assertEquals(false, SystemUtils.IS_JAVA_1_4);
239             assertEquals(false, SystemUtils.IS_JAVA_1_5);
240         } else if (javaVersion.startsWith("1.1")) {
241             assertTrue(SystemUtils.IS_JAVA_1_1);
242         } else if (javaVersion.startsWith("1.2")) {
243             assertTrue(SystemUtils.IS_JAVA_1_2);
244         } else if (javaVersion.startsWith("1.3")) {
245             assertTrue(SystemUtils.IS_JAVA_1_3);
246         } else if (javaVersion.startsWith("1.4")) {
247             assertTrue(SystemUtils.IS_JAVA_1_4);
248         } else if (javaVersion.startsWith("1.5")) {
249             assertTrue(SystemUtils.IS_JAVA_1_5);
250         } else {
251             System.out.println("Can't test IS_JAVA value");
252         }
253     }
254
255     public void testIS_OS() {
256         String JavaDoc osName = System.getProperty("os.name");
257         if (osName == null) {
258             assertEquals(false, SystemUtils.IS_OS_WINDOWS);
259             assertEquals(false, SystemUtils.IS_OS_UNIX);
260             assertEquals(false, SystemUtils.IS_OS_SOLARIS);
261             assertEquals(false, SystemUtils.IS_OS_LINUX);
262             assertEquals(false, SystemUtils.IS_OS_MAC_OSX);
263         } else if (osName.startsWith("Windows")) {
264             assertEquals(false, SystemUtils.IS_OS_UNIX);
265             assertEquals(true, SystemUtils.IS_OS_WINDOWS);
266         } else if (osName.startsWith("Solaris")) {
267             assertEquals(true, SystemUtils.IS_OS_SOLARIS);
268             assertEquals(true, SystemUtils.IS_OS_UNIX);
269             assertEquals(false, SystemUtils.IS_OS_WINDOWS);
270         } else if (osName.toLowerCase().startsWith("linux")) {
271             assertEquals(true, SystemUtils.IS_OS_LINUX);
272             assertEquals(true, SystemUtils.IS_OS_UNIX);
273             assertEquals(false, SystemUtils.IS_OS_WINDOWS);
274         } else if (osName.startsWith("Mac OS X")) {
275             assertEquals(true, SystemUtils.IS_OS_MAC_OSX);
276             assertEquals(true, SystemUtils.IS_OS_UNIX);
277             assertEquals(false, SystemUtils.IS_OS_WINDOWS);
278         } else if (osName.startsWith("OS/2")) {
279             assertEquals(true, SystemUtils.IS_OS_OS2);
280             assertEquals(false, SystemUtils.IS_OS_UNIX);
281             assertEquals(false, SystemUtils.IS_OS_WINDOWS);
282         } else if (osName.startsWith("SunOS")) {
283             assertEquals(true, SystemUtils.IS_OS_SUN_OS);
284             assertEquals(true, SystemUtils.IS_OS_UNIX);
285             assertEquals(false, SystemUtils.IS_OS_WINDOWS);
286         } else {
287             System.out.println("Can't test IS_OS value");
288         }
289     }
290
291     //-----------------------------------------------------------------------
292
public void testJavaVersion() {
293         assertEquals(SystemUtils.JAVA_VERSION_FLOAT, SystemUtils.getJavaVersion(), 0f);
294     }
295
296     public void testJavaVersionAsFloat() {
297         JAVA_VERSION = null;
298         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
299         assertEquals(0f, getJavaVersionAsFloat(), 0.000001f);
300         JAVA_VERSION = "1.1";
301         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
302         assertEquals(1.1f, getJavaVersionAsFloat(), 0.000001f);
303         JAVA_VERSION = "1.2";
304         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
305         assertEquals(1.2f, getJavaVersionAsFloat(), 0.000001f);
306         JAVA_VERSION = "1.3.0";
307         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
308         assertEquals(1.3f, getJavaVersionAsFloat(), 0.000001f);
309         JAVA_VERSION = "1.3.1";
310         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
311         assertEquals(1.31f, getJavaVersionAsFloat(), 0.000001f);
312         JAVA_VERSION = "1.4.0";
313         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
314         assertEquals(1.4f, getJavaVersionAsFloat(), 0.000001f);
315         JAVA_VERSION = "1.4.1";
316         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
317         assertEquals(1.41f, getJavaVersionAsFloat(), 0.000001f);
318         JAVA_VERSION = "1.5.0";
319         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
320         assertEquals(1.5f, getJavaVersionAsFloat(), 0.000001f);
321         JAVA_VERSION = "1.6.0";
322         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
323         assertEquals(1.6f, getJavaVersionAsFloat(), 0.000001f);
324         JAVA_VERSION = "JavaVM-1.3.1"; //HP-UX
325
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
326         assertEquals(1.31f, getJavaVersionAsFloat(), 0.000001f);
327     }
328
329     public void testJavaVersionAsInt() {
330         JAVA_VERSION = null;
331         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
332         assertEquals(0, getJavaVersionAsInt());
333         JAVA_VERSION = "1.1";
334         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
335         assertEquals(110, getJavaVersionAsInt());
336         JAVA_VERSION = "1.2";
337         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
338         assertEquals(120, getJavaVersionAsInt());
339         JAVA_VERSION = "1.3.0";
340         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
341         assertEquals(130, getJavaVersionAsInt());
342         JAVA_VERSION = "1.3.1";
343         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
344         assertEquals(131, getJavaVersionAsInt());
345         JAVA_VERSION = "1.4.0";
346         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
347         assertEquals(140, getJavaVersionAsInt());
348         JAVA_VERSION = "1.4.1";
349         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
350         assertEquals(141, getJavaVersionAsInt());
351         JAVA_VERSION = "1.5.0";
352         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
353         assertEquals(150, getJavaVersionAsInt());
354         JAVA_VERSION = "1.6.0";
355         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
356         assertEquals(160, getJavaVersionAsInt());
357         JAVA_VERSION = "JavaVM-1.3.1"; //HP-UX
358
JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
359         assertEquals(131, getJavaVersionAsInt());
360     }
361
362     public void testJavaVersionAtLeastFloat() {
363         float version = SystemUtils.JAVA_VERSION_FLOAT;
364         assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
365         version -= 0.1f;
366         assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
367         version += 0.2f;
368         assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
369     }
370
371     public void testJavaVersionAtLeastInt() {
372         int version = SystemUtils.JAVA_VERSION_INT;
373         assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
374         version -= 10;
375         assertEquals(true, SystemUtils.isJavaVersionAtLeast(version));
376         version += 20;
377         assertEquals(false, SystemUtils.isJavaVersionAtLeast(version));
378     }
379
380     //-----------------------------------------------------------------------
381
public void testJavaVersionMatches() {
382         JAVA_VERSION = null;
383         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
384         assertEquals(false, getJavaVersionMatches("1.1"));
385         assertEquals(false, getJavaVersionMatches("1.2"));
386         assertEquals(false, getJavaVersionMatches("1.3"));
387         assertEquals(false, getJavaVersionMatches("1.4"));
388         assertEquals(false, getJavaVersionMatches("1.5"));
389         JAVA_VERSION = "1.1";
390         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
391         assertEquals(true, getJavaVersionMatches("1.1"));
392         assertEquals(false, getJavaVersionMatches("1.2"));
393         assertEquals(false, getJavaVersionMatches("1.3"));
394         assertEquals(false, getJavaVersionMatches("1.4"));
395         assertEquals(false, getJavaVersionMatches("1.5"));
396         JAVA_VERSION = "1.2";
397         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
398         assertEquals(false, getJavaVersionMatches("1.1"));
399         assertEquals(true, getJavaVersionMatches("1.2"));
400         assertEquals(false, getJavaVersionMatches("1.3"));
401         assertEquals(false, getJavaVersionMatches("1.4"));
402         assertEquals(false, getJavaVersionMatches("1.5"));
403         JAVA_VERSION = "1.3.0";
404         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
405         assertEquals(false, getJavaVersionMatches("1.1"));
406         assertEquals(false, getJavaVersionMatches("1.2"));
407         assertEquals(true, getJavaVersionMatches("1.3"));
408         assertEquals(false, getJavaVersionMatches("1.4"));
409         assertEquals(false, getJavaVersionMatches("1.5"));
410         JAVA_VERSION = "1.3.1";
411         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
412         assertEquals(false, getJavaVersionMatches("1.1"));
413         assertEquals(false, getJavaVersionMatches("1.2"));
414         assertEquals(true, getJavaVersionMatches("1.3"));
415         assertEquals(false, getJavaVersionMatches("1.4"));
416         assertEquals(false, getJavaVersionMatches("1.5"));
417         JAVA_VERSION = "1.4.0";
418         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
419         assertEquals(false, getJavaVersionMatches("1.1"));
420         assertEquals(false, getJavaVersionMatches("1.2"));
421         assertEquals(false, getJavaVersionMatches("1.3"));
422         assertEquals(true, getJavaVersionMatches("1.4"));
423         assertEquals(false, getJavaVersionMatches("1.5"));
424         JAVA_VERSION = "1.4.1";
425         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
426         assertEquals(false, getJavaVersionMatches("1.1"));
427         assertEquals(false, getJavaVersionMatches("1.2"));
428         assertEquals(false, getJavaVersionMatches("1.3"));
429         assertEquals(true, getJavaVersionMatches("1.4"));
430         assertEquals(false, getJavaVersionMatches("1.5"));
431         JAVA_VERSION = "1.5.0";
432         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
433         assertEquals(false, getJavaVersionMatches("1.1"));
434         assertEquals(false, getJavaVersionMatches("1.2"));
435         assertEquals(false, getJavaVersionMatches("1.3"));
436         assertEquals(false, getJavaVersionMatches("1.4"));
437         assertEquals(true, getJavaVersionMatches("1.5"));
438         JAVA_VERSION = "1.6.0";
439         JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
440         assertEquals(false, getJavaVersionMatches("1.1"));
441         assertEquals(false, getJavaVersionMatches("1.2"));
442         assertEquals(false, getJavaVersionMatches("1.3"));
443         assertEquals(false, getJavaVersionMatches("1.4"));
444         assertEquals(false, getJavaVersionMatches("1.5"));
445     }
446
447     public void testOSMatches() {
448         OS_NAME = null;
449         assertEquals(false, getOSMatches("Windows"));
450         OS_NAME = "Windows 95";
451         assertEquals(true, getOSMatches("Windows"));
452         OS_NAME = "Windows NT";
453         assertEquals(true, getOSMatches("Windows"));
454         OS_NAME = "OS/2";
455         assertEquals(false, getOSMatches("Windows"));
456     }
457
458     public void testOSMatches2() {
459         OS_NAME = null;
460         OS_VERSION = null;
461         assertEquals(false, getOSMatches("Windows 9", "4.1"));
462         OS_NAME = "Windows 95";
463         OS_VERSION = "4.0";
464         assertEquals(false, getOSMatches("Windows 9", "4.1"));
465         OS_NAME = "Windows 95";
466         OS_VERSION = "4.1";
467         assertEquals(true, getOSMatches("Windows 9", "4.1"));
468         OS_NAME = "Windows 98";
469         OS_VERSION = "4.1";
470         assertEquals(true, getOSMatches("Windows 9", "4.1"));
471         OS_NAME = "Windows NT";
472         OS_VERSION = "4.0";
473         assertEquals(false, getOSMatches("Windows 9", "4.1"));
474         OS_NAME = "OS/2";
475         OS_VERSION = "4.0";
476         assertEquals(false, getOSMatches("Windows 9", "4.1"));
477     }
478
479     public void testJavaAwtHeadless() {
480         boolean atLeastJava14 = SystemUtils.isJavaVersionAtLeast(140);
481         String JavaDoc expectedStringValue = System.getProperty("java.awt.headless");
482         String JavaDoc expectedStringValueWithDefault = System.getProperty("java.awt.headless", "false");
483         assertNotNull(expectedStringValueWithDefault);
484         if (atLeastJava14) {
485             boolean expectedValue = Boolean.valueOf(expectedStringValue).booleanValue();
486             if (expectedStringValue != null) {
487                 assertEquals(expectedStringValue, SystemUtils.JAVA_AWT_HEADLESS);
488             }
489             assertEquals(expectedValue, SystemUtils.isJavaAwtHeadless());
490         } else {
491             assertNull(expectedStringValue);
492             assertNull(SystemUtils.JAVA_AWT_HEADLESS);
493             assertEquals(expectedStringValueWithDefault, "" + SystemUtils.isJavaAwtHeadless());
494         }
495         assertEquals(expectedStringValueWithDefault, "" + SystemUtils.isJavaAwtHeadless());
496     }
497 }
498
Popular Tags