01: package abbot.util;
02:
03: import java.awt.EventQueue;
04: import java.util.Properties;
05:
06: import junit.extensions.abbot.TestHelper;
07: import junit.framework.TestCase;
08:
09: public class EventDispatchExceptionHandlerTest extends TestCase {
10:
11: private Runnable empty = new Runnable() {
12: public void run() {
13: }
14: };
15:
16: private Properties props;
17:
18: /** Preserve System properties across invocations. */
19: protected void setUp() {
20: props = (Properties) System.getProperties().clone();
21: }
22:
23: /** Preserve System properties across invocations. */
24: protected void tearDown() {
25: System.setProperties(props);
26: }
27:
28: public void testInstall() {
29: try {
30: new Catcher().install();
31: } catch (RuntimeException e) {
32: assertTrue("Handler could not be installed", Catcher
33: .isInstalled());
34: throw e;
35: }
36: }
37:
38: /** Ensure we can install an instance and catch an exception. */
39: public void testCatchException() throws Throwable {
40: new Catcher().install();
41: Catcher.throwable = null;
42: EventQueue.invokeLater(new Runnable() {
43: public void run() {
44: throw new RuntimeException("Test exception for "
45: + getName());
46: }
47: });
48: EventQueue.invokeAndWait(empty);
49: assertNotNull("No exception caught", Catcher.throwable);
50: }
51:
52: /** Ensure subsequently set handlers will get called. */
53: public void testForwardException() throws Throwable {
54: // ensure the standard handler is always installed first
55: new Catcher().install();
56: Catcher.throwable = null;
57: SampleCatcher.throwable = null;
58: System.setProperty(EventDispatchExceptionHandler.PROP_NAME,
59: SampleCatcher.class.getName());
60: EventQueue.invokeLater(new Runnable() {
61: public void run() {
62: throw new RuntimeException("Test exception for "
63: + getName());
64: }
65: });
66: EventQueue.invokeAndWait(empty);
67: // Only one handler can be installed at a time, so if there was
68: // already a handler installed, this handler won't see the
69: // exception.
70: //assertNotNull("No exception caught", Catcher.throwable);
71: assertNotNull("Exception not forwarded",
72: SampleCatcher.throwable);
73: }
74:
75: public EventDispatchExceptionHandlerTest(String name) {
76: super (name);
77: }
78:
79: public static void main(String[] args) {
80: TestHelper.runTests(args,
81: EventDispatchExceptionHandlerTest.class);
82: }
83:
84: public static class Catcher extends EventDispatchExceptionHandler {
85: public static Throwable throwable = null;
86:
87: public void exceptionCaught(Throwable thr) {
88: throwable = thr;
89: }
90: }
91:
92: public static class SampleCatcher {
93: public static Throwable throwable = null;
94:
95: public void handle(Throwable thr) {
96: throwable = thr;
97: }
98: }
99: }
|