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 java.util.Arrays.asList;
018: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
019:
020: import java.util.List;
021:
022: import org.apache.tapestry.ioc.ObjectCreator;
023: import org.apache.tapestry.ioc.ServiceDecorator;
024: import org.testng.annotations.Test;
025:
026: public class InterceptorStackBuilderTest extends IOCInternalTestCase {
027: private static final String SERVICE_ID = "foo.bar.Baz";
028:
029: @Test
030: public void no_decorators() {
031: ObjectCreator core = mockObjectCreator();
032: Module module = mockModule();
033: Object coreObject = new Object();
034: List<ServiceDecorator> decorators = newList();
035:
036: train_createObject(core, coreObject);
037:
038: train_findDecoratorsForService(module, SERVICE_ID, decorators);
039:
040: replay();
041:
042: ObjectCreator isb = new InterceptorStackBuilder(module,
043: SERVICE_ID, core);
044:
045: Object intercepted = isb.createObject();
046:
047: assertSame(intercepted, coreObject);
048:
049: verify();
050: }
051:
052: @Test
053: public void decorator_returns_null_interceptor() {
054: ObjectCreator core = mockObjectCreator();
055: Module module = mockModule();
056: Object coreObject = new Object();
057: ServiceDecorator decorator = mockServiceDecorator();
058:
059: List<ServiceDecorator> decorators = asList(decorator);
060:
061: train_createObject(core, coreObject);
062:
063: train_findDecoratorsForService(module, SERVICE_ID, decorators);
064:
065: train_createInterceptor(decorator, coreObject, null);
066:
067: replay();
068:
069: ObjectCreator isb = new InterceptorStackBuilder(module,
070: SERVICE_ID, core);
071:
072: Object intercepted = isb.createObject();
073:
074: assertSame(intercepted, coreObject);
075:
076: verify();
077: }
078:
079: @Test
080: public void decorator_orderering() {
081: ObjectCreator core = mockObjectCreator();
082: Module module = mockModule();
083: Object coreObject = new Object();
084: Object interceptor1 = new Object();
085: Object interceptor2 = new Object();
086: ServiceDecorator decorator1 = mockServiceDecorator();
087: ServiceDecorator decorator2 = mockServiceDecorator();
088:
089: List<ServiceDecorator> decorators = asList(decorator1,
090: decorator2);
091:
092: train_createObject(core, coreObject);
093:
094: train_findDecoratorsForService(module, SERVICE_ID, decorators);
095:
096: // Notice: reverse order!
097:
098: train_createInterceptor(decorator2, coreObject, interceptor2);
099: train_createInterceptor(decorator1, interceptor2, interceptor1);
100:
101: replay();
102:
103: ObjectCreator isb = new InterceptorStackBuilder(module,
104: SERVICE_ID, core);
105:
106: Object intercepted = isb.createObject();
107:
108: assertSame(intercepted, interceptor1);
109:
110: verify();
111: }
112: }
|