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