Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 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 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 11 | import junit.framework.Test; |
| 12 | import junit.framework.TestSuite; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 13 | |
| 14 | public class ThreadTest extends JSR166TestCase { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 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() { |
Przemyslaw Szczepaniak | b8b7511 | 2016-03-11 15:59:10 +0000 | [diff] [blame] | 22 | // return new TestSuite(ThreadTest.class); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 23 | // } |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 24 | |
| 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 Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 41 | assertSame(tg, current.getUncaughtExceptionHandler()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 42 | current.setUncaughtExceptionHandler(eh); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 43 | try { |
| 44 | assertSame(eh, current.getUncaughtExceptionHandler()); |
| 45 | } finally { |
| 46 | current.setUncaughtExceptionHandler(null); |
| 47 | } |
| 48 | assertSame(tg, current.getUncaughtExceptionHandler()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * getDefaultUncaughtExceptionHandler returns value of last |
| 53 | * setDefaultUncaughtExceptionHandler. |
| 54 | */ |
| 55 | public void testGetAndSetDefaultUncaughtExceptionHandler() { |
Narayan Kamath | 05fd64a | 2015-04-30 10:36:15 +0100 | [diff] [blame] | 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()); |
Przemyslaw Szczepaniak | b8b7511 | 2016-03-11 15:59:10 +0000 | [diff] [blame] | 60 | // failure due to SecurityException is OK. |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 61 | // Would be nice to explicitly test both ways, but cannot yet. |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 62 | Thread.UncaughtExceptionHandler defaultHandler |
| 63 | = Thread.getDefaultUncaughtExceptionHandler(); |
| 64 | MyHandler eh = new MyHandler(); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 65 | try { |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 66 | Thread.setDefaultUncaughtExceptionHandler(eh); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 67 | try { |
| 68 | assertSame(eh, Thread.getDefaultUncaughtExceptionHandler()); |
| 69 | } finally { |
| 70 | Thread.setDefaultUncaughtExceptionHandler(defaultHandler); |
| 71 | } |
| 72 | } catch (SecurityException ok) { |
| 73 | assertNotNull(System.getSecurityManager()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 74 | } |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 75 | assertSame(defaultHandler, Thread.getDefaultUncaughtExceptionHandler()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // How to test actually using UEH within junit? |
| 79 | |
| 80 | } |