View Javadoc

1   // Copyright (C) 2005 - 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Unit tests for {@link SwingDispatcherFactoryImplementation}.
43   *
44   * @author Philip Aston
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      // Wait for a dummy event to be processed by the swing event
68      // queue.
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