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.isA;
018:
019: import java.util.Collections;
020: import java.util.List;
021:
022: import org.apache.tapestry.ioc.AnnotationProvider;
023: import org.apache.tapestry.ioc.Registry;
024: import org.apache.tapestry.ioc.RegistryBuilder;
025: import org.apache.tapestry.ioc.ServiceDecorator;
026: import org.apache.tapestry.ioc.ServiceLifecycle;
027: import org.apache.tapestry.ioc.def.ServiceDef;
028: import org.apache.tapestry.ioc.services.ClassFactory;
029: import org.apache.tapestry.ioc.test.IOCTestCase;
030: import org.testng.annotations.AfterMethod;
031: import org.testng.annotations.AfterSuite;
032: import org.testng.annotations.BeforeSuite;
033:
034: public class IOCInternalTestCase extends IOCTestCase implements
035: Registry {
036: private static Registry _registry;
037:
038: private static ClassFactory _classFactory;
039:
040: @AfterMethod
041: public final void cleanupThread() {
042: _registry.cleanupThread();
043: }
044:
045: public final ClassFactory getClassFactory() {
046: return _classFactory;
047: }
048:
049: public final <T> T getObject(Class<T> objectType,
050: AnnotationProvider annotationProvider) {
051: return _registry.getObject(objectType, annotationProvider);
052: }
053:
054: public final <T> T getService(Class<T> serviceInterface) {
055: return _registry.getService(serviceInterface);
056: }
057:
058: public final <T> T getService(String serviceId,
059: Class<T> serviceInterface) {
060: return _registry.getService(serviceId, serviceInterface);
061: }
062:
063: public final <T> T autobuild(Class<T> clazz) {
064: return _registry.autobuild(clazz);
065: }
066:
067: public final void eagerLoadServices() {
068: _registry.eagerLoadServices();
069: }
070:
071: @BeforeSuite
072: public final void setup_registry() {
073: RegistryBuilder builder = new RegistryBuilder();
074:
075: _registry = builder.build();
076:
077: _registry.eagerLoadServices();
078:
079: _classFactory = _registry.getService(ClassFactory.class);
080: }
081:
082: public final void shutdown() {
083: throw new UnsupportedOperationException(
084: "No registry shutdown until @AfterSuite.");
085: }
086:
087: @AfterSuite
088: public final void shutdown_registry() {
089: _registry.shutdown();
090:
091: _registry = null;
092: _classFactory = null;
093: }
094:
095: protected final InternalRegistry mockInternalRegistry() {
096: return newMock(InternalRegistry.class);
097: }
098:
099: protected final Module mockModule() {
100: return newMock(Module.class);
101: }
102:
103: protected final ObjectCreatorSource mockObjectCreatorSource() {
104: return newMock(ObjectCreatorSource.class);
105: }
106:
107: protected final void train_findDecoratorsForService(
108: InternalRegistry registry) {
109: List<ServiceDecorator> result = Collections.emptyList();
110:
111: expect(registry.findDecoratorsForService(isA(ServiceDef.class)))
112: .andReturn(result);
113: }
114:
115: protected final void train_findDecoratorsForService(Module module,
116: String serviceId, List<ServiceDecorator> decorators) {
117: expect(module.findDecoratorsForService(serviceId)).andReturn(
118: decorators);
119: }
120:
121: protected final void train_getDescription(
122: ObjectCreatorSource source, String description) {
123: expect(source.getDescription()).andReturn(description)
124: .atLeastOnce();
125: }
126:
127: protected final void train_getLifecycle(InternalRegistry registry,
128: String scope, ServiceLifecycle lifecycle) {
129: expect(registry.getServiceLifecycle(scope))
130: .andReturn(lifecycle);
131: }
132:
133: protected final <T> void train_getService(
134: InternalRegistry registry, String serviceId,
135: Class<T> serviceInterface, T service) {
136: expect(registry.getService(serviceId, serviceInterface))
137: .andReturn(service);
138: }
139:
140: }
|