KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.ErrorLog;
23 import org.apache.hivemind.Location;
24 import org.apache.hivemind.test.HiveMindTestCase;
25 import org.apache.tapestry.IRequestCycle;
26 import org.apache.tapestry.engine.IEngineService;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.services.impl.ServiceMapImpl}.
31  *
32  * @author Howard Lewis Ship
33  * @since 4.0
34  */

35 public class TestServiceMap extends HiveMindTestCase
36 {
37     private IEngineService newService(String JavaDoc name)
38     {
39         MockControl control = newControl(IEngineService.class);
40         IEngineService service = (IEngineService) control.getMock();
41
42         service.getName();
43         control.setReturnValue(name);
44
45         return service;
46     }
47
48     private IEngineService newService()
49     {
50         return (IEngineService) newMock(IEngineService.class);
51     }
52
53     private IRequestCycle newCycle()
54     {
55         return (IRequestCycle) newMock(IRequestCycle.class);
56     }
57
58     private EngineServiceContribution constructService(String JavaDoc name, IEngineService service)
59     {
60         EngineServiceContribution result = new EngineServiceContribution();
61         result.setName(name);
62         result.setService(service);
63
64         return result;
65     }
66
67     /**
68      * Gets an application-defined and factory-defined service where there are no naming conflicts.
69      * Because ServiceMap now returns proxies, we have to do a little extra indirection to ensure
70      * that we get what's expected.
71      */

72     public void testGetNoConflict() throws Exception JavaDoc
73     {
74         IRequestCycle cycle1 = newCycle();
75         IRequestCycle cycle2 = newCycle();
76
77         IEngineService factory = newService("factory");
78         IEngineService application = newService("application");
79
80         EngineServiceContribution factoryc = constructService("factory", factory);
81         EngineServiceContribution applicationc = constructService("application", application);
82
83         factory.service(cycle1);
84         application.service(cycle2);
85
86         replayControls();
87
88         ServiceMapImpl m = new ServiceMapImpl();
89
90         m.setFactoryServices(Collections.singletonList(factoryc));
91         m.setApplicationServices(Collections.singletonList(applicationc));
92
93         m.initializeService();
94
95         m.getService("factory").service(cycle1);
96         m.getService("application").service(cycle2);
97
98         verifyControls();
99     }
100
101     public void testNameMismatch() throws Exception JavaDoc
102     {
103         IRequestCycle cycle = newCycle();
104         Location l = fabricateLocation(1289);
105
106         IEngineService service = newService("actual-name");
107
108         EngineServiceContribution contribution = constructService("expected-name", service);
109         contribution.setLocation(l);
110
111         replayControls();
112
113         ServiceMapImpl m = new ServiceMapImpl();
114
115         m.setFactoryServices(Collections.singletonList(contribution));
116         m.setApplicationServices(Collections.EMPTY_LIST);
117
118         m.initializeService();
119
120         IEngineService proxy = m.getService("expected-name");
121
122         try
123         {
124             proxy.service(cycle);
125             unreachable();
126         }
127         catch (ApplicationRuntimeException ex)
128         {
129             assertEquals(
130                     "Engine service EasyMock for interface org.apache.tapestry.engine.IEngineService is mapped to name 'expected-name' but indicates a name of 'actual-name'.",
131                     ex.getMessage());
132             assertSame(l, ex.getLocation());
133         }
134
135         verifyControls();
136     }
137
138     public void testGetServiceRepeated()
139     {
140         IEngineService application = newService();
141         EngineServiceContribution applicationc = constructService("application", application);
142
143         replayControls();
144
145         ServiceMapImpl m = new ServiceMapImpl();
146
147         m.setFactoryServices(Collections.EMPTY_LIST);
148         m.setApplicationServices(Collections.singletonList(applicationc));
149
150         m.initializeService();
151
152         IEngineService service = m.getService("application");
153         assertSame(service, m.getService("application"));
154
155         verifyControls();
156     }
157
158     public void testApplicationOverridesFactory() throws Exception JavaDoc
159     {
160         IRequestCycle cycle = newCycle();
161         IEngineService factory = newService();
162         IEngineService application = newService("override");
163
164         EngineServiceContribution factoryc = constructService("override", factory);
165         EngineServiceContribution applicationc = constructService("override", application);
166
167         application.service(cycle);
168
169         replayControls();
170
171         ServiceMapImpl m = new ServiceMapImpl();
172
173         m.setFactoryServices(Collections.singletonList(factoryc));
174         m.setApplicationServices(Collections.singletonList(applicationc));
175
176         m.initializeService();
177
178         m.getService("override").service(cycle);
179
180         verifyControls();
181     }
182
183     public void testUnknownService()
184     {
185         ServiceMapImpl m = new ServiceMapImpl();
186
187         m.setFactoryServices(Collections.EMPTY_LIST);
188         m.setApplicationServices(Collections.EMPTY_LIST);
189
190         m.initializeService();
191
192         try
193         {
194             m.getService("missing");
195             unreachable();
196         }
197         catch (ApplicationRuntimeException ex)
198         {
199             assertEquals(ImplMessages.noSuchService("missing"), ex.getMessage());
200         }
201
202         try
203         {
204             m.resolveEngineService("resolve-missing");
205             unreachable();
206         }
207         catch (ApplicationRuntimeException ex)
208         {
209             assertEquals(ImplMessages.noSuchService("resolve-missing"), ex.getMessage());
210
211         }
212     }
213
214     public void testDuplicateName() throws Exception JavaDoc
215     {
216         Location l = fabricateLocation(37);
217         IRequestCycle cycle = newCycle();
218
219         IEngineService first = newService("duplicate");
220         IEngineService second = newService();
221
222         EngineServiceContribution firstc = constructService("duplicate", first);
223         firstc.setLocation(l);
224
225         EngineServiceContribution secondc = constructService("duplicate", second);
226
227         List JavaDoc list = new ArrayList JavaDoc();
228         list.add(firstc);
229         list.add(secondc);
230
231         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
232
233         first.service(cycle);
234
235         log.error(ImplMessages.dupeService("duplicate", firstc), l, null);
236
237         replayControls();
238
239         ServiceMapImpl m = new ServiceMapImpl();
240
241         m.setFactoryServices(list);
242         m.setApplicationServices(Collections.EMPTY_LIST);
243         m.setErrorLog(log);
244
245         m.initializeService();
246
247         m.getService("duplicate").service(cycle);
248
249         verifyControls();
250     }
251 }
Popular Tags