1 /*
2  * Written by Doug Lea with assistance from members of JCP JSR-166
3  * Expert Group and released to the public domain, as explained at
4  * http://creativecommons.org/publicdomain/zero/1.0/
5  * Other contributors include Andrew Wright, Jeffrey Hayes,
6  * Pat Fisher, Mike Judd.
7  */
8 
9 package jsr166;
10 
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13 
14 public class ThreadTest extends JSR166TestCase {
15     // android-note: Removed because the CTS runner does a bad job of
16     // retrying tests that have suite() declarations.
17     //
18     // public static void main(String[] args) {
19     //     main(suite(), args);
20     // }
21     // public static Test suite() {
22     //     return new TestSuite(ThreadTest.class);
23     // }
24 
25     static class MyHandler implements Thread.UncaughtExceptionHandler {
uncaughtException(Thread t, Throwable e)26         public void uncaughtException(Thread t, Throwable e) {
27             e.printStackTrace();
28         }
29     }
30 
31     /**
32      * getUncaughtExceptionHandler returns ThreadGroup unless set,
33      * otherwise returning value of last setUncaughtExceptionHandler.
34      */
testGetAndSetUncaughtExceptionHandler()35     public void testGetAndSetUncaughtExceptionHandler() {
36         // these must be done all at once to avoid state
37         // dependencies across tests
38         Thread current = Thread.currentThread();
39         ThreadGroup tg = current.getThreadGroup();
40         MyHandler eh = new MyHandler();
41         assertSame(tg, current.getUncaughtExceptionHandler());
42         current.setUncaughtExceptionHandler(eh);
43         try {
44             assertSame(eh, current.getUncaughtExceptionHandler());
45         } finally {
46             current.setUncaughtExceptionHandler(null);
47         }
48         assertSame(tg, current.getUncaughtExceptionHandler());
49     }
50 
51     /**
52      * getDefaultUncaughtExceptionHandler returns value of last
53      * setDefaultUncaughtExceptionHandler.
54      */
testGetAndSetDefaultUncaughtExceptionHandler()55     public void testGetAndSetDefaultUncaughtExceptionHandler() {
56         // android-note: Removed assertion; all "normal" android apps (including CTS tests) have a
57         // default uncaught exception handler installed by the framework.
58         //
59         // assertEquals(null, Thread.getDefaultUncaughtExceptionHandler());
60         // failure due to SecurityException is OK.
61         // Would be nice to explicitly test both ways, but cannot yet.
62         Thread.UncaughtExceptionHandler defaultHandler
63             = Thread.getDefaultUncaughtExceptionHandler();
64         MyHandler eh = new MyHandler();
65         try {
66             Thread.setDefaultUncaughtExceptionHandler(eh);
67             try {
68                 assertSame(eh, Thread.getDefaultUncaughtExceptionHandler());
69             } finally {
70                 Thread.setDefaultUncaughtExceptionHandler(defaultHandler);
71             }
72         } catch (SecurityException ok) {
73             assertNotNull(System.getSecurityManager());
74         }
75         assertSame(defaultHandler, Thread.getDefaultUncaughtExceptionHandler());
76     }
77 
78     // How to test actually using UEH within junit?
79 
80 }
81