SystemTest.java revision dafd6348b2e1b46d10e1dbfa64fd2dd44bcfedf2
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.lang; 19 20import dalvik.annotation.AndroidOnly; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 24import dalvik.annotation.TestTargetClass; 25import tests.util.TestEnvironment; 26 27import java.io.ByteArrayInputStream; 28import java.io.ByteArrayOutputStream; 29import java.io.IOException; 30import java.io.InputStream; 31import java.io.PrintStream; 32import java.nio.channels.Channel; 33import java.nio.channels.spi.SelectorProvider; 34import java.security.Permission; 35import java.util.Map; 36import java.util.Properties; 37import java.util.Vector; 38 39@TestTargetClass(System.class) 40public class SystemTest extends junit.framework.TestCase { 41 42 static boolean flag = false; 43 44 static boolean ranFinalize = false; 45 46 /** 47 * @tests java.lang.System#setIn(java.io.InputStream) 48 */ 49 @TestTargetNew( 50 level = TestLevel.COMPLETE, 51 notes = "", 52 method = "setIn", 53 args = {java.io.InputStream.class} 54 ) 55 public void test_setInLjava_io_InputStream() { 56 InputStream orgIn = System.in; 57 InputStream in = new ByteArrayInputStream(new byte[0]); 58 System.setIn(in); 59 assertTrue("in not set", System.in == in); 60 System.setIn(orgIn); 61 62 SecurityManager sm = new SecurityManager() { 63 64 public void checkPermission(Permission perm) { 65 if(perm.getName().equals("setIO")) { 66 throw new SecurityException(); 67 } 68 } 69 }; 70 71 SecurityManager oldSm = System.getSecurityManager(); 72 System.setSecurityManager(sm); 73 74 try { 75 System.setIn(in); 76 fail("SecurityException should be thrown."); 77 } catch (SecurityException e) { 78 // expected 79 } finally { 80 System.setSecurityManager(oldSm); 81 } 82 } 83 84 /** 85 * @tests java.lang.System#setOut(java.io.PrintStream) 86 */ 87 @TestTargetNew( 88 level = TestLevel.COMPLETE, 89 notes = "", 90 method = "setOut", 91 args = {java.io.PrintStream.class} 92 ) 93 public void test_setOutLjava_io_PrintStream() { 94 PrintStream orgOut = System.out; 95 PrintStream out = new PrintStream(new ByteArrayOutputStream()); 96 System.setOut(out); 97 assertTrue("out not set", System.out == out); 98 System.setOut(orgOut); 99 100 SecurityManager sm = new SecurityManager() { 101 102 public void checkPermission(Permission perm) { 103 if(perm.getName().equals("setIO")) { 104 throw new SecurityException(); 105 } 106 } 107 }; 108 109 SecurityManager oldSm = System.getSecurityManager(); 110 System.setSecurityManager(sm); 111 112 try { 113 System.setOut(out); 114 fail("SecurityException should be thrown."); 115 } catch (SecurityException e) { 116 // expected 117 } finally { 118 System.setSecurityManager(oldSm); 119 } 120 } 121 122 /** 123 * @tests java.lang.System#setErr(java.io.PrintStream) 124 */ 125 @TestTargetNew( 126 level = TestLevel.COMPLETE, 127 notes = "", 128 method = "setErr", 129 args = {java.io.PrintStream.class} 130 ) 131 public void test_setErrLjava_io_PrintStream() { 132 PrintStream orgErr = System.err; 133 PrintStream err = new PrintStream(new ByteArrayOutputStream()); 134 System.setErr(err); 135 assertTrue("err not set", System.err == err); 136 System.setErr(orgErr); 137 138 SecurityManager sm = new SecurityManager() { 139 140 public void checkPermission(Permission perm) { 141 if(perm.getName().equals("setIO")) { 142 throw new SecurityException(); 143 } 144 } 145 }; 146 147 SecurityManager oldSm = System.getSecurityManager(); 148 System.setSecurityManager(sm); 149 150 try { 151 System.setErr(err); 152 fail("SecurityException should be thrown."); 153 } catch (SecurityException e) { 154 // expected 155 } finally { 156 System.setSecurityManager(oldSm); 157 } 158 } 159 160 /** 161 * @tests java.lang.System#arraycopy(java.lang.Object, int, 162 * java.lang.Object, int, int) 163 */ 164 @TestTargetNew( 165 level = TestLevel.COMPLETE, 166 notes = "", 167 method = "arraycopy", 168 args = {java.lang.Object.class, int.class, java.lang.Object.class, 169 int.class, int.class} 170 ) 171 public void test_arraycopyLjava_lang_ObjectILjava_lang_ObjectII() { 172 // Test for method void java.lang.System.arraycopy(java.lang.Object, 173 // int, java.lang.Object, int, int) 174 Integer a[] = new Integer[20]; 175 Integer b[] = new Integer[20]; 176 int i = 0; 177 while (i < a.length) { 178 a[i] = new Integer(i); 179 ++i; 180 } 181 System.arraycopy(a, 0, b, 0, a.length); 182 for (i = 0; i < a.length; i++) 183 assertTrue("Copied elements incorrectly", a[i].equals(b[i])); 184 185 /* Non primitive array types don't need to be identical */ 186 String[] source1 = new String[] { "element1" }; 187 Object[] dest1 = new Object[1]; 188 System.arraycopy(source1, 0, dest1, 0, dest1.length); 189 assertTrue("Invalid copy 1", dest1[0] == source1[0]); 190 191 char[][] source = new char[][] { { 'H', 'e', 'l', 'l', 'o' }, 192 { 'W', 'o', 'r', 'l', 'd' } }; 193 char[][] dest = new char[2][]; 194 System.arraycopy(source, 0, dest, 0, dest.length); 195 assertTrue("Invalid copy 2", dest[0] == source[0] 196 && dest[1] == source[1]); 197 198 try { 199 // copy from non array object into Object array 200 System.arraycopy(new Object(), 0, b, 0, 0); 201 fail("ArrayStoreException is not thrown."); 202 } catch(ArrayStoreException ase) { 203 //expected 204 } 205 206 try { 207 // copy from Object array into non array object 208 System.arraycopy(a, 0, new Object(), 0, 0); 209 fail("ArrayStoreException is not thrown."); 210 } catch(ArrayStoreException ase) { 211 //expected 212 } 213 214 try { 215 // copy from primitive array into object array 216 System.arraycopy(new char[] {'a'}, 0, new String[1], 0, 1); 217 fail("ArrayStoreException is not thrown."); 218 } catch(ArrayStoreException ase) { 219 //expected 220 } 221 222 try { 223 // copy from object array into primitive array 224 System.arraycopy(new String[] {"a"}, 0, new char[1], 0, 1); 225 fail("ArrayStoreException is not thrown."); 226 } catch(ArrayStoreException ase) { 227 //expected 228 } 229 230 try { 231 // copy from primitive array into an array of another primitive type 232 System.arraycopy(new char[] {'a'}, 0, new int[1], 0, 1); 233 fail("ArrayStoreException is not thrown."); 234 } catch(ArrayStoreException ase) { 235 //expected 236 } 237 238 try { 239 // copy from object array into an array of another Object type 240 System.arraycopy(new Character[] {'a'}, 0, new Integer[1], 0, 1); 241 fail("ArrayStoreException is not thrown."); 242 } catch(ArrayStoreException ase) { 243 //expected 244 } 245 246 try { 247 // copy from null into an array of a primitive type 248 System.arraycopy(null, 0, new int[1], 0, 1); 249 fail("NullPointerException is not thrown."); 250 } catch(NullPointerException npe) { 251 //expected 252 } 253 254 try { 255 // copy from a primitive array into null 256 System.arraycopy(new int[]{'1'}, 0, null, 0, 1); 257 fail("NullPointerException is not thrown."); 258 } catch(NullPointerException npe) { 259 //expected 260 } 261 262 try { 263 System.arraycopy(a, a.length + 1, b, 0, 1); 264 fail("IndexOutOfBoundsException is not thrown."); 265 } catch(IndexOutOfBoundsException ioobe) { 266 //expected 267 } 268 269 try { 270 System.arraycopy(a, -1, b, 0, 1); 271 fail("IndexOutOfBoundsException is not thrown."); 272 } catch(IndexOutOfBoundsException ioobe) { 273 //expected 274 } 275 276 try { 277 System.arraycopy(a, 0, b, -1, 1); 278 fail("IndexOutOfBoundsException is not thrown."); 279 } catch(IndexOutOfBoundsException ioobe) { 280 //expected 281 } 282 283 try { 284 System.arraycopy(a, 0, b, 0, -1); 285 fail("IndexOutOfBoundsException is not thrown."); 286 } catch(IndexOutOfBoundsException ioobe) { 287 //expected 288 } 289 290 try { 291 System.arraycopy(a, 11, b, 0, 10); 292 fail("IndexOutOfBoundsException is not thrown."); 293 } catch(IndexOutOfBoundsException ioobe) { 294 //expected 295 } 296 297 try { 298 System.arraycopy(a, Integer.MAX_VALUE, b, 0, 10); 299 fail("IndexOutOfBoundsException is not thrown."); 300 } catch(IndexOutOfBoundsException ioobe) { 301 //expected 302 } 303 304 try { 305 System.arraycopy(a, 0, b, Integer.MAX_VALUE, 10); 306 fail("IndexOutOfBoundsException is not thrown."); 307 } catch(IndexOutOfBoundsException ioobe) { 308 //expected 309 } 310 311 try { 312 System.arraycopy(a, 0, b, 10, Integer.MAX_VALUE); 313 fail("IndexOutOfBoundsException is not thrown."); 314 } catch(IndexOutOfBoundsException ioobe) { 315 //expected 316 } 317 } 318 319 /** 320 * @tests java.lang.System#currentTimeMillis() 321 */ 322 @TestTargetNew( 323 level = TestLevel.COMPLETE, 324 notes = "", 325 method = "currentTimeMillis", 326 args = {} 327 ) 328 public void test_currentTimeMillis() { 329 // Test for method long java.lang.System.currentTimeMillis() 330 try { 331 long firstRead = System.currentTimeMillis(); 332 try { 333 Thread.sleep(150); 334 } catch (InterruptedException e) { 335 } 336 long secondRead = System.currentTimeMillis(); 337 assertTrue("Incorrect times returned: " + firstRead + ", " 338 + secondRead, firstRead < secondRead); 339 } catch (Exception e) { 340 fail("Exception during test: " + e.toString()); 341 } 342 } 343 344 /** 345 * @tests java.lang.System#exit(int) 346 */ 347 @TestTargetNew( 348 level = TestLevel.COMPLETE, 349 notes = "Verifies SecurityException.", 350 method = "exit", 351 args = {int.class} 352 ) 353 public void test_exitI() { 354 SecurityManager sm = new SecurityManager() { 355 356 final String forbidenPermissionName = "user.dir"; 357 358 public void checkPermission(Permission perm) { 359 if (perm.getName().equals(forbidenPermissionName)) { 360 throw new SecurityException(); 361 } 362 } 363 364 public void checkExit(int status) { 365 if(status == -1) 366 throw new SecurityException(); 367 } 368 369 }; 370 371 SecurityManager oldSm = System.getSecurityManager(); 372 System.setSecurityManager(sm); 373 try { 374 System.exit(-1); 375 fail("Should throw SecurityException"); 376 } catch (SecurityException e) { 377 // expected 378 } finally { 379 System.setSecurityManager(oldSm); 380 } 381 } 382 383 /** 384 * @tests java.lang.System#getProperties() 385 */ 386 @TestTargetNew( 387 level = TestLevel.COMPLETE, 388 notes = "", 389 method = "getProperties", 390 args = {} 391 ) 392 public void test_getProperties() { 393 394 // Test for method java.util.Properties java.lang.System.getProperties() 395 /* String[] props = { "java.version", "java.vendor", "java.vendor.url", 396 "java.home", "java.vm.specification.version", 397 "java.vm.specification.vendor", "java.vm.specification.name", 398 "java.vm.version", "java.vm.vendor", "java.vm.name", 399 "java.specification.name", "java.specification.vendor", 400 "java.specification.name", "java.class.version", 401 "java.class.path", "java.ext.dirs", "os.name", "os.arch", 402 "os.version", "file.separator", "path.separator", 403 "line.separator", "user.name", "user.home", "user.dir", }; 404 */ 405 406 String [] props = {"java.vendor.url", 407 "java.class.path", "user.home", 408 "java.class.version", "os.version", 409 "java.vendor", "user.dir", 410 /*"user.timezone",*/ "path.separator", 411 "os.name", "os.arch", 412 "line.separator", "file.separator", 413 "user.name", "java.version", "java.home" }; 414 415 /* Available properties. 416 String [] props = {"java.boot.class.path", "java.class.path", 417 "java.class.version", "java.compiler", "java.ext.dirs", 418 "java.home", "java.io.tmpdir", "java.library.path", 419 "java.vendor", "java.vendor.url", "java.version", 420 "java.vm.name", "java.vm.specification.name", 421 "java.vm.specification.vendor", "java.vm.specification.version", 422 "java.vm.vendor", "java.vm.version", "java.specification.name", 423 "java.specification.vendor", "java.specification.version", 424 "os.arch", "os.name", "os.version", "user.home", "user.name", 425 "user.dir", "file.separator", "line.separator", 426 "path.separator", "java.runtime.name", "java.runtime.version", 427 "java.vm.vendor.url", "file.encoding","user.language", 428 "user.region"}; 429 */ 430 431 Properties p = System.getProperties(); 432 assertTrue(p.size() > 0); 433 434 // Ensure spec'ed properties are non-null. See System.getProperties() 435 // spec. 436 437 for (int i = 0; i < props.length; i++) { 438 assertNotNull("There is no property among returned properties: " 439 + props[i], p.getProperty(props[i])); 440 assertNotNull("System property is null: " + props[i], 441 System.getProperty(props[i])); 442 } 443 444 SecurityManager sm = new SecurityManager() { 445 446 public void checkPermission(Permission perm) { 447 } 448 449 public void checkPropertiesAccess() { 450 throw new SecurityException(); 451 } 452 }; 453 454 SecurityManager oldSm = System.getSecurityManager(); 455 System.setSecurityManager(sm); 456 457 try { 458 System.getProperties(); 459 fail("SecurityException should be thrown."); 460 } catch (SecurityException e) { 461 // expected 462 } finally { 463 System.setSecurityManager(oldSm); 464 } 465 } 466 467 /** 468 * @tests java.lang.System#getProperty(java.lang.String) 469 */ 470 @TestTargetNew( 471 level = TestLevel.COMPLETE, 472 notes = "", 473 method = "getProperty", 474 args = {java.lang.String.class} 475 ) 476 public void test_getPropertyLjava_lang_String() { 477 // Test for method java.lang.String 478 // java.lang.System.getProperty(java.lang.String) 479 assertTrue("Failed to return correct property value", System 480 .getProperty("line.separator").indexOf("\n", 0) >= 0); 481 482 boolean is8859_1 = true; 483 String encoding = System.getProperty("file.encoding"); 484 byte[] bytes = new byte[128]; 485 char[] chars = new char[128]; 486 for (int i = 0; i < bytes.length; i++) { 487 bytes[i] = (byte) (i + 128); 488 chars[i] = (char) (i + 128); 489 } 490 String charResult = new String(bytes); 491 byte[] byteResult = new String(chars).getBytes(); 492 if (charResult.length() == 128 && byteResult.length == 128) { 493 for (int i = 0; i < bytes.length; i++) { 494 if (charResult.charAt(i) != (char) (i + 128) 495 || byteResult[i] != (byte) (i + 128)) 496 is8859_1 = false; 497 } 498 } else 499 is8859_1 = false; 500 String[] possibles = new String[] { "ISO8859_1", "8859_1", "ISO8859-1", 501 "ISO-8859-1", "ISO_8859-1", "ISO_8859-1:1978", "ISO-IR-100", 502 "LATIN1", "CSISOLATIN1" }; 503 boolean found8859_1 = false; 504 for (int i = 0; i < possibles.length; i++) { 505 if (possibles[i].equals(encoding)) { 506 found8859_1 = true; 507 break; 508 } 509 } 510 assertTrue("Wrong encoding: " + encoding, !is8859_1 || found8859_1); 511 512 try { 513 System.getProperty(null); 514 fail("NullPointerException should be thrown."); 515 } catch(NullPointerException npe) { 516 //expected 517 } 518 519 try { 520 System.getProperty(""); 521 fail("IllegalArgumentException should be thrown."); 522 } catch(IllegalArgumentException iae) { 523 //expected 524 } 525 526 SecurityManager sm = new SecurityManager() { 527 528 public void checkPermission(Permission perm) { 529 } 530 531 @SuppressWarnings("unused") 532 public void checkPropertyAccess(String key) { 533 throw new SecurityException(); 534 } 535 }; 536 537 SecurityManager oldSm = System.getSecurityManager(); 538 System.setSecurityManager(sm); 539 540 try { 541 System.getProperty("user.name"); 542 fail("SecurityException should be thrown."); 543 } catch (SecurityException e) { 544 // expected 545 } finally { 546 System.setSecurityManager(oldSm); 547 } 548 } 549 550 /** 551 * @tests java.lang.System#getProperty(java.lang.String, java.lang.String) 552 */ 553 @TestTargetNew( 554 level = TestLevel.COMPLETE, 555 notes = "", 556 method = "getProperty", 557 args = {java.lang.String.class, java.lang.String.class} 558 ) 559 public void test_getPropertyLjava_lang_StringLjava_lang_String() { 560 // Test for method java.lang.String 561 // java.lang.System.getProperty(java.lang.String, java.lang.String) 562 assertTrue("Failed to return correct property value: " 563 + System.getProperty("line.separator", "99999"), System 564 .getProperty("line.separator", "99999").indexOf("\n", 0) >= 0); 565 assertEquals("Failed to return correct property value", "bogus", System 566 .getProperty("bogus.prop", "bogus")); 567 568 try { 569 System.getProperty(null, "0.0"); 570 fail("NullPointerException should be thrown."); 571 } catch(NullPointerException npe) { 572 //expected 573 } 574 575 try { 576 System.getProperty("", "0"); 577 fail("IllegalArgumentException should be thrown."); 578 } catch(IllegalArgumentException iae) { 579 //expected 580 } 581 582 SecurityManager sm = new SecurityManager() { 583 584 public void checkPermission(Permission perm) { 585 } 586 587 @SuppressWarnings("unused") 588 public void checkPropertyAccess(String key) { 589 throw new SecurityException(); 590 } 591 }; 592 593 SecurityManager oldSm = System.getSecurityManager(); 594 System.setSecurityManager(sm); 595 596 try { 597 System.getProperty("java.version", "0"); 598 fail("SecurityException should be thrown."); 599 } catch (SecurityException e) { 600 // expected 601 } finally { 602 System.setSecurityManager(oldSm); 603 } 604 } 605 606 /** 607 * @tests java.lang.System#setProperty(java.lang.String, java.lang.String) 608 */ 609 @TestTargetNew( 610 level = TestLevel.COMPLETE, 611 notes = "", 612 method = "setProperty", 613 args = {java.lang.String.class, java.lang.String.class} 614 ) 615 public void test_setPropertyLjava_lang_StringLjava_lang_String() { 616 // Test for method java.lang.String 617 // java.lang.System.setProperty(java.lang.String, java.lang.String) 618 619 assertNull("Failed to return null", System.setProperty("testing", 620 "value1")); 621 assertTrue("Failed to return old value", System.setProperty("testing", 622 "value2") == "value1"); 623 assertTrue("Failed to find value", 624 System.getProperty("testing") == "value2"); 625 626 boolean exception = false; 627 try { 628 System.setProperty("", "default"); 629 } catch (IllegalArgumentException e) { 630 exception = true; 631 } 632 assertTrue("Expected IllegalArgumentException", exception); 633 } 634 635 /** 636 * @tests java.lang.System#getSecurityManager() 637 */ 638 @TestTargetNew( 639 level = TestLevel.PARTIAL, 640 notes = "Doesn't check positive functionality.", 641 method = "getSecurityManager", 642 args = {} 643 ) 644 public void test_getSecurityManager() { 645 // Test for method java.lang.SecurityManager 646 // java.lang.System.getSecurityManager() 647 assertNull("Returned incorrect SecurityManager", System 648 .getSecurityManager()); 649 } 650 651 /** 652 * @tests java.lang.System#identityHashCode(java.lang.Object) 653 */ 654 @TestTargetNew( 655 level = TestLevel.COMPLETE, 656 notes = "", 657 method = "identityHashCode", 658 args = {java.lang.Object.class} 659 ) 660 public void test_identityHashCodeLjava_lang_Object() { 661 // Test for method int 662 // java.lang.System.identityHashCode(java.lang.Object) 663 Object o = new Object(); 664 String s = "Gabba"; 665 assertEquals("Nonzero returned for null", 666 0, System.identityHashCode(null)); 667 assertTrue("Nonequal has returned for Object", System 668 .identityHashCode(o) == o.hashCode()); 669 assertTrue("Same as usual hash returned for String", System 670 .identityHashCode(s) != s.hashCode()); 671 } 672 673 /** 674 * @throws IOException 675 * @tests java.lang.System#inheritedChannel() 676 */ 677 @TestTargetNew( 678 level = TestLevel.COMPLETE, 679 notes = "", 680 method = "inheritedChannel", 681 args = {} 682 ) 683 public void test_inheritedChannel() throws IOException { 684 Channel iChannel = System.inheritedChannel(); 685 assertNull("Incorrect value of channel", iChannel); 686 SelectorProvider sp = SelectorProvider.provider(); 687 assertEquals("Incorrect value of channel", 688 sp.inheritedChannel(), iChannel); 689 690 SecurityManager sm = new SecurityManager() { 691 692 public void checkPermission(Permission perm) { 693 if(perm.getName().equals("inheritedChannel")) { 694 throw new SecurityException(); 695 } 696 } 697 }; 698 699 SecurityManager oldSm = System.getSecurityManager(); 700 System.setSecurityManager(sm); 701 702 try { 703 System.inheritedChannel(); 704 fail("SecurityException should be thrown."); 705 } catch (SecurityException e) { 706 // expected 707 } finally { 708 System.setSecurityManager(oldSm); 709 } 710 } 711 712 /** 713 * @tests java.lang.System#runFinalization() 714 */ 715 @TestTargetNew( 716 level = TestLevel.COMPLETE, 717 notes = "", 718 method = "runFinalization", 719 args = {} 720 ) 721 public void test_runFinalization() { 722 // Test for method void java.lang.System.runFinalization() 723 724 flag = true; 725 createInstance(); 726 int count = 10; 727 // the gc below likely bogosifies the test, but will have to do for 728 // the moment 729 while (!ranFinalize && count-- > 0) { 730 System.gc(); 731 System.runFinalization(); 732 } 733 assertTrue("Failed to run finalization", ranFinalize); 734 } 735 736 /** 737 * @tests java.lang.System#runFinalizersOnExit(boolean) 738 */ 739 @TestTargetNew( 740 level = TestLevel.COMPLETE, 741 notes = "", 742 method = "runFinalizersOnExit", 743 args = {boolean.class} 744 ) 745 @SuppressWarnings("deprecation") 746 public void test_runFinalizersOnExitZ() { 747 // Can we call the method at least? 748 try { 749 System.runFinalizersOnExit(false); 750 } catch (Throwable t) { 751 fail("Failed to set runFinalizersOnExit"); 752 } 753 754 try { 755 System.runFinalizersOnExit(true); 756 } catch (Throwable t) { 757 fail("Failed to set runFinalizersOnExit"); 758 } 759 760 SecurityManager sm = new SecurityManager() { 761 762 public void checkPermission(Permission perm) { 763 } 764 765 public void checkExit(int status) { 766 throw new SecurityException(); 767 } 768 }; 769 770 SecurityManager oldSm = System.getSecurityManager(); 771 System.setSecurityManager(sm); 772 773 try { 774 System.runFinalizersOnExit(true); 775 fail("SecurityException should be thrown."); 776 } catch (SecurityException e) { 777 // expected 778 } finally { 779 System.setSecurityManager(oldSm); 780 } 781 } 782 783 /** 784 * @tests java.lang.System#setProperties(java.util.Properties) 785 */ 786 @TestTargetNew( 787 level = TestLevel.COMPLETE, 788 notes = "", 789 method = "setProperties", 790 args = {java.util.Properties.class} 791 ) 792 public void test_setPropertiesLjava_util_Properties() { 793 // Test for method void 794 // java.lang.System.setProperties(java.util.Properties) 795 796 Properties orgProps = System.getProperties(); 797 java.util.Properties tProps = new java.util.Properties(); 798 tProps.put("test.prop", "this is a test property"); 799 tProps.put("bogus.prop", "bogus"); 800 System.setProperties(tProps); 801 try { 802 assertEquals("Failed to set properties", "this is a test property", System.getProperties() 803 .getProperty("test.prop")); 804 } finally { 805 // restore the original properties 806 System.setProperties(orgProps); 807 } 808 } 809 810 //Regression Test for Harmony-2356 811 @TestTargetNew( 812 level = TestLevel.COMPLETE, 813 notes = "", 814 method = "getenv", 815 args = {} 816 ) 817 public void testEnvUnmodifiable() { 818 Map map = System.getenv(); 819 try { 820 map.containsKey(null); 821 fail("Should throw NullPointerExcepiton."); 822 } catch (NullPointerException e) { 823 // expected 824 } 825 826 try { 827 map.containsKey(new Integer(10)); 828 fail("Should throw ClassCastException."); 829 } catch (ClassCastException e) { 830 // expected 831 } 832 833 try { 834 map.containsValue(null); 835 fail("Should throw NullPointerExcepiton."); 836 } catch (NullPointerException e) { 837 // expected 838 } 839 840 try { 841 map.containsValue(new Integer(10)); 842 fail("Should throw ClassCastException."); 843 } catch (ClassCastException e) { 844 // expected 845 } 846 847 try { 848 map.get(null); 849 fail("Should throw NullPointerExcepiton."); 850 } catch (NullPointerException e) { 851 // expected 852 } 853 854 try { 855 map.get(new Integer(10)); 856 fail("Should throw ClassCastException."); 857 } catch (ClassCastException e) { 858 // expected 859 } 860 861 try { 862 map.put(null, "AAA"); 863 fail("Should throw UnsupportedOperationExcepiton."); 864 } catch (UnsupportedOperationException e) { 865 // expected 866 } 867 868 try { 869 map.put("AAA", new Integer(10)); 870 fail("Should throw UnsupportedOperationException."); 871 } catch (UnsupportedOperationException e) { 872 // expected 873 } 874 875 try { 876 map.put("AAA", "BBB"); 877 fail("Should throw UnsupportedOperationException."); 878 } catch (UnsupportedOperationException e) { 879 // expected 880 } 881 882 try { 883 map.clear(); 884 fail("Should throw UnsupportedOperationException."); 885 } catch (UnsupportedOperationException e) { 886 // expected 887 } 888 889 try { 890 map.remove(null); 891 fail("Should throw UnsupportedOperationException."); 892 } catch (UnsupportedOperationException e) { 893 // expected 894 } 895 896 } 897 @TestTargets({ 898 @TestTargetNew( 899 level = TestLevel.COMPLETE, 900 notes = "", 901 method = "setSecurityManager", 902 args = {java.lang.SecurityManager.class} 903 ), 904 @TestTargetNew( 905 level = TestLevel.COMPLETE, 906 notes = "", 907 method = "getSecurityManager", 908 args = {} 909 ) 910 }) 911 public void test_setSecurityManagerLjava_lang_SecurityManager() { 912 assertEquals("Incorrect SecurityManager", 913 null, System.getSecurityManager()); 914 try { 915 SecurityManager localManager = new MockSecurityManager(); 916 System.setSecurityManager(localManager); 917 assertEquals("Incorrect SecurityManager", 918 localManager, System.getSecurityManager()); 919 } finally { 920 System.setSecurityManager(null); 921 } 922 } 923 924 @TestTargetNew( 925 level = TestLevel.COMPLETE, 926 notes = "", 927 method = "clearProperty", 928 args = {java.lang.String.class} 929 ) 930 public void test_clearProperty() { 931 System.setProperty("test", "value"); 932 System.clearProperty("test"); 933 assertNull("Property was not deleted.", System.getProperty("test")); 934 935 try { 936 System.clearProperty(null); 937 fail("NullPointerException is not thrown."); 938 } catch(NullPointerException npe) { 939 //expected 940 } 941 942 try { 943 System.clearProperty(""); 944 fail("IllegalArgumentException is not thrown."); 945 } catch(IllegalArgumentException iae) { 946 //expected 947 } 948 949 SecurityManager sm = new SecurityManager() { 950 951 public void checkPermission(Permission perm) { 952 if (perm.getName().equals("test")) { 953 throw new SecurityException(); 954 } 955 } 956 }; 957 958 SecurityManager oldSm = System.getSecurityManager(); 959 System.setSecurityManager(sm); 960 try { 961 System.clearProperty("test"); 962 fail("SecurityException should be thrown."); 963 } catch (SecurityException e) { 964 // expected 965 } finally { 966 System.setSecurityManager(oldSm); 967 } 968 } 969 970 @TestTargetNew( 971 level = TestLevel.COMPLETE, 972 notes = "", 973 method = "gc", 974 args = {} 975 ) 976 public void test_gc() { 977 Runtime rt = Runtime.getRuntime(); 978 Vector<StringBuffer> vec = new Vector<StringBuffer>(); 979 long beforeTest = rt.freeMemory(); 980 while(rt.freeMemory() < beforeTest * 2/3) { 981 vec.add(new StringBuffer(1000)); 982 } 983 vec = null; 984 long beforeGC = rt.freeMemory(); 985 System.gc(); 986 long afterGC = rt.freeMemory(); 987 assertTrue("memory was not released after calling System.gc()." + 988 "before gc: " + beforeGC + "; after gc: " + afterGC, 989 beforeGC < afterGC); 990 } 991 992 @TestTargetNew( 993 level = TestLevel.COMPLETE, 994 notes = "", 995 method = "getenv", 996 args = {} 997 ) 998 public void test_getenv() { 999 1000 // String[] props = { "PATH", "HOME", "USER"}; 1001 // only PATH of these three exists on android 1002 String[] props = { "PATH" }; 1003 1004 Map<String,String> envMap = System.getenv(); 1005 assertFalse("environment map is empty.", envMap.isEmpty()); 1006 assertTrue("env map contains less than 3 keys.", 1007 props.length < envMap.keySet().size()); 1008 for(int i = 0; i < props.length; i++) { 1009 assertNotNull("There is no property: " + props[i], 1010 envMap.get(props[i])); 1011 } 1012 1013 SecurityManager sm = new SecurityManager() { 1014 1015 public void checkPermission(Permission perm) { 1016 if(perm.getName().equals("getenv.*")) { 1017 throw new SecurityException(); 1018 } 1019 } 1020 }; 1021 1022 SecurityManager oldSm = System.getSecurityManager(); 1023 System.setSecurityManager(sm); 1024 1025 try { 1026 System.getenv(); 1027 fail("SecurityException should be thrown."); 1028 } catch (SecurityException e) { 1029 // expected 1030 } finally { 1031 System.setSecurityManager(oldSm); 1032 } 1033 } 1034 1035 @TestTargetNew( 1036 level = TestLevel.COMPLETE, 1037 notes = "", 1038 method = "getenv", 1039 args = {java.lang.String.class} 1040 ) 1041 public void test_getenvLString() { 1042 1043 assertNotNull("PATH environment variable is not found", 1044 System.getenv("PATH")); 1045 1046 assertNull("Doesn't return NULL for non existent property", 1047 System.getenv("nonexistent.property")); 1048 1049 SecurityManager sm = new SecurityManager() { 1050 1051 public void checkPermission(Permission perm) { 1052 if(perm.getName().equals("getenv.PATH")) { 1053 throw new SecurityException(); 1054 } 1055 } 1056 }; 1057 1058 SecurityManager oldSm = System.getSecurityManager(); 1059 System.setSecurityManager(sm); 1060 1061 try { 1062 System.getenv("PATH"); 1063 fail("SecurityException should be thrown."); 1064 } catch (SecurityException e) { 1065 // expected 1066 } finally { 1067 System.setSecurityManager(oldSm); 1068 } 1069 1070 try { 1071 System.getenv(null); 1072 fail("NullPointerException is not thrown."); 1073 } catch(NullPointerException npe) { 1074 //expected 1075 } 1076 } 1077 1078 @TestTargetNew( 1079 level = TestLevel.COMPLETE, 1080 notes = "", 1081 method = "load", 1082 args = {java.lang.String.class} 1083 ) 1084 @AndroidOnly("No x86 version of this library") 1085 public void test_load() { 1086 try { 1087 new TestLibrary().checkString(); 1088 fail("UnsatisfiedLinkError was not thrown."); 1089 } catch(UnsatisfiedLinkError e) { 1090 //expected 1091 } 1092 1093 try { 1094 System.load("nonExistentLibrary"); 1095 fail("UnsatisfiedLinkError was not thrown."); 1096 } catch(UnsatisfiedLinkError ule) { 1097 //expected 1098 } 1099 1100 try { 1101 System.load(null); 1102 fail("NullPointerException was not thrown."); 1103 } catch(NullPointerException npe) { 1104 //expected 1105 } 1106 1107 SecurityManager sm = new SecurityManager() { 1108 1109 public void checkPermission(Permission perm) { 1110 1111 } 1112 1113 public void checkLink(String lib) { 1114 throw new SecurityException(); 1115 } 1116 }; 1117 1118 SecurityManager oldSm = System.getSecurityManager(); 1119 System.setSecurityManager(sm); 1120 try { 1121 System.load("libTestLibrary.so"); 1122 fail("SecurityException should be thrown."); 1123 } catch (SecurityException e) { 1124 // expected 1125 } finally { 1126 System.setSecurityManager(oldSm); 1127 } 1128 } 1129 1130 @TestTargetNew( 1131 level = TestLevel.COMPLETE, 1132 notes = "", 1133 method = "loadLibrary", 1134 args = {java.lang.String.class} 1135 ) 1136 public void test_loadLibrary() { 1137 1138 try { 1139 System.loadLibrary("nonExistentLibrary"); 1140 fail("UnsatisfiedLinkError was not thrown."); 1141 } catch(UnsatisfiedLinkError ule) { 1142 //expected 1143 } 1144 1145 try { 1146 System.loadLibrary(null); 1147 fail("NullPointerException was not thrown."); 1148 } catch(NullPointerException npe) { 1149 //expected 1150 } 1151 1152 SecurityManager sm = new SecurityManager() { 1153 1154 public void checkPermission(Permission perm) { 1155 } 1156 1157 public void checkLink(String lib) { 1158 throw new SecurityException(); 1159 } 1160 }; 1161 1162 SecurityManager oldSm = System.getSecurityManager(); 1163 System.setSecurityManager(sm); 1164 try { 1165 System.loadLibrary("libTestLibrary.so"); 1166 fail("SecurityException should be thrown."); 1167 } catch (SecurityException e) { 1168 // expected 1169 } finally { 1170 System.setSecurityManager(oldSm); 1171 } 1172 } 1173 1174 @TestTargetNew( 1175 level = TestLevel.COMPLETE, 1176 notes = "", 1177 method = "mapLibraryName", 1178 args = {java.lang.String.class} 1179 ) 1180 public void test_mapLibraryName() { 1181 assertEquals("libname.so", System.mapLibraryName("name")); 1182 1183 try { 1184 System.mapLibraryName(null); 1185 fail("NullPointerException is not thrown."); 1186 } catch(NullPointerException npe) { 1187 //expected 1188 } 1189 } 1190 1191 @TestTargetNew( 1192 level = TestLevel.COMPLETE, 1193 notes = "", 1194 method = "nanoTime", 1195 args = {} 1196 ) 1197 public void test_nanoTime() { 1198 long sleepTime = 5000; 1199 long beginTime = System.nanoTime(); 1200 try { 1201 Thread.sleep(sleepTime); 1202 } catch(Exception e) { 1203 fail("Unknown exception was thrown."); 1204 } 1205 long endTime = System.nanoTime(); 1206 assertTrue((endTime - beginTime) > sleepTime * 1000000); 1207 } 1208 1209 @Override 1210 protected void setUp() { 1211 TestEnvironment.reset(); 1212 flag = false; 1213 ranFinalize = false; 1214 } 1215 1216 @Override protected void tearDown() throws Exception { 1217 TestEnvironment.reset(); 1218 super.tearDown(); 1219 } 1220 1221 protected SystemTest createInstance() { 1222 return new SystemTest("FT"); 1223 } 1224 1225 @Override 1226 protected void finalize() { 1227 if (flag) 1228 ranFinalize = true; 1229 } 1230 1231 public SystemTest() { 1232 } 1233 1234 public SystemTest(String name) { 1235 super(name); 1236 } 1237 1238 private class MockSecurityManager extends SecurityManager { 1239 @Override 1240 public void checkPermission(Permission perm) { 1241 if (perm.equals(new RuntimePermission("inheritedChannel"))) 1242 throw new SecurityException("Incorrect permission"); 1243 } 1244 } 1245} 1246