KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > adapters > 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.adapters;
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.Method JavaDoc;
30 import java.lang.reflect.Proxy JavaDoc;
31
32
33 /**
34  * @author Jörg Schaible
35  */

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

41     public void testInstanceIsBorged() {
42         final MutablePicoContainer mpc = new DefaultPicoContainer();
43         final ComponentAdapter componentAdapter = new CachingComponentAdapter(new ConstructorInjectionComponentAdapter(
44                 CompatibleTouchable.class, CompatibleTouchable.class));
45         mpc.registerComponent(new AssimilatingComponentAdapter(Touchable.class, componentAdapter));
46         final CompatibleTouchable compatibleTouchable = (CompatibleTouchable)componentAdapter.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.getComponentInstance(mpc);
63         final Touchable touchable = (Touchable)mpc.getComponentInstance("Touchy");
64         assertFalse(compatibleTouchable.wasTouched());
65         touchable.touch();
66         assertTrue(compatibleTouchable.wasTouched());
67         assertTrue(Proxy.isProxyClass(touchable.getClass()));
68     }
69
70     /**
71      * Test if proxy generation is omitted, if types are compatible.
72      */

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

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

95     public void testComponentMustImplementInterface() {
96         try {
97             new AssimilatingComponentAdapter(SimpleTouchable.class, new InstanceComponentAdapter(TestCase.class, this));
98             fail("PicoIntrospectionException expected");
99         } catch (final PicoIntrospectionException e) {
100             assertTrue(e.getMessage().endsWith(SimpleTouchable.class.getName()));
101         }
102     }
103
104     /**
105      * Test fail-fast for components without matching methods.
106      * @throws NoSuchMethodException
107      */

108     public void testComponentMustHaveMathichMethods() throws NoSuchMethodException JavaDoc {
109         final Method JavaDoc touch = Touchable.class.getMethod("touch", (Class JavaDoc[])null);
110         try {
111             new AssimilatingComponentAdapter(Touchable.class, new InstanceComponentAdapter(TestCase.class, this));
112             fail("PicoIntrospectionException expected");
113         } catch (final PicoIntrospectionException e) {
114             assertTrue(e.getMessage().endsWith(touch.toString()));
115         }
116     }
117
118     // -------- TCK -----------
119

120     protected Class JavaDoc getComponentAdapterType() {
121         return AssimilatingComponentAdapter.class;
122     }
123
124     protected int getComponentAdapterNature() {
125         return super.getComponentAdapterNature() & ~(RESOLVING | VERIFYING | INSTANTIATING);
126     }
127
128     private ComponentAdapter createComponentAdapterWithTouchable() {
129         return new AssimilatingComponentAdapter(Touchable.class, new ConstructorInjectionComponentAdapter(
130                 CompatibleTouchable.class, CompatibleTouchable.class));
131     }
132
133     protected ComponentAdapter prepDEF_verifyWithoutDependencyWorks(MutablePicoContainer picoContainer) {
134         return createComponentAdapterWithTouchable();
135     }
136
137     protected ComponentAdapter prepDEF_verifyDoesNotInstantiate(MutablePicoContainer picoContainer) {
138         return createComponentAdapterWithTouchable();
139     }
140
141     protected ComponentAdapter prepDEF_visitable() {
142         return createComponentAdapterWithTouchable();
143     }
144
145     protected ComponentAdapter prepSER_isSerializable(MutablePicoContainer picoContainer) {
146         return new AssimilatingComponentAdapter(Touchable.class, new InstanceComponentAdapter(
147                 CompatibleTouchable.class, new CompatibleTouchable()), new CglibProxyFactory());
148     }
149
150     protected ComponentAdapter prepSER_isXStreamSerializable(MutablePicoContainer picoContainer) {
151         return createComponentAdapterWithTouchable();
152     }
153 }
154
Popular Tags