KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 public class TestExtensionLookupFactory extends HiveMindTestCase
35 {
36     private List JavaDoc createParameters(String JavaDoc extensionName)
37     {
38         return createParameters(extensionName, null);
39     }
40
41     private List JavaDoc createParameters(String JavaDoc extensionName, Object JavaDoc defaultValue)
42     {
43         ExtensionLookupParameter p = new ExtensionLookupParameter();
44
45         p.setExtensionName(extensionName);
46         p.setDefault(defaultValue);
47
48         return Collections.singletonList(p);
49     }
50
51     public void testInSpecification()
52     {
53         MockControl specControl = newControl(IApplicationSpecification.class);
54         IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock();
55
56         Runnable JavaDoc r = (Runnable JavaDoc) newMock(Runnable JavaDoc.class);
57
58         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
59         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
60                 .getMock();
61
62         // Training
63

64         fp.getParameters();
65         fpc.setReturnValue(createParameters("foo.bar"));
66
67         fp.getServiceInterface();
68         fpc.setReturnValue(Runnable JavaDoc.class);
69
70         spec.checkExtension("foo.bar");
71         specControl.setReturnValue(true);
72
73         spec.getExtension("foo.bar", Runnable JavaDoc.class);
74         specControl.setReturnValue(r);
75
76         replayControls();
77
78         ExtensionLookupFactory f = new ExtensionLookupFactory();
79         f.setSpecification(spec);
80
81         Object JavaDoc actual = f.createCoreServiceImplementation(fp);
82
83         assertSame(r, actual);
84
85         verifyControls();
86     }
87
88     public void testSyntheticDefault()
89     {
90         MockControl specControl = newControl(IApplicationSpecification.class);
91         IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock();
92
93         MockControl dibControl = newControl(DefaultImplementationBuilder.class);
94         DefaultImplementationBuilder dib = (DefaultImplementationBuilder) dibControl.getMock();
95
96         Runnable JavaDoc r = (Runnable JavaDoc) newMock(Runnable JavaDoc.class);
97
98         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
99         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
100                 .getMock();
101
102         // Training
103

104         fp.getParameters();
105         fpc.setReturnValue(createParameters("foo.bar"));
106
107         fp.getServiceInterface();
108         fpc.setReturnValue(Runnable JavaDoc.class);
109
110         spec.checkExtension("foo.bar");
111         specControl.setReturnValue(false);
112
113         dib.buildDefaultImplementation(Runnable JavaDoc.class);
114         dibControl.setReturnValue(r);
115
116         replayControls();
117
118         ExtensionLookupFactory f = new ExtensionLookupFactory();
119         f.setSpecification(spec);
120         f.setDefaultBuilder(dib);
121
122         Object JavaDoc actual = f.createCoreServiceImplementation(fp);
123
124         assertSame(r, actual);
125
126         verifyControls();
127     }
128
129     public void testConfigurationDefault()
130     {
131         MockControl specControl = newControl(IApplicationSpecification.class);
132         IApplicationSpecification spec = (IApplicationSpecification) specControl.getMock();
133
134         Runnable JavaDoc r = (Runnable JavaDoc) newMock(Runnable JavaDoc.class);
135
136         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
137         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
138                 .getMock();
139
140         // Training
141

142         fp.getParameters();
143         fpc.setReturnValue(createParameters("foo.bar", r));
144
145         fp.getServiceInterface();
146         fpc.setReturnValue(Runnable JavaDoc.class);
147
148         spec.checkExtension("foo.bar");
149         specControl.setReturnValue(false);
150
151         replayControls();
152
153         ExtensionLookupFactory f = new ExtensionLookupFactory();
154         f.setSpecification(spec);
155
156         Object JavaDoc actual = f.createCoreServiceImplementation(fp);
157
158         assertSame(r, actual);
159
160         verifyControls();
161     }
162
163     public void testFailure()
164     {
165         Location l = fabricateLocation(264);
166         ExtensionLookupParameter p = new ExtensionLookupParameter();
167
168         p.setLocation(l);
169         p.setExtensionName("gnip.gnop");
170
171         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
172         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
173                 .getMock();
174
175         fp.getParameters();
176         fpc.setReturnValue(Collections.singletonList(p));
177
178         fp.getServiceInterface();
179         fpc.setReturnValue(null);
180
181         ExtensionLookupFactory f = new ExtensionLookupFactory();
182
183         replayControls();
184
185         try
186         {
187             f.createCoreServiceImplementation(fp);
188
189             unreachable();
190         }
191         catch (ApplicationRuntimeException ex)
192         {
193             assertSame(l, ex.getLocation());
194         }
195
196         verifyControls();
197     }
198 }
Popular Tags