KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > AssimilatingComponentAdapterTest


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Joerg Schaibe *
9  *****************************************************************************/

10
11 package org.picocontainer.gems;
12
13 import com.thoughtworks.proxy.factory.CglibProxyFactory;
14
15 import junit.framework.TestCase;
16
17 import org.picocontainer.ComponentAdapter;
18 import org.picocontainer.MutablePicoContainer;
19 import org.picocontainer.PicoIntrospectionException;
20 import org.picocontainer.defaults.CachingComponentAdapter;
21 import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
22 import org.picocontainer.defaults.DefaultPicoContainer;
23 import org.picocontainer.defaults.InstanceComponentAdapter;
24 import org.picocontainer.tck.AbstractComponentAdapterTestCase;
25 import org.picocontainer.testmodel.CompatibleTouchable;
26 import org.picocontainer.testmodel.SimpleTouchable;
27 import org.picocontainer.testmodel.Touchable;
28
29 import java.lang.reflect.Proxy JavaDoc;
30
31
32 /**
33  * @author Jörg Schaible
34  */

35 public class AssimilatingComponentAdapterTest extends AbstractComponentAdapterTestCase {
36
37     /**
38      * Test if an instance can be assimilated.
39      */

40     public void testInstanceIsBorged() {
41         final MutablePicoContainer mpc = new DefaultPicoContainer();
42         final ComponentAdapter componentAdapter = new CachingComponentAdapter(new ConstructorInjectionComponentAdapter(
43                 CompatibleTouchable.class, CompatibleTouchable.class));
44         mpc.registerComponent(new AssimilatingComponentAdapter(Touchable.class, componentAdapter));
45         final CompatibleTouchable compatibleTouchable = (CompatibleTouchable) componentAdapter
46                 .getComponentInstance(mpc);
47         final Touchable touchable = (Touchable) mpc.getComponentInstanceOfType(Touchable.class);
48         assertFalse(compatibleTouchable.wasTouched());
49         touchable.touch();
50         assertTrue(compatibleTouchable.wasTouched());
51         assertTrue(Proxy.isProxyClass(touchable.getClass()));
52     }
53     
54     /**
55      * Test if the component key is preserved if it is not a class type.
56      */

57     public void testComponentKeyIsPreserved() {
58         final MutablePicoContainer mpc = new DefaultPicoContainer();
59         final ComponentAdapter componentAdapter = new CachingComponentAdapter(new ConstructorInjectionComponentAdapter(
60                 "Touchy", CompatibleTouchable.class));
61         mpc.registerComponent(new AssimilatingComponentAdapter(Touchable.class, componentAdapter));
62         final CompatibleTouchable compatibleTouchable = (CompatibleTouchable) componentAdapter
63                 .getComponentInstance(mpc);
64         final Touchable touchable = (Touchable) mpc.getComponentInstance("Touchy");
65         assertFalse(compatibleTouchable.wasTouched());
66         touchable.touch();
67         assertTrue(compatibleTouchable.wasTouched());
68         assertTrue(Proxy.isProxyClass(touchable.getClass()));
69     }
70
71     /**
72      * Test if proxy generation is omitted, if types are compatible.
73      */

74     public void testAvoidUnnecessaryProxy() {
75         final MutablePicoContainer mpc = new DefaultPicoContainer();
76         mpc.registerComponent(new AssimilatingComponentAdapter(TestCase.class, new InstanceComponentAdapter(TestCase.class, this)));
77         final TestCase self = (TestCase) mpc.getComponentInstanceOfType(TestCase.class);
78         assertFalse(Proxy.isProxyClass(self.getClass()));
79         assertSame(this, self);
80     }
81
82     /**
83      * Test if proxy generation is omitted, if types are compatible and that the component key is not changed.
84      */

85     public void testAvoidedProxyDoesNotChangeComponentKey() {
86         final MutablePicoContainer mpc = new DefaultPicoContainer();
87         mpc.registerComponent(new AssimilatingComponentAdapter(TestCase.class, new InstanceComponentAdapter(getClass(), this)));
88         final TestCase self = (TestCase) mpc.getComponentInstance(getClass());
89         assertNotNull(self);
90         assertSame(this, self);
91     }
92     
93     /**
94      * Test fail-fast for components without interface.
95      */

96     public void testComponentMustImplementInterface() {
97         try {
98             new AssimilatingComponentAdapter(SimpleTouchable.class, new InstanceComponentAdapter(TestCase.class, this));
99             fail("PicoIntrospectionException expected");
100         } catch (final PicoIntrospectionException e) {
101             assertTrue(e.getMessage().endsWith(SimpleTouchable.class.getName()));
102         }
103     }
104
105     // -------- TCK -----------
106

107     protected Class JavaDoc getComponentAdapterType() {
108         return AssimilatingComponentAdapter.class;
109     }
110
111     protected int getComponentAdapterNature() {
112         return super.getComponentAdapterNature() & ~(RESOLVING | VERIFYING | INSTANTIATING);
113     }
114
115     private ComponentAdapter createComponentAdapterWithTouchable() {
116         return new AssimilatingComponentAdapter(Touchable.class, new ConstructorInjectionComponentAdapter(
117                 CompatibleTouchable.class, CompatibleTouchable.class));
118     }
119
120     /**
121      * {@inheritDoc}
122      */

123     protected ComponentAdapter prepDEF_verifyWithoutDependencyWorks(MutablePicoContainer picoContainer) {
124         return createComponentAdapterWithTouchable();
125     }
126
127     /**
128      * {@inheritDoc}
129      */

130     protected ComponentAdapter prepDEF_verifyDoesNotInstantiate(MutablePicoContainer picoContainer) {
131         return createComponentAdapterWithTouchable();
132     }
133
134     /**
135      * {@inheritDoc}
136      */

137     protected ComponentAdapter prepDEF_visitable() {
138         return createComponentAdapterWithTouchable();
139     }
140
141     /**
142      * {@inheritDoc}
143      */

144     protected ComponentAdapter prepSER_isSerializable(MutablePicoContainer picoContainer) {
145         return new AssimilatingComponentAdapter(Touchable.class, new InstanceComponentAdapter(
146                 CompatibleTouchable.class, new CompatibleTouchable()), new CglibProxyFactory());
147     }
148
149     /**
150      * {@inheritDoc}
151      */

152     protected ComponentAdapter prepSER_isXStreamSerializable(MutablePicoContainer picoContainer) {
153         return createComponentAdapterWithTouchable();
154     }
155 }
156
Popular Tags