blob: 265220190abd1406c6aa9fee30a845eecd7e35e2 [file] [log] [blame]
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001/*
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
9package jsr166;
10
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010011import junit.framework.Test;
12import junit.framework.TestSuite;
Calin Juravle8f0d92b2013-08-01 17:26:00 +010013
14public class ThreadTest extends JSR166TestCase {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010015 // 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(...);
23 // }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010024
25 static class MyHandler implements Thread.UncaughtExceptionHandler {
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 */
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();
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010041 assertSame(tg, current.getUncaughtExceptionHandler());
Calin Juravle8f0d92b2013-08-01 17:26:00 +010042 current.setUncaughtExceptionHandler(eh);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010043 try {
44 assertSame(eh, current.getUncaughtExceptionHandler());
45 } finally {
46 current.setUncaughtExceptionHandler(null);
47 }
48 assertSame(tg, current.getUncaughtExceptionHandler());
Calin Juravle8f0d92b2013-08-01 17:26:00 +010049 }
50
51 /**
52 * getDefaultUncaughtExceptionHandler returns value of last
53 * setDefaultUncaughtExceptionHandler.
54 */
55 public void testGetAndSetDefaultUncaughtExceptionHandler() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010056 assertEquals(null, Thread.getDefaultUncaughtExceptionHandler());
Calin Juravle8f0d92b2013-08-01 17:26:00 +010057 // failure due to securityException is OK.
58 // Would be nice to explicitly test both ways, but cannot yet.
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010059 Thread.UncaughtExceptionHandler defaultHandler
60 = Thread.getDefaultUncaughtExceptionHandler();
61 MyHandler eh = new MyHandler();
Calin Juravle8f0d92b2013-08-01 17:26:00 +010062 try {
Calin Juravle8f0d92b2013-08-01 17:26:00 +010063 Thread.setDefaultUncaughtExceptionHandler(eh);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010064 try {
65 assertSame(eh, Thread.getDefaultUncaughtExceptionHandler());
66 } finally {
67 Thread.setDefaultUncaughtExceptionHandler(defaultHandler);
68 }
69 } catch (SecurityException ok) {
70 assertNotNull(System.getSecurityManager());
Calin Juravle8f0d92b2013-08-01 17:26:00 +010071 }
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010072 assertSame(defaultHandler, Thread.getDefaultUncaughtExceptionHandler());
Calin Juravle8f0d92b2013-08-01 17:26:00 +010073 }
74
75 // How to test actually using UEH within junit?
76
77}