KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > TestLinkFactory


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.services.impl;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.hivemind.ErrorLog;
25 import org.apache.hivemind.impl.DefaultClassResolver;
26 import org.apache.hivemind.test.HiveMindTestCase;
27 import org.apache.tapestry.IEngine;
28 import org.apache.tapestry.IRequestCycle;
29 import org.apache.tapestry.engine.ILink;
30 import org.apache.tapestry.engine.ServiceEncoder;
31 import org.apache.tapestry.engine.ServiceEncoding;
32 import org.apache.tapestry.engine.encoders.PageServiceEncoder;
33 import org.apache.tapestry.record.PropertyPersistenceStrategy;
34 import org.apache.tapestry.record.PropertyPersistenceStrategySource;
35 import org.apache.tapestry.services.ServiceConstants;
36 import org.apache.tapestry.util.io.DataSqueezerImpl;
37 import org.apache.tapestry.web.WebRequest;
38 import org.easymock.MockControl;
39
40 /**
41  * Tests for {@link org.apache.tapestry.services.impl.LinkFactoryImpl}.
42  *
43  * @author Howard M. Lewis Ship
44  * @since 4.0
45  */

46 public class TestLinkFactory extends HiveMindTestCase
47 {
48     private ErrorLog newErrorLog()
49     {
50         return (ErrorLog) newMock(ErrorLog.class);
51     }
52
53     private WebRequest newRequest()
54     {
55         return (WebRequest) newMock(WebRequest.class);
56     }
57
58     private static class NoopEncoder implements ServiceEncoder
59     {
60         public void decode(ServiceEncoding encoding)
61         {
62             //
63
}
64
65         public void encode(ServiceEncoding encoding)
66         {
67             //
68
}
69     }
70
71     private static class MockSource implements PropertyPersistenceStrategySource
72     {
73
74         public PropertyPersistenceStrategy getStrategy(String JavaDoc name)
75         {
76             return null;
77         }
78
79         public Collection JavaDoc getAllStoredChanges(String JavaDoc pageName, IRequestCycle cycle)
80         {
81             return null;
82         }
83
84         public void discardAllStoredChanged(String JavaDoc pageName, IRequestCycle cycle)
85         {
86         }
87
88         public void addParametersForPersistentProperties(ServiceEncoding encoding,
89                 IRequestCycle cycle)
90         {
91             encoding.setParameterValue("foo", "bar");
92         }
93
94     }
95
96     private ServiceEncoderContribution newContribution(String JavaDoc id, ServiceEncoder encoder)
97     {
98         ServiceEncoderContribution result = new ServiceEncoderContribution();
99
100         result.setId(id);
101         result.setEncoder(encoder);
102
103         return result;
104     }
105
106     private IRequestCycle newCycle()
107     {
108         MockControl enginec = newControl(IEngine.class);
109         IEngine engine = (IEngine) enginec.getMock();
110
111         MockControl cyclec = newControl(IRequestCycle.class);
112         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
113
114         cycle.getEngine();
115         cyclec.setReturnValue(engine);
116
117         engine.getOutputEncoding();
118         enginec.setReturnValue("utf-8");
119
120         return cycle;
121     }
122
123     public void testNoEncoders()
124     {
125         ErrorLog log = newErrorLog();
126         WebRequest request = newRequest();
127         IRequestCycle cycle = newCycle();
128
129         replayControls();
130
131         LinkFactoryImpl lf = new LinkFactoryImpl();
132
133         lf.setContributions(Collections.EMPTY_LIST);
134         lf.setErrorLog(log);
135         lf.setContextPath("/context");
136         lf.setServletPath("/app");
137         lf.setRequest(request);
138
139         lf.initializeService();
140
141         Map JavaDoc parameters = new HashMap JavaDoc();
142         parameters.put(ServiceConstants.SERVICE, "myservice");
143
144         ILink link = lf.constructLink(cycle, parameters, false);
145
146         verifyControls();
147
148         assertEquals("/context/app?service=myservice", link.getURL());
149     }
150
151     public void testStatefulRequest()
152     {
153         ErrorLog log = newErrorLog();
154         WebRequest request = newRequest();
155         MockControl enginec = newControl(IEngine.class);
156         IEngine engine = (IEngine) enginec.getMock();
157
158         MockControl cyclec = newControl(IRequestCycle.class);
159         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
160
161         cycle.getEngine();
162         cyclec.setReturnValue(engine);
163
164         engine.getOutputEncoding();
165         enginec.setReturnValue("utf-8");
166         
167         cycle.encodeURL("/context/app?foo=bar&service=myservice");
168         cyclec.setReturnValue("{encoded}");
169
170         replayControls();
171
172         LinkFactoryImpl lf = new LinkFactoryImpl();
173
174         lf.setContributions(Collections.EMPTY_LIST);
175         lf.setErrorLog(log);
176         lf.setContextPath("/context");
177         lf.setServletPath("/app");
178         lf.setRequest(request);
179         lf.setPersistenceStrategySource(new MockSource());
180
181         lf.initializeService();
182
183         Map JavaDoc parameters = new HashMap JavaDoc();
184         parameters.put(ServiceConstants.SERVICE, "myservice");
185
186         ILink link = lf.constructLink(cycle, parameters, true);
187
188         assertEquals("{encoded}", link.getURL());
189
190         verifyControls();
191     }
192
193     public void testNoopEncoders()
194     {
195         WebRequest request = newRequest();
196         IRequestCycle cycle = newCycle();
197         ErrorLog log = newErrorLog();
198
199         replayControls();
200
201         List JavaDoc l = new ArrayList JavaDoc();
202         l.add(newContribution("fred", new NoopEncoder()));
203         l.add(newContribution("barney", new NoopEncoder()));
204
205         LinkFactoryImpl lf = new LinkFactoryImpl();
206
207         lf.setContributions(l);
208         lf.setErrorLog(log);
209         lf.setContextPath("/context");
210         lf.setServletPath("/app");
211         lf.setRequest(request);
212
213         lf.initializeService();
214
215         Map JavaDoc parameters = new HashMap JavaDoc();
216         parameters.put(ServiceConstants.SERVICE, "myservice");
217
218         ILink link = lf.constructLink(cycle, parameters, false);
219
220         verifyControls();
221
222         assertEquals("/context/app?service=myservice", link.getURL());
223     }
224
225     public void testActiveEncoder()
226     {
227         WebRequest request = newRequest();
228         IRequestCycle cycle = newCycle();
229         ErrorLog log = newErrorLog();
230
231         replayControls();
232         PageServiceEncoder e = new PageServiceEncoder();
233         e.setServiceName("page");
234         e.setExtension("html");
235
236         List JavaDoc l = Collections.singletonList(newContribution("encoder", e));
237
238         LinkFactoryImpl lf = new LinkFactoryImpl();
239
240         lf.setContributions(l);
241         lf.setErrorLog(log);
242         lf.setContextPath("/context");
243         lf.setServletPath("/app");
244         lf.setRequest(request);
245
246         lf.initializeService();
247
248         Map JavaDoc parameters = new HashMap JavaDoc();
249         parameters.put(ServiceConstants.SERVICE, "page");
250         parameters.put(ServiceConstants.PAGE, "Barney");
251
252         ILink link = lf.constructLink(cycle, parameters, false);
253
254         verifyControls();
255
256         assertEquals("/context/Barney.html", link.getURL());
257     }
258
259     public void testWithServiceParameters()
260     {
261         WebRequest request = newRequest();
262         IRequestCycle cycle = newCycle();
263         ErrorLog log = newErrorLog();
264
265         replayControls();
266
267         PageServiceEncoder e = new PageServiceEncoder();
268         e.setServiceName("external");
269         e.setExtension("ext");
270
271         List JavaDoc l = Collections.singletonList(newContribution("encoder", e));
272
273         LinkFactoryImpl lf = new LinkFactoryImpl();
274
275         lf.setContributions(l);
276         lf.setErrorLog(log);
277         lf.setContextPath("/context");
278         lf.setServletPath("/app");
279         lf.setDataSqueezer(new DataSqueezerImpl(new DefaultClassResolver()));
280         lf.setRequest(request);
281
282         lf.initializeService();
283
284         Map JavaDoc parameters = new HashMap JavaDoc();
285         parameters.put(ServiceConstants.SERVICE, "external");
286         parameters.put(ServiceConstants.PAGE, "Barney");
287         parameters.put(ServiceConstants.PARAMETER, new Object JavaDoc[]
288         { Boolean.TRUE });
289
290         ILink link = lf.constructLink(cycle, parameters, false);
291
292         verifyControls();
293
294         assertEquals("/context/Barney.ext?sp=T", link.getURL());
295     }
296 }
Popular Tags