1 10 package org.picocontainer.defaults; 11 12 import junit.framework.TestCase; 13 import org.picocontainer.Parameter; 14 import org.picocontainer.testmodel.DecoratedTouchable; 15 import org.picocontainer.testmodel.SimpleTouchable; 16 import org.picocontainer.testmodel.Touchable; 17 import org.picocontainer.testmodel.DependsOnTouchable; 18 19 import java.util.Collections ; 20 21 26 public class ComponentKeysTestCase extends TestCase { 27 public void testComponensRegisteredWithClassKeyTakePrecedenceOverOthersWhenThereAreMultipleImplementations() throws Exception { 28 DefaultPicoContainer pico = new DefaultPicoContainer(); 29 pico.registerComponentImplementation("default", SimpleTouchable.class); 30 31 34 pico.registerComponentImplementation(Touchable.class, DecoratedTouchable.class, new Parameter[]{ 35 new ComponentParameter("default") 36 }); 37 38 Touchable touchable = (Touchable) pico.getComponentInstanceOfType(Touchable.class); 39 assertEquals(DecoratedTouchable.class, touchable.getClass()); 40 } 41 42 public void testComponentAdapterResolutionIsFirstLookedForByClassKeyToTheTopOfTheContainerHierarchy() { 43 DefaultPicoContainer pico = new DefaultPicoContainer(); 44 pico.registerComponentImplementation("default", SimpleTouchable.class); 45 46 pico.registerComponentImplementation(Touchable.class, DecoratedTouchable.class, Collections.singletonList(new ComponentParameter("default"))); 48 49 DefaultPicoContainer grandChild = new DefaultPicoContainer(new DefaultPicoContainer(new DefaultPicoContainer(pico))); 50 51 Touchable touchable = (Touchable) grandChild.getComponentInstanceOfType(Touchable.class); 52 assertEquals(DecoratedTouchable.class, touchable.getClass()); 53 54 } 55 56 public void testComponentKeysFromParentCannotConfuseTheChild() throws Exception { 57 DefaultPicoContainer pico = new DefaultPicoContainer(); 58 pico.registerComponentImplementation("test", SimpleTouchable.class); 59 60 DefaultPicoContainer child = new DefaultPicoContainer(pico); 61 62 child.registerComponentImplementation("test", DependsOnTouchable.class); 63 64 DependsOnTouchable dot = (DependsOnTouchable) child.getComponentInstance("test"); 65 66 assertNotNull(dot); 67 } 68 69 } 70 | Popular Tags |