1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.console.swingui;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.beans.PropertyChangeEvent;
30 import java.beans.PropertyChangeListener;
31 import java.io.Serializable;
32
33 import javax.swing.SwingUtilities;
34
35 import net.grinder.console.common.ErrorHandler;
36 import net.grinder.testutility.RandomStubFactory;
37
38 import org.junit.Test;
39
40
41
42
43
44
45
46 public class TestSwingDispatcherFactory {
47
48 private Runnable m_voidRunnable = new Runnable() { public void run() {} };
49
50 @Test public void testPropertyChangeListenerDispatch() throws Exception {
51 final MyPropertyChangeListener listener = new MyPropertyChangeListener();
52
53 final RandomStubFactory<ErrorHandler> errorHandlerStubFactory =
54 RandomStubFactory.create(ErrorHandler.class);
55 final SwingDispatcherFactory swingDispatcherFactory =
56 new SwingDispatcherFactoryImplementation(
57 errorHandlerStubFactory.getStub());
58
59 final PropertyChangeListener swingDispatchedListener =
60 swingDispatcherFactory.create(PropertyChangeListener.class, listener);
61
62 final PropertyChangeEvent event =
63 new PropertyChangeEvent(this, "my property", "before", "after");
64
65 swingDispatchedListener.propertyChange(event);
66
67
68
69 SwingUtilities.invokeAndWait(m_voidRunnable);
70
71 assertEquals(event, listener.getLastEvent());
72 errorHandlerStubFactory.assertNoMoreCalls();
73
74 final RuntimeException e = new RuntimeException("Problem");
75 listener.setThrowException(e);
76
77 swingDispatchedListener.propertyChange(event);
78 SwingUtilities.invokeAndWait(m_voidRunnable);
79
80 assertNull(listener.getLastEvent());
81
82 errorHandlerStubFactory.assertSuccess("handleException", e);
83 errorHandlerStubFactory.assertNoMoreCalls();
84 }
85
86 @Test public void testDelegateWithDuplicateInterfaces() throws Exception {
87 final RandomStubFactory<ErrorHandler> errorHandlerStubFactory =
88 RandomStubFactory.create(ErrorHandler.class);
89 final SwingDispatcherFactory swingDispatcherFactory =
90 new SwingDispatcherFactoryImplementation(
91 errorHandlerStubFactory.getStub());
92
93 final Object proxy =
94 swingDispatcherFactory.create(PropertyChangeListener.class,
95 new FooFoo());
96
97 assertFalse(proxy instanceof Foo);
98 assertTrue(proxy instanceof PropertyChangeListener);
99 assertFalse(proxy instanceof Bah);
100 }
101
102 private static final class MyPropertyChangeListener
103 implements PropertyChangeListener {
104
105 private PropertyChangeEvent m_propertyChangeEvent;
106 private RuntimeException m_nextException;
107
108 public void propertyChange(PropertyChangeEvent event) {
109 if (m_nextException != null) {
110 try {
111 throw m_nextException;
112 }
113 finally {
114 m_nextException = null;
115 }
116 }
117
118 m_propertyChangeEvent = event;
119 }
120
121 public PropertyChangeEvent getLastEvent() {
122 try {
123 return m_propertyChangeEvent;
124 }
125 finally {
126 m_propertyChangeEvent = null;
127 }
128 }
129
130 public void setThrowException(RuntimeException e) {
131 m_nextException = e;
132 }
133 }
134
135 public interface Foo extends PropertyChangeListener { }
136
137 public static class FooBase { }
138
139 public static class FooImpl extends FooBase implements Foo, Serializable {
140 public void propertyChange(PropertyChangeEvent e) { }
141 }
142
143 private interface Bah { }
144
145 public static abstract class Foo2 implements Foo { }
146
147 public static class FooFoo extends Foo2 implements Bah {
148 public void propertyChange(PropertyChangeEvent e) { }
149 }
150 }
151