001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal;
016:
017: import static org.easymock.EasyMock.contains;
018: import static org.easymock.EasyMock.isA;
019:
020: import java.util.Arrays;
021: import java.util.Collection;
022: import java.util.Set;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.tapestry.ioc.Registry;
026: import org.apache.tapestry.ioc.RegistryBuilder;
027: import org.apache.tapestry.ioc.def.DecoratorDef;
028: import org.apache.tapestry.ioc.def.ModuleDef;
029: import org.apache.tapestry.ioc.def.ServiceDef;
030: import org.apache.tapestry.ioc.internal.services.ClassFactoryImpl;
031: import org.apache.tapestry.ioc.services.ClassFactory;
032: import org.apache.tapestry.ioc.services.RegistryShutdownListener;
033: import org.testng.annotations.Test;
034:
035: public class ModuleImplTest extends IOCInternalTestCase {
036: @Test
037: public void get_service_by_id_exists() {
038: InternalRegistry registry = mockInternalRegistry();
039: Log log = mockLog();
040: ClassFactory factory = new ClassFactoryImpl();
041:
042: ModuleDef moduleDef = new DefaultModuleDefImpl(
043: ModuleImplTestModule.class, log, getClassFactory());
044:
045: Module module = new ModuleImpl(registry, moduleDef, null, log);
046:
047: expect(registry.logForService("Upcase")).andReturn(log);
048:
049: train_isDebugEnabled(log, true);
050: log.debug("Creating service 'Upcase'.");
051:
052: train_getLifecycle(registry, "singleton",
053: new SingletonServiceLifecycle());
054:
055: train_newClass(registry, factory, UpcaseService.class);
056:
057: registry
058: .addRegistryShutdownListener(isA(RegistryShutdownListener.class));
059:
060: train_isDebugEnabled(log, false);
061:
062: train_findDecoratorsForService(registry);
063:
064: replay();
065:
066: UpcaseService service = module.getService("Upcase",
067: UpcaseService.class);
068:
069: assertEquals(service.upcase("hello"), "HELLO");
070:
071: verify();
072: }
073:
074: protected final void train_newClass(InternalRegistry registry,
075: ClassFactory factory, Class serviceInterface) {
076: expect(registry.newClass(serviceInterface)).andReturn(
077: factory.newClass(serviceInterface));
078: }
079:
080: @Test
081: public void find_service_ids_for_interface() {
082: InternalRegistry registry = mockInternalRegistry();
083: Log log = mockLog();
084:
085: ModuleDef moduleDef = new DefaultModuleDefImpl(
086: ModuleImplTestModule.class, log, null);
087:
088: Module module = new ModuleImpl(registry, moduleDef, null, log);
089:
090: replay();
091:
092: Collection<String> ids = module
093: .findServiceIdsForInterface(FieService.class);
094:
095: assertEquals(ids.size(), 2);
096: assertTrue(ids.contains("Fie"));
097: assertTrue(ids.contains("OtherFie"));
098:
099: verify();
100: }
101:
102: @SuppressWarnings("unchecked")
103: @Test
104: public void find_decorator_defs_for_service() {
105: InternalRegistry registry = mockInternalRegistry();
106: ServiceDef serviceDef = mockServiceDef();
107: DecoratorDef def1 = mockDecoratorDef();
108: DecoratorDef def2 = mockDecoratorDef();
109: Set<DecoratorDef> rawDefs = newMock(Set.class);
110: Log log = mockLog();
111:
112: ModuleDef moduleDef = mockModuleDef();
113:
114: expect(moduleDef.getDecoratorDefs()).andReturn(rawDefs);
115:
116: expect(rawDefs.iterator()).andReturn(
117: Arrays.asList(def1, def2).iterator());
118:
119: train_matches(def1, serviceDef, false);
120: train_matches(def2, serviceDef, true);
121:
122: replay();
123:
124: Module module = new ModuleImpl(registry, moduleDef, null, log);
125:
126: Set<DecoratorDef> defs = module
127: .findMatchingDecoratorDefs(serviceDef);
128:
129: assertEquals(defs.size(), 1);
130: assertTrue(defs.contains(def2));
131:
132: verify();
133: }
134:
135: @Test
136: public void no_public_constructor_on_module_builder_class() {
137: InternalRegistry registry = mockInternalRegistry();
138: Log log = mockLog();
139: ModuleDef def = new DefaultModuleDefImpl(
140: PrivateConstructorModule.class, log, null);
141:
142: replay();
143:
144: Module module = new ModuleImpl(registry, def, null, log);
145:
146: try {
147: module.getModuleBuilder();
148: unreachable();
149: } catch (RuntimeException ex) {
150: assertEquals(
151: ex.getMessage(),
152: "Module builder class org.apache.tapestry.ioc.internal.PrivateConstructorModule "
153: + "does not contain any public constructors.");
154: }
155:
156: verify();
157:
158: }
159:
160: @Test
161: public void too_many_public_constructors_on_module_builder_class() {
162: InternalRegistry registry = mockInternalRegistry();
163: Log log = mockLog();
164: ModuleDef def = new DefaultModuleDefImpl(
165: ExtraPublicConstructorsModule.class, log, null);
166: ClassFactory factory = newMock(ClassFactory.class);
167: Module module = new ModuleImpl(registry, def, null, log);
168:
169: log.warn(contains("contains more than one public constructor"));
170:
171: train_expandSymbols(registry, "ClassFactory", "ClassFactory");
172:
173: train_getService(registry, "ClassFactory", ClassFactory.class,
174: factory);
175:
176: replay();
177:
178: assertTrue(module.getModuleBuilder() instanceof ExtraPublicConstructorsModule);
179:
180: verify();
181: }
182:
183: protected void train_expandSymbols(InternalRegistry registry,
184: String input, String expanded) {
185: expect(registry.expandSymbols(input)).andReturn(expanded);
186: }
187:
188: private Registry buildRegistry() {
189: RegistryBuilder builder = new RegistryBuilder();
190: builder.add(ModuleImplTestModule.class);
191:
192: return builder.build();
193: }
194:
195: /** The following tests work better as integration tests. */
196:
197: @Test
198: public void integration_tests() {
199: Registry registry = buildRegistry();
200:
201: UpcaseService us = registry.getService(UpcaseService.class);
202:
203: assertEquals(us.upcase("hello"), "HELLO");
204: assertEquals(us.toString(),
205: "<Proxy for Upcase(org.apache.tapestry.ioc.internal.UpcaseService)>");
206:
207: ToStringService ts = registry.getService(ToStringService.class);
208:
209: assertEquals(ts.toString(), "<ToStringService: ToString>");
210: }
211:
212: @Test
213: public void recursive_singleton_integration_test() {
214: Registry registry = buildRegistry();
215:
216: FoeService foe = registry.getService("RecursiveFoe",
217: FoeService.class);
218:
219: try {
220: foe.foe();
221: unreachable();
222: } catch (RuntimeException ex) {
223: // The details are checked elsewhere.
224: }
225: }
226:
227: }
|