blob: e69b4220308f628979d3f4fe8dd1585204da3064 [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() {
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000022 // return new TestSuite(ThreadTest.class);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010023 // }
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 Kamath05fd64a2015-04-30 10:36:15 +010056 // 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());
Przemyslaw Szczepaniakb8b75112016-03-11 15:59:10 +000060 // failure due to SecurityException is OK.
Calin Juravle8f0d92b2013-08-01 17:26:00 +010061 // Would be nice to explicitly test both ways, but cannot yet.
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010062 Thread.UncaughtExceptionHandler defaultHandler
63 = Thread.getDefaultUncaughtExceptionHandler();
64 MyHandler eh = new MyHandler();
Calin Juravle8f0d92b2013-08-01 17:26:00 +010065 try {
Calin Juravle8f0d92b2013-08-01 17:26:00 +010066 Thread.setDefaultUncaughtExceptionHandler(eh);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010067 try {
68 assertSame(eh, Thread.getDefaultUncaughtExceptionHandler());
69 } finally {
70 Thread.setDefaultUncaughtExceptionHandler(defaultHandler);
71 }
72 } catch (SecurityException ok) {
73 assertNotNull(System.getSecurityManager());
Calin Juravle8f0d92b2013-08-01 17:26:00 +010074 }
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010075 assertSame(defaultHandler, Thread.getDefaultUncaughtExceptionHandler());
Calin Juravle8f0d92b2013-08-01 17:26:00 +010076 }
77
78 // How to test actually using UEH within junit?
79
80}