KickJava   Java API By Example, From Geeks To Geeks.

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


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.IComponent;
23 import org.apache.tapestry.IDirect;
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.DirectService}.
37  *
38  * @author Howard M. Lewis Ship
39  * @since 4.0
40  */

41 public class TestDirectService extends ServiceTestCase
42 {
43     public void testGetLinkOnSamePage()
44     {
45         IPage page = newPage("ThePage");
46
47         MockControl cc = newControl(IDirect.class);
48         IDirect c = (IDirect) cc.getMock();
49
50         MockControl cyclec = newControl(IRequestCycle.class);
51         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
52
53         WebRequest request = newWebRequest(false, null);
54
55         cycle.getPage();
56         cyclec.setReturnValue(page);
57
58         c.getPage();
59         cc.setReturnValue(page);
60
61         c.getIdPath();
62         cc.setReturnValue("fred.barney");
63
64         Object JavaDoc[] serviceParameters = new Object JavaDoc[0];
65
66         Map JavaDoc parameters = new HashMap JavaDoc();
67
68         parameters.put(ServiceConstants.SERVICE, Tapestry.DIRECT_SERVICE);
69         parameters.put(ServiceConstants.PAGE, "ThePage");
70         parameters.put(ServiceConstants.COMPONENT, "fred.barney");
71         parameters.put(ServiceConstants.CONTAINER, null);
72         parameters.put(ServiceConstants.SESSION, null);
73         parameters.put(ServiceConstants.PARAMETER, serviceParameters);
74
75         MockControl lfc = newControl(LinkFactory.class);
76         LinkFactory lf = (LinkFactory) lfc.getMock();
77
78         ILink link = newLink();
79
80         lf.constructLink(cycle, parameters, true);
81         lfc.setReturnValue(link);
82
83         replayControls();
84
85         DirectService ds = new DirectService();
86
87         ds.setLinkFactory(lf);
88         ds.setRequest(request);
89
90         assertSame(link, ds.getLink(cycle, new DirectServiceParameter(c, serviceParameters)));
91
92         verifyControls();
93     }
94
95     public void testGetLinkOnSamePageStateful()
96     {
97         IPage page = newPage("ThePage");
98         WebRequest request = newWebRequest(false, newWebSession());
99
100         MockControl cc = newControl(IDirect.class);
101         IDirect c = (IDirect) cc.getMock();
102
103         MockControl cyclec = newControl(IRequestCycle.class);
104         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
105
106         cycle.getPage();
107         cyclec.setReturnValue(page);
108
109         c.getPage();
110         cc.setReturnValue(page);
111
112         c.getIdPath();
113         cc.setReturnValue("fred.barney");
114
115         Object JavaDoc[] serviceParameters = new Object JavaDoc[0];
116
117         Map JavaDoc parameters = new HashMap JavaDoc();
118
119         parameters.put(ServiceConstants.SERVICE, Tapestry.DIRECT_SERVICE);
120         parameters.put(ServiceConstants.PAGE, "ThePage");
121         parameters.put(ServiceConstants.COMPONENT, "fred.barney");
122         parameters.put(ServiceConstants.CONTAINER, null);
123         parameters.put(ServiceConstants.SESSION, "T");
124         parameters.put(ServiceConstants.PARAMETER, serviceParameters);
125
126         MockControl lfc = newControl(LinkFactory.class);
127         LinkFactory lf = (LinkFactory) lfc.getMock();
128
129         ILink link = newLink();
130
131         lf.constructLink(cycle, parameters, true);
132         lfc.setReturnValue(link);
133
134         replayControls();
135
136         DirectService ds = new DirectService();
137         ds.setLinkFactory(lf);
138         ds.setRequest(request);
139
140         assertSame(link, ds.getLink(cycle, new DirectServiceParameter(c, serviceParameters)));
141
142         verifyControls();
143     }
144
145     public void testGetLinkOnDifferentPage()
146     {
147         IPage page = newPage("ActivePage");
148         IPage componentPage = newPage("ComponentPage");
149         WebRequest request = newWebRequest(false, null);
150         MockControl cc = newControl(IDirect.class);
151         IDirect c = (IDirect) cc.getMock();
152
153         MockControl cyclec = newControl(IRequestCycle.class);
154         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
155
156         cycle.getPage();
157         cyclec.setReturnValue(page);
158
159         c.getPage();
160         cc.setReturnValue(componentPage);
161
162         c.getIdPath();
163         cc.setReturnValue("fred.barney");
164
165         Object JavaDoc[] serviceParameters = new Object JavaDoc[0];
166
167         Map JavaDoc parameters = new HashMap JavaDoc();
168
169         parameters.put(ServiceConstants.SERVICE, Tapestry.DIRECT_SERVICE);
170         parameters.put(ServiceConstants.PAGE, "ActivePage");
171         parameters.put(ServiceConstants.COMPONENT, "fred.barney");
172         parameters.put(ServiceConstants.CONTAINER, "ComponentPage");
173         parameters.put(ServiceConstants.SESSION, null);
174         parameters.put(ServiceConstants.PARAMETER, serviceParameters);
175
176         MockControl lfc = newControl(LinkFactory.class);
177         LinkFactory lf = (LinkFactory) lfc.getMock();
178
179         ILink link = newLink();
180
181         lf.constructLink(cycle, parameters, true);
182         lfc.setReturnValue(link);
183
184         replayControls();
185
186         DirectService ds = new DirectService();
187         ds.setLinkFactory(lf);
188         ds.setRequest(request);
189
190         assertSame(link, ds.getLink(cycle, new DirectServiceParameter(c, serviceParameters)));
191
192         verifyControls();
193     }
194
195     public void testServiceSimple() throws Exception JavaDoc
196     {
197         Object JavaDoc[] parameters = new Object JavaDoc[0];
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(ServiceConstants.SESSION);
212         cyclec.setReturnValue(null);
213
214         MockControl pagec = newControl(IPage.class);
215         IPage page = (IPage) pagec.getMock();
216
217         cycle.getPage("ActivePage");
218         cyclec.setReturnValue(page);
219
220         cycle.activate(page);
221
222         MockControl dc = newControl(IDirect.class);
223         IDirect d = (IDirect) dc.getMock();
224
225         page.getNestedComponent("fred.barney");
226         pagec.setReturnValue(d);
227
228         MockControl lfc = newControl(LinkFactory.class);
229         LinkFactory lf = (LinkFactory) lfc.getMock();
230
231         lf.extractListenerParameters(cycle);
232         lfc.setReturnValue(parameters);
233
234         cycle.setListenerParameters(parameters);
235
236         d.trigger(cycle);
237
238         ResponseRenderer rr = newResponseRenderer();
239
240         rr.renderResponse(cycle);
241
242         replayControls();
243
244         DirectService ds = new DirectService();
245         ds.setLinkFactory(lf);
246         ds.setResponseRenderer(rr);
247
248         ds.service(cycle);
249
250         verifyControls();
251     }
252
253     /**
254      * The complex case is where the component is contained on a different page than the active (at
255      * the time of render) page.
256      */

257     public void testServiceComplex() throws Exception JavaDoc
258     {
259         Object JavaDoc[] parameters = new Object JavaDoc[0];
260
261         MockControl cyclec = newControl(IRequestCycle.class);
262         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
263
264         cycle.getParameter(ServiceConstants.COMPONENT);
265         cyclec.setReturnValue("fred.barney");
266
267         cycle.getParameter(ServiceConstants.CONTAINER);
268         cyclec.setReturnValue("ComponentPage");
269
270         cycle.getParameter(ServiceConstants.PAGE);
271         cyclec.setReturnValue("ActivePage");
272
273         cycle.getParameter(ServiceConstants.SESSION);
274         cyclec.setReturnValue(null);
275
276         MockControl activePagec = newControl(IPage.class);
277         IPage activePage = (IPage) activePagec.getMock();
278
279         cycle.getPage("ActivePage");
280         cyclec.setReturnValue(activePage);
281
282         cycle.activate(activePage);
283
284         MockControl componentPagec = newControl(IPage.class);
285         IPage componentPage = (IPage) componentPagec.getMock();
286
287         cycle.getPage("ComponentPage");
288         cyclec.setReturnValue(componentPage);
289
290         MockControl dc = newControl(IDirect.class);
291         IDirect d = (IDirect) dc.getMock();
292
293         componentPage.getNestedComponent("fred.barney");
294         componentPagec.setReturnValue(d);
295
296         MockControl lfc = newControl(LinkFactory.class);
297         LinkFactory lf = (LinkFactory) lfc.getMock();
298
299         lf.extractListenerParameters(cycle);
300         lfc.setReturnValue(parameters);
301
302         cycle.setListenerParameters(parameters);
303
304         d.trigger(cycle);
305
306         ResponseRenderer rr = newResponseRenderer();
307
308         rr.renderResponse(cycle);
309
310         replayControls();
311
312         DirectService ds = new DirectService();
313         ds.setLinkFactory(lf);
314         ds.setResponseRenderer(rr);
315
316         ds.service(cycle);
317
318         verifyControls();
319     }
320
321     public void testServiceNotDirect() throws Exception JavaDoc
322     {
323         Location l = fabricateLocation(7);
324
325         MockControl cyclec = newControl(IRequestCycle.class);
326         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
327
328         cycle.getParameter(ServiceConstants.COMPONENT);
329         cyclec.setReturnValue("fred.barney");
330
331         cycle.getParameter(ServiceConstants.CONTAINER);
332         cyclec.setReturnValue(null);
333
334         cycle.getParameter(ServiceConstants.PAGE);
335         cyclec.setReturnValue("ActivePage");
336
337         cycle.getParameter(ServiceConstants.SESSION);
338         cyclec.setReturnValue(null);
339
340         MockControl pagec = newControl(IPage.class);
341         IPage page = (IPage) pagec.getMock();
342
343         cycle.getPage("ActivePage");
344         cyclec.setReturnValue(page);
345
346         cycle.activate(page);
347
348         MockControl cc = newControl(IComponent.class);
349         IComponent c = (IComponent) cc.getMock();
350
351         page.getNestedComponent("fred.barney");
352         pagec.setReturnValue(c);
353
354         c.getExtendedId();
355         cc.setReturnValue("ActivePage/fred.barney");
356
357         c.getLocation();
358         cc.setReturnValue(l);
359
360         replayControls();
361
362         DirectService ds = new DirectService();
363
364         try
365         {
366             ds.service(cycle);
367             unreachable();
368         }
369         catch (ApplicationRuntimeException ex)
370         {
371             assertEquals(
372                     "Component ActivePage/fred.barney does not implement the org.apache.tapestry.IDirect interface.",
373                     ex.getMessage());
374             assertSame(c, ex.getComponent());
375             assertSame(l, ex.getLocation());
376         }
377
378         verifyControls();
379     }
380
381     public void testSessionActiveAndRequired() throws Exception JavaDoc
382     {
383         Object JavaDoc[] parameters = new Object JavaDoc[0];
384
385         MockControl cyclec = newControl(IRequestCycle.class);
386         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
387
388         cycle.getParameter(ServiceConstants.COMPONENT);
389         cyclec.setReturnValue("fred.barney");
390
391         cycle.getParameter(ServiceConstants.CONTAINER);
392         cyclec.setReturnValue(null);
393
394         cycle.getParameter(ServiceConstants.PAGE);
395         cyclec.setReturnValue("ActivePage");
396
397         cycle.getParameter(ServiceConstants.SESSION);
398         cyclec.setReturnValue("T");
399
400         MockControl pagec = newControl(IPage.class);
401         IPage page = (IPage) pagec.getMock();
402
403         cycle.getPage("ActivePage");
404         cyclec.setReturnValue(page);
405
406         cycle.activate(page);
407
408         MockControl dc = newControl(IDirect.class);
409         IDirect d = (IDirect) dc.getMock();
410
411         page.getNestedComponent("fred.barney");
412         pagec.setReturnValue(d);
413
414         d.isStateful();
415         dc.setReturnValue(true);
416
417         WebSession session = newWebSession(false);
418         WebRequest request = newWebRequest(session);
419
420         MockControl lfc = newControl(LinkFactory.class);
421         LinkFactory lf = (LinkFactory) lfc.getMock();
422
423         lf.extractListenerParameters(cycle);
424         lfc.setReturnValue(parameters);
425
426         cycle.setListenerParameters(parameters);
427
428         d.trigger(cycle);
429
430         ResponseRenderer rr = newResponseRenderer();
431
432         rr.renderResponse(cycle);
433
434         replayControls();
435
436         DirectService ds = new DirectService();
437         ds.setLinkFactory(lf);
438         ds.setResponseRenderer(rr);
439         ds.setRequest(request);
440
441         ds.service(cycle);
442
443         verifyControls();
444     }
445
446     public void testStaleSession() throws Exception JavaDoc
447     {
448         Location l = fabricateLocation(99);
449
450         MockControl cyclec = newControl(IRequestCycle.class);
451         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
452
453         cycle.getParameter(ServiceConstants.COMPONENT);
454         cyclec.setReturnValue("fred.barney");
455
456         cycle.getParameter(ServiceConstants.CONTAINER);
457         cyclec.setReturnValue(null);
458
459         cycle.getParameter(ServiceConstants.PAGE);
460         cyclec.setReturnValue("ActivePage");
461
462         cycle.getParameter(ServiceConstants.SESSION);
463         cyclec.setReturnValue("T");
464
465         MockControl pagec = newControl(IPage.class);
466         IPage page = (IPage) pagec.getMock();
467
468         cycle.getPage("ActivePage");
469         cyclec.setReturnValue(page);
470
471         cycle.activate(page);
472
473         MockControl dc = newControl(IDirect.class);
474         IDirect d = (IDirect) dc.getMock();
475
476         page.getNestedComponent("fred.barney");
477         pagec.setReturnValue(d);
478
479         d.isStateful();
480         dc.setReturnValue(true);
481
482         WebRequest request = newWebRequest(null);
483
484         d.getExtendedId();
485         dc.setReturnValue("ActivePage/fred.barney");
486
487         page.getLocation();
488         pagec.setReturnValue(l);
489
490         page.getPageName();
491         pagec.setReturnValue("ActivePage");
492
493         replayControls();
494
495         DirectService ds = new DirectService();
496         ds.setRequest(request);
497
498         try
499         {
500             ds.service(cycle);
501             unreachable();
502         }
503         catch (StaleSessionException ex)
504         {
505             assertEquals(
506                     "Component ActivePage/fred.barney is stateful, but the HttpSession has expired (or has not yet been created).",
507                     ex.getMessage());
508             assertSame(page, ex.getComponent());
509             assertSame(l, ex.getLocation());
510             assertEquals("ActivePage", ex.getPageName());
511             assertSame(page, ex.getPage());
512         }
513
514         verifyControls();
515     }
516 }
Popular Tags