1 10 11 package org.picocontainer.defaults; 12 13 import junit.framework.TestCase; 14 15 import org.picocontainer.MutablePicoContainer; 16 import org.picocontainer.Parameter; 17 import org.picocontainer.PicoInitializationException; 18 import org.picocontainer.PicoIntrospectionException; 19 import org.picocontainer.PicoRegistrationException; 20 import org.picocontainer.testmodel.DependsOnTouchable; 21 import org.picocontainer.testmodel.SimpleTouchable; 22 import org.picocontainer.testmodel.Touchable; 23 import org.picocontainer.testmodel.Webster; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 public class NoneOfTheseTestsAffectCoverageMeaningTheyCouldGoTestCase extends TestCase { 29 30 public void testGetComponentSpecification() throws PicoRegistrationException, DuplicateComponentKeyRegistrationException, AssignabilityRegistrationException, AmbiguousComponentResolutionException, PicoIntrospectionException { 32 DefaultPicoContainer pico = new DefaultPicoContainer(); 33 34 assertNull(pico.getComponentAdapterOfType(Touchable.class)); 35 pico.registerComponentImplementation(SimpleTouchable.class); 36 assertNotNull(pico.getComponentAdapterOfType(SimpleTouchable.class)); 37 assertNotNull(pico.getComponentAdapterOfType(Touchable.class)); 38 } 39 40 41 public void testMultipleImplementationsAccessedThroughKey() 43 throws PicoInitializationException, PicoRegistrationException, PicoInvocationTargetInitializationException { 44 SimpleTouchable Touchable1 = new SimpleTouchable(); 45 SimpleTouchable Touchable2 = new SimpleTouchable(); 46 DefaultPicoContainer pico = new DefaultPicoContainer(); 47 pico.registerComponentInstance("Touchable1", Touchable1); 48 pico.registerComponentInstance("Touchable2", Touchable2); 49 pico.registerComponentImplementation("fred1", DependsOnTouchable.class, new Parameter[]{new ComponentParameter("Touchable1")}); 50 pico.registerComponentImplementation("fred2", DependsOnTouchable.class, new Parameter[]{new ComponentParameter("Touchable2")}); 51 52 DependsOnTouchable fred1 = (DependsOnTouchable) pico.getComponentInstance("fred1"); 53 DependsOnTouchable fred2 = (DependsOnTouchable) pico.getComponentInstance("fred2"); 54 55 assertFalse(fred1 == fred2); 56 assertSame(Touchable1, fred1.getTouchable()); 57 assertSame(Touchable2, fred2.getTouchable()); 58 } 59 60 public void testRegistrationByName() throws Exception { 62 DefaultPicoContainer pico = new DefaultPicoContainer(); 63 64 Webster one = new Webster(new ArrayList ()); 65 Touchable two = new SimpleTouchable(); 66 67 pico.registerComponentInstance("one", one); 68 pico.registerComponentInstance("two", two); 69 70 assertEquals("Wrong number of comps in the internals", 2, pico.getComponentInstances().size()); 71 72 assertEquals("Looking up one Touchable", one, pico.getComponentInstance("one")); 73 assertEquals("Looking up two Touchable", two, pico.getComponentInstance("two")); 74 75 assertTrue("Object one the same", one == pico.getComponentInstance("one")); 76 assertTrue("Object two the same", two == pico.getComponentInstance("two")); 77 78 assertEquals("Lookup of unknown key should return null", null, pico.getComponentInstance("unknown")); 79 } 80 81 public void testRegistrationByNameAndClassWithResolving() throws Exception { 82 DefaultPicoContainer pico = new DefaultPicoContainer(); 83 84 pico.registerComponentInstance(List .class, new ArrayList ()); 85 pico.registerComponentImplementation("one", Webster.class); 86 pico.registerComponentImplementation("two", SimpleTouchable.class); 87 88 assertEquals("Wrong number of comps in the internals", 3, pico.getComponentInstances().size()); 89 90 assertNotNull("Object one the same", pico.getComponentInstance("one")); 91 assertNotNull("Object two the same", pico.getComponentInstance("two")); 92 93 assertNull("Lookup of unknown key should return null", pico.getComponentInstance("unknown")); 94 } 95 96 public void testDuplicateRegistrationWithTypeAndObject() throws PicoRegistrationException, PicoIntrospectionException { 97 DefaultPicoContainer pico = new DefaultPicoContainer(); 98 99 pico.registerComponentImplementation(SimpleTouchable.class); 100 try { 101 pico.registerComponentInstance(SimpleTouchable.class, new SimpleTouchable()); 102 fail("Should have barfed with dupe registration"); 103 } catch (DuplicateComponentKeyRegistrationException e) { 104 assertTrue(e.getDuplicateKey() == SimpleTouchable.class); 106 assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) > 0); 107 } 108 } 109 110 111 public void testComponentRegistrationMismatch() throws PicoRegistrationException, PicoIntrospectionException { 112 MutablePicoContainer pico = new DefaultPicoContainer(); 113 114 try { 115 pico.registerComponentImplementation(List .class, SimpleTouchable.class); 116 } catch (AssignabilityRegistrationException e) { 117 assertTrue(e.getMessage().indexOf(List .class.getName()) > 0); 119 assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) > 0); 120 } 121 122 } 123 124 interface Animal { 125 126 String getFood(); 127 } 128 129 public static class Dino implements Animal { 130 String food; 131 132 public Dino(String food) { 133 this.food = food; 134 } 135 136 public String getFood() { 137 return food; 138 } 139 } 140 141 public static class Dino2 extends Dino { 142 public Dino2(int number) { 143 super(String.valueOf(number)); 144 } 145 } 146 147 public static class Dino3 extends Dino { 148 public Dino3(String a, String b) { 149 super(a + b); 150 } 151 } 152 153 public static class Dino4 extends Dino { 154 public Dino4(String a, int n, String b, Touchable Touchable) { 155 super(a + n + b + " " + Touchable.getClass().getName()); 156 } 157 } 158 159 public void testParameterCanBePassedToConstructor() throws Exception { 160 DefaultPicoContainer pico = new DefaultPicoContainer(); 161 pico.registerComponentImplementation(Animal.class, 162 Dino.class, 163 new Parameter[]{ 164 new ConstantParameter("bones") 165 }); 166 167 Animal animal = (Animal) pico.getComponentInstance(Animal.class); 168 assertNotNull("Component not null", animal); 169 assertEquals("bones", animal.getFood()); 170 } 171 172 public void testParameterCanBePrimitive() throws Exception { 173 DefaultPicoContainer pico = new DefaultPicoContainer(); 174 pico.registerComponentImplementation(Animal.class, Dino2.class, new Parameter[]{new ConstantParameter(new Integer (22))}); 175 176 Animal animal = (Animal) pico.getComponentInstance(Animal.class); 177 assertNotNull("Component not null", animal); 178 assertEquals("22", animal.getFood()); 179 } 180 181 public void testMultipleParametersCanBePassed() throws Exception { 182 DefaultPicoContainer pico = new DefaultPicoContainer(); 183 pico.registerComponentImplementation(Animal.class, Dino3.class, new Parameter[]{ 184 new ConstantParameter("a"), 185 new ConstantParameter("b") 186 }); 187 188 Animal animal = (Animal) pico.getComponentInstance(Animal.class); 189 assertNotNull("Component not null", animal); 190 assertEquals("ab", animal.getFood()); 191 192 } 193 194 public void testParametersCanBeMixedWithComponentsCanBePassed() throws Exception { 195 DefaultPicoContainer pico = new DefaultPicoContainer(); 196 pico.registerComponentImplementation(Touchable.class, SimpleTouchable.class); 197 pico.registerComponentImplementation(Animal.class, Dino4.class, new Parameter[]{ 198 new ConstantParameter("a"), 199 new ConstantParameter(new Integer (3)), 200 new ConstantParameter("b"), 201 ComponentParameter.DEFAULT 202 }); 203 204 Animal animal = (Animal) pico.getComponentInstance(Animal.class); 205 assertNotNull("Component not null", animal); 206 assertEquals("a3b org.picocontainer.testmodel.SimpleTouchable", animal.getFood()); 207 } 208 209 } 210 | Popular Tags |