KickJava   Java API By Example, From Geeks To Geeks.

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


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.IExternalPage;
23 import org.apache.tapestry.IPage;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.Tapestry;
26 import org.apache.tapestry.services.LinkFactory;
27 import org.apache.tapestry.services.ResponseRenderer;
28 import org.apache.tapestry.services.ServiceConstants;
29 import org.easymock.MockControl;
30
31 /**
32  * Tests for {@link org.apache.tapestry.engine.ExternalService}.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public class TestExternalService extends ServiceTestCase
38 {
39     private LinkFactory newLinkFactory(IRequestCycle cycle, Object JavaDoc[] serviceParameters)
40     {
41         MockControl control = newControl(LinkFactory.class);
42         LinkFactory lf = (LinkFactory) control.getMock();
43
44         lf.extractListenerParameters(cycle);
45         control.setReturnValue(serviceParameters);
46
47         return lf;
48     }
49
50     public void testGetLink()
51     {
52         Object JavaDoc[] serviceParameters = new Object JavaDoc[0];
53
54         Map JavaDoc parameters = new HashMap JavaDoc();
55         parameters.put(ServiceConstants.SERVICE, Tapestry.EXTERNAL_SERVICE);
56         parameters.put(ServiceConstants.PAGE, "ActivePage");
57         parameters.put(ServiceConstants.PARAMETER, serviceParameters);
58
59         MockControl lfc = newControl(LinkFactory.class);
60         LinkFactory lf = (LinkFactory) lfc.getMock();
61
62         ILink link = (ILink) newMock(ILink.class);
63
64         IRequestCycle cycle = (IRequestCycle) newMock(IRequestCycle.class);
65
66         lf.constructLink(cycle, parameters, true);
67         lfc.setReturnValue(link);
68
69         replayControls();
70
71         ExternalService es = new ExternalService();
72         es.setLinkFactory(lf);
73
74         ExternalServiceParameter p = new ExternalServiceParameter("ActivePage", serviceParameters);
75
76         assertSame(link, es.getLink(cycle, p));
77
78         verifyControls();
79     }
80
81     public void testService() throws Exception JavaDoc
82     {
83         MockControl cyclec = newControl(IRequestCycle.class);
84         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
85
86         IExternalPage page = (IExternalPage) newMock(IExternalPage.class);
87
88         Object JavaDoc[] parameters = new Object JavaDoc[0];
89
90         cycle.getParameter(ServiceConstants.PAGE);
91         cyclec.setReturnValue("ActivePage");
92
93         cycle.getPage("ActivePage");
94         cyclec.setReturnValue(page);
95
96         LinkFactory lf = newLinkFactory(cycle, parameters);
97
98         cycle.setListenerParameters(parameters);
99         cycle.activate(page);
100         page.activateExternalPage(parameters, cycle);
101
102         ResponseRenderer rr = (ResponseRenderer) newMock(ResponseRenderer.class);
103
104         rr.renderResponse(cycle);
105
106         replayControls();
107
108         ExternalService es = new ExternalService();
109         es.setLinkFactory(lf);
110         es.setResponseRenderer(rr);
111
112         es.service(cycle);
113
114         verifyControls();
115     }
116
117     public void testServiceWrongType() throws Exception JavaDoc
118     {
119         MockControl cyclec = newControl(IRequestCycle.class);
120         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
121
122         MockControl pagec = newControl(IPage.class);
123         IPage page = (IPage) pagec.getMock();
124
125         cycle.getParameter(ServiceConstants.PAGE);
126         cyclec.setReturnValue("ActivePage");
127
128         cycle.getPage("ActivePage");
129         cyclec.setReturnValue(page);
130
131         page.getPageName();
132         pagec.setReturnValue("ActivePage");
133
134         Location l = fabricateLocation(17);
135
136         page.getLocation();
137         pagec.setReturnValue(l);
138
139         replayControls();
140
141         ExternalService es = new ExternalService();
142
143         try
144         {
145             es.service(cycle);
146             unreachable();
147         }
148         catch (ApplicationRuntimeException ex)
149         {
150             assertEquals(
151                     "Page ActivePage does not implement the org.apache.tapestry.IExternalPage interface.",
152                     ex.getMessage());
153             assertSame(l, ex.getLocation());
154             assertSame(page, ex.getComponent());
155         }
156
157         verifyControls();
158     }
159 }
Popular Tags