Home | History | Annotate | Download | only in lang
      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 
     18 package org.apache.harmony.tests.java.lang;
     19 
     20 import java.lang.Thread.UncaughtExceptionHandler;
     21 import java.util.Map;
     22 
     23 public class ThreadTest extends junit.framework.TestCase {
     24 
     25     static class SimpleThread implements Runnable {
     26         int delay;
     27 
     28         public void run() {
     29             try {
     30                 synchronized (this) {
     31                     this.notify();
     32                     this.wait(delay);
     33                 }
     34             } catch (InterruptedException e) {
     35                 return;
     36             }
     37 
     38         }
     39 
     40         public SimpleThread(int d) {
     41             if (d >= 0)
     42                 delay = d;
     43         }
     44     }
     45 
     46     static class YieldThread implements Runnable {
     47         volatile int delay;
     48 
     49         public void run() {
     50             int x = 0;
     51             while (true) {
     52                 ++x;
     53             }
     54         }
     55 
     56         public YieldThread(int d) {
     57             if (d >= 0)
     58                 delay = d;
     59         }
     60     }
     61 
     62     static class ResSupThread implements Runnable {
     63         Thread parent;
     64 
     65         volatile int checkVal = -1;
     66 
     67         public void run() {
     68             try {
     69                 synchronized (this) {
     70                     this.notify();
     71                 }
     72                 while (true) {
     73                     checkVal++;
     74                     zz();
     75                     Thread.sleep(100);
     76                 }
     77             } catch (InterruptedException e) {
     78                 return;
     79             } catch (BogusException e) {
     80                 try {
     81                     // Give parent a chance to sleep
     82                     Thread.sleep(500);
     83                 } catch (InterruptedException x) {
     84                 }
     85                 parent.interrupt();
     86                 while (!Thread.currentThread().isInterrupted()) {
     87                     // Don't hog the CPU
     88                     try {
     89                         Thread.sleep(50);
     90                     } catch (InterruptedException x) {
     91                         // This is what we've been waiting for...don't throw it
     92                         // away!
     93                         break;
     94                     }
     95                 }
     96             }
     97         }
     98 
     99         public void zz() throws BogusException {
    100         }
    101 
    102         public ResSupThread(Thread t) {
    103             parent = t;
    104         }
    105 
    106         public synchronized int getCheckVal() {
    107             return checkVal;
    108         }
    109     }
    110 
    111     static class BogusException extends Throwable {
    112 
    113         private static final long serialVersionUID = 1L;
    114 
    115         public BogusException(String s) {
    116             super(s);
    117         }
    118     }
    119 
    120     Thread st, ct, spinner;
    121 
    122     /**
    123      * java.lang.Thread#Thread(java.lang.Runnable)
    124      */
    125     public void test_ConstructorLjava_lang_Runnable() {
    126         // Test for method java.lang.Thread(java.lang.Runnable)
    127         ct = new Thread(new SimpleThread(10));
    128         ct.start();
    129     }
    130 
    131     /**
    132      * java.lang.Thread#Thread(java.lang.Runnable, java.lang.String)
    133      */
    134     public void test_ConstructorLjava_lang_RunnableLjava_lang_String() {
    135         // Test for method java.lang.Thread(java.lang.Runnable,
    136         // java.lang.String)
    137         Thread st1 = new Thread(new SimpleThread(1), "SimpleThread1");
    138         assertEquals("Constructed thread with incorrect thread name", "SimpleThread1", st1
    139                 .getName());
    140         st1.start();
    141     }
    142 
    143     /**
    144      * java.lang.Thread#Thread(java.lang.String)
    145      */
    146     public void test_ConstructorLjava_lang_String() {
    147         // Test for method java.lang.Thread(java.lang.String)
    148         Thread t = new Thread("Testing");
    149         assertEquals("Created tread with incorrect name",
    150                 "Testing", t.getName());
    151         t.start();
    152     }
    153 
    154     /**
    155      * java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable)
    156      */
    157     public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_Runnable() {
    158         // Test for method java.lang.Thread(java.lang.ThreadGroup,
    159         // java.lang.Runnable)
    160         ThreadGroup tg = new ThreadGroup("Test Group1");
    161         st = new Thread(tg, new SimpleThread(1), "SimpleThread2");
    162         assertTrue("Returned incorrect thread group", st.getThreadGroup() == tg);
    163         st.start();
    164         try {
    165             st.join();
    166         } catch (InterruptedException e) {
    167         }
    168         tg.destroy();
    169     }
    170 
    171     /**
    172      * java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable,
    173      *java.lang.String)
    174      */
    175     @SuppressWarnings("DeadThread")
    176     public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_RunnableLjava_lang_String() {
    177         // Test for method java.lang.Thread(java.lang.ThreadGroup,
    178         // java.lang.Runnable, java.lang.String)
    179         ThreadGroup tg = new ThreadGroup("Test Group2");
    180         st = new Thread(tg, new SimpleThread(1), "SimpleThread3");
    181         assertTrue("Constructed incorrect thread", (st.getThreadGroup() == tg)
    182                 && st.getName().equals("SimpleThread3"));
    183         st.start();
    184         try {
    185             st.join();
    186         } catch (InterruptedException e) {
    187         }
    188         tg.destroy();
    189 
    190         Runnable r = new Runnable() {
    191             public void run() {
    192             }
    193         };
    194 
    195         ThreadGroup foo = null;
    196         try {
    197             new Thread(foo = new ThreadGroup("foo"), r, null);
    198             // Should not get here
    199             fail("Null cannot be accepted as Thread name");
    200         } catch (NullPointerException npe) {
    201             assertTrue("Null cannot be accepted as Thread name", true);
    202             foo.destroy();
    203         }
    204 
    205     }
    206 
    207     /**
    208      * java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.String)
    209      */
    210     public void test_ConstructorLjava_lang_ThreadGroupLjava_lang_String() {
    211         // Test for method java.lang.Thread(java.lang.ThreadGroup,
    212         // java.lang.String)
    213         st = new Thread(new SimpleThread(1), "SimpleThread4");
    214         assertEquals("Returned incorrect thread name",
    215                 "SimpleThread4", st.getName());
    216         st.start();
    217     }
    218 
    219     /**
    220      * java.lang.Thread#activeCount()
    221      */
    222     public void test_activeCount() {
    223         // Test for method int java.lang.Thread.activeCount()
    224         Thread t = new Thread(new SimpleThread(10));
    225         int active = 0;
    226         synchronized (t) {
    227             t.start();
    228             active = Thread.activeCount();
    229         }
    230         assertTrue("Incorrect activeCount for current group: " + active, active > 1);
    231         try {
    232             t.join();
    233         } catch (InterruptedException e) {
    234         }
    235     }
    236 
    237     /**
    238      * java.lang.Thread#checkAccess()
    239      */
    240     public void test_checkAccess() {
    241         // Test for method void java.lang.Thread.checkAccess()
    242         ThreadGroup tg = new ThreadGroup("Test Group3");
    243         try {
    244             st = new Thread(tg, new SimpleThread(1), "SimpleThread5");
    245             st.checkAccess();
    246             assertTrue("CheckAccess passed", true);
    247         } catch (SecurityException e) {
    248             fail("CheckAccess failed : " + e.getMessage());
    249         }
    250         st.start();
    251         try {
    252             st.join();
    253         } catch (InterruptedException e) {
    254         }
    255         tg.destroy();
    256     }
    257 
    258     /**
    259      * java.lang.Thread#countStackFrames()
    260      */
    261     @SuppressWarnings("deprecation")
    262     public void test_countStackFrames() {
    263         /*
    264          * Thread.countStackFrames() is unpredictable, so we just test that it
    265          * doesn't throw an exception.
    266          */
    267         Thread.currentThread().countStackFrames();
    268     }
    269 
    270     /**
    271      * java.lang.Thread#currentThread()
    272      */
    273     public void test_currentThread() {
    274         assertNotNull(Thread.currentThread());
    275     }
    276 
    277     public void test_destroy_throwsUnsupportedOperationException() {
    278         try {
    279             new Thread().destroy();
    280             fail();
    281         } catch (UnsupportedOperationException expected) {
    282         }
    283     }
    284 
    285     /**
    286      * java.lang.Thread#enumerate(java.lang.Thread[])
    287      */
    288     public void test_enumerate$Ljava_lang_Thread() {
    289         // Test for method int java.lang.Thread.enumerate(java.lang.Thread [])
    290         // The test has been updated according to HARMONY-1974 JIRA issue.
    291 
    292         class MyThread extends Thread {
    293             MyThread(ThreadGroup tg, String name) {
    294                 super(tg, name);
    295             }
    296 
    297             boolean failed = false;
    298             String failMessage = null;
    299 
    300             public void run() {
    301                 SimpleThread st1 = null;
    302                 SimpleThread st2 = null;
    303                 ThreadGroup mytg = null;
    304                 Thread firstOne = null;
    305                 Thread secondOne = null;
    306                 try {
    307                     int arrayLength = 10;
    308                     Thread[] tarray = new Thread[arrayLength];
    309                     st1 = new SimpleThread(-1);
    310                     st2 = new SimpleThread(-1);
    311                     mytg = new ThreadGroup("jp");
    312                     firstOne = new Thread(mytg, st1, "firstOne2");
    313                     secondOne = new Thread(mytg, st2, "secondOne1");
    314                     int count = Thread.enumerate(tarray);
    315                     assertEquals("Incorrect value returned1",
    316                             1, count);
    317                     synchronized (st1) {
    318                         firstOne.start();
    319                         try {
    320                             st1.wait();
    321                         } catch (InterruptedException e) {
    322                         }
    323                     }
    324                     count = Thread.enumerate(tarray);
    325                     assertEquals("Incorrect value returned2",
    326                             2, count);
    327                     synchronized (st2) {
    328                         secondOne.start();
    329                         try {
    330                             st2.wait();
    331                         } catch (InterruptedException e) {
    332                         }
    333                     }
    334                     count = Thread.enumerate(tarray);
    335                     assertEquals("Incorrect value returned3",
    336                             3, count);
    337                 } catch (junit.framework.AssertionFailedError e) {
    338                     failed = true;
    339                     failMessage = e.getMessage();
    340                 } finally {
    341                     synchronized (st1) {
    342                         firstOne.interrupt();
    343                     }
    344                     synchronized (st2) {
    345                         secondOne.interrupt();
    346                     }
    347                     try {
    348                         firstOne.join();
    349                         secondOne.join();
    350                     } catch (InterruptedException e) {
    351                     }
    352                     mytg.destroy();
    353                 }
    354             }
    355         }
    356         ;
    357 
    358         ThreadGroup tg = new ThreadGroup("tg");
    359         MyThread t = new MyThread(tg, "top");
    360         t.start();
    361         try {
    362             t.join();
    363         } catch (InterruptedException e) {
    364             fail("Unexpected interrupt");
    365         } finally {
    366             tg.destroy();
    367         }
    368         assertFalse(t.failMessage, t.failed);
    369     }
    370 
    371     /**
    372      * java.lang.Thread#getContextClassLoader()
    373      */
    374     public void test_getContextClassLoader() {
    375         // Test for method java.lang.ClassLoader
    376         // java.lang.Thread.getContextClassLoader()
    377         Thread t = new Thread();
    378         assertTrue("Incorrect class loader returned",
    379                 t.getContextClassLoader() == Thread.currentThread()
    380                         .getContextClassLoader());
    381         t.start();
    382 
    383     }
    384 
    385     /**
    386      * java.lang.Thread#getName()
    387      */
    388     public void test_getName() {
    389         // Test for method java.lang.String java.lang.Thread.getName()
    390         st = new Thread(new SimpleThread(1), "SimpleThread6");
    391         assertEquals("Returned incorrect thread name",
    392                 "SimpleThread6", st.getName());
    393         st.start();
    394     }
    395 
    396     /**
    397      * java.lang.Thread#getPriority()
    398      */
    399     public void test_getPriority() {
    400         // Test for method int java.lang.Thread.getPriority()
    401         st = new Thread(new SimpleThread(1));
    402         st.setPriority(Thread.MAX_PRIORITY);
    403         assertTrue("Returned incorrect thread priority",
    404                 st.getPriority() == Thread.MAX_PRIORITY);
    405         st.start();
    406     }
    407 
    408     /**
    409      * java.lang.Thread#getThreadGroup()
    410      */
    411     public void test_getThreadGroup() {
    412         // Test for method java.lang.ThreadGroup
    413         // java.lang.Thread.getThreadGroup()
    414         ThreadGroup tg = new ThreadGroup("Test Group4");
    415         st = new Thread(tg, new SimpleThread(1), "SimpleThread8");
    416         assertTrue("Returned incorrect thread group", st.getThreadGroup() == tg);
    417         st.start();
    418         try {
    419             st.join();
    420         } catch (InterruptedException e) {
    421         }
    422         assertNull("group should be null", st.getThreadGroup());
    423         assertNotNull("toString() should not be null", st.toString());
    424         tg.destroy();
    425 
    426         final Object lock = new Object();
    427         Thread t = new Thread() {
    428             @Override
    429             public void run() {
    430                 synchronized (lock) {
    431                     lock.notifyAll();
    432                 }
    433             }
    434         };
    435         synchronized (lock) {
    436             t.start();
    437             try {
    438                 lock.wait();
    439             } catch (InterruptedException e) {
    440             }
    441         }
    442         int running = 0;
    443         while (t.isAlive())
    444             running++;
    445         ThreadGroup group = t.getThreadGroup();
    446         assertNull("ThreadGroup is not null", group);
    447     }
    448 
    449     /**
    450      * java.lang.Thread#interrupt()
    451      */
    452     public void test_interrupt() {
    453         // Test for method void java.lang.Thread.interrupt()
    454         final Object lock = new Object();
    455         class ChildThread1 extends Thread {
    456             Thread parent;
    457 
    458             boolean sync;
    459 
    460             @Override
    461             public void run() {
    462                 if (sync) {
    463                     synchronized (lock) {
    464                         lock.notify();
    465                         try {
    466                             lock.wait();
    467                         } catch (InterruptedException e) {
    468                         }
    469                     }
    470                 }
    471                 parent.interrupt();
    472             }
    473 
    474             public ChildThread1(Thread p, String name, boolean sync) {
    475                 super(name);
    476                 parent = p;
    477                 this.sync = sync;
    478             }
    479         }
    480         boolean interrupted = false;
    481         try {
    482             ct = new ChildThread1(Thread.currentThread(), "Interrupt Test1",
    483                     false);
    484             synchronized (lock) {
    485                 ct.start();
    486                 lock.wait();
    487             }
    488         } catch (InterruptedException e) {
    489             interrupted = true;
    490         }
    491         assertTrue("Failed to Interrupt thread1", interrupted);
    492 
    493         interrupted = false;
    494         try {
    495             ct = new ChildThread1(Thread.currentThread(), "Interrupt Test2",
    496                     true);
    497             synchronized (lock) {
    498                 ct.start();
    499                 lock.wait();
    500                 lock.notify();
    501             }
    502             Thread.sleep(20000);
    503         } catch (InterruptedException e) {
    504             interrupted = true;
    505         }
    506         assertTrue("Failed to Interrupt thread2", interrupted);
    507 
    508     }
    509 
    510     /**
    511      * java.lang.Thread#interrupted()
    512      */
    513     public void test_interrupted() {
    514         assertFalse("Interrupted returned true for non-interrupted thread", Thread
    515                 .interrupted());
    516         Thread.currentThread().interrupt();
    517         assertTrue("Interrupted returned true for non-interrupted thread", Thread.interrupted());
    518         assertFalse("Failed to clear interrupted flag", Thread.interrupted());
    519     }
    520 
    521     /**
    522      * java.lang.Thread#isAlive()
    523      */
    524     public void test_isAlive() {
    525         // Test for method boolean java.lang.Thread.isAlive()
    526         SimpleThread simple;
    527         st = new Thread(simple = new SimpleThread(500));
    528         assertFalse("A thread that wasn't started is alive.", st.isAlive());
    529         synchronized (simple) {
    530             st.start();
    531             try {
    532                 simple.wait();
    533             } catch (InterruptedException e) {
    534             }
    535         }
    536         assertTrue("Started thread returned false", st.isAlive());
    537         try {
    538             st.join();
    539         } catch (InterruptedException e) {
    540             fail("Thread did not die");
    541         }
    542         assertTrue("Stopped thread returned true", !st.isAlive());
    543     }
    544 
    545     /**
    546      * java.lang.Thread#isDaemon()
    547      */
    548     public void test_isDaemon() {
    549         // Test for method boolean java.lang.Thread.isDaemon()
    550         st = new Thread(new SimpleThread(1), "SimpleThread10");
    551         assertEquals(Thread.currentThread().isDaemon(), st.isDaemon());
    552         st.setDaemon(true);
    553         assertTrue("Daemon thread returned false", st.isDaemon());
    554         st.start();
    555         try {
    556             st.join();
    557         } catch (InterruptedException ie) {
    558             fail();
    559         }
    560     }
    561 
    562     /**
    563      * java.lang.Thread#isInterrupted()
    564      */
    565     public void test_isInterrupted() {
    566         // Test for method boolean java.lang.Thread.isInterrupted()
    567         class SpinThread implements Runnable {
    568             public volatile boolean done = false;
    569 
    570             public void run() {
    571                 while (!Thread.currentThread().isInterrupted())
    572                     ;
    573                 while (!done)
    574                     ;
    575             }
    576         }
    577 
    578         SpinThread spin = new SpinThread();
    579         spinner = new Thread(spin);
    580         spinner.start();
    581         Thread.yield();
    582         try {
    583             assertTrue("Non-Interrupted thread returned true", !spinner
    584                     .isInterrupted());
    585             spinner.interrupt();
    586             assertTrue("Interrupted thread returned false", spinner
    587                     .isInterrupted());
    588             spin.done = true;
    589         } finally {
    590             spinner.interrupt();
    591             spin.done = true;
    592         }
    593     }
    594 
    595     /**
    596      * java.lang.Thread#join()
    597      */
    598     public void test_join() {
    599         // Test for method void java.lang.Thread.join()
    600         SimpleThread simple;
    601         try {
    602             st = new Thread(simple = new SimpleThread(100));
    603             // cause isAlive() to be compiled by the JIT, as it must be called
    604             // within 100ms below.
    605             assertTrue("Thread is alive", !st.isAlive());
    606             synchronized (simple) {
    607                 st.start();
    608                 simple.wait();
    609             }
    610             st.join();
    611         } catch (InterruptedException e) {
    612             fail("Join failed ");
    613         }
    614         assertTrue("Joined thread is still alive", !st.isAlive());
    615         boolean result = true;
    616         Thread th = new Thread("test");
    617         try {
    618             th.join();
    619         } catch (InterruptedException e) {
    620             result = false;
    621         }
    622         assertTrue("Hung joining a non-started thread", result);
    623         th.start();
    624     }
    625 
    626     /**
    627      * java.lang.Thread#join(long)
    628      */
    629     public void test_joinJ() {
    630         // Test for method void java.lang.Thread.join(long)
    631         SimpleThread simple;
    632         try {
    633             st = new Thread(simple = new SimpleThread(1000), "SimpleThread12");
    634             // cause isAlive() to be compiled by the JIT, as it must be called
    635             // within 100ms below.
    636             assertTrue("Thread is alive", !st.isAlive());
    637             synchronized (simple) {
    638                 st.start();
    639                 simple.wait();
    640             }
    641             st.join(10);
    642         } catch (InterruptedException e) {
    643             fail("Join failed ");
    644         }
    645         assertTrue("Join failed to timeout", st.isAlive());
    646 
    647         st.interrupt();
    648         try {
    649             st = new Thread(simple = new SimpleThread(100), "SimpleThread13");
    650             synchronized (simple) {
    651                 st.start();
    652                 simple.wait();
    653             }
    654             st.join(1000);
    655         } catch (InterruptedException e) {
    656             fail("Join failed : " + e.getMessage());
    657             return;
    658         }
    659         assertTrue("Joined thread is still alive", !st.isAlive());
    660 
    661         final Object lock = new Object();
    662         final Thread main = Thread.currentThread();
    663         Thread killer = new Thread(new Runnable() {
    664             public void run() {
    665                 try {
    666                     synchronized (lock) {
    667                         lock.notify();
    668                     }
    669                     Thread.sleep(100);
    670                 } catch (InterruptedException e) {
    671                     return;
    672                 }
    673                 main.interrupt();
    674             }
    675         });
    676         boolean result = true;
    677         Thread th = new Thread("test");
    678         try {
    679             synchronized (lock) {
    680                 killer.start();
    681                 lock.wait();
    682             }
    683             th.join(200);
    684         } catch (InterruptedException e) {
    685             result = false;
    686         }
    687         killer.interrupt();
    688         assertTrue("Hung joining a non-started thread", result);
    689         th.start();
    690     }
    691 
    692     /**
    693      * java.lang.Thread#join(long, int)
    694      */
    695     public void test_joinJI() throws Exception {
    696         // Test for method void java.lang.Thread.join(long, int)
    697         SimpleThread simple;
    698         st = new Thread(simple = new SimpleThread(1000), "Squawk1");
    699         assertTrue("Thread is alive", !st.isAlive());
    700         synchronized (simple) {
    701             st.start();
    702             simple.wait();
    703         }
    704 
    705         long firstRead = System.currentTimeMillis();
    706         st.join(100, 999999);
    707         long secondRead = System.currentTimeMillis();
    708         assertTrue("Did not join by appropriate time: " + secondRead + "-"
    709                 + firstRead + "=" + (secondRead - firstRead), secondRead
    710                 - firstRead <= 300);
    711         assertTrue("Joined thread is not alive", st.isAlive());
    712         st.interrupt();
    713 
    714         final Object lock = new Object();
    715         final Thread main = Thread.currentThread();
    716         Thread killer = new Thread(new Runnable() {
    717             public void run() {
    718                 try {
    719                     synchronized (lock) {
    720                         lock.notify();
    721                     }
    722                     Thread.sleep(100);
    723                 } catch (InterruptedException e) {
    724                     return;
    725                 }
    726                 main.interrupt();
    727             }
    728         });
    729         boolean result = true;
    730         Thread th = new Thread("test");
    731         try {
    732             synchronized (lock) {
    733                 killer.start();
    734                 lock.wait();
    735             }
    736             th.join(200, 20);
    737         } catch (InterruptedException e) {
    738             result = false;
    739         }
    740         killer.interrupt();
    741         assertTrue("Hung joining a non-started thread", result);
    742         th.start();
    743     }
    744 
    745     /**
    746      * java.lang.Thread#run()
    747      */
    748     public void test_run() {
    749         // Test for method void java.lang.Thread.run()
    750         class RunThread implements Runnable {
    751             boolean didThreadRun = false;
    752 
    753             public void run() {
    754                 didThreadRun = true;
    755             }
    756         }
    757         RunThread rt = new RunThread();
    758         Thread t = new Thread(rt);
    759         try {
    760             t.start();
    761             int count = 0;
    762             while (!rt.didThreadRun && count < 20) {
    763                 Thread.sleep(100);
    764                 count++;
    765             }
    766             assertTrue("Thread did not run", rt.didThreadRun);
    767             t.join();
    768         } catch (InterruptedException e) {
    769             assertTrue("Joined thread was interrupted", true);
    770         }
    771         assertTrue("Joined thread is still alive", !t.isAlive());
    772     }
    773 
    774     /**
    775      * java.lang.Thread#setDaemon(boolean)
    776      */
    777     public void test_setDaemonZ() {
    778         // Test for method void java.lang.Thread.setDaemon(boolean)
    779         st = new Thread(new SimpleThread(1), "SimpleThread14");
    780         st.setDaemon(true);
    781         assertTrue("Failed to set thread as daemon thread", st.isDaemon());
    782         st.start();
    783     }
    784 
    785     /**
    786      * java.lang.Thread#setName(java.lang.String)
    787      */
    788     public void test_setNameLjava_lang_String() {
    789         // Test for method void java.lang.Thread.setName(java.lang.String)
    790         st = new Thread(new SimpleThread(1), "SimpleThread15");
    791         st.setName("Bogus Name");
    792         assertEquals("Failed to set thread name",
    793                 "Bogus Name", st.getName());
    794         try {
    795             st.setName(null);
    796             fail("Null should not be accepted as a valid name");
    797         } catch (NullPointerException e) {
    798             // success
    799             assertTrue("Null should not be accepted as a valid name", true);
    800         }
    801         st.start();
    802     }
    803 
    804     /**
    805      * java.lang.Thread#setPriority(int)
    806      */
    807     public void test_setPriorityI() {
    808         // Test for method void java.lang.Thread.setPriority(int)
    809         st = new Thread(new SimpleThread(1));
    810         st.setPriority(Thread.MAX_PRIORITY);
    811         assertTrue("Failed to set priority",
    812                 st.getPriority() == Thread.MAX_PRIORITY);
    813         st.start();
    814     }
    815 
    816     /**
    817      * java.lang.Thread#sleep(long)
    818      */
    819     public void test_sleepJ() {
    820         // Test for method void java.lang.Thread.sleep(long)
    821 
    822         // TODO : Test needs enhancing.
    823         long stime = 0, ftime = 0;
    824         try {
    825             stime = System.currentTimeMillis();
    826             Thread.sleep(1000);
    827             ftime = System.currentTimeMillis();
    828         } catch (InterruptedException e) {
    829             fail("Unexpected interrupt received");
    830         }
    831         assertTrue("Failed to sleep long enough", (ftime - stime) >= 800);
    832     }
    833 
    834     /**
    835      * java.lang.Thread#sleep(long, int)
    836      */
    837     public void test_sleepJI() {
    838         // Test for method void java.lang.Thread.sleep(long, int)
    839 
    840         // TODO : Test needs revisiting.
    841         long stime = 0, ftime = 0;
    842         try {
    843             stime = System.currentTimeMillis();
    844             Thread.sleep(1000, 999999);
    845             ftime = System.currentTimeMillis();
    846         } catch (InterruptedException e) {
    847             fail("Unexpected interrupt received");
    848         }
    849         long result = ftime - stime;
    850         assertTrue("Failed to sleep long enough: " + result, result >= 900
    851                 && result <= 1100);
    852     }
    853 
    854     /**
    855      * java.lang.Thread#start()
    856      */
    857     public void test_start() {
    858         // Test for method void java.lang.Thread.start()
    859         try {
    860             ResSupThread t = new ResSupThread(Thread.currentThread());
    861             synchronized (t) {
    862                 ct = new Thread(t, "Interrupt Test4");
    863                 ct.start();
    864                 t.wait();
    865             }
    866             assertTrue("Thread is not running1", ct.isAlive());
    867             // Let the child thread get going.
    868             int orgval = t.getCheckVal();
    869             Thread.sleep(150);
    870             assertTrue("Thread is not running2", orgval != t.getCheckVal());
    871             ct.interrupt();
    872         } catch (InterruptedException e) {
    873             fail("Unexpected interrupt occurred");
    874         }
    875     }
    876 
    877     /**
    878      * java.lang.Thread#toString()
    879      */
    880     public void test_toString() {
    881         // Test for method java.lang.String java.lang.Thread.toString()
    882         ThreadGroup tg = new ThreadGroup("Test Group5");
    883         st = new Thread(tg, new SimpleThread(1), "SimpleThread17");
    884         final String stString = st.toString();
    885         final String expected = "Thread[SimpleThread17,5,Test Group5]";
    886         assertTrue("Returned incorrect string: " + stString + "\t(expecting :"
    887                 + expected + ")", stString.equals(expected));
    888         st.start();
    889         try {
    890             st.join();
    891         } catch (InterruptedException e) {
    892         }
    893         tg.destroy();
    894     }
    895 
    896     /**
    897      * java.lang.Thread#getAllStackTraces()
    898      */
    899     public void test_getAllStackTraces() {
    900         Map<Thread, StackTraceElement[]> stMap = Thread.getAllStackTraces();
    901         assertNotNull(stMap);
    902         //TODO add security-based tests
    903     }
    904 
    905     /**
    906      * java.lang.Thread#getDefaultUncaughtExceptionHandler
    907      * java.lang.Thread#setDefaultUncaughtExceptionHandler
    908      */
    909     public void test_get_setDefaultUncaughtExceptionHandler() {
    910         class Handler implements UncaughtExceptionHandler {
    911             public void uncaughtException(Thread thread, Throwable ex) {
    912             }
    913         }
    914 
    915         final Handler handler = new Handler();
    916         Thread.setDefaultUncaughtExceptionHandler(handler);
    917         assertSame(handler, Thread.getDefaultUncaughtExceptionHandler());
    918 
    919         Thread.setDefaultUncaughtExceptionHandler(null);
    920         assertNull(Thread.getDefaultUncaughtExceptionHandler());
    921         //TODO add security-based tests
    922     }
    923 
    924     /**
    925      * java.lang.Thread#getStackTrace()
    926      */
    927     public void test_getStackTrace() {
    928         StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    929 
    930         assertNotNull(stackTrace);
    931 
    932         stack_trace_loop:
    933         {
    934             for (int i = 0; i < stackTrace.length; i++) {
    935                 StackTraceElement e = stackTrace[i];
    936                 if (getClass().getName().equals(e.getClassName())) {
    937                     if ("test_getStackTrace".equals(e.getMethodName())) {
    938                         break stack_trace_loop;
    939                     }
    940                 }
    941             }
    942             fail("class and method not found in stack trace");
    943         }
    944 
    945         //TODO add security-based tests
    946     }
    947 
    948     /**
    949      * java.lang.Thread#getState()
    950      */
    951     public void test_getState() {
    952         Thread.State state = Thread.currentThread().getState();
    953         assertNotNull(state);
    954         assertEquals(Thread.State.RUNNABLE, state);
    955         //TODO add additional state tests
    956     }
    957 
    958     /**
    959      * java.lang.Thread#getUncaughtExceptionHandler
    960      * java.lang.Thread#setUncaughtExceptionHandler
    961      */
    962     public void test_get_setUncaughtExceptionHandler() {
    963         class Handler implements UncaughtExceptionHandler {
    964             public void uncaughtException(Thread thread, Throwable ex) {
    965             }
    966         }
    967 
    968         final Handler handler = new Handler();
    969         Thread.currentThread().setUncaughtExceptionHandler(handler);
    970         assertSame(handler, Thread.currentThread().getUncaughtExceptionHandler());
    971 
    972         Thread.currentThread().setUncaughtExceptionHandler(null);
    973 
    974         //TODO add security-based tests
    975     }
    976 
    977     /**
    978      * java.lang.Thread#getId()
    979      */
    980     public void test_getId() {
    981         assertTrue("current thread's ID is not positive", Thread.currentThread().getId() > 0);
    982 
    983         //check all the current threads for positive IDs
    984         Map<Thread, StackTraceElement[]> stMap = Thread.getAllStackTraces();
    985         for (Thread thread : stMap.keySet()) {
    986             assertTrue("thread's ID is not positive: " + thread.getName(), thread.getId() > 0);
    987         }
    988     }
    989 
    990 
    991     @Override
    992     protected void tearDown() {
    993         try {
    994             if (st != null)
    995                 st.interrupt();
    996         } catch (Exception e) {
    997         }
    998         try {
    999             if (spinner != null)
   1000                 spinner.interrupt();
   1001         } catch (Exception e) {
   1002         }
   1003         try {
   1004             if (ct != null)
   1005                 ct.interrupt();
   1006         } catch (Exception e) {
   1007         }
   1008 
   1009         try {
   1010             spinner = null;
   1011             st = null;
   1012             ct = null;
   1013             System.runFinalization();
   1014         } catch (Exception e) {
   1015         }
   1016     }
   1017 }
   1018