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.test;
016:
017: import static org.easymock.EasyMock.isA;
018:
019: import java.lang.annotation.Annotation;
020: import java.lang.reflect.Method;
021: import java.net.URL;
022: import java.util.Locale;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.tapestry.ioc.AnnotationProvider;
026: import org.apache.tapestry.ioc.Configuration;
027: import org.apache.tapestry.ioc.Location;
028: import org.apache.tapestry.ioc.LogSource;
029: import org.apache.tapestry.ioc.MappedConfiguration;
030: import org.apache.tapestry.ioc.MessageFormatter;
031: import org.apache.tapestry.ioc.Messages;
032: import org.apache.tapestry.ioc.ObjectCreator;
033: import org.apache.tapestry.ioc.ObjectProvider;
034: import org.apache.tapestry.ioc.OrderedConfiguration;
035: import org.apache.tapestry.ioc.Registry;
036: import org.apache.tapestry.ioc.RegistryBuilder;
037: import org.apache.tapestry.ioc.Resource;
038: import org.apache.tapestry.ioc.ServiceBuilderResources;
039: import org.apache.tapestry.ioc.ServiceDecorator;
040: import org.apache.tapestry.ioc.ObjectLocator;
041: import org.apache.tapestry.ioc.ServiceResources;
042: import org.apache.tapestry.ioc.def.ContributionDef;
043: import org.apache.tapestry.ioc.def.DecoratorDef;
044: import org.apache.tapestry.ioc.def.ModuleDef;
045: import org.apache.tapestry.ioc.def.ServiceDef;
046: import org.apache.tapestry.ioc.services.SymbolSource;
047: import org.apache.tapestry.ioc.services.ThreadLocale;
048: import org.apache.tapestry.ioc.services.TypeCoercer;
049:
050: /** Add factory and trainer methods for the public interfaces of Tapestry IOC. */
051: public class IOCTestCase extends TestBase {
052: protected final Registry buildRegistry(Class... moduleClasses) {
053: RegistryBuilder builder = new RegistryBuilder();
054:
055: builder.add(moduleClasses);
056:
057: return builder.build();
058: }
059:
060: protected final Method findMethod(Class clazz, String methodName) {
061: for (Method method : clazz.getMethods()) {
062: if (method.getName().equals(methodName))
063: return method;
064: }
065:
066: throw new IllegalArgumentException(String.format(
067: "Class %s does not provide a method named '%s'.", clazz
068: .getName(), methodName));
069: }
070:
071: protected final Method findMethod(Object subject, String methodName) {
072: return findMethod(subject.getClass(), methodName);
073: }
074:
075: protected final Method findMethod(String methodName) {
076: return findMethod(this , methodName);
077: }
078:
079: /** Combines a series of lines by forming a string with a line separator after each line. */
080: protected final String join(String... lines) {
081: StringBuilder result = new StringBuilder();
082:
083: for (String line : lines) {
084: result.append(line);
085: result.append("\n");
086: }
087:
088: return result.toString();
089: }
090:
091: protected final AnnotationProvider mockAnnotationProvider() {
092: return newMock(AnnotationProvider.class);
093: }
094:
095: @SuppressWarnings("unchecked")
096: protected final <T> Configuration<T> mockConfiguration() {
097: return newMock(Configuration.class);
098: }
099:
100: protected final ContributionDef mockContributionDef() {
101: return newMock(ContributionDef.class);
102: }
103:
104: protected final DecoratorDef mockDecoratorDef() {
105: return newMock(DecoratorDef.class);
106: }
107:
108: protected final Location mockLocation() {
109: return newMock(Location.class);
110: }
111:
112: protected final Log mockLog() {
113: return newMock(Log.class);
114: }
115:
116: @SuppressWarnings("unchecked")
117: protected final <K, V> MappedConfiguration<K, V> mockMappedConfiguration() {
118: return newMock(MappedConfiguration.class);
119: }
120:
121: protected final MessageFormatter mockMessageFormatter() {
122: return newMock(MessageFormatter.class);
123: }
124:
125: protected final Messages mockMessages() {
126: return newMock(Messages.class);
127: }
128:
129: protected final ModuleDef mockModuleDef() {
130: return newMock(ModuleDef.class);
131: }
132:
133: protected final ObjectCreator mockObjectCreator() {
134: return newMock(ObjectCreator.class);
135: }
136:
137: protected final ObjectProvider mockObjectProvider() {
138: return newMock(ObjectProvider.class);
139: }
140:
141: @SuppressWarnings("unchecked")
142: protected final <T> OrderedConfiguration<T> mockOrderedConfiguration() {
143: return newMock(OrderedConfiguration.class);
144: }
145:
146: protected final Resource mockResource() {
147: return newMock(Resource.class);
148: }
149:
150: /** Frequently used as a placeholder for an arbitrary service (but its nice and simple). */
151: protected final Runnable mockRunnable() {
152: return newMock(Runnable.class);
153: }
154:
155: protected final ServiceBuilderResources mockServiceBuilderResources() {
156: return newMock(ServiceBuilderResources.class);
157: }
158:
159: protected final ServiceDecorator mockServiceDecorator() {
160: return newMock(ServiceDecorator.class);
161: }
162:
163: protected final ServiceDef mockServiceDef() {
164: return newMock(ServiceDef.class);
165: }
166:
167: protected final ObjectLocator mockObjectLocator() {
168: return newMock(ObjectLocator.class);
169: }
170:
171: protected final ServiceResources mockServiceResources() {
172: return newMock(ServiceResources.class);
173: }
174:
175: protected final SymbolSource mockSymbolSource() {
176: return newMock(SymbolSource.class);
177: }
178:
179: protected final ThreadLocale mockThreadLocale() {
180: return newMock(ThreadLocale.class);
181: }
182:
183: protected final TypeCoercer mockTypeCoercer() {
184: return newMock(TypeCoercer.class);
185: }
186:
187: protected final void stub_contains(Messages messages,
188: boolean contained) {
189: expect(messages.contains(isA(String.class))).andStubReturn(
190: contained);
191: }
192:
193: protected <S, T> void train_coerce(TypeCoercer coercer, S input,
194: Class<T> expectedType, T coercedValue) {
195: expect(coercer.coerce(input, expectedType)).andReturn(
196: coercedValue);
197: }
198:
199: protected final void train_contains(Messages messages, String key,
200: boolean result) {
201: expect(messages.contains(key)).andReturn(result).atLeastOnce();
202: }
203:
204: protected final void train_createInterceptor(
205: ServiceDecorator decorator, Object coreObject,
206: Object interceptor) {
207: expect(decorator.createInterceptor(coreObject)).andReturn(
208: interceptor);
209: }
210:
211: protected final void train_createObject(ObjectCreator creator,
212: Object service) {
213: expect(creator.createObject()).andReturn(service);
214: }
215:
216: protected final void train_expandSymbols(SymbolSource source,
217: String input) {
218: train_expandSymbols(source, input, input);
219:
220: }
221:
222: protected final void train_expandSymbols(SymbolSource source,
223: String input, String expanded) {
224: expect(source.expandSymbols(input)).andReturn(expanded);
225: }
226:
227: protected final void train_forFile(Resource resource,
228: String relativePath, Resource file) {
229: expect(resource.forFile(relativePath)).andReturn(file);
230: }
231:
232: protected final void train_forLocale(Resource base, Locale locale,
233: Resource resource) {
234: expect(base.forLocale(locale)).andReturn(resource);
235: }
236:
237: /** Have to put the result before the varargs. */
238: protected void train_format(MessageFormatter formatter,
239: String result, Object... arguments) {
240: expect(formatter.format(arguments)).andReturn(result);
241: }
242:
243: protected final void train_get(Messages messages, String key,
244: String message) {
245: expect(messages.get(key)).andReturn(message).atLeastOnce();
246: }
247:
248: protected final void train_getLocale(ThreadLocale threadLocale,
249: Locale locale) {
250: expect(threadLocale.getLocale()).andReturn(locale);
251: }
252:
253: protected final void train_getLog(LogSource source,
254: String serviceId, Log log) {
255: expect(source.getLog(serviceId)).andReturn(log).atLeastOnce();
256: }
257:
258: protected final void train_getMessageFormatter(Messages messages,
259: String key, MessageFormatter formatter) {
260: expect(messages.getFormatter(key)).andReturn(formatter)
261: .atLeastOnce();
262: }
263:
264: protected final void train_getPath(Resource r, String path) {
265: expect(r.getPath()).andReturn(path).atLeastOnce();
266: }
267:
268: protected final <T> void train_getService(ObjectLocator locator,
269: Class<T> serviceInterface, T service) {
270: expect(locator.getService(serviceInterface)).andReturn(service);
271: }
272:
273: protected final <T> void train_getService(ObjectLocator locator,
274: String serviceId, Class<T> serviceInterface, T service) {
275: expect(locator.getService(serviceId, serviceInterface))
276: .andReturn(service);
277: }
278:
279: protected final void train_getServiceId(ServiceDef def,
280: String serviceId) {
281: expect(def.getServiceId()).andReturn(serviceId).atLeastOnce();
282: }
283:
284: protected final void train_getServiceId(ServiceResources resources,
285: String serviceId) {
286: expect(resources.getServiceId()).andReturn(serviceId)
287: .atLeastOnce();
288:
289: }
290:
291: protected final void train_getServiceInterface(ServiceDef def,
292: Class serviceInterface) {
293: expect(def.getServiceInterface()).andReturn(serviceInterface)
294: .atLeastOnce();
295: }
296:
297: protected final void train_getServiceInterface(
298: ServiceResources resources, Class serviceInterface) {
299: expect(resources.getServiceInterface()).andReturn(
300: serviceInterface).atLeastOnce();
301: }
302:
303: protected final void train_getServiceLog(
304: ServiceResources resources, Log log) {
305: expect(resources.getServiceLog()).andReturn(log).atLeastOnce();
306:
307: }
308:
309: protected final void train_isDebugEnabled(Log log,
310: boolean debugEnabled) {
311: expect(log.isDebugEnabled()).andReturn(debugEnabled);
312: }
313:
314: protected final void train_matches(DecoratorDef decoratorDef,
315: ServiceDef serviceDef, boolean matches) {
316: expect(decoratorDef.matches(serviceDef)).andReturn(matches);
317: }
318:
319: protected final <T> void train_provide(ObjectProvider provider,
320: Class<T> objectType, AnnotationProvider annotationProvider,
321: ObjectLocator locator, T object) {
322: expect(
323: provider.provide(objectType, annotationProvider,
324: locator)).andReturn(object);
325: }
326:
327: protected final void train_toURL(Resource resource, URL url) {
328: expect(resource.toURL()).andReturn(url).atLeastOnce();
329: }
330:
331: protected final <T extends Annotation> void train_getAnnotation(
332: AnnotationProvider annotationProvider,
333: Class<T> annotationClass, T annotation) {
334: expect(annotationProvider.getAnnotation(annotationClass))
335: .andReturn(annotation);
336: }
337: }
|