1 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 ; 30 import java.lang.reflect.Proxy ; 31 32 33 36 public class AssimilatingComponentAdapterTest extends AbstractComponentAdapterTestCase { 37 38 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 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 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 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 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 108 public void testComponentMustHaveMathichMethods() throws NoSuchMethodException { 109 final Method touch = Touchable.class.getMethod("touch", (Class [])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 120 protected Class 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 |