1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.gui;
39
40 import java.awt.event.ActionEvent;
41 import java.lang.reflect.InvocationTargetException;
42 import java.util.ArrayList;
43 import java.util.List;
44 import junit.framework.TestCase;
45
46 /***
47 * Tests for ReflectedAction
48 *
49 * @version $Revision: 1.10 $
50 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51 */
52 public class ReflectedActionTest extends TestCase {
53 private List resultList_ = new ArrayList();
54
55 /***
56 * Create a test
57 * @param name The name of the test
58 */
59 public ReflectedActionTest( final String name ) {
60 super(name);
61 }
62
63
64 /***
65 * Create an instance with a null object
66 */
67 public void testConstructor_NullObject() {
68
69 try {
70 new ReflectedAction(null, "foo");
71 fail("Expected exception for null object");
72 }
73 catch( final NullPointerException e ) {
74
75 }
76 }
77
78
79 /***
80 * Create an instance with a null method name
81 */
82 public void testConstructor_NullMethodName() {
83
84 try {
85 new ReflectedAction(this, null);
86 fail("Expected exception for null method name");
87 }
88 catch( final NullPointerException e ) {
89
90 }
91 }
92
93
94 /***
95 * Create an instance with a bad method name
96 */
97 public void testConstructor_BadMethodName() {
98
99 try {
100 new ReflectedAction(this, "foo");
101 fail("Expected exception for method name that doesn't exist [foo]");
102 }
103 catch( final IllegalArgumentException e ) {
104
105 }
106
107 try {
108 new ReflectedAction(this, "");
109 fail("Expected exception for empty method name");
110 }
111 catch( final IllegalArgumentException e ) {
112
113 }
114
115 try {
116 new ReflectedAction(this, "methodWithTwoParameters");
117 fail("Expected exception for empty method name");
118 }
119 catch( final IllegalArgumentException e ) {
120
121 }
122 }
123
124
125 /***
126 * Create an instance with legal values
127 */
128 public void testConstructor() {
129 new ReflectedAction(this, "methodWithNoParameters");
130 new ReflectedAction(this, "methodWithActionEventParameter");
131 }
132
133
134 /***
135 * Create the actionPerformed() method
136 */
137 public void testActionPerformed() {
138 resultList_.clear();
139
140 ReflectedAction action;
141 final ActionEvent actionEvent = new ActionEvent(this, 0, "");
142
143 action = new ReflectedAction(this, "methodWithNoParameters");
144 action.actionPerformed( actionEvent );
145 assertEquals( "resultList.size()", 1, resultList_.size());
146 assertEquals( "resultList[0]", "methodWithNoParameters", resultList_.get(0));
147
148 action = new ReflectedAction(this, "methodWithActionEventParameter");
149 action.actionPerformed( actionEvent );
150 assertEquals( "resultList.size()", 2, resultList_.size());
151 assertEquals( "resultList[1]", actionEvent, resultList_.get(1));
152 }
153
154
155 /***
156 * Test the actionPerformed() method when it throws an exception
157 */
158 public void testActionPerformed_ExceptionThrown() {
159 /*** Fake action for testing */
160 class ReflectedActionWithErrorHandling extends ReflectedAction {
161 private static final long serialVersionUID = 1L;
162 private Exception exception = null;
163 /***
164 * @param object Object to invoke
165 * @param methodName Method to invoke
166 */
167 public ReflectedActionWithErrorHandling(final Object object, final String methodName ) {
168 super(object, methodName);
169 }
170 /*** @param e Exception that was thrown */
171 protected void exceptionThrown( final Exception e ) {
172 exception = e;
173 }
174 /*** @return The exception that was thrown */
175 public Exception getException() {
176 return exception;
177 }
178 }
179
180 final ReflectedActionWithErrorHandling action
181 = new ReflectedActionWithErrorHandling(this, "methodThatThrowsException");
182 final ActionEvent actionEvent = new ActionEvent(this, 0, "");
183
184 action.actionPerformed(actionEvent);
185 assertNotNull(action.getException());
186 assertTrue("Outer exception is InvocationTargetException",
187 action.getException() instanceof InvocationTargetException );
188 assertTrue("Inner exception is IllegalStateException",
189 ((InvocationTargetException)action.getException()).getTargetException() instanceof IllegalStateException );
190 }
191
192
193 /***
194 * Checkstyle warns us about unused private methods. There are a number
195 * of private methods in this class that are required for the tests but
196 * are never called directly so checkstyle complains about them. This
197 * method (which should never be executed) calls all these private methods
198 * just to fool checkstyle.
199 * @throws Exception If something goes wrong.
200 */
201 void fakeOutCheckstyle() throws Exception {
202 methodWithNoParameters();
203 methodWithActionEventParameter(null);
204 methodWithTwoParameters(null, null);
205 methodThatThrowsException();
206 }
207
208 private void methodWithNoParameters() {
209 resultList_.add("methodWithNoParameters");
210 }
211
212
213 private void methodWithActionEventParameter( final ActionEvent event ) {
214 resultList_.add(event);
215 }
216
217
218 private void methodWithTwoParameters( final ActionEvent event, final String s ) {
219 }
220
221
222 private void methodThatThrowsException() {
223 throw new IllegalStateException();
224 }
225 }