ThreadTest.java revision 77a0d8ed528e8f3f4dbaed9e1c81761293c15c35
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 java.io.ByteArrayOutputStream; 21import java.io.PrintStream; 22import java.lang.Thread.UncaughtExceptionHandler; 23import java.security.Permission; 24import java.util.Map; 25import java.util.concurrent.Semaphore; 26 27import dalvik.annotation.AndroidOnly; 28import dalvik.annotation.KnownFailure; 29import dalvik.annotation.TestLevel; 30import dalvik.annotation.TestTargetClass; 31import dalvik.annotation.TestTargetNew; 32import dalvik.annotation.TestTargets; 33 34@TestTargetClass(Thread.class) 35public class ThreadTest extends junit.framework.TestCase { 36 37 int counter = 0; 38 39 static class SimpleThread implements Runnable { 40 int delay; 41 42 public void run() { 43 try { 44 synchronized (this) { 45 this.notify(); 46 this.wait(delay); 47 } 48 } catch (InterruptedException e) { 49 return; 50 } 51 52 } 53 54 public SimpleThread(int d) { 55 if (d >= 0) 56 delay = d; 57 } 58 } 59 60 static class YieldThread implements Runnable { 61 volatile int delay; 62 63 public void run() { 64 int x = 0; 65 while (true) { 66 ++x; 67 } 68 } 69 70 public YieldThread(int d) { 71 if (d >= 0) 72 delay = d; 73 } 74 } 75 76 static class ResSupThread implements Runnable { 77 Thread parent; 78 79 volatile int checkVal = -1; 80 81 public void run() { 82 try { 83 synchronized (this) { 84 this.notify(); 85 } 86 while (true) { 87 checkVal++; 88 zz(); 89 Thread.sleep(100); 90 } 91 } catch (InterruptedException e) { 92 return; 93 } catch (BogusException e) { 94 try { 95 // Give parent a chance to sleep 96 Thread.sleep(500); 97 } catch (InterruptedException x) { 98 } 99 parent.interrupt(); 100 while (!Thread.currentThread().isInterrupted()) { 101 // Don't hog the CPU 102 try { 103 Thread.sleep(50); 104 } catch (InterruptedException x) { 105 // This is what we've been waiting for...don't throw it 106 // away! 107 break; 108 } 109 } 110 } 111 } 112 113 public void zz() throws BogusException { 114 } 115 116 public ResSupThread(Thread t) { 117 parent = t; 118 } 119 120 public synchronized int getCheckVal() { 121 return checkVal; 122 } 123 } 124 125 static class BogusException extends Throwable { 126 127 private static final long serialVersionUID = 1L; 128 129 public BogusException(String s) { 130 super(s); 131 } 132 } 133 134 // TODO android-added 135 class MonitoredClass { 136 public synchronized void enterLocked() { 137 boolean b = Thread.holdsLock(this); 138 assertTrue("Thread should hold lock for object", b); 139 } 140 141 public void enterNonLocked() { 142 boolean b = Thread.holdsLock(this); 143 assertFalse("Thread should not hold lock for object", b); 144 } 145 } 146 147 Thread st, ct, spinner; 148 149 static boolean calledMySecurityManager = false; 150 151 boolean wasInterrupted = false; 152 153 /** 154 * @tests java.lang.Thread#Thread() 155 */ 156 @TestTargetNew( 157 level = TestLevel.COMPLETE, 158 notes = "", 159 method = "Thread", 160 args = {} 161 ) 162 public void test_Constructor() { 163 // Test for method java.lang.Thread() 164 165 Thread t; 166 SecurityManager m = new SecurityManager() { 167 @Override 168 public ThreadGroup getThreadGroup() { 169 calledMySecurityManager = true; 170 return Thread.currentThread().getThreadGroup(); 171 } 172 173 @Override 174 public void checkPermission(Permission permission) { 175 if (permission.getName().equals("setSecurityManager")) { 176 return; 177 } 178 super.checkPermission(permission); 179 } 180 }; 181 try { 182 // To see if it checks Thread creation with our SecurityManager 183 System.setSecurityManager(m); 184 t = new Thread(); 185 } finally { 186 // restore original, no side-effects 187 System.setSecurityManager(null); 188 } 189 assertTrue("Did not call SecurityManager.getThreadGroup ()", 190 calledMySecurityManager); 191 t.start(); 192 } 193 194 /** 195 * @tests java.lang.Thread#Thread(java.lang.Runnable) 196 */ 197 @TestTargetNew( 198 level = TestLevel.COMPLETE, 199 notes = "", 200 method = "Thread", 201 args = {java.lang.Runnable.class} 202 ) 203 public void test_ConstructorLjava_lang_Runnable() { 204 // Test for method java.lang.Thread(java.lang.Runnable) 205 try { 206 ct = new Thread(new SimpleThread(10)); 207 ct.start(); 208 } catch (Exception e) { 209 fail("Failed to create subthread : " + e.getMessage()); 210 } 211 } 212 213 /** 214 * @tests java.lang.Thread#Thread(java.lang.Runnable, java.lang.String) 215 */ 216 @TestTargetNew( 217 level = TestLevel.COMPLETE, 218 notes = "", 219 method = "Thread", 220 args = {java.lang.Runnable.class, java.lang.String.class} 221 ) 222 public void test_ConstructorLjava_lang_RunnableLjava_lang_String() { 223 // Test for method java.lang.Thread(java.lang.Runnable, 224 // java.lang.String) 225 Thread st1 = new Thread(new SimpleThread(1), "SimpleThread1"); 226 assertEquals("Constructed thread with incorrect thread name", "SimpleThread1", st1 227 .getName()); 228 st1.start(); 229 } 230 231 /** 232 * @tests java.lang.Thread#Thread(java.lang.String) 233 */ 234 @TestTargetNew( 235 level = TestLevel.COMPLETE, 236 notes = "", 237 method = "Thread", 238 args = {java.lang.String.class} 239 ) 240 public void test_ConstructorLjava_lang_String() { 241 // Test for method java.lang.Thread(java.lang.String) 242 Thread t = new Thread("Testing"); 243 assertEquals("Created tread with incorrect name", 244 "Testing", t.getName()); 245 t.start(); 246 } 247 248 /** 249 * @tests java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable) 250 */ 251 @TestTargetNew( 252 level = TestLevel.COMPLETE, 253 notes = "", 254 method = "Thread", 255 args = {java.lang.ThreadGroup.class, java.lang.Runnable.class} 256 ) 257 public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_Runnable() { 258 // Test for method java.lang.Thread(java.lang.ThreadGroup, 259 // java.lang.Runnable) 260 ThreadGroup tg = new ThreadGroup("Test Group1"); 261 st = new Thread(tg, new SimpleThread(1), "SimpleThread2"); 262 assertTrue("Returned incorrect thread group", st.getThreadGroup() == tg); 263 st.start(); 264 try { 265 st.join(); 266 } catch (InterruptedException e) { 267 } 268 tg.destroy(); 269 } 270 271 /** 272 * @tests java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable, 273 * java.lang.String)lo 274 */ 275 @TestTargetNew( 276 level = TestLevel.COMPLETE, 277 notes = "", 278 method = "Thread", 279 args = {java.lang.ThreadGroup.class, java.lang.Runnable.class, java.lang.String.class} 280 ) 281 public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_RunnableLjava_lang_String() { 282 // Test for method java.lang.Thread(java.lang.ThreadGroup, 283 // java.lang.Runnable, java.lang.String) 284 ThreadGroup tg = new ThreadGroup("Test Group2"); 285 st = new Thread(tg, new SimpleThread(1), "SimpleThread3"); 286 assertTrue("Constructed incorrect thread", (st.getThreadGroup() == tg) 287 && st.getName().equals("SimpleThread3")); 288 st.start(); 289 try { 290 st.join(); 291 } catch (InterruptedException e) { 292 } 293 tg.destroy(); 294 295 Runnable r = new Runnable() { 296 public void run() { 297 } 298 }; 299 300 ThreadGroup foo = null; 301 try { 302 new Thread(foo = new ThreadGroup("foo"), r, null); 303 // Should not get here 304 fail("Null cannot be accepted as Thread name"); 305 } catch (NullPointerException npe) { 306 assertTrue("Null cannot be accepted as Thread name", true); 307 foo.destroy(); 308 } 309 } 310 311 @TestTargetNew( 312 level = TestLevel.COMPLETE, 313 notes = "", 314 method = "Thread", 315 args = {java.lang.ThreadGroup.class, java.lang.Runnable.class, java.lang.String.class, long.class} 316 ) 317 public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_RunnableLjava_lang_StringL$L() { 318 ThreadGroup tg = new ThreadGroup("Test Group2"); 319 st = new Thread(tg, new SimpleThread(1), "SimpleThread3", 1); 320 assertTrue("Constructed incorrect thread", (st.getThreadGroup() == tg) 321 && st.getName().equals("SimpleThread3")); 322 st.start(); 323 try { 324 st.join(); 325 } catch (InterruptedException e) { 326 } 327 tg.destroy(); 328 329 Runnable r = new Runnable() { 330 public void run() { 331 } 332 }; 333 334 try { 335 new Thread(tg, new SimpleThread(1), "SimpleThread3", 336 Integer.MAX_VALUE); 337 fail("StackOverflowError/OutOfMemoryError is not thrown."); 338 } catch(IllegalThreadStateException itse) { 339 //expected 340 } 341 342 } 343 344 /** 345 * @tests java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.String) 346 */ 347 @TestTargetNew( 348 level = TestLevel.COMPLETE, 349 notes = "", 350 method = "Thread", 351 args = {java.lang.ThreadGroup.class, java.lang.String.class} 352 ) 353 public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_String() { 354 // Test for method java.lang.Thread(java.lang.ThreadGroup, 355 // java.lang.String) 356 st = new Thread(new SimpleThread(1), "SimpleThread4"); 357 assertEquals("Returned incorrect thread name", 358 "SimpleThread4", st.getName()); 359 st.start(); 360 } 361 362 /** 363 * @tests java.lang.Thread#activeCount() 364 */ 365 @TestTargetNew( 366 level = TestLevel.COMPLETE, 367 notes = "", 368 method = "activeCount", 369 args = {} 370 ) 371 public void test_activeCount() { 372 // Test for method int java.lang.Thread.activeCount() 373 Thread t = new Thread(new SimpleThread(1)); 374 int active = Thread.activeCount(); 375 assertTrue("Incorrect read made: " + active, active > 0); 376 t.start(); 377 try { 378 t.join(); 379 } catch (InterruptedException e) { 380 } 381 } 382 383 /** 384 * @tests java.lang.Thread#checkAccess() 385 */ 386 @TestTargetNew( 387 level = TestLevel.COMPLETE, 388 notes = "", 389 method = "checkAccess", 390 args = {} 391 ) 392 public void test_checkAccess() { 393 // Test for method void java.lang.Thread.checkAccess() 394 ThreadGroup tg = new ThreadGroup("Test Group3"); 395 try { 396 st = new Thread(tg, new SimpleThread(1), "SimpleThread5"); 397 st.checkAccess(); 398 assertTrue("CheckAccess passed", true); 399 } catch (SecurityException e) { 400 fail("CheckAccess failed : " + e.getMessage()); 401 } 402 st.start(); 403 try { 404 st.join(); 405 } catch (InterruptedException e) { 406 } 407 tg.destroy(); 408 409 SecurityManager sm = new SecurityManager() { 410 411 public void checkPermission(Permission perm) { 412 } 413 414 public void checkAccess(Thread t) { 415 throw new SecurityException(); 416 } 417 }; 418 419 SecurityManager oldSm = System.getSecurityManager(); 420 System.setSecurityManager(sm); 421 try { 422 st.checkAccess(); 423 fail("Should throw SecurityException"); 424 } catch (SecurityException e) { 425 // expected 426 } finally { 427 System.setSecurityManager(oldSm); 428 } 429 } 430 431 /** 432 * @tests java.lang.Thread#countStackFrames() 433 */ 434 @TestTargetNew( 435 level = TestLevel.COMPLETE, 436 notes = "", 437 method = "countStackFrames", 438 args = {} 439 ) 440 @SuppressWarnings("deprecation") 441 public void test_countStackFrames() { 442 /* 443 * Thread.countStackFrames() is unpredictable, so we just test that it 444 * doesn't throw an exception. 445 */ 446 try { 447 Thread.currentThread().countStackFrames(); 448 } catch (Throwable t) { 449 fail("unexpected throwable: " + t.toString()); 450 } 451 } 452 453 /** 454 * @tests java.lang.Thread#currentThread() 455 */ 456 @TestTargetNew( 457 level = TestLevel.COMPLETE, 458 notes = "", 459 method = "currentThread", 460 args = {} 461 ) 462 public void test_currentThread() { 463 assertNotNull(Thread.currentThread()); 464 } 465 466 /** 467 * @tests java.lang.Thread#destroy() 468 */ 469 @TestTargetNew( 470 level = TestLevel.COMPLETE, 471 notes = "", 472 method = "destroy", 473 args = {} 474 ) 475 @SuppressWarnings("deprecation") 476 public void test_destroy() { 477 try { 478 new Thread().destroy(); 479 // FIXME uncomment when IBM VME is updated 480 fail("NoSuchMethodError was not thrown"); 481 } catch (NoSuchMethodError e) { 482 } 483 } 484 485 @TestTargetNew( 486 level = TestLevel.COMPLETE, 487 notes = "", 488 method = "dumpStack", 489 args = {} 490 ) 491 public void test_dumpStack() { 492 try { 493 PrintStream savedErr = System.err; 494 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 495 System.setErr(new PrintStream(baos)); 496 Thread.dumpStack(); 497 System.setErr(savedErr); 498 499 String s = new String(baos.toByteArray()); 500 501 assertTrue(s.contains("java.lang.Thread.dumpStack")); 502 503 } catch(Exception e) { 504 fail("Unexpected exception was thrown: " + e.toString()); 505 } 506 } 507 508 /** 509 * @tests java.lang.Thread#enumerate(java.lang.Thread[]) 510 */ 511 @TestTargetNew( 512 level = TestLevel.COMPLETE, 513 notes = "", 514 method = "enumerate", 515 args = {java.lang.Thread[].class} 516 ) 517 public void test_enumerate$Ljava_lang_Thread() { 518 // Test for method int java.lang.Thread.enumerate(java.lang.Thread []) 519 // The test has been updated according to HARMONY-1974 JIRA issue. 520 521 class MyThread extends Thread { 522 MyThread(ThreadGroup tg, String name) { 523 super(tg, name); 524 } 525 526 boolean failed = false; 527 String failMessage = null; 528 529 public void run() { 530 SimpleThread st1 = null; 531 SimpleThread st2 = null; 532 ThreadGroup mytg = null; 533 Thread firstOne = null; 534 Thread secondOne = null; 535 try { 536 int arrayLength = 10; 537 Thread[] tarray = new Thread[arrayLength]; 538 st1 = new SimpleThread(-1); 539 st2 = new SimpleThread(-1); 540 mytg = new ThreadGroup("jp"); 541 firstOne = new Thread(mytg, st1, "firstOne2"); 542 secondOne = new Thread(mytg, st2, "secondOne1"); 543 int orgCount = Thread.enumerate(tarray); 544 synchronized (st1) { 545 firstOne.start(); 546 try { 547 st1.wait(); 548 } catch (InterruptedException e) { 549 } 550 } 551 int count = Thread.enumerate(tarray); 552 assertEquals("Incorrect value returned2", 553 orgCount + 1, count); 554 synchronized (st2) { 555 secondOne.start(); 556 try { 557 st2.wait(); 558 } catch (InterruptedException e) { 559 } 560 } 561 count = Thread.enumerate(tarray); 562 assertEquals("Incorrect value returned3", 563 orgCount + 2, count); 564 } catch (junit.framework.AssertionFailedError e) { 565 failed = true; 566 failMessage = e.getMessage(); 567 e.printStackTrace(); 568 } finally { 569 synchronized (st1) { 570 firstOne.interrupt(); 571 } 572 synchronized (st2) { 573 secondOne.interrupt(); 574 } 575 try { 576 firstOne.join(); 577 secondOne.join(); 578 } catch (InterruptedException e) { 579 } 580 mytg.destroy(); 581 } 582 } 583 }; 584 585 ThreadGroup tg = new ThreadGroup("tg"); 586 MyThread t = new MyThread(tg, "top"); 587 t.start(); 588 try { 589 t.join(); 590 } catch (InterruptedException e) { 591 fail("Unexpected interrupt"); 592 } finally { 593 tg.destroy(); 594 } 595 assertFalse(t.failMessage, t.failed); 596 } 597 598 /** 599 * @tests java.lang.Thread#getContextClassLoader() 600 */ 601 @TestTargetNew( 602 level = TestLevel.COMPLETE, 603 notes = "", 604 method = "getContextClassLoader", 605 args = {} 606 ) 607 public void test_getContextClassLoader() { 608 // Test for method java.lang.ClassLoader 609 // java.lang.Thread.getContextClassLoader() 610 Thread t = new Thread(); 611 assertNotNull(Thread.currentThread().getContextClassLoader()); 612 assertTrue("Incorrect class loader returned", 613 t.getContextClassLoader() == Thread.currentThread() 614 .getContextClassLoader()); 615 } 616 617 /** 618 * @tests java.lang.Thread#getName() 619 */ 620 @TestTargetNew( 621 level = TestLevel.COMPLETE, 622 notes = "", 623 method = "getName", 624 args = {} 625 ) 626 public void test_getName() { 627 // Test for method java.lang.String java.lang.Thread.getName() 628 st = new Thread(new SimpleThread(1), "SimpleThread6"); 629 assertEquals("Returned incorrect thread name", 630 "SimpleThread6", st.getName()); 631 st.start(); 632 } 633 634 /** 635 * @tests java.lang.Thread#getPriority() 636 */ 637 @TestTargetNew( 638 level = TestLevel.COMPLETE, 639 notes = "", 640 method = "getPriority", 641 args = {} 642 ) 643 public void test_getPriority() { 644 // Test for method int java.lang.Thread.getPriority() 645 st = new Thread(new SimpleThread(1)); 646 st.setPriority(Thread.MAX_PRIORITY); 647 assertTrue("Returned incorrect thread priority", 648 st.getPriority() == Thread.MAX_PRIORITY); 649 st.start(); 650 } 651 652 /** 653 * @tests java.lang.Thread#getThreadGroup() 654 */ 655 @TestTargetNew( 656 level = TestLevel.COMPLETE, 657 notes = "", 658 method = "getThreadGroup", 659 args = {} 660 ) 661 public void test_getThreadGroup() { 662 // Test for method java.lang.ThreadGroup 663 // java.lang.Thread.getThreadGroup() 664 ThreadGroup tg = new ThreadGroup("Test Group4"); 665 st = new Thread(tg, /* new SimpleThread(1), */ "SimpleThread8"); 666 assertTrue("Returned incorrect thread group", st.getThreadGroup() == tg); 667 st.start(); 668 try { 669 st.join(); 670 } catch (InterruptedException e) { 671 } 672 assertNull("group should be null", st.getThreadGroup()); 673 assertNotNull("toString() should not be null", st.toString()); 674 tg.destroy(); 675 676 final Object lock = new Object(); 677 Thread t = new Thread() { 678 @Override 679 public void run() { 680 synchronized (lock) { 681 lock.notifyAll(); 682 } 683 } 684 }; 685 synchronized (lock) { 686 t.start(); 687 try { 688 lock.wait(); 689 } catch (InterruptedException e) { 690 } 691 } 692 int running = 0; 693 while (t.isAlive()) 694 running++; 695 ThreadGroup group = t.getThreadGroup(); 696 assertNull("ThreadGroup is not null", group); 697 } 698 699 /** 700 * @tests java.lang.Thread#interrupt() 701 */ 702 @TestTargetNew( 703 level = TestLevel.COMPLETE, 704 notes = "", 705 method = "interrupt", 706 args = {} 707 ) 708 public void test_interrupt() { 709 // Test for method void java.lang.Thread.interrupt() 710 final Object lock = new Object(); 711 class ChildThread1 extends Thread { 712 Thread parent; 713 714 boolean sync; 715 716 @Override 717 public void run() { 718 if (sync) { 719 synchronized (lock) { 720 lock.notify(); 721 try { 722 lock.wait(); 723 } catch (InterruptedException e) { 724 } 725 } 726 } 727 parent.interrupt(); 728 } 729 730 public ChildThread1(Thread p, String name, boolean sync) { 731 super(name); 732 parent = p; 733 this.sync = sync; 734 } 735 } 736 boolean interrupted = false; 737 try { 738 ct = new ChildThread1(Thread.currentThread(), "Interrupt Test1", 739 false); 740 synchronized (lock) { 741 ct.start(); 742 lock.wait(); 743 } 744 } catch (InterruptedException e) { 745 interrupted = true; 746 } 747 assertTrue("Failed to Interrupt thread1", interrupted); 748 749 interrupted = false; 750 try { 751 ct = new ChildThread1(Thread.currentThread(), "Interrupt Test2", 752 true); 753 synchronized (lock) { 754 ct.start(); 755 lock.wait(); 756 lock.notify(); 757 } 758 Thread.sleep(20000); 759 } catch (InterruptedException e) { 760 interrupted = true; 761 } 762 assertTrue("Failed to Interrupt thread2", interrupted); 763 764 SecurityManager sm = new SecurityManager() { 765 766 public void checkPermission(Permission perm) { 767 } 768 769 public void checkAccess(Thread t) { 770 throw new SecurityException(); 771 } 772 }; 773 st = new Thread(); 774 SecurityManager oldSm = System.getSecurityManager(); 775 System.setSecurityManager(sm); 776 try { 777 st.interrupt(); 778 fail("Should throw SecurityException"); 779 } catch (SecurityException e) { 780 // expected 781 } finally { 782 System.setSecurityManager(oldSm); 783 } 784 } 785 786 /** 787 * @tests java.lang.Thread#interrupted() 788 */ 789 @TestTargetNew( 790 level = TestLevel.COMPLETE, 791 notes = "", 792 method = "interrupted", 793 args = {} 794 ) 795 public void test_interrupted() { 796 assertFalse("Interrupted returned true for non-interrupted thread", Thread 797 .interrupted()); 798 Thread.currentThread().interrupt(); 799 assertTrue("Interrupted returned true for non-interrupted thread", Thread.interrupted()); 800 assertFalse("Failed to clear interrupted flag", Thread.interrupted()); 801 } 802 803 /** 804 * @tests java.lang.Thread#isAlive() 805 */ 806 @TestTargetNew( 807 level = TestLevel.COMPLETE, 808 notes = "", 809 method = "isAlive", 810 args = {} 811 ) 812 public void test_isAlive() { 813 // Test for method boolean java.lang.Thread.isAlive() 814 SimpleThread simple; 815 st = new Thread(simple = new SimpleThread(500)); 816 assertFalse("A thread that wasn't started is alive.", st.isAlive()); 817 synchronized (simple) { 818 st.start(); 819 try { 820 simple.wait(); 821 } catch (InterruptedException e) { 822 } 823 } 824 assertTrue("Started thread returned false", st.isAlive()); 825 826 try { 827 st.join(); 828 } catch (InterruptedException e) { 829 fail("Thread did not die"); 830 } 831 assertTrue("Stopped thread returned true", !st.isAlive()); 832 } 833 834 /** 835 * @tests java.lang.Thread#isDaemon() 836 */ 837 @TestTargetNew( 838 level = TestLevel.COMPLETE, 839 notes = "", 840 method = "isDaemon", 841 args = {} 842 ) 843 public void test_isDaemon() { 844 // Test for method boolean java.lang.Thread.isDaemon() 845 st = new Thread(new SimpleThread(1), "SimpleThread10"); 846 assertTrue("Non-Daemon thread returned true", !st.isDaemon()); 847 st.setDaemon(true); 848 assertTrue("Daemon thread returned false", st.isDaemon()); 849 st.start(); 850 } 851 852 /** 853 * @tests java.lang.Thread#isInterrupted() 854 */ 855 @TestTargetNew( 856 level = TestLevel.COMPLETE, 857 notes = "", 858 method = "isInterrupted", 859 args = {} 860 ) 861 public void test_isInterrupted() { 862 // Test for method boolean java.lang.Thread.isInterrupted() 863 class SpinThread implements Runnable { 864 public volatile boolean done = false; 865 866 public void run() { 867 while (!Thread.currentThread().isInterrupted()) 868 ; 869 while (!done) 870 ; 871 } 872 } 873 874 SpinThread spin = new SpinThread(); 875 spinner = new Thread(spin); 876 spinner.start(); 877 Thread.yield(); 878 try { 879 assertTrue("Non-Interrupted thread returned true", !spinner 880 .isInterrupted()); 881 spinner.interrupt(); 882 assertTrue("Interrupted thread returned false", spinner 883 .isInterrupted()); 884 spin.done = true; 885 } finally { 886 spinner.interrupt(); 887 spin.done = true; 888 } 889 } 890 891 /** 892 * @tests java.lang.Thread#join() 893 */ 894 @TestTargetNew( 895 level = TestLevel.COMPLETE, 896 notes = "", 897 method = "join", 898 args = {} 899 ) 900 public void test_join() { 901 // Test for method void java.lang.Thread.join() 902 SimpleThread simple; 903 try { 904 st = new Thread(simple = new SimpleThread(100)); 905 // cause isAlive() to be compiled by the JIT, as it must be called 906 // within 100ms below. 907 assertTrue("Thread is alive", !st.isAlive()); 908 synchronized (simple) { 909 st.start(); 910 simple.wait(); 911 } 912 st.join(); 913 } catch (InterruptedException e) { 914 fail("Join failed "); 915 } 916 assertTrue("Joined thread is still alive", !st.isAlive()); 917 boolean result = true; 918 Thread th = new Thread("test"); 919 try { 920 th.join(); 921 } catch (InterruptedException e) { 922 result = false; 923 } 924 assertTrue("Hung joining a non-started thread", result); 925 th.start(); 926 927 st = new Thread() { 928 public void run() { 929 try { 930 join(); 931 fail("InterruptedException was not thrown."); 932 } catch(InterruptedException ie) { 933 //expected 934 } 935 } 936 }; 937 938 st.start(); 939 } 940 941 /** 942 * @tests java.lang.Thread#join(long) 943 */ 944 @TestTargetNew( 945 level = TestLevel.COMPLETE, 946 notes = "", 947 method = "join", 948 args = {long.class} 949 ) 950 public void test_joinJ() { 951 // Test for method void java.lang.Thread.join(long) 952 SimpleThread simple; 953 try { 954 st = new Thread(simple = new SimpleThread(1000), "SimpleThread12"); 955 // cause isAlive() to be compiled by the JIT, as it must be called 956 // within 100ms below. 957 assertTrue("Thread is alive", !st.isAlive()); 958 synchronized (simple) { 959 st.start(); 960 simple.wait(); 961 } 962 st.join(10); 963 } catch (InterruptedException e) { 964 fail("Join failed "); 965 } 966 assertTrue("Join failed to timeout", st.isAlive()); 967 968 st.interrupt(); 969 try { 970 st = new Thread(simple = new SimpleThread(100), "SimpleThread13"); 971 synchronized (simple) { 972 st.start(); 973 simple.wait(); 974 } 975 st.join(1000); 976 } catch (InterruptedException e) { 977 fail("Join failed : " + e.getMessage()); 978 return; 979 } 980 assertTrue("Joined thread is still alive", !st.isAlive()); 981 982 final Object lock = new Object(); 983 final Thread main = Thread.currentThread(); 984 Thread killer = new Thread(new Runnable() { 985 public void run() { 986 try { 987 synchronized (lock) { 988 lock.notify(); 989 } 990 Thread.sleep(100); 991 } catch (InterruptedException e) { 992 return; 993 } 994 main.interrupt(); 995 } 996 }); 997 boolean result = true; 998 Thread th = new Thread("test"); 999 try { 1000 synchronized (lock) { 1001 killer.start(); 1002 lock.wait(); 1003 } 1004 th.join(200); 1005 } catch (InterruptedException e) { 1006 result = false; 1007 } 1008 killer.interrupt(); 1009 assertTrue("Hung joining a non-started thread", result); 1010 th.start(); 1011 1012 st = new Thread() { 1013 public void run() { 1014 try { 1015 join(1000); 1016 fail("InterruptedException was not thrown."); 1017 } catch(InterruptedException ie) { 1018 //expected 1019 } 1020 } 1021 }; 1022 1023 st.start(); 1024 } 1025 1026 /** 1027 * @tests java.lang.Thread#join(long, int) 1028 */ 1029 @TestTargetNew( 1030 level = TestLevel.COMPLETE, 1031 notes = "", 1032 method = "join", 1033 args = {long.class, int.class} 1034 ) 1035 public void test_joinJI() { 1036 // Test for method void java.lang.Thread.join(long, int) 1037 SimpleThread simple; 1038 try { 1039 st = new Thread(simple = new SimpleThread(1000), "Squawk1"); 1040 assertTrue("Thread is alive", !st.isAlive()); 1041 synchronized (simple) { 1042 st.start(); 1043 simple.wait(); 1044 } 1045 1046 long firstRead = System.currentTimeMillis(); 1047 st.join(100, 999999); 1048 long secondRead = System.currentTimeMillis(); 1049 assertTrue("Did not join by appropriate time: " + secondRead + "-" 1050 + firstRead + "=" + (secondRead - firstRead), secondRead 1051 - firstRead <= 300); 1052 assertTrue("Joined thread is not alive", st.isAlive()); 1053 st.interrupt(); 1054 } catch (Exception e) { 1055 fail("Exception during test : " + e.getMessage()); 1056 } 1057 1058 final Object lock = new Object(); 1059 final Thread main = Thread.currentThread(); 1060 Thread killer = new Thread(new Runnable() { 1061 public void run() { 1062 try { 1063 synchronized (lock) { 1064 lock.notify(); 1065 } 1066 Thread.sleep(100); 1067 } catch (InterruptedException e) { 1068 return; 1069 } 1070 main.interrupt(); 1071 } 1072 }); 1073 boolean result = true; 1074 Thread th = new Thread("test"); 1075 try { 1076 synchronized (lock) { 1077 killer.start(); 1078 lock.wait(); 1079 } 1080 th.join(200, 20); 1081 } catch (InterruptedException e) { 1082 result = false; 1083 } 1084 killer.interrupt(); 1085 assertTrue("Hung joining a non-started thread", result); 1086 th.start(); 1087 1088 st = new Thread() { 1089 public void run() { 1090 try { 1091 join(1000, 20); 1092 fail("InterruptedException was not thrown."); 1093 } catch(InterruptedException ie) { 1094 //expected 1095 } 1096 } 1097 }; 1098 1099 st.start(); 1100 } 1101 1102 @TestTargets({ 1103 @TestTargetNew( 1104 level = TestLevel.COMPLETE, 1105 notes = "", 1106 method = "setContextClassLoader", 1107 args = {java.lang.ClassLoader.class} 1108 ), 1109 @TestTargetNew( 1110 level = TestLevel.COMPLETE, 1111 notes = "", 1112 method = "getContextClassLoader", 1113 args = {} 1114 ) 1115 }) 1116 public void test_setContextClassLoader() { 1117 PublicClassLoader pcl = new PublicClassLoader(); 1118 st = new Thread(); 1119 st.setContextClassLoader(pcl); 1120 assertEquals(pcl, st.getContextClassLoader()); 1121 1122 st.setContextClassLoader(null); 1123 assertNull(st.getContextClassLoader()); 1124 1125 SecurityManager sm = new SecurityManager() { 1126 public void checkPermission(Permission perm) { 1127 if (perm.getName().equals("setContextClassLoader") 1128 || perm.getName().equals("getClassLoader") ) { 1129 throw new SecurityException(); 1130 } 1131 } 1132 }; 1133 1134 SecurityManager oldSm = System.getSecurityManager(); 1135 System.setSecurityManager(sm); 1136 try { 1137 st.setContextClassLoader(pcl); 1138 fail("Should throw SecurityException"); 1139 } catch (SecurityException e) { 1140 // expected 1141 } finally { 1142 System.setSecurityManager(oldSm); 1143 } 1144 } 1145 1146 private Thread launchFiveSecondDummyThread() { 1147 Thread thread = new Thread() { 1148 public void run() { 1149 try { 1150 Thread.sleep(5000); 1151 } catch (InterruptedException e) { 1152 // Ignore 1153 } 1154 } 1155 }; 1156 1157 thread.start(); 1158 1159 return thread; 1160 } 1161 1162 private class ThreadSecurityManager extends SecurityManager { 1163 public void checkPermission(Permission perm) { 1164 } 1165 1166 public void checkAccess(Thread t) { 1167 throw new SecurityException(); 1168 } 1169 }; 1170 1171 /** 1172 * @tests java.lang.Thread#resume() 1173 */ 1174 @TestTargetNew( 1175 level = TestLevel.COMPLETE, 1176 notes = "", 1177 method = "resume", 1178 args = {} 1179 ) 1180 @AndroidOnly("RI does implement this method, whereas Android does not") 1181 @SuppressWarnings("deprecation") 1182 public void test_resume() { 1183 Thread thread = launchFiveSecondDummyThread(); 1184 1185 try { 1186 Thread.sleep(1000); 1187 } catch (InterruptedException e) { 1188 // Ignore 1189 } 1190 1191 // No-op in Android. Must neither have an effect nor throw an exception. 1192 Thread.State state = thread.getState(); 1193 thread.resume(); 1194 assertEquals(state, thread.getState()); 1195 1196 // Security checks are made even though method is not supported. 1197 SecurityManager oldSm = System.getSecurityManager(); 1198 System.setSecurityManager(new ThreadSecurityManager()); 1199 try { 1200 thread.resume(); 1201 fail("Should throw SecurityException"); 1202 } catch (SecurityException e) { 1203 // expected 1204 } finally { 1205 System.setSecurityManager(oldSm); 1206 } 1207 1208 try { 1209 thread.join(); 1210 } catch (InterruptedException e) { 1211 // Ignore 1212 } 1213 } 1214 1215 /** 1216 * @tests java.lang.Thread#run() 1217 */ 1218 @TestTargetNew( 1219 level = TestLevel.COMPLETE, 1220 notes = "", 1221 method = "run", 1222 args = {} 1223 ) 1224 public void test_run() { 1225 // Test for method void java.lang.Thread.run() 1226 class RunThread implements Runnable { 1227 boolean didThreadRun = false; 1228 1229 public void run() { 1230 didThreadRun = true; 1231 } 1232 } 1233 RunThread rt = new RunThread(); 1234 Thread t = new Thread(rt); 1235 try { 1236 t.start(); 1237 int count = 0; 1238 while (!rt.didThreadRun && count < 20) { 1239 Thread.sleep(100); 1240 count++; 1241 } 1242 assertTrue("Thread did not run", rt.didThreadRun); 1243 t.join(); 1244 } catch (InterruptedException e) { 1245 assertTrue("Joined thread was interrupted", true); 1246 } 1247 assertTrue("Joined thread is still alive", !t.isAlive()); 1248 } 1249 1250 /** 1251 * @tests java.lang.Thread#setDaemon(boolean) 1252 */ 1253 @TestTargetNew( 1254 level = TestLevel.COMPLETE, 1255 notes = "", 1256 method = "setDaemon", 1257 args = {boolean.class} 1258 ) 1259 public void test_setDaemonZ() { 1260 // Test for method void java.lang.Thread.setDaemon(boolean) 1261 st = new Thread(new SimpleThread(1), "SimpleThread14"); 1262 st.setDaemon(true); 1263 assertTrue("Failed to set thread as daemon thread", st.isDaemon()); 1264 st.start(); 1265 1266 // BEGIN android-added 1267 st = new Thread(new SimpleThread(5)); 1268 st.start(); 1269 try { 1270 st.setDaemon(false); 1271 fail("setDaemon() must throw exception for started thread"); 1272 } catch (IllegalThreadStateException ex) { 1273 // We expect this one. 1274 } 1275 // END android-added 1276 1277 SecurityManager sm = new SecurityManager() { 1278 1279 public void checkPermission(Permission perm) { 1280 } 1281 1282 public void checkAccess(Thread t) { 1283 throw new SecurityException(); 1284 } 1285 }; 1286 1287 SecurityManager oldSm = System.getSecurityManager(); 1288 System.setSecurityManager(sm); 1289 try { 1290 st.setDaemon(false); 1291 fail("Should throw SecurityException"); 1292 } catch (SecurityException e) { 1293 // expected 1294 } finally { 1295 System.setSecurityManager(oldSm); 1296 } 1297 } 1298 1299 /** 1300 * @tests java.lang.Thread#setName(java.lang.String) 1301 */ 1302 @TestTargetNew( 1303 level = TestLevel.COMPLETE, 1304 notes = "", 1305 method = "setName", 1306 args = {java.lang.String.class} 1307 ) 1308 public void test_setNameLjava_lang_String() { 1309 // Test for method void java.lang.Thread.setName(java.lang.String) 1310 st = new Thread(new SimpleThread(1), "SimpleThread15"); 1311 st.setName("Bogus Name"); 1312 assertEquals("Failed to set thread name", 1313 "Bogus Name", st.getName()); 1314 try { 1315 st.setName(null); 1316 fail("Null should not be accepted as a valid name"); 1317 } catch (NullPointerException e) { 1318 // success 1319 assertTrue("Null should not be accepted as a valid name", true); 1320 } 1321 st.start(); 1322 1323 SecurityManager sm = new SecurityManager() { 1324 1325 public void checkPermission(Permission perm) { 1326 } 1327 1328 public void checkAccess(Thread t) { 1329 throw new SecurityException(); 1330 } 1331 }; 1332 1333 SecurityManager oldSm = System.getSecurityManager(); 1334 System.setSecurityManager(sm); 1335 try { 1336 st.setName("Bogus Name"); 1337 fail("Should throw SecurityException"); 1338 } catch (SecurityException e) { 1339 // expected 1340 } finally { 1341 System.setSecurityManager(oldSm); 1342 } 1343 } 1344 1345 /** 1346 * @tests java.lang.Thread#setPriority(int) 1347 */ 1348 @TestTargetNew( 1349 level = TestLevel.COMPLETE, 1350 notes = "", 1351 method = "setPriority", 1352 args = {int.class} 1353 ) 1354 public void test_setPriorityI() { 1355 // Test for method void java.lang.Thread.setPriority(int) 1356 st = new Thread(new SimpleThread(1)); 1357 st.setPriority(Thread.MAX_PRIORITY); 1358 assertTrue("Failed to set priority", 1359 st.getPriority() == Thread.MAX_PRIORITY); 1360 st.start(); 1361 1362 SecurityManager sm = new SecurityManager() { 1363 1364 public void checkPermission(Permission perm) { 1365 } 1366 1367 public void checkAccess(Thread t) { 1368 throw new SecurityException(); 1369 } 1370 }; 1371 1372 SecurityManager oldSm = System.getSecurityManager(); 1373 System.setSecurityManager(sm); 1374 try { 1375 st.setPriority(Thread.MIN_PRIORITY); 1376 fail("Should throw SecurityException"); 1377 } catch (SecurityException e) { 1378 // expected 1379 } finally { 1380 System.setSecurityManager(oldSm); 1381 } 1382 1383 try { 1384 st.setPriority(Thread.MIN_PRIORITY - 1); 1385 fail("IllegalArgumentException is not thrown."); 1386 } catch(IllegalArgumentException iae) { 1387 //expected 1388 } 1389 1390 try { 1391 st.setPriority(Thread.MAX_PRIORITY + 1); 1392 fail("IllegalArgumentException is not thrown."); 1393 } catch(IllegalArgumentException iae) { 1394 //expected 1395 } 1396 } 1397 1398 /** 1399 * @tests java.lang.Thread#sleep(long) 1400 */ 1401 @TestTargetNew( 1402 level = TestLevel.COMPLETE, 1403 notes = "", 1404 method = "sleep", 1405 args = {long.class} 1406 ) 1407 public void test_sleepJ() { 1408 // Note: Not too much we can test here that can be reliably measured. 1409 1410 // Check that basic behavior is about right (with some tolerance) 1411 long stime = System.currentTimeMillis(); 1412 1413 try { 1414 Thread.sleep(1000); 1415 } catch (InterruptedException e) { 1416 fail("Unexpected InterruptedException was thrown"); 1417 } 1418 1419 long ftime = System.currentTimeMillis(); 1420 1421 assertTrue("Failed to sleep long enough", (ftime - stime) >= 500); 1422 assertTrue("Failed to wake up early enough", (ftime - stime) <= 1500); 1423 1424 // Check that interrupt works 1425 st = new Thread() { 1426 public void run() { 1427 try { 1428 sleep(10000); 1429 } catch(InterruptedException ie) { 1430 wasInterrupted = true; 1431 } 1432 } 1433 }; 1434 1435 st.start(); 1436 1437 try { 1438 Thread.sleep(5000); 1439 } catch(InterruptedException e) { 1440 fail("Unexpected InterruptedException was thrown"); 1441 } 1442 1443 st.interrupt(); 1444 1445 try { 1446 Thread.sleep(5000); 1447 } catch(InterruptedException e) { 1448 fail("Unexpected InterruptedException was thrown"); 1449 } 1450 1451 assertTrue(wasInterrupted); 1452 } 1453 1454 /** 1455 * @tests java.lang.Thread#sleep(long, int) 1456 */ 1457 @TestTargetNew( 1458 level = TestLevel.COMPLETE, 1459 notes = "", 1460 method = "sleep", 1461 args = {long.class, int.class} 1462 ) 1463 public void test_sleepJI() { 1464 // Note: Not too much we can test here that can be reliably measured. 1465 1466 // Check that basic behavior is about right (with some tolerance) 1467 long stime = System.currentTimeMillis(); 1468 1469 try { 1470 Thread.sleep(1000, 99999); 1471 } catch (InterruptedException e) { 1472 fail("Unexpected InterruptedException was thrown"); 1473 } 1474 1475 long ftime = System.currentTimeMillis(); 1476 1477 assertTrue("Failed to sleep long enough", (ftime - stime) >= 500); 1478 assertTrue("Failed to wake up early enough", (ftime - stime) <= 1500); 1479 1480 // Check that interrupt works 1481 st = new Thread() { 1482 public void run() { 1483 try { 1484 sleep(10000, 99999); 1485 } catch(InterruptedException ie) { 1486 wasInterrupted = true; 1487 } 1488 } 1489 }; 1490 1491 st.start(); 1492 1493 try { 1494 Thread.sleep(5000, 99999); 1495 } catch(InterruptedException e) { 1496 fail("Unexpected InterruptedException was thrown"); 1497 } 1498 1499 st.interrupt(); 1500 1501 try { 1502 Thread.sleep(5000); 1503 } catch(InterruptedException e) { 1504 fail("Unexpected InterruptedException was thrown"); 1505 } 1506 1507 assertTrue(wasInterrupted); 1508 } 1509 1510 /** 1511 * @tests java.lang.Thread#start() 1512 */ 1513 @TestTargetNew( 1514 level = TestLevel.COMPLETE, 1515 notes = "", 1516 method = "start", 1517 args = {} 1518 ) 1519 public void test_start() { 1520 // Test for method void java.lang.Thread.start() 1521 try { 1522 ResSupThread t = new ResSupThread(Thread.currentThread()); 1523 synchronized (t) { 1524 ct = new Thread(t, "Interrupt Test4"); 1525 ct.start(); 1526 t.wait(); 1527 } 1528 assertTrue("Thread is not running1", ct.isAlive()); 1529 // Let the child thread get going. 1530 int orgval = t.getCheckVal(); 1531 Thread.sleep(150); 1532 assertTrue("Thread is not running2", orgval != t.getCheckVal()); 1533 ct.interrupt(); 1534 } catch (InterruptedException e) { 1535 fail("Unexpected interrupt occurred"); 1536 } 1537 Thread thr = new Thread(); 1538 thr.start(); 1539 try { 1540 thr.start(); 1541 } catch(IllegalThreadStateException itse){ 1542 //expected 1543 } 1544 } 1545 1546 /** 1547 * @tests java.lang.Thread#stop() 1548 */ 1549 @TestTargetNew( 1550 level = TestLevel.COMPLETE, 1551 notes = "", 1552 method = "stop", 1553 args = {} 1554 ) 1555 @AndroidOnly("RI does implement this method, whereas Android does not") 1556 @SuppressWarnings("deprecation") 1557 public void test_stop() { 1558 Thread thread = launchFiveSecondDummyThread(); 1559 1560 try { 1561 Thread.sleep(1000); 1562 } catch (InterruptedException e) { 1563 // Ignore 1564 } 1565 1566 // No-op in Android. Must neither have an effect nor throw an exception. 1567 Thread.State state = thread.getState(); 1568 thread.stop(); 1569 assertEquals(state, thread.getState()); 1570 1571 // Security checks are made even though method is not supported. 1572 SecurityManager oldSm = System.getSecurityManager(); 1573 System.setSecurityManager(new ThreadSecurityManager()); 1574 try { 1575 thread.stop(); 1576 fail("Should throw SecurityException"); 1577 } catch (SecurityException e) { 1578 // expected 1579 } finally { 1580 System.setSecurityManager(oldSm); 1581 } 1582 1583 try { 1584 thread.join(); 1585 } catch (InterruptedException e) { 1586 // Ignore 1587 } 1588 } 1589 1590 /** 1591 * @tests java.lang.Thread#stop(java.lang.Throwable) 1592 */ 1593 @TestTargetNew( 1594 level = TestLevel.COMPLETE, 1595 notes = "Verifies security.", 1596 method = "stop", 1597 args = {java.lang.Throwable.class} 1598 ) 1599 @SuppressWarnings("deprecation") 1600 public void test_stopLjava_lang_Throwable_subtest0() { 1601 Thread thread = new Thread() { 1602 public void run() { 1603 try { 1604 Thread.sleep(5000); 1605 } catch (InterruptedException e) { 1606 // Ignore 1607 } 1608 } 1609 }; 1610 1611 thread.start(); 1612 try { 1613 Thread.sleep(1000); 1614 } catch (InterruptedException e) { 1615 // Ignore 1616 } 1617 1618 // No-op in Android. Must neither have an effect nor throw an exception. 1619 Thread.State state = thread.getState(); 1620 thread.stop(new Exception("Oops!")); 1621 assertEquals(state, thread.getState()); 1622 1623 // Security checks are made even though method is not supported. 1624 SecurityManager sm = new SecurityManager() { 1625 public void checkPermission(Permission perm) { 1626 } 1627 1628 public void checkAccess(Thread t) { 1629 throw new SecurityException(); 1630 } 1631 }; 1632 1633 SecurityManager oldSm = System.getSecurityManager(); 1634 System.setSecurityManager(sm); 1635 try { 1636 thread.stop(); 1637 fail("Should throw SecurityException"); 1638 } catch (SecurityException e) { 1639 // expected 1640 } finally { 1641 System.setSecurityManager(oldSm); 1642 } 1643 1644 try { 1645 thread.join(); 1646 } catch (InterruptedException e) { 1647 // Ignore 1648 } 1649 } 1650 1651 /** 1652 * @tests java.lang.Thread#suspend() 1653 */ 1654 @TestTargetNew( 1655 level = TestLevel.COMPLETE, 1656 notes = "", 1657 method = "suspend", 1658 args = {} 1659 ) 1660 @AndroidOnly("RI does implement this method, whereas Android does not") 1661 @SuppressWarnings("deprecation") 1662 public void test_suspend() { 1663 Thread thread = launchFiveSecondDummyThread(); 1664 1665 try { 1666 Thread.sleep(1000); 1667 } catch (InterruptedException e) { 1668 // Ignore 1669 } 1670 1671 // No-op in Android. Must neither have an effect nor throw an exception. 1672 Thread.State state = thread.getState(); 1673 thread.suspend(); 1674 assertEquals(state, thread.getState()); 1675 1676 // Security checks are made even though method is not supported. 1677 SecurityManager oldSm = System.getSecurityManager(); 1678 System.setSecurityManager(new ThreadSecurityManager()); 1679 try { 1680 thread.suspend(); 1681 fail("Should throw SecurityException"); 1682 } catch (SecurityException e) { 1683 // expected 1684 } finally { 1685 System.setSecurityManager(oldSm); 1686 } 1687 1688 try { 1689 thread.join(); 1690 } catch (InterruptedException e) { 1691 // Ignore 1692 } 1693 } 1694 1695 /** 1696 * @tests java.lang.Thread#toString() 1697 */ 1698 @TestTargetNew( 1699 level = TestLevel.COMPLETE, 1700 notes = "", 1701 method = "toString", 1702 args = {} 1703 ) 1704 public void test_toString() { 1705 // Test for method java.lang.String java.lang.Thread.toString() 1706 ThreadGroup tg = new ThreadGroup("Test Group5"); 1707 st = new Thread(tg, new SimpleThread(1), "SimpleThread17"); 1708 final String stString = st.toString(); 1709 final String expected = "Thread[SimpleThread17,5,Test Group5]"; 1710 assertTrue("Returned incorrect string: " + stString + "\t(expecting :" 1711 + expected + ")", stString.equals(expected)); 1712 st.start(); 1713 try { 1714 st.join(); 1715 } catch (InterruptedException e) { 1716 } 1717 tg.destroy(); 1718 } 1719 1720 @TestTargetNew( 1721 level = TestLevel.COMPLETE, 1722 notes = "", 1723 method = "yield", 1724 args = {} 1725 ) 1726 public void test_yield() { 1727 1728 Counter [] countersNotYeld = new Counter[10]; 1729 1730 for(int i = 0; i < 10; i++) { 1731 countersNotYeld[i] = new Counter(false); 1732 } 1733 Counter countersYeld = new Counter(true); 1734 try { 1735 Thread.sleep(11000); 1736 } catch(InterruptedException ie) {} 1737 1738 for(Counter c:countersNotYeld) { 1739 assertTrue(countersYeld.counter == c.counter); 1740 } 1741 } 1742 1743 class Counter extends Thread { 1744 public int counter = 0; 1745 boolean isDoYield = false; 1746 1747 public Counter(boolean isDoYield) { 1748 this.isDoYield = isDoYield; 1749 start(); 1750 } 1751 1752 public void run() { 1753 for(int i = 0; i < 10000; i++) { 1754 if(isDoYield) 1755 yield(); 1756 counter ++; 1757 } 1758 } 1759 } 1760 1761 1762 /** 1763 * @tests java.lang.Thread#getAllStackTraces() 1764 */ 1765 @TestTargetNew( 1766 level = TestLevel.COMPLETE, 1767 notes = "", 1768 method = "getAllStackTraces", 1769 args = {} 1770 ) 1771 public void test_getAllStackTraces() { 1772 Map<Thread, StackTraceElement[]> stMap = Thread.getAllStackTraces(); 1773 assertNotNull(stMap); 1774 //TODO add security-based tests 1775 1776 SecurityManager sm = new SecurityManager() { 1777 1778 public void checkPermission(Permission perm) { 1779 if(perm.getName().equals("modifyThreadGroup")) { 1780 throw new SecurityException(); 1781 } 1782 } 1783 }; 1784 1785 SecurityManager oldSm = System.getSecurityManager(); 1786 System.setSecurityManager(sm); 1787 try { 1788 Thread.getAllStackTraces(); 1789 fail("Should throw SecurityException"); 1790 } catch (SecurityException e) { 1791 // expected 1792 } finally { 1793 System.setSecurityManager(oldSm); 1794 } 1795 } 1796 1797 /** 1798 * @tests java.lang.Thread#getDefaultUncaughtExceptionHandler 1799 * @tests java.lang.Thread#setDefaultUncaughtExceptionHandler 1800 */ 1801 @TestTargets({ 1802 @TestTargetNew( 1803 level = TestLevel.COMPLETE, 1804 notes = "", 1805 method = "setDefaultUncaughtExceptionHandler", 1806 args = {java.lang.Thread.UncaughtExceptionHandler.class} 1807 ), 1808 @TestTargetNew( 1809 level = TestLevel.COMPLETE, 1810 notes = "", 1811 method = "getDefaultUncaughtExceptionHandler", 1812 args = {} 1813 ) 1814 }) 1815 public void test_get_setDefaultUncaughtExceptionHandler() { 1816 class Handler implements UncaughtExceptionHandler { 1817 public void uncaughtException(Thread thread, Throwable ex) { 1818 } 1819 } 1820 1821 final Handler handler = new Handler(); 1822 Thread.setDefaultUncaughtExceptionHandler(handler); 1823 assertSame(handler, Thread.getDefaultUncaughtExceptionHandler()); 1824 1825 Thread.setDefaultUncaughtExceptionHandler(null); 1826 assertNull(Thread.getDefaultUncaughtExceptionHandler()); 1827 //TODO add security-based tests 1828 1829 SecurityManager sm = new SecurityManager() { 1830 1831 public void checkPermission(Permission perm) { 1832 if(perm.getName(). 1833 equals("setDefaultUncaughtExceptionHandler")) { 1834 throw new SecurityException(); 1835 } 1836 } 1837 }; 1838 1839 SecurityManager oldSm = System.getSecurityManager(); 1840 System.setSecurityManager(sm); 1841 try { 1842 st.setDefaultUncaughtExceptionHandler(handler); 1843 fail("Should throw SecurityException"); 1844 } catch (SecurityException e) { 1845 // expected 1846 } finally { 1847 System.setSecurityManager(oldSm); 1848 } 1849 } 1850 1851 /** 1852 * @tests java.lang.Thread#getStackTrace() 1853 */ 1854 @TestTargetNew( 1855 level = TestLevel.COMPLETE, 1856 notes = "", 1857 method = "getStackTrace", 1858 args = {} 1859 ) 1860 public void test_getStackTrace() { 1861 StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); 1862 1863 assertNotNull(stackTrace); 1864 1865 stack_trace_loop: { 1866 for (int i = 0; i < stackTrace.length; i++) { 1867 StackTraceElement e = stackTrace[i]; 1868 if (getClass().getName().equals(e.getClassName())) { 1869 if ("test_getStackTrace".equals(e.getMethodName())) { 1870 break stack_trace_loop; 1871 } 1872 } 1873 } 1874 fail("class and method not found in stack trace"); 1875 } 1876 1877 //TODO add security-based tests 1878 1879 SecurityManager sm = new SecurityManager() { 1880 1881 public void checkPermission(Permission perm) { 1882 if(perm.getName(). 1883 equals("getStackTrace")) { 1884 throw new SecurityException(); 1885 } 1886 } 1887 }; 1888 st = new Thread(); 1889 SecurityManager oldSm = System.getSecurityManager(); 1890 System.setSecurityManager(sm); 1891 try { 1892 st.getStackTrace(); 1893 fail("Should throw SecurityException"); 1894 } catch (SecurityException e) { 1895 // expected 1896 } finally { 1897 System.setSecurityManager(oldSm); 1898 } 1899 } 1900 1901 /** 1902 * @tests java.lang.Thread#getState() 1903 */ 1904 @TestTargetNew( 1905 level = TestLevel.COMPLETE, 1906 notes = "", 1907 method = "getState", 1908 args = {} 1909 ) 1910 public void test_getState() { 1911 Thread.State state = Thread.currentThread().getState(); 1912 assertNotNull(state); 1913 assertEquals(Thread.State.RUNNABLE, state); 1914 1915 final Semaphore sem = new Semaphore(0); 1916 final Object lock = new Object(); 1917 Thread th = new Thread() { 1918 @Override 1919 public void run() { 1920 while (!sem.hasQueuedThreads()) {} 1921 sem.release(); 1922 while (run) {} 1923 try { 1924 sem.acquire(); 1925 } catch (InterruptedException e) { 1926 fail("InterruptedException was thrown."); 1927 } 1928 synchronized (lock) { 1929 lock.equals(new Object()); 1930 } 1931 synchronized (lock) { 1932 try { 1933 sem.release(); 1934 lock.wait(Long.MAX_VALUE); 1935 } catch (InterruptedException e) { 1936 // expected 1937 } 1938 } 1939 } 1940 }; 1941 assertEquals(Thread.State.NEW, th.getState()); 1942 th.start(); 1943 try { 1944 sem.acquire(); 1945 } catch (InterruptedException e) { 1946 fail("InterruptedException was thrown."); 1947 } 1948 assertEquals(Thread.State.RUNNABLE, th.getState()); 1949 run = false; 1950 1951 while (!sem.hasQueuedThreads()){} 1952 1953 assertEquals(Thread.State.WAITING, th.getState()); 1954 synchronized (lock) { 1955 sem.release(); 1956 long start = System.currentTimeMillis(); 1957 while(start + 1000 > System.currentTimeMillis()) {} 1958 assertEquals(Thread.State.BLOCKED, th.getState()); 1959 } 1960 1961 try { 1962 sem.acquire(); 1963 } catch (InterruptedException e) { 1964 fail("InterruptedException was thrown."); 1965 } 1966 1967 synchronized (lock) { 1968 assertEquals(Thread.State.TIMED_WAITING, th.getState()); 1969 th.interrupt(); 1970 } 1971 1972 try { 1973 th.join(1000); 1974 } catch(InterruptedException ie) { 1975 fail("InterruptedException was thrown."); 1976 } 1977 assertEquals(Thread.State.TERMINATED, th.getState()); 1978 } 1979 boolean run = true; 1980 1981 /** 1982 * @tests java.lang.Thread#getUncaughtExceptionHandler 1983 * @tests java.lang.Thread#setUncaughtExceptionHandler 1984 */ 1985 @TestTargets({ 1986 @TestTargetNew( 1987 level = TestLevel.COMPLETE, 1988 notes = "", 1989 method = "getUncaughtExceptionHandler", 1990 args = {} 1991 ), 1992 @TestTargetNew( 1993 level = TestLevel.COMPLETE, 1994 notes = "", 1995 method = "setUncaughtExceptionHandler", 1996 args = {java.lang.Thread.UncaughtExceptionHandler.class} 1997 ) 1998 }) 1999 public void test_get_setUncaughtExceptionHandler() { 2000 class Handler implements UncaughtExceptionHandler { 2001 public void uncaughtException(Thread thread, Throwable ex) { 2002 } 2003 } 2004 2005 final Handler handler = new Handler(); 2006 Thread.currentThread().setUncaughtExceptionHandler(handler); 2007 assertSame(handler, Thread.currentThread().getUncaughtExceptionHandler()); 2008 2009 Thread.currentThread().setUncaughtExceptionHandler(null); 2010 2011 //TODO add security-based tests 2012 SecurityManager sm = new SecurityManager() { 2013 2014 public void checkPermission(Permission perm) { 2015 } 2016 2017 public void checkAccess(Thread t) { 2018 throw new SecurityException(); 2019 } 2020 }; 2021 st = new Thread(); 2022 SecurityManager oldSm = System.getSecurityManager(); 2023 System.setSecurityManager(sm); 2024 try { 2025 st.setUncaughtExceptionHandler(handler); 2026 fail("Should throw SecurityException"); 2027 } catch (SecurityException e) { 2028 // expected 2029 } finally { 2030 System.setSecurityManager(oldSm); 2031 } 2032 2033 } 2034 2035 /** 2036 * @tests java.lang.Thread#getId() 2037 */ 2038 @TestTargetNew( 2039 level = TestLevel.COMPLETE, 2040 notes = "", 2041 method = "getId", 2042 args = {} 2043 ) 2044 public void test_getId() { 2045 assertTrue("current thread's ID is not positive", Thread.currentThread().getId() > 0); 2046 2047 //check all the current threads for positive IDs 2048 Map<Thread, StackTraceElement[]> stMap = Thread.getAllStackTraces(); 2049 for (Thread thread : stMap.keySet()) { 2050 assertTrue("thread's ID is not positive: " + thread.getName(), thread.getId() > 0); 2051 } 2052 } 2053 2054 /** 2055 * @tests java.lang.Thread#holdLock() 2056 */ 2057 @TestTargetNew( 2058 level = TestLevel.COMPLETE, 2059 notes = "", 2060 method = "holdsLock", 2061 args = {java.lang.Object.class} 2062 ) 2063 public void test_holdsLock() { 2064 MonitoredClass monitor = new MonitoredClass(); 2065 2066 monitor.enterLocked(); 2067 monitor.enterNonLocked(); 2068 2069 try { 2070 Thread.holdsLock(null); 2071 fail("NullPointerException was not thrown."); 2072 } catch(NullPointerException npe) { 2073 //expected 2074 } 2075 } 2076 2077 @Override 2078 protected void tearDown() { 2079 try { 2080 if (st != null) 2081 st.interrupt(); 2082 } catch (Exception e) { 2083 } 2084 try { 2085 if (spinner != null) 2086 spinner.interrupt(); 2087 } catch (Exception e) { 2088 } 2089 try { 2090 if (ct != null) 2091 ct.interrupt(); 2092 } catch (Exception e) { 2093 } 2094 2095 try { 2096 spinner = null; 2097 st = null; 2098 ct = null; 2099 System.runFinalization(); 2100 } catch (Exception e) { 2101 } 2102 } 2103} 2104