KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > listener > TestListenerMapSource


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.listener;
16
17 import java.lang.reflect.Method JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.test.HiveMindTestCase;
21 import org.apache.tapestry.IPage;
22 import org.apache.tapestry.IRequestCycle;
23 import org.easymock.MockControl;
24
25 /**
26  * Tests for {@link org.apache.tapestry.listener.ListenerMapSourceImpl} and
27  * {@link org.apache.tapestry.listener.ListenerMethodInvokerImpl}.
28  *
29  * @author Howard M. Lewis Ship
30  * @since 4.0
31  */

32 public class TestListenerMapSource extends HiveMindTestCase
33 {
34     private IRequestCycle newCycle(Object JavaDoc[] listenerParameters)
35     {
36         MockControl control = newControl(IRequestCycle.class);
37         IRequestCycle cycle = (IRequestCycle) control.getMock();
38
39         cycle.getListenerParameters();
40         control.setReturnValue(listenerParameters);
41
42         return cycle;
43     }
44
45     private Method JavaDoc findMethod(Class JavaDoc clazz, String JavaDoc name)
46     {
47         Method JavaDoc[] methods = clazz.getMethods();
48
49         for (int i = 0; i < methods.length; i++)
50         {
51             if (methods[i].getName().equals(name))
52                 return methods[i];
53         }
54
55         throw new IllegalArgumentException JavaDoc("No method '" + name + "' in " + clazz + ".");
56     }
57
58     private void attemptReturnType(boolean expected, Class JavaDoc clazz, String JavaDoc methodName)
59     {
60         Method JavaDoc m = findMethod(clazz, methodName);
61
62         ListenerMapSourceImpl lms = new ListenerMapSourceImpl();
63
64         assertEquals(expected, lms.isAcceptibleListenerMethodReturnType(m));
65     }
66
67     public void testAcceptibleListenerMethodReturnTypes()
68     {
69         Class JavaDoc clazz = ListenerMethodHolder.class;
70
71         attemptReturnType(true, clazz, "fred");
72         attemptReturnType(true, clazz, "returnsString");
73         attemptReturnType(true, clazz, "returnsBasePage");
74         attemptReturnType(false, clazz, "returnsObject");
75         attemptReturnType(false, clazz, "returnsInt");
76     }
77
78     public void testFoundWithParameters()
79     {
80         IRequestCycle cycle = newCycle(new Object JavaDoc[]
81         { "Hello", new Integer JavaDoc(7) });
82         ListenerMethodHolder holder = newHolder();
83
84         holder.fred("Hello", 7);
85
86         replayControls();
87
88         ListenerMapSource source = new ListenerMapSourceImpl();
89
90         ListenerMap map = source.getListenerMapForObject(holder);
91
92         map.getListener("fred").actionTriggered(null, cycle);
93
94         verifyControls();
95     }
96
97     public void testFoundWithCycleAndParameters()
98     {
99         IRequestCycle cycle = newCycle(new Object JavaDoc[]
100         { new Integer JavaDoc(7) });
101         ListenerMethodHolder holder = newHolder();
102
103         holder.wilma(cycle, 7);
104
105         replayControls();
106
107         ListenerMapSource source = new ListenerMapSourceImpl();
108
109         ListenerMap map = source.getListenerMapForObject(holder);
110
111         map.getListener("wilma").actionTriggered(null, cycle);
112
113         verifyControls();
114     }
115
116     /**
117      * No exact match on parameter count, fall through to the no-arguments method implementation.
118      */

119
120     public void testNoParameterMatch()
121     {
122         IRequestCycle cycle = newCycle(new Object JavaDoc[]
123         { "Hello", new Integer JavaDoc(7) });
124         ListenerMethodHolder holder = newHolder();
125
126         holder.barney();
127
128         replayControls();
129
130         ListenerMapSource source = new ListenerMapSourceImpl();
131
132         ListenerMap map = source.getListenerMapForObject(holder);
133
134         map.getListener("barney").actionTriggered(null, cycle);
135
136         verifyControls();
137     }
138
139     public void testFallbackToJustCycle()
140     {
141         IRequestCycle cycle = newCycle(new Object JavaDoc[]
142         { "Hello", new Integer JavaDoc(7) });
143
144         ListenerMethodHolder holder = newHolder();
145
146         holder.pebbles(cycle);
147
148         replayControls();
149
150         ListenerMapSource source = new ListenerMapSourceImpl();
151
152         ListenerMap map = source.getListenerMapForObject(holder);
153
154         map.getListener("pebbles").actionTriggered(null, cycle);
155
156         verifyControls();
157     }
158
159     public void testReturnPageName()
160     {
161         IRequestCycle cycle = newCycle(null);
162         ListenerMethodHolder holder = new ListenerMethodHolder("PageName");
163
164         cycle.activate("PageName");
165
166         replayControls();
167
168         ListenerMapSource source = new ListenerMapSourceImpl();
169
170         ListenerMap map = source.getListenerMapForObject(holder);
171
172         map.getListener("returnsPageName").actionTriggered(null, cycle);
173
174         verifyControls();
175     }
176
177     public void testReturnPageInstance()
178     {
179         IPage page = (IPage) newMock(IPage.class);
180         IRequestCycle cycle = newCycle(null);
181         ListenerMethodHolder holder = new ListenerMethodHolder(page);
182
183         cycle.activate(page);
184
185         replayControls();
186
187         ListenerMapSource source = new ListenerMapSourceImpl();
188
189         ListenerMap map = source.getListenerMapForObject(holder);
190
191         map.getListener("returnsPage").actionTriggered(null, cycle);
192
193         verifyControls();
194     }
195
196     public void testNoMatch()
197     {
198         IRequestCycle cycle = newCycle(new Object JavaDoc[]
199         { "Hello", new Integer JavaDoc(7) });
200
201         replayControls();
202
203         ListenerMethodHolder holder = new ListenerMethodHolder();
204
205         ListenerMapSource source = new ListenerMapSourceImpl();
206
207         ListenerMap map = source.getListenerMapForObject(holder);
208
209         try
210         {
211             map.getListener("noMatch").actionTriggered(null, cycle);
212             unreachable();
213         }
214         catch (ApplicationRuntimeException ex)
215         {
216             assertEquals(
217                     "No listener method named 'noMatch' suitable for 2 listener parameters found in ListenerMethodHolder.",
218                     ex.getMessage());
219             assertSame(holder, ex.getComponent());
220         }
221
222         verifyControls();
223     }
224
225     public void testMismatchedTypes()
226     {
227         IRequestCycle cycle = newCycle(new Object JavaDoc[]
228         { "Hello" });
229
230         replayControls();
231
232         ListenerMethodHolder holder = new ListenerMethodHolder();
233
234         ListenerMapSource source = new ListenerMapSourceImpl();
235
236         ListenerMap map = source.getListenerMapForObject(holder);
237
238         try
239         {
240             map.getListener("wrongTypes").actionTriggered(null, cycle);
241             unreachable();
242         }
243         catch (ApplicationRuntimeException ex)
244         {
245             assertEquals(
246                     "Failure invoking listener method 'public void org.apache.tapestry.listener.ListenerMethodHolder.wrongTypes(java.util.Map)' on ListenerMethodHolder: argument type mismatch",
247                     ex.getMessage());
248             assertSame(holder, ex.getComponent());
249         }
250
251         verifyControls();
252     }
253
254     public void testInvocationTargetException()
255     {
256         IRequestCycle cycle = newCycle(new Object JavaDoc[]
257         { "Hello", new Integer JavaDoc(7) });
258
259         ListenerMethodHolder holder = new ListenerMethodHolder();
260
261         RuntimeException JavaDoc exception = new IllegalArgumentException JavaDoc("Just for kicks.");
262
263         holder.setException(exception);
264
265         replayControls();
266
267         ListenerMapSource source = new ListenerMapSourceImpl();
268
269         ListenerMap map = source.getListenerMapForObject(holder);
270
271         try
272         {
273             map.getListener("throwsException").actionTriggered(null, cycle);
274             unreachable();
275         }
276         catch (ApplicationRuntimeException ex)
277         {
278             assertEquals(
279                     "Failure invoking listener method 'public void org.apache.tapestry.listener.ListenerMethodHolder.throwsException()' on ListenerMethodHolder: Just for kicks.",
280                     ex.getMessage());
281             assertSame(holder, ex.getComponent());
282             assertSame(exception, ex.getRootCause());
283         }
284
285         verifyControls();
286     }
287
288     public void testInvocationTargetExceptionForApplicationRuntimeException()
289     {
290         IRequestCycle cycle = newCycle(new Object JavaDoc[]
291         { "Hello", new Integer JavaDoc(7) });
292
293         ListenerMethodHolder holder = new ListenerMethodHolder();
294
295         RuntimeException JavaDoc exception = new ApplicationRuntimeException("Just for kicks.");
296
297         holder.setException(exception);
298
299         replayControls();
300
301         ListenerMapSource source = new ListenerMapSourceImpl();
302
303         ListenerMap map = source.getListenerMapForObject(holder);
304
305         try
306         {
307             map.getListener("throwsException").actionTriggered(null, cycle);
308             unreachable();
309         }
310         catch (ApplicationRuntimeException ex)
311         {
312             assertSame(exception, ex);
313         }
314
315         verifyControls();
316     }
317
318     private ListenerMethodHolder newHolder()
319     {
320         return (ListenerMethodHolder) newMock(ListenerMethodHolder.class);
321     }
322 }
Popular Tags