KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > TestActionService


1 // Copyright 2004, 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.engine;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.Location;
22 import org.apache.tapestry.IAction;
23 import org.apache.tapestry.IComponent;
24 import org.apache.tapestry.IPage;
25 import org.apache.tapestry.IRequestCycle;
26 import org.apache.tapestry.StaleSessionException;
27 import org.apache.tapestry.Tapestry;
28 import org.apache.tapestry.services.LinkFactory;
29 import org.apache.tapestry.services.ResponseRenderer;
30 import org.apache.tapestry.services.ServiceConstants;
31 import org.apache.tapestry.web.WebRequest;
32 import org.apache.tapestry.web.WebSession;
33 import org.easymock.MockControl;
34
35 /**
36  * Tests for {@link org.apache.tapestry.engine.ActionService}.
37  *
38  * @author Howard M. Lewis Ship
39  * @since 4.0
40  */

41 public class TestActionService extends ServiceTestCase
42 {
43
44     public void testGetLinkSimple()
45     {
46         MockControl componentc = newControl(IComponent.class);
47         IComponent component = (IComponent) componentc.getMock();
48
49         IPage page = newPage("ActivePage");
50
51         MockControl cyclec = newControl(IRequestCycle.class);
52         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
53
54         WebRequest request = newWebRequest(false, null);
55
56         cycle.getPage();
57         cyclec.setReturnValue(page);
58
59         component.getPage();
60         componentc.setReturnValue(page);
61
62         Map JavaDoc parameters = new HashMap JavaDoc();
63
64         parameters.put(ServiceConstants.SERVICE, Tapestry.ACTION_SERVICE);
65
66         component.getIdPath();
67         componentc.setReturnValue("fred.barney");
68
69         parameters.put(ServiceConstants.COMPONENT, "fred.barney");
70         parameters.put(ServiceConstants.PAGE, "ActivePage");
71         parameters.put(ServiceConstants.CONTAINER, null);
72         parameters.put("action", "action-id");
73         parameters.put(ServiceConstants.SESSION, null);
74
75         ILink link = newLink();
76
77         MockControl lfc = newControl(LinkFactory.class);
78         LinkFactory lf = (LinkFactory) lfc.getMock();
79
80         lf.constructLink(cycle, parameters, true);
81         lfc.setReturnValue(link);
82
83         replayControls();
84
85         ActionService as = new ActionService();
86         as.setLinkFactory(lf);
87         as.setRequest(request);
88
89         ActionServiceParameter p = new ActionServiceParameter(component, "action-id");
90
91         assertSame(link, as.getLink(cycle, p));
92
93         verifyControls();
94     }
95
96     public void testGetLinkComplex()
97     {
98         MockControl componentc = newControl(IComponent.class);
99         IComponent component = (IComponent) componentc.getMock();
100         WebRequest request = newWebRequest(false, newWebSession());
101
102         IPage activePage = newPage("ActivePage");
103         IPage componentPage = newPage("ComponentPage");
104
105         MockControl cyclec = newControl(IRequestCycle.class);
106         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
107
108         cycle.getPage();
109         cyclec.setReturnValue(activePage);
110
111         component.getPage();
112         componentc.setReturnValue(componentPage);
113
114         Map JavaDoc parameters = new HashMap JavaDoc();
115
116         parameters.put(ServiceConstants.SERVICE, Tapestry.ACTION_SERVICE);
117
118         component.getIdPath();
119         componentc.setReturnValue("fred.barney");
120
121         parameters.put(ServiceConstants.COMPONENT, "fred.barney");
122         parameters.put(ServiceConstants.PAGE, "ActivePage");
123         parameters.put(ServiceConstants.CONTAINER, "ComponentPage");
124         parameters.put("action", "action-id");
125         parameters.put(ServiceConstants.SESSION, "T");
126
127         ILink link = newLink();
128
129         MockControl lfc = newControl(LinkFactory.class);
130         LinkFactory lf = (LinkFactory) lfc.getMock();
131
132         lf.constructLink(cycle, parameters, true);
133         lfc.setReturnValue(link);
134
135         replayControls();
136
137         ActionService as = new ActionService();
138         as.setLinkFactory(lf);
139         as.setRequest(request);
140
141         ActionServiceParameter p = new ActionServiceParameter(component, "action-id");
142
143         assertSame(link, as.getLink(cycle, p));
144
145         verifyControls();
146     }
147
148     public void testServiceSimple() throws Exception JavaDoc
149     {
150         MockControl cyclec = newControl(IRequestCycle.class);
151         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
152
153         cycle.getParameter(ServiceConstants.COMPONENT);
154         cyclec.setReturnValue("fred.barney");
155
156         cycle.getParameter(ServiceConstants.CONTAINER);
157         cyclec.setReturnValue(null);
158
159         cycle.getParameter(ServiceConstants.PAGE);
160         cyclec.setReturnValue("ActivePage");
161
162         cycle.getParameter("action");
163         cyclec.setReturnValue("action-id");
164
165         cycle.getParameter(ServiceConstants.SESSION);
166         cyclec.setReturnValue(null);
167
168         MockControl pagec = newControl(IPage.class);
169         IPage page = (IPage) pagec.getMock();
170
171         cycle.getPage("ActivePage");
172         cyclec.setReturnValue(page);
173
174         cycle.activate(page);
175
176         IAction action = (IAction) newMock(IAction.class);
177
178         page.getNestedComponent("fred.barney");
179         pagec.setReturnValue(action);
180
181         cycle.rewindPage("action-id", action);
182
183         ResponseRenderer rr = newResponseRenderer();
184
185         rr.renderResponse(cycle);
186
187         replayControls();
188
189         ActionService as = new ActionService();
190         as.setResponseRenderer(rr);
191
192         as.service(cycle);
193
194         verifyControls();
195     }
196
197     public void testSeviceActiveSession() throws Exception JavaDoc
198     {
199         MockControl cyclec = newControl(IRequestCycle.class);
200         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
201
202         cycle.getParameter(ServiceConstants.COMPONENT);
203         cyclec.setReturnValue("fred.barney");
204
205         cycle.getParameter(ServiceConstants.CONTAINER);
206         cyclec.setReturnValue(null);
207
208         cycle.getParameter(ServiceConstants.PAGE);
209         cyclec.setReturnValue("ActivePage");
210
211         cycle.getParameter("action");
212         cyclec.setReturnValue("action-id");
213
214         cycle.getParameter(ServiceConstants.SESSION);
215         cyclec.setReturnValue("T");
216
217         MockControl pagec = newControl(IPage.class);
218         IPage page = (IPage) pagec.getMock();
219
220         cycle.getPage("ActivePage");
221         cyclec.setReturnValue(page);
222
223         cycle.activate(page);
224
225         MockControl actionc = newControl(IAction.class);
226         IAction action = (IAction) actionc.getMock();
227
228         page.getNestedComponent("fred.barney");
229         pagec.setReturnValue(action);
230
231         action.getRequiresSession();
232         actionc.setReturnValue(true);
233
234         WebSession session = newWebSession(false);
235         WebRequest request = newWebRequest(session);
236
237         cycle.rewindPage("action-id", action);
238
239         ResponseRenderer rr = newResponseRenderer();
240
241         rr.renderResponse(cycle);
242
243         replayControls();
244
245         ActionService as = new ActionService();
246         as.setResponseRenderer(rr);
247         as.setRequest(request);
248
249         as.service(cycle);
250
251         verifyControls();
252     }
253
254     public void testServiceNotAction() throws Exception JavaDoc
255     {
256         MockControl cyclec = newControl(IRequestCycle.class);
257         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
258
259         cycle.getParameter(ServiceConstants.COMPONENT);
260         cyclec.setReturnValue("fred.barney");
261
262         cycle.getParameter(ServiceConstants.CONTAINER);
263         cyclec.setReturnValue(null);
264
265         cycle.getParameter(ServiceConstants.PAGE);
266         cyclec.setReturnValue("ActivePage");
267
268         cycle.getParameter("action");
269         cyclec.setReturnValue("action-id");
270
271         cycle.getParameter(ServiceConstants.SESSION);
272         cyclec.setReturnValue("T");
273
274         MockControl pagec = newControl(IPage.class);
275         IPage page = (IPage) pagec.getMock();
276
277         cycle.getPage("ActivePage");
278         cyclec.setReturnValue(page);
279
280         cycle.activate(page);
281
282         MockControl componentc = newControl(IComponent.class);
283         IComponent component = (IComponent) componentc.getMock();
284
285         page.getNestedComponent("fred.barney");
286         pagec.setReturnValue(component);
287
288         component.getExtendedId();
289         componentc.setReturnValue("ActivePage/fred.barney");
290
291         Location l = fabricateLocation(17);
292
293         component.getLocation();
294         componentc.setReturnValue(l);
295
296         replayControls();
297
298         ActionService as = new ActionService();
299
300         try
301         {
302             as.service(cycle);
303             unreachable();
304         }
305         catch (ApplicationRuntimeException ex)
306         {
307             assertEquals(
308                     "Component ActivePage/fred.barney does not implement the org.apache.tapestry.IAction interface.",
309                     ex.getMessage());
310             assertSame(component, ex.getComponent());
311             assertSame(l, ex.getLocation());
312         }
313
314         verifyControls();
315     }
316
317     public void testServiceStaleSession() throws Exception JavaDoc
318     {
319         MockControl cyclec = newControl(IRequestCycle.class);
320         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
321
322         cycle.getParameter(ServiceConstants.COMPONENT);
323         cyclec.setReturnValue("fred.barney");
324
325         cycle.getParameter(ServiceConstants.CONTAINER);
326         cyclec.setReturnValue(null);
327
328         cycle.getParameter(ServiceConstants.PAGE);
329         cyclec.setReturnValue("ActivePage");
330
331         cycle.getParameter("action");
332         cyclec.setReturnValue("action-id");
333
334         cycle.getParameter(ServiceConstants.SESSION);
335         cyclec.setReturnValue("T");
336
337         MockControl pagec = newControl(IPage.class);
338         IPage page = (IPage) pagec.getMock();
339
340         cycle.getPage("ActivePage");
341         cyclec.setReturnValue(page);
342
343         cycle.activate(page);
344
345         MockControl actionc = newControl(IAction.class);
346         IAction action = (IAction) actionc.getMock();
347
348         page.getNestedComponent("fred.barney");
349         pagec.setReturnValue(action);
350
351         action.getRequiresSession();
352         actionc.setReturnValue(true);
353
354         WebRequest request = newWebRequest(null);
355
356         action.getExtendedId();
357         actionc.setReturnValue("ActivePage/fred.barney");
358
359         Location l = fabricateLocation(2);
360
361         page.getLocation();
362         pagec.setReturnValue(l);
363
364         page.getPageName();
365         pagec.setReturnValue("ActivePage");
366
367         replayControls();
368
369         ActionService as = new ActionService();
370         as.setRequest(request);
371
372         try
373         {
374             as.service(cycle);
375             unreachable();
376         }
377         catch (StaleSessionException ex)
378         {
379             assertEquals(
380                     "Component ActivePage/fred.barney is stateful, but the HttpSession has expired (or has not yet been created).",
381                     ex.getMessage());
382             assertSame(l, ex.getLocation());
383             assertSame(page, ex.getPage());
384             assertEquals("ActivePage", ex.getPageName());
385         }
386
387         verifyControls();
388     }
389
390     public void testServiceComplex() throws Exception JavaDoc
391     {
392         MockControl cyclec = newControl(IRequestCycle.class);
393         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
394
395         cycle.getParameter(ServiceConstants.COMPONENT);
396         cyclec.setReturnValue("fred.barney");
397
398         cycle.getParameter(ServiceConstants.CONTAINER);
399         cyclec.setReturnValue("ComponentPage");
400
401         cycle.getParameter(ServiceConstants.PAGE);
402         cyclec.setReturnValue("ActivePage");
403
404         cycle.getParameter("action");
405         cyclec.setReturnValue("action-id");
406
407         cycle.getParameter(ServiceConstants.SESSION);
408         cyclec.setReturnValue(null);
409
410         MockControl activePagec = newControl(IPage.class);
411         IPage activePage = (IPage) activePagec.getMock();
412
413         cycle.getPage("ActivePage");
414         cyclec.setReturnValue(activePage);
415
416         cycle.activate(activePage);
417
418         MockControl componentPagec = newControl(IPage.class);
419         IPage componentPage = (IPage) componentPagec.getMock();
420
421         cycle.getPage("ComponentPage");
422         cyclec.setReturnValue(componentPage);
423
424         IAction action = (IAction) newMock(IAction.class);
425
426         componentPage.getNestedComponent("fred.barney");
427         componentPagec.setReturnValue(action);
428
429         cycle.rewindPage("action-id", action);
430
431         ResponseRenderer rr = newResponseRenderer();
432
433         rr.renderResponse(cycle);
434
435         replayControls();
436
437         ActionService as = new ActionService();
438         as.setResponseRenderer(rr);
439
440         as.service(cycle);
441
442         verifyControls();
443     }
444 }
Popular Tags