1 |
dl |
1.1 |
/* |
2 |
dl |
1.8 |
* 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 |
jsr166 |
1.13 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.10 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.15 |
import junit.framework.Test; |
10 |
|
|
import junit.framework.TestSuite; |
11 |
dl |
1.1 |
|
12 |
dl |
1.3 |
public class ThreadTest extends JSR166TestCase { |
13 |
dl |
1.1 |
public static void main(String[] args) { |
14 |
jsr166 |
1.18 |
main(suite(), args); |
15 |
dl |
1.1 |
} |
16 |
jsr166 |
1.10 |
|
17 |
dl |
1.1 |
public static Test suite() { |
18 |
jsr166 |
1.12 |
return new TestSuite(ThreadTest.class); |
19 |
dl |
1.1 |
} |
20 |
|
|
|
21 |
|
|
static class MyHandler implements Thread.UncaughtExceptionHandler { |
22 |
|
|
public void uncaughtException(Thread t, Throwable e) { |
23 |
|
|
e.printStackTrace(); |
24 |
|
|
} |
25 |
|
|
} |
26 |
jsr166 |
1.10 |
|
27 |
dl |
1.5 |
/** |
28 |
dl |
1.6 |
* getUncaughtExceptionHandler returns ThreadGroup unless set, |
29 |
dl |
1.9 |
* otherwise returning value of last setUncaughtExceptionHandler. |
30 |
dl |
1.5 |
*/ |
31 |
dl |
1.1 |
public void testGetAndSetUncaughtExceptionHandler() { |
32 |
|
|
// these must be done all at once to avoid state |
33 |
|
|
// dependencies across tests |
34 |
|
|
Thread current = Thread.currentThread(); |
35 |
|
|
ThreadGroup tg = current.getThreadGroup(); |
36 |
|
|
MyHandler eh = new MyHandler(); |
37 |
jsr166 |
1.16 |
assertSame(tg, current.getUncaughtExceptionHandler()); |
38 |
dl |
1.1 |
current.setUncaughtExceptionHandler(eh); |
39 |
jsr166 |
1.16 |
try { |
40 |
|
|
assertSame(eh, current.getUncaughtExceptionHandler()); |
41 |
|
|
} finally { |
42 |
|
|
current.setUncaughtExceptionHandler(null); |
43 |
|
|
} |
44 |
|
|
assertSame(tg, current.getUncaughtExceptionHandler()); |
45 |
dl |
1.1 |
} |
46 |
dl |
1.4 |
|
47 |
dl |
1.5 |
/** |
48 |
dl |
1.6 |
* getDefaultUncaughtExceptionHandler returns value of last |
49 |
jsr166 |
1.10 |
* setDefaultUncaughtExceptionHandler. |
50 |
dl |
1.5 |
*/ |
51 |
dl |
1.21 |
@SuppressWarnings("removal") |
52 |
dl |
1.4 |
public void testGetAndSetDefaultUncaughtExceptionHandler() { |
53 |
jsr166 |
1.20 |
assertNull(Thread.getDefaultUncaughtExceptionHandler()); |
54 |
jsr166 |
1.19 |
// failure due to SecurityException is OK. |
55 |
dl |
1.4 |
// Would be nice to explicitly test both ways, but cannot yet. |
56 |
jsr166 |
1.16 |
Thread.UncaughtExceptionHandler defaultHandler |
57 |
|
|
= Thread.getDefaultUncaughtExceptionHandler(); |
58 |
|
|
MyHandler eh = new MyHandler(); |
59 |
dl |
1.4 |
try { |
60 |
dl |
1.6 |
Thread.setDefaultUncaughtExceptionHandler(eh); |
61 |
jsr166 |
1.16 |
try { |
62 |
|
|
assertSame(eh, Thread.getDefaultUncaughtExceptionHandler()); |
63 |
|
|
} finally { |
64 |
|
|
Thread.setDefaultUncaughtExceptionHandler(defaultHandler); |
65 |
|
|
} |
66 |
|
|
} catch (SecurityException ok) { |
67 |
|
|
assertNotNull(System.getSecurityManager()); |
68 |
dl |
1.6 |
} |
69 |
jsr166 |
1.16 |
assertSame(defaultHandler, Thread.getDefaultUncaughtExceptionHandler()); |
70 |
dl |
1.6 |
} |
71 |
|
|
|
72 |
dl |
1.1 |
// How to test actually using UEH within junit? |
73 |
|
|
|
74 |
|
|
} |