1 16 17 package org.apache.commons.lang; 18 19 import java.io.File ; 20 import java.lang.reflect.Constructor ; 21 import java.lang.reflect.Modifier ; 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 38 public class SystemUtilsTest extends TestCase { 39 public static void main(String [] 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 private String JAVA_VERSION; 53 54 private String JAVA_VERSION_TRIMMED; 55 56 private String OS_NAME; 57 58 private String OS_VERSION; 59 60 public SystemUtilsTest(String name) { 61 super(name); 62 } 63 64 78 private float getJavaVersionAsFloat() { 79 if (JAVA_VERSION_TRIMMED == null) { 80 return 0f; 81 } 82 String 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 103 private int getJavaVersionAsInt() { 104 if (JAVA_VERSION == null) { 105 return 0; 106 } 107 String 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 122 private String 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 141 private boolean getJavaVersionMatches(String versionPrefix) { 142 if (JAVA_VERSION_TRIMMED == null) { 143 return false; 144 } 145 return JAVA_VERSION_TRIMMED.startsWith(versionPrefix); 146 } 147 148 155 private boolean getOSMatches(String osNamePrefix) { 156 if (OS_NAME == null) { 157 return false; 158 } 159 return OS_NAME.startsWith(osNamePrefix); 160 } 161 162 171 private boolean getOSMatches(String osNamePrefix, String 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 { 179 super.setUp(); 180 } 181 182 protected void tearDown() throws Exception { 183 super.tearDown(); 184 } 185 186 public void testConstructor() { 188 assertNotNull(new SystemUtils()); 189 Constructor [] 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 199 public void testGetJavaHome() { 200 File dir = SystemUtils.getJavaHome(); 201 Assert.assertNotNull(dir); 202 Assert.assertTrue(dir.exists()); 203 } 204 205 208 public void testGetJavaIoTmpDir() { 209 File dir = SystemUtils.getJavaIoTmpDir(); 210 Assert.assertNotNull(dir); 211 Assert.assertTrue(dir.exists()); 212 } 213 214 217 public void testGetUserDir() { 218 File dir = SystemUtils.getUserDir(); 219 Assert.assertNotNull(dir); 220 Assert.assertTrue(dir.exists()); 221 } 222 223 226 public void testGetUserHome() { 227 File dir = SystemUtils.getUserHome(); 228 Assert.assertNotNull(dir); 229 Assert.assertTrue(dir.exists()); 230 } 231 232 public void testIS_JAVA() { 233 String 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 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 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"; 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"; 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 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 expectedStringValue = System.getProperty("java.awt.headless"); 482 String 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 |