PageRenderTime 6ms CodeModel.GetById 4ms app.highlight 68ms RepoModel.GetById 1ms app.codeStats 0ms

/example/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java

https://bitbucket.org/Ranmanli/prfl
Java | 376 lines | 323 code | 16 blank | 37 comment | 36 complexity | 697c82b7f6736e01ba4d580efb2e8299 MD5 | raw file
  1/*
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 * http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing,
 13 * software distributed under the License is distributed on an
 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15 * KIND, either express or implied.  See the License for the
 16 * specific language governing permissions and limitations
 17 * under the License.
 18 */
 19
 20package org.apache.commons.lang3;
 21
 22import org.junit.Test;
 23import static org.junit.Assert.*;
 24import static org.apache.commons.lang3.JavaVersion.JAVA_1_4;
 25
 26import java.io.File;
 27import java.lang.reflect.Constructor;
 28import java.lang.reflect.Modifier;
 29import java.util.Locale;
 30
 31import junit.framework.Assert;
 32
 33/**
 34 * Unit tests {@link org.apache.commons.lang3.SystemUtils}.
 35 * 
 36 * Only limited testing can be performed.
 37 * 
 38 * @version $Id$
 39 */
 40public class SystemUtilsTest {
 41
 42    @Test
 43    public void testConstructor() {
 44        assertNotNull(new SystemUtils());
 45        Constructor<?>[] cons = SystemUtils.class.getDeclaredConstructors();
 46        assertEquals(1, cons.length);
 47        assertTrue(Modifier.isPublic(cons[0].getModifiers()));
 48        assertTrue(Modifier.isPublic(SystemUtils.class.getModifiers()));
 49        assertFalse(Modifier.isFinal(SystemUtils.class.getModifiers()));
 50    }
 51
 52    /**
 53     * Assums no security manager exists.
 54     */
 55    @Test
 56    public void testGetJavaHome() {
 57        File dir = SystemUtils.getJavaHome();
 58        Assert.assertNotNull(dir);
 59        Assert.assertTrue(dir.exists());
 60    }
 61
 62    /**
 63     * Assums no security manager exists.
 64     */
 65    @Test
 66    public void testGetJavaIoTmpDir() {
 67        File dir = SystemUtils.getJavaIoTmpDir();
 68        Assert.assertNotNull(dir);
 69        Assert.assertTrue(dir.exists());
 70    }
 71
 72    /**
 73     * Assums no security manager exists.
 74     */
 75    @Test
 76    public void testGetUserDir() {
 77        File dir = SystemUtils.getUserDir();
 78        Assert.assertNotNull(dir);
 79        Assert.assertTrue(dir.exists());
 80    }
 81
 82    /**
 83     * Assums no security manager exists.
 84     */
 85    @Test
 86    public void testGetUserHome() {
 87        File dir = SystemUtils.getUserHome();
 88        Assert.assertNotNull(dir);
 89        Assert.assertTrue(dir.exists());
 90    }
 91
 92    @Test
 93    public void testIS_JAVA() {
 94        String javaVersion = System.getProperty("java.version");
 95        if (javaVersion == null) {
 96            assertFalse(SystemUtils.IS_JAVA_1_1);
 97            assertFalse(SystemUtils.IS_JAVA_1_2);
 98            assertFalse(SystemUtils.IS_JAVA_1_3);
 99            assertFalse(SystemUtils.IS_JAVA_1_4);
100            assertFalse(SystemUtils.IS_JAVA_1_5);
101            assertFalse(SystemUtils.IS_JAVA_1_6);
102            assertFalse(SystemUtils.IS_JAVA_1_7);
103        } else if (javaVersion.startsWith("1.1")) {
104            assertTrue(SystemUtils.IS_JAVA_1_1);
105            assertFalse(SystemUtils.IS_JAVA_1_2);
106            assertFalse(SystemUtils.IS_JAVA_1_3);
107            assertFalse(SystemUtils.IS_JAVA_1_4);
108            assertFalse(SystemUtils.IS_JAVA_1_5);
109            assertFalse(SystemUtils.IS_JAVA_1_6);
110            assertFalse(SystemUtils.IS_JAVA_1_7);
111        } else if (javaVersion.startsWith("1.2")) {
112            assertFalse(SystemUtils.IS_JAVA_1_1);
113            assertTrue(SystemUtils.IS_JAVA_1_2);
114            assertFalse(SystemUtils.IS_JAVA_1_3);
115            assertFalse(SystemUtils.IS_JAVA_1_4);
116            assertFalse(SystemUtils.IS_JAVA_1_5);
117            assertFalse(SystemUtils.IS_JAVA_1_6);
118            assertFalse(SystemUtils.IS_JAVA_1_7);
119        } else if (javaVersion.startsWith("1.3")) {
120            assertFalse(SystemUtils.IS_JAVA_1_1);
121            assertFalse(SystemUtils.IS_JAVA_1_2);
122            assertTrue(SystemUtils.IS_JAVA_1_3);
123            assertFalse(SystemUtils.IS_JAVA_1_4);
124            assertFalse(SystemUtils.IS_JAVA_1_5);
125            assertFalse(SystemUtils.IS_JAVA_1_6);
126            assertFalse(SystemUtils.IS_JAVA_1_7);
127        } else if (javaVersion.startsWith("1.4")) {
128            assertFalse(SystemUtils.IS_JAVA_1_1);
129            assertFalse(SystemUtils.IS_JAVA_1_2);
130            assertFalse(SystemUtils.IS_JAVA_1_3);
131            assertTrue(SystemUtils.IS_JAVA_1_4);
132            assertFalse(SystemUtils.IS_JAVA_1_5);
133            assertFalse(SystemUtils.IS_JAVA_1_6);
134            assertFalse(SystemUtils.IS_JAVA_1_7);
135        } else if (javaVersion.startsWith("1.5")) {
136            assertFalse(SystemUtils.IS_JAVA_1_1);
137            assertFalse(SystemUtils.IS_JAVA_1_2);
138            assertFalse(SystemUtils.IS_JAVA_1_3);
139            assertFalse(SystemUtils.IS_JAVA_1_4);
140            assertTrue(SystemUtils.IS_JAVA_1_5);
141            assertFalse(SystemUtils.IS_JAVA_1_6);
142            assertFalse(SystemUtils.IS_JAVA_1_7);
143        } else if (javaVersion.startsWith("1.6")) {
144            assertFalse(SystemUtils.IS_JAVA_1_1);
145            assertFalse(SystemUtils.IS_JAVA_1_2);
146            assertFalse(SystemUtils.IS_JAVA_1_3);
147            assertFalse(SystemUtils.IS_JAVA_1_4);
148            assertFalse(SystemUtils.IS_JAVA_1_5);
149            assertTrue(SystemUtils.IS_JAVA_1_6);
150            assertFalse(SystemUtils.IS_JAVA_1_7);
151        } else {
152            System.out.println("Can't test IS_JAVA value: "+javaVersion);
153        }
154    }
155
156    @Test
157    public void testIS_OS() {
158        String osName = System.getProperty("os.name");
159        if (osName == null) {
160            assertFalse(SystemUtils.IS_OS_WINDOWS);
161            assertFalse(SystemUtils.IS_OS_UNIX);
162            assertFalse(SystemUtils.IS_OS_SOLARIS);
163            assertFalse(SystemUtils.IS_OS_LINUX);
164            assertFalse(SystemUtils.IS_OS_MAC_OSX);
165        } else if (osName.startsWith("Windows")) {
166            assertFalse(SystemUtils.IS_OS_UNIX);
167            assertTrue(SystemUtils.IS_OS_WINDOWS);
168        } else if (osName.startsWith("Solaris")) {
169            assertTrue(SystemUtils.IS_OS_SOLARIS);
170            assertTrue(SystemUtils.IS_OS_UNIX);
171            assertFalse(SystemUtils.IS_OS_WINDOWS);
172        } else if (osName.toLowerCase(Locale.ENGLISH).startsWith("linux")) {
173            assertTrue(SystemUtils.IS_OS_LINUX);
174            assertTrue(SystemUtils.IS_OS_UNIX);
175            assertFalse(SystemUtils.IS_OS_WINDOWS);
176        } else if (osName.startsWith("Mac OS X")) {
177            assertTrue(SystemUtils.IS_OS_MAC_OSX);
178            assertTrue(SystemUtils.IS_OS_UNIX);
179            assertFalse(SystemUtils.IS_OS_WINDOWS);
180        } else if (osName.startsWith("OS/2")) {
181            assertTrue(SystemUtils.IS_OS_OS2);
182            assertFalse(SystemUtils.IS_OS_UNIX);
183            assertFalse(SystemUtils.IS_OS_WINDOWS);
184        } else if (osName.startsWith("SunOS")) {
185            assertTrue(SystemUtils.IS_OS_SUN_OS);
186            assertTrue(SystemUtils.IS_OS_UNIX);
187            assertFalse(SystemUtils.IS_OS_WINDOWS);
188        } else if (osName.startsWith("FreeBSD")) {
189            assertTrue(SystemUtils.IS_OS_FREE_BSD);
190            assertTrue(SystemUtils.IS_OS_UNIX);
191            assertFalse(SystemUtils.IS_OS_WINDOWS);
192        } else {
193            System.out.println("Can't test IS_OS value: "+osName);
194        }
195    }
196
197    @Test
198    public void testJavaVersionMatches() {
199        String javaVersion = null;
200        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
201        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
202        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
203        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
204        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
205        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
206        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
207        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
208        javaVersion = "";
209        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
210        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
211        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
212        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
213        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
214        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
215        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
216        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
217        javaVersion = "1.0";
218        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
219        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
220        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
221        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
222        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
223        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
224        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
225        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
226        javaVersion = "1.1";
227        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
228        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
229        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
230        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
231        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
232        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
233        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
234        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
235        javaVersion = "1.2";
236        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
237        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
238        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
239        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
240        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
241        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
242        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
243        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
244        javaVersion = "1.3.0";
245        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
246        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
247        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
248        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
249        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
250        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
251        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
252        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
253        javaVersion = "1.3.1";
254        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
255        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
256        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
257        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
258        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
259        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
260        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
261        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
262        javaVersion = "1.4.0";
263        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
264        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
265        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
266        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
267        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
268        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
269        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
270        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
271        javaVersion = "1.4.1";
272        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
273        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
274        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
275        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
276        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
277        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
278        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
279        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
280        javaVersion = "1.4.2";
281        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
282        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
283        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
284        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
285        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
286        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
287        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
288        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
289        javaVersion = "1.5.0";
290        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
291        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
292        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
293        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
294        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
295        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
296        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
297        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
298        javaVersion = "1.6.0";
299        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
300        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
301        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
302        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
303        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
304        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
305        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
306        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
307        javaVersion = "1.7.0";
308        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.0"));
309        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.1"));
310        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.2"));
311        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.3"));
312        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.4"));
313        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.5"));
314        assertFalse(SystemUtils.isJavaVersionMatch(javaVersion, "1.6"));
315        assertTrue(SystemUtils.isJavaVersionMatch(javaVersion, "1.7"));
316    }
317
318    @Test
319    public void testOSMatchesName() {
320        String osName = null;
321        assertFalse(SystemUtils.isOSNameMatch(osName, "Windows"));
322        osName = "";
323        assertFalse(SystemUtils.isOSNameMatch(osName, "Windows"));
324        osName = "Windows 95";
325        assertTrue(SystemUtils.isOSNameMatch(osName, "Windows"));
326        osName = "Windows NT";
327        assertTrue(SystemUtils.isOSNameMatch(osName, "Windows"));
328        osName = "OS/2";
329        assertFalse(SystemUtils.isOSNameMatch(osName, "Windows"));
330    }
331
332    @Test
333    public void testOSMatchesNameAndVersion() {
334        String osName = null;
335        String osVersion = null;
336        assertFalse(SystemUtils.isOSMatch(osName, osVersion, "Windows 9", "4.1"));
337        osName = "";
338        osVersion = "";
339        assertFalse(SystemUtils.isOSMatch(osName, osVersion, "Windows 9", "4.1"));
340        osName = "Windows 95";
341        osVersion = "4.0";
342        assertFalse(SystemUtils.isOSMatch(osName, osVersion, "Windows 9", "4.1"));
343        osName = "Windows 95";
344        osVersion = "4.1";
345        assertTrue(SystemUtils.isOSMatch(osName, osVersion, "Windows 9", "4.1"));
346        osName = "Windows 98";
347        osVersion = "4.1";
348        assertTrue(SystemUtils.isOSMatch(osName, osVersion, "Windows 9", "4.1"));
349        osName = "Windows NT";
350        osVersion = "4.0";
351        assertFalse(SystemUtils.isOSMatch(osName, osVersion, "Windows 9", "4.1"));
352        osName = "OS/2";
353        osVersion = "4.0";
354        assertFalse(SystemUtils.isOSMatch(osName, osVersion, "Windows 9", "4.1"));
355    }
356
357    @Test
358    public void testJavaAwtHeadless() {
359        boolean atLeastJava14 = SystemUtils.isJavaVersionAtLeast(JAVA_1_4);
360        String expectedStringValue = System.getProperty("java.awt.headless");
361        String expectedStringValueWithDefault = System.getProperty("java.awt.headless", "false");
362        assertNotNull(expectedStringValueWithDefault);
363        if (atLeastJava14) {
364            boolean expectedValue = Boolean.valueOf(expectedStringValue).booleanValue();
365            if (expectedStringValue != null) {
366                assertEquals(expectedStringValue, SystemUtils.JAVA_AWT_HEADLESS);
367            }
368            assertEquals(expectedValue, SystemUtils.isJavaAwtHeadless());
369        } else {
370            assertNull(expectedStringValue);
371            assertNull(SystemUtils.JAVA_AWT_HEADLESS);
372            assertEquals(expectedStringValueWithDefault, "" + SystemUtils.isJavaAwtHeadless());
373        }
374        assertEquals(expectedStringValueWithDefault, "" + SystemUtils.isJavaAwtHeadless());
375    }
376}