1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
< |
import junit.framework.*; |
9 |
> |
import junit.framework.Test; |
10 |
> |
import junit.framework.TestSuite; |
11 |
|
|
12 |
|
public class ThreadTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run(suite()); |
14 |
> |
main(suite(), args); |
15 |
|
} |
16 |
< |
|
16 |
> |
|
17 |
|
public static Test suite() { |
18 |
< |
return new TestSuite(ThreadTest.class); |
18 |
> |
return new TestSuite(ThreadTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
static class MyHandler implements Thread.UncaughtExceptionHandler { |
23 |
|
e.printStackTrace(); |
24 |
|
} |
25 |
|
} |
26 |
< |
|
26 |
> |
|
27 |
|
/** |
28 |
|
* getUncaughtExceptionHandler returns ThreadGroup unless set, |
29 |
< |
* otherwise returning vlaue of last setUncaughtExceptionHandler. |
29 |
> |
* otherwise returning value of last setUncaughtExceptionHandler. |
30 |
|
*/ |
31 |
|
public void testGetAndSetUncaughtExceptionHandler() { |
32 |
|
// these must be done all at once to avoid state |
34 |
|
Thread current = Thread.currentThread(); |
35 |
|
ThreadGroup tg = current.getThreadGroup(); |
36 |
|
MyHandler eh = new MyHandler(); |
37 |
< |
assertEquals(tg, current.getUncaughtExceptionHandler()); |
37 |
> |
assertSame(tg, current.getUncaughtExceptionHandler()); |
38 |
|
current.setUncaughtExceptionHandler(eh); |
39 |
< |
assertEquals(eh, current.getUncaughtExceptionHandler()); |
40 |
< |
current.setUncaughtExceptionHandler(null); |
41 |
< |
assertEquals(tg, current.getUncaughtExceptionHandler()); |
39 |
> |
try { |
40 |
> |
assertSame(eh, current.getUncaughtExceptionHandler()); |
41 |
> |
} finally { |
42 |
> |
current.setUncaughtExceptionHandler(null); |
43 |
> |
} |
44 |
> |
assertSame(tg, current.getUncaughtExceptionHandler()); |
45 |
|
} |
46 |
|
|
47 |
|
/** |
48 |
|
* getDefaultUncaughtExceptionHandler returns value of last |
49 |
< |
* setDefaultUncaughtExceptionHandler. |
49 |
> |
* setDefaultUncaughtExceptionHandler. |
50 |
|
*/ |
51 |
|
public void testGetAndSetDefaultUncaughtExceptionHandler() { |
52 |
|
assertEquals(null, Thread.getDefaultUncaughtExceptionHandler()); |
53 |
< |
// failure due to securityException is OK. |
53 |
> |
// failure due to SecurityException is OK. |
54 |
|
// Would be nice to explicitly test both ways, but cannot yet. |
55 |
+ |
Thread.UncaughtExceptionHandler defaultHandler |
56 |
+ |
= Thread.getDefaultUncaughtExceptionHandler(); |
57 |
+ |
MyHandler eh = new MyHandler(); |
58 |
|
try { |
51 |
– |
Thread current = Thread.currentThread(); |
52 |
– |
ThreadGroup tg = current.getThreadGroup(); |
53 |
– |
MyHandler eh = new MyHandler(); |
59 |
|
Thread.setDefaultUncaughtExceptionHandler(eh); |
60 |
< |
assertEquals(eh, Thread.getDefaultUncaughtExceptionHandler()); |
61 |
< |
Thread.setDefaultUncaughtExceptionHandler(null); |
60 |
> |
try { |
61 |
> |
assertSame(eh, Thread.getDefaultUncaughtExceptionHandler()); |
62 |
> |
} finally { |
63 |
> |
Thread.setDefaultUncaughtExceptionHandler(defaultHandler); |
64 |
> |
} |
65 |
> |
} catch (SecurityException ok) { |
66 |
> |
assertNotNull(System.getSecurityManager()); |
67 |
|
} |
68 |
< |
catch(SecurityException ok) { |
59 |
< |
} |
60 |
< |
assertEquals(null, Thread.getDefaultUncaughtExceptionHandler()); |
68 |
> |
assertSame(defaultHandler, Thread.getDefaultUncaughtExceptionHandler()); |
69 |
|
} |
70 |
|
|
63 |
– |
/** |
64 |
– |
* getUncaughtExceptionHandler returns value of last |
65 |
– |
* setDefaultUncaughtExceptionHandler if non-null |
66 |
– |
*/ |
67 |
– |
public void testGetAfterSetDefaultUncaughtExceptionHandler() { |
68 |
– |
assertEquals(null, Thread.getDefaultUncaughtExceptionHandler()); |
69 |
– |
// failure due to securityException is OK. |
70 |
– |
// Would be nice to explicitly test both ways, but cannot yet. |
71 |
– |
try { |
72 |
– |
Thread current = Thread.currentThread(); |
73 |
– |
ThreadGroup tg = current.getThreadGroup(); |
74 |
– |
MyHandler eh = new MyHandler(); |
75 |
– |
assertEquals(tg, current.getUncaughtExceptionHandler()); |
76 |
– |
Thread.setDefaultUncaughtExceptionHandler(eh); |
77 |
– |
assertEquals(eh, current.getUncaughtExceptionHandler()); |
78 |
– |
Thread.setDefaultUncaughtExceptionHandler(null); |
79 |
– |
assertEquals(tg, current.getUncaughtExceptionHandler()); |
80 |
– |
} |
81 |
– |
catch(SecurityException ok) { |
82 |
– |
} |
83 |
– |
assertEquals(null, Thread.getDefaultUncaughtExceptionHandler()); |
84 |
– |
} |
85 |
– |
|
71 |
|
// How to test actually using UEH within junit? |
72 |
|
|
73 |
|
} |
89 |
– |
|