001: /*****************************************************************************
002: * Copyright (c) PicoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: * Idea by Rachel Davies, Original code by various *
009: *****************************************************************************/package org.nanocontainer.aop.dynaop;
010:
011: import org.nanocontainer.aop.AbstractAopTestCase;
012: import org.nanocontainer.aop.AspectablePicoContainer;
013: import org.nanocontainer.aop.AspectablePicoContainerFactory;
014: import org.nanocontainer.aop.LoggingInterceptor;
015: import org.nanocontainer.aop.PointcutsFactory;
016: import org.nanocontainer.testmodel.AnotherInterface;
017: import org.nanocontainer.testmodel.Dao;
018: import org.nanocontainer.testmodel.DaoImpl;
019: import org.nanocontainer.testmodel.IdGenerator;
020: import org.nanocontainer.testmodel.IdGeneratorImpl;
021: import org.nanocontainer.testmodel.Identifiable;
022: import org.nanocontainer.testmodel.IdentifiableMixin;
023: import org.nanocontainer.testmodel.OrderEntity;
024: import org.nanocontainer.testmodel.OrderEntityImpl;
025: import org.picocontainer.MutablePicoContainer;
026: import org.picocontainer.PicoContainer;
027: import org.picocontainer.defaults.DefaultPicoContainer;
028: import org.picocontainer.testmodel.SimpleTouchable;
029:
030: /**
031: * @author Stephen Molitor
032: */
033: public class DynaopAspectablePicoContainerFactoryTestCase extends
034: AbstractAopTestCase {
035:
036: private AspectablePicoContainerFactory containerFactory = new DynaopAspectablePicoContainerFactory();
037: private AspectablePicoContainer pico = containerFactory
038: .createContainer();
039: private PointcutsFactory cuts = pico.getPointcutsFactory();
040:
041: public void testInterceptor() {
042: StringBuffer log = new StringBuffer();
043: pico.registerInterceptor(cuts.instancesOf(Dao.class), cuts
044: .allMethods(), new LoggingInterceptor(log));
045: pico.registerComponentImplementation(Dao.class, DaoImpl.class);
046: Dao dao = (Dao) pico.getComponentInstance(Dao.class);
047: verifyIntercepted(dao, log);
048: }
049:
050: public void testContainerSuppliedInterceptor() {
051: pico.registerInterceptor(cuts.instancesOf(Dao.class), cuts
052: .allMethods(), LoggingInterceptor.class);
053:
054: pico.registerComponentImplementation("log", StringBuffer.class);
055: pico.registerComponentImplementation(LoggingInterceptor.class);
056: pico.registerComponentImplementation(Dao.class, DaoImpl.class);
057:
058: Dao dao = (Dao) pico.getComponentInstance(Dao.class);
059: StringBuffer log = (StringBuffer) pico
060: .getComponentInstance("log");
061: verifyIntercepted(dao, log);
062: }
063:
064: public void testComponentInterceptor() {
065: StringBuffer log = new StringBuffer();
066:
067: pico.registerInterceptor(cuts.component("intercepted"), cuts
068: .allMethods(), new LoggingInterceptor(log));
069: pico.registerComponentImplementation("intercepted",
070: DaoImpl.class);
071: pico.registerComponentImplementation("notIntercepted",
072: DaoImpl.class);
073:
074: Dao intercepted = (Dao) pico
075: .getComponentInstance("intercepted");
076: Dao notIntercepted = (Dao) pico
077: .getComponentInstance("notIntercepted");
078:
079: verifyIntercepted(intercepted, log);
080: verifyNotIntercepted(notIntercepted, log);
081: }
082:
083: public void testContainerSuppliedComponentInterceptor() {
084: pico.registerInterceptor(cuts.component("intercepted"), cuts
085: .allMethods(), LoggingInterceptor.class);
086:
087: pico.registerComponentImplementation("log", StringBuffer.class);
088: pico.registerComponentImplementation(LoggingInterceptor.class);
089: pico.registerComponentImplementation("intercepted",
090: DaoImpl.class);
091: pico.registerComponentImplementation("notIntercepted",
092: DaoImpl.class);
093:
094: StringBuffer log = (StringBuffer) pico
095: .getComponentInstance("log");
096: Dao intercepted = (Dao) pico
097: .getComponentInstance("intercepted");
098: Dao notIntercepted = (Dao) pico
099: .getComponentInstance("notIntercepted");
100:
101: verifyIntercepted(intercepted, log);
102: verifyNotIntercepted(notIntercepted, log);
103: }
104:
105: public void testMixin() {
106: pico.registerMixin(cuts.instancesOf(Dao.class),
107: IdentifiableMixin.class);
108: pico.registerComponentImplementation(Dao.class, DaoImpl.class);
109: Dao dao = (Dao) pico.getComponentInstance(Dao.class);
110: verifyMixin(dao);
111: assertTrue(dao instanceof AnotherInterface);
112: }
113:
114: public void testContainerSuppliedMixin() {
115: pico.registerComponentImplementation(IdGenerator.class,
116: IdGeneratorImpl.class);
117: pico.registerComponentImplementation("order1",
118: OrderEntityImpl.class);
119: pico.registerComponentImplementation("order2",
120: OrderEntityImpl.class);
121: pico.registerMixin(cuts.instancesOf(OrderEntity.class),
122: new Class[] { Identifiable.class },
123: IdentifiableMixin.class);
124:
125: Identifiable i1 = (Identifiable) pico
126: .getComponentInstance("order1");
127: Identifiable i2 = (Identifiable) pico
128: .getComponentInstance("order2");
129:
130: assertEquals(new Integer(1), i1.getId());
131: assertEquals(new Integer(2), i2.getId());
132:
133: i1.setId(new Integer(3));
134: assertEquals(new Integer(3), i1.getId());
135: assertEquals(new Integer(2), i2.getId());
136: }
137:
138: public void testComponentMixin() {
139: pico.registerComponentImplementation("hasMixin", DaoImpl.class);
140: pico.registerComponentImplementation("noMixin", DaoImpl.class);
141:
142: pico.registerMixin(cuts.component("hasMixin"),
143: IdentifiableMixin.class);
144:
145: Dao hasMixin = (Dao) pico.getComponentInstance("hasMixin");
146: Dao noMixin = (Dao) pico.getComponentInstance("noMixin");
147:
148: verifyMixin(hasMixin);
149: verifyNoMixin(noMixin);
150: assertTrue(hasMixin instanceof AnotherInterface);
151: }
152:
153: public void testContainerSuppliedComponentMixin() {
154: pico.registerComponentImplementation(IdGenerator.class,
155: IdGeneratorImpl.class);
156: pico.registerMixin(cuts.componentName("hasMixin*"),
157: new Class[] { Identifiable.class },
158: IdentifiableMixin.class);
159: pico.registerComponentImplementation("hasMixin1",
160: OrderEntityImpl.class);
161: pico.registerComponentImplementation("hasMixin2",
162: OrderEntityImpl.class);
163: pico.registerComponentImplementation("noMixin",
164: OrderEntityImpl.class);
165:
166: OrderEntity hasMixin1 = (OrderEntity) pico
167: .getComponentInstance("hasMixin1");
168: OrderEntity hasMixin2 = (OrderEntity) pico
169: .getComponentInstance("hasMixin2");
170: OrderEntity noMixin = (OrderEntity) pico
171: .getComponentInstance("noMixin");
172:
173: assertTrue(hasMixin1 instanceof Identifiable);
174: assertTrue(hasMixin2 instanceof Identifiable);
175: assertFalse(noMixin instanceof Identifiable);
176:
177: assertEquals(new Integer(1), ((Identifiable) hasMixin1).getId());
178: assertEquals(new Integer(2), ((Identifiable) hasMixin2).getId());
179: }
180:
181: public void testMixinExplicitInterfaces() {
182: pico.registerMixin(cuts.instancesOf(Dao.class),
183: new Class[] { Identifiable.class },
184: IdentifiableMixin.class);
185: pico.registerComponentImplementation(Dao.class, DaoImpl.class);
186: Dao dao = (Dao) pico.getComponentInstance(Dao.class);
187: verifyMixin(dao);
188: assertFalse(dao instanceof AnotherInterface);
189: }
190:
191: public void testComponentMixinExplicitInterfaces() {
192: pico.registerComponentImplementation("hasMixin", DaoImpl.class);
193: pico.registerComponentImplementation("noMixin", DaoImpl.class);
194:
195: pico.registerMixin(cuts.component("hasMixin"),
196: new Class[] { Identifiable.class },
197: IdentifiableMixin.class);
198:
199: Dao hasMixin = (Dao) pico.getComponentInstance("hasMixin");
200: Dao noMixin = (Dao) pico.getComponentInstance("noMixin");
201:
202: verifyMixin(hasMixin);
203: verifyNoMixin(noMixin);
204:
205: assertFalse(hasMixin instanceof AnotherInterface);
206: }
207:
208: public void testCreateWithParentContainer() {
209: MutablePicoContainer parent = new DefaultPicoContainer();
210: parent.registerComponentInstance("key", "value");
211: AspectablePicoContainerFactory containerFactory = new DynaopAspectablePicoContainerFactory();
212: PicoContainer child = containerFactory.createContainer(parent);
213: assertEquals("value", child.getComponentInstance("key"));
214: }
215:
216: public void testMakeChildContainer() {
217: AspectablePicoContainerFactory aspectableContainerFactory = new DynaopAspectablePicoContainerFactory();
218: AspectablePicoContainer parent = aspectableContainerFactory
219: .createContainer();
220: parent.registerComponentImplementation("t1",
221: SimpleTouchable.class);
222: AspectablePicoContainer child = aspectableContainerFactory
223: .makeChildContainer(parent);
224: Object t1 = child.getParent().getComponentInstance("t1");
225: assertNotNull(t1);
226: assertTrue(t1 instanceof SimpleTouchable);
227: }
228:
229: public void testInterfacesWithClassPointcut() {
230: pico.registerComponentImplementation(Dao.class, DaoImpl.class);
231: pico.registerMixin(cuts.instancesOf(Dao.class),
232: IdentifiableMixin.class);
233: pico.registerInterfaces(cuts.instancesOf(Dao.class),
234: new Class[] { AnotherInterface.class });
235: Dao dao = (Dao) pico.getComponentInstance(Dao.class);
236: assertTrue(dao instanceof Identifiable);
237: assertTrue(dao instanceof AnotherInterface);
238: }
239:
240: public void testInterfacesWithClassPointcutNoAdvice() {
241: pico.registerComponentImplementation(Dao.class, DaoImpl.class);
242: pico.registerInterfaces(cuts.instancesOf(Dao.class),
243: new Class[] { AnotherInterface.class });
244: Dao dao = (Dao) pico.getComponentInstance(Dao.class);
245:
246: // dynaop doesn't add any interfaces if there's no advice applied to the
247: // object:
248: assertFalse(dao instanceof AnotherInterface);
249: }
250:
251: public void testInterfacesWithComponentPointcut() {
252: pico.registerComponentImplementation(Dao.class, DaoImpl.class);
253: pico.registerMixin(cuts.component(Dao.class),
254: IdentifiableMixin.class);
255: pico.registerInterfaces(cuts.component(Dao.class),
256: new Class[] { AnotherInterface.class });
257: Dao dao = (Dao) pico.getComponentInstance(Dao.class);
258: assertTrue(dao instanceof Identifiable);
259: assertTrue(dao instanceof AnotherInterface);
260: }
261:
262: }
|