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 java.lang.reflect.Method;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.tapestry.ioc.ModuleBuilderSource;
021: import org.apache.tapestry.ioc.ServiceResources;
022: import org.apache.tapestry.ioc.internal.util.InternalUtils;
023: import org.testng.annotations.Test;
024:
025: public class ServiceDecoratorImplTest extends IOCInternalTestCase {
026: private static final String SERVICE_ID = "ioc.Fie";
027:
028: private ModuleBuilderSource newSource(final Object builder) {
029: return new ModuleBuilderSource() {
030: public Object getModuleBuilder() {
031: return builder;
032: }
033: };
034: }
035:
036: /**
037: * Also, test logging of decorator method invocation.
038: *
039: * @throws Exception
040: */
041: @Test
042: public void decorator_returns_interceptor() throws Exception {
043: ServiceDecoratorFixture fixture = new ServiceDecoratorFixture();
044: Method m = findMethod(fixture, "decoratorReturnsInterceptor");
045:
046: ServiceResources resources = mockServiceResources();
047: Log log = mockLog();
048: fixture._expectedDelegate = mockFieService();
049: fixture._interceptorToReturn = mockFieService();
050: ModuleBuilderSource source = newSource(fixture);
051:
052: trainForConstructor(resources, log);
053:
054: train_isDebugEnabled(log, true);
055:
056: log.debug(IOCMessages.invokingMethod(InternalUtils.asString(m,
057: getClassFactory())));
058:
059: replay();
060:
061: // Check that the delegate gets passed in; check that the return value of the
062: // decorator method is the return value of the ServiceDecorator.
063:
064: ServiceDecoratorImpl decorator = new ServiceDecoratorImpl(m,
065: source, resources, getClassFactory());
066:
067: Object interceptor = decorator
068: .createInterceptor(fixture._expectedDelegate);
069:
070: assertSame(interceptor, fixture._interceptorToReturn);
071:
072: verify();
073: }
074:
075: @Test
076: public void decorator_returns_null_interceptor() throws Exception {
077: ServiceDecoratorFixture fixture = new ServiceDecoratorFixture();
078: ModuleBuilderSource source = newSource(fixture);
079: ServiceResources resources = mockServiceResources();
080: Log log = mockLog();
081: Object delegate = mockFieService();
082:
083: trainForConstructor(resources, log);
084:
085: train_isDebugEnabled(log, false);
086:
087: replay();
088:
089: Method m = findMethod(fixture, "decorateReturnNull");
090:
091: ServiceDecoratorImpl decorator = new ServiceDecoratorImpl(m,
092: source, resources, getClassFactory());
093:
094: Object interceptor = decorator.createInterceptor(delegate);
095:
096: assertNull(interceptor);
097:
098: verify();
099: }
100:
101: @Test
102: public void decorator_returns_incorrect_type() throws Exception {
103: ServiceDecoratorFixture fixture = new ServiceDecoratorFixture();
104: ModuleBuilderSource source = newSource(fixture);
105: ServiceResources resources = mockServiceResources();
106: Log log = mockLog();
107: fixture._expectedDelegate = mockFieService();
108: fixture._interceptorToReturn = newMock(FoeService.class);
109:
110: Method m = findMethod(fixture, "decoratorUntyped");
111:
112: trainForConstructor(resources, log);
113:
114: train_isDebugEnabled(log, false);
115:
116: log.warn(IOCMessages.decoratorReturnedWrongType(m, SERVICE_ID,
117: fixture._interceptorToReturn, FieService.class), null);
118:
119: replay();
120:
121: ServiceDecoratorImpl decorator = new ServiceDecoratorImpl(m,
122: source, resources, getClassFactory());
123:
124: Object interceptor = decorator
125: .createInterceptor(fixture._expectedDelegate);
126:
127: assertNull(interceptor);
128:
129: verify();
130: }
131:
132: @Test
133: public void decorator_method_throws_exception() throws Exception {
134: ServiceDecoratorFixture fixture = new ServiceDecoratorFixture();
135: ModuleBuilderSource source = newSource(fixture);
136: ServiceResources resources = mockServiceResources();
137: Log log = mockLog();
138: Object delegate = mockFieService();
139: fixture._exception = new RuntimeException("Ouch!");
140:
141: trainForConstructor(resources, log);
142:
143: train_isDebugEnabled(log, false);
144:
145: replay();
146:
147: Method m = findMethod(fixture, "decoratorThrowsException");
148:
149: ServiceDecoratorImpl decorator = new ServiceDecoratorImpl(m,
150: source, resources, getClassFactory());
151:
152: try {
153: decorator.createInterceptor(delegate);
154: unreachable();
155: } catch (RuntimeException ex) {
156: assertEquals(ex.getMessage(), IOCMessages
157: .decoratorMethodError(m, SERVICE_ID, ex.getCause()));
158:
159: Throwable cause = ex.getCause();
160:
161: assertSame(cause, fixture._exception);
162: }
163:
164: verify();
165: }
166:
167: private FieService mockFieService() {
168: return newMock(FieService.class);
169: }
170:
171: private void trainForConstructor(ServiceResources resources, Log log) {
172: train_getServiceId(resources, SERVICE_ID);
173:
174: train_getServiceInterface(resources, FieService.class);
175:
176: train_getServiceLog(resources, log);
177: }
178:
179: }
|