1 10 package org.picocontainer.defaults; 11 12 import junit.framework.TestCase; 13 14 import org.picocontainer.ComponentAdapter; 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 24 25 29 public class ParameterTestCase extends TestCase { 30 31 public void testComponentParameterFetches() throws PicoRegistrationException, PicoInitializationException { 32 DefaultPicoContainer pico = new DefaultPicoContainer(); 33 ComponentAdapter adapter = pico.registerComponentImplementation(Touchable.class, SimpleTouchable.class); 34 assertNotNull(adapter); 35 assertNotNull(pico.getComponentInstance(Touchable.class)); 36 Touchable touchable = (Touchable) ComponentParameter.DEFAULT.resolveInstance(pico, null, Touchable.class); 37 assertNotNull(touchable); 38 } 39 40 public void testComponentParameterExcludesSelf() throws PicoRegistrationException, PicoInitializationException { 41 DefaultPicoContainer pico = new DefaultPicoContainer(); 42 ComponentAdapter adapter = pico.registerComponentImplementation(Touchable.class, SimpleTouchable.class); 43 44 assertNotNull(pico.getComponentInstance(Touchable.class)); 45 Touchable touchable = (Touchable) ComponentParameter.DEFAULT.resolveInstance(pico, adapter, Touchable.class); 46 assertNull(touchable); 47 } 48 49 public void testConstantParameter() throws PicoInitializationException, AssignabilityRegistrationException, NotConcreteRegistrationException, PicoIntrospectionException { 50 Object value = new Object (); 51 ConstantParameter parameter = new ConstantParameter(value); 52 MutablePicoContainer picoContainer = new DefaultPicoContainer(); 53 assertSame(value, parameter.resolveInstance(picoContainer, null, Object .class)); 54 } 55 56 public void testDependsOnTouchableWithTouchableSpecifiedAsConstant() throws PicoRegistrationException, PicoInitializationException { 57 DefaultPicoContainer pico = new DefaultPicoContainer(); 58 SimpleTouchable touchable = new SimpleTouchable(); 59 pico.registerComponentImplementation(DependsOnTouchable.class, DependsOnTouchable.class, new Parameter[]{ 60 new ConstantParameter(touchable) 61 }); 62 pico.getComponentInstances(); 63 assertTrue(touchable.wasTouched); 64 } 65 66 public void testComponentParameterRespectsExpectedType() { 67 MutablePicoContainer picoContainer = new DefaultPicoContainer(); 68 ComponentAdapter adapter = picoContainer.registerComponentImplementation(Touchable.class, SimpleTouchable.class); 69 assertNull(ComponentParameter.DEFAULT.resolveInstance(picoContainer, adapter, TestCase.class)); 70 } 71 72 public void testComponentParameterResolvesPrimitiveType() { 73 MutablePicoContainer picoContainer = new DefaultPicoContainer(); 74 ComponentAdapter adapter = picoContainer.registerComponentInstance("glarch", new Integer (239)); 75 assertNotNull(adapter); 76 Parameter parameter = new ComponentParameter("glarch"); 77 assertNotNull(parameter.resolveInstance(picoContainer,null,Integer.TYPE)); 78 assertEquals(239, ((Integer )parameter.resolveInstance(picoContainer,null,Integer.TYPE)).intValue()); 79 } 80 81 public void testConstantParameterRespectsExpectedType() { 82 MutablePicoContainer picoContainer = new DefaultPicoContainer(); 83 Parameter parameter = new ConstantParameter(new SimpleTouchable()); 84 ComponentAdapter adapter = picoContainer.registerComponentImplementation(Touchable.class, SimpleTouchable.class); 85 assertFalse(parameter.isResolvable(picoContainer, adapter, TestCase.class)); 86 } 87 88 public void testParameterRespectsExpectedType() throws PicoInitializationException, NotConcreteRegistrationException, PicoIntrospectionException { 89 Parameter parameter = new ConstantParameter(Touchable.class); 90 MutablePicoContainer picoContainer = new DefaultPicoContainer(); 91 assertFalse(parameter.isResolvable(picoContainer, null, TestCase.class)); 92 93 ComponentAdapter adapter = picoContainer.registerComponentImplementation(Touchable.class, SimpleTouchable.class); 94 95 assertNull(ComponentParameter.DEFAULT.resolveInstance(picoContainer, adapter, TestCase.class)); 96 } 97 98 public void testConstantParameterWithPrimitives() throws PicoInitializationException, AssignabilityRegistrationException, NotConcreteRegistrationException, PicoIntrospectionException { 99 MutablePicoContainer picoContainer = new DefaultPicoContainer(); 100 Byte byteValue = new Byte ((byte) 5); 101 ConstantParameter parameter = new ConstantParameter(byteValue); 102 assertSame(byteValue, parameter.resolveInstance(picoContainer, null, Byte.TYPE)); 103 assertSame(byteValue, parameter.resolveInstance(picoContainer, null, Byte .class)); 104 Short shortValue = new Short ((short) 5); 105 parameter = new ConstantParameter(shortValue); 106 assertSame(shortValue, parameter.resolveInstance(picoContainer, null, Short.TYPE)); 107 assertSame(shortValue, parameter.resolveInstance(picoContainer, null, Short .class)); 108 Integer intValue = new Integer (5); 109 parameter = new ConstantParameter(intValue); 110 assertSame(intValue, parameter.resolveInstance(picoContainer, null, Integer.TYPE)); 111 assertSame(intValue, parameter.resolveInstance(picoContainer, null, Integer .class)); 112 Long longValue = new Long (5); 113 parameter = new ConstantParameter(longValue); 114 assertSame(longValue, parameter.resolveInstance(picoContainer, null, Long.TYPE)); 115 assertSame(longValue, parameter.resolveInstance(picoContainer, null, Long .class)); 116 Float floatValue = new Float (5.5); 117 parameter = new ConstantParameter(floatValue); 118 assertSame(floatValue, parameter.resolveInstance(picoContainer, null, Float.TYPE)); 119 assertSame(floatValue, parameter.resolveInstance(picoContainer, null, Float .class)); 120 Double doubleValue = new Double (5.5); 121 parameter = new ConstantParameter(doubleValue); 122 assertSame(doubleValue, parameter.resolveInstance(picoContainer, null, Double.TYPE)); 123 assertSame(doubleValue, parameter.resolveInstance(picoContainer, null, Double .class)); 124 Boolean booleanValue = new Boolean (true); 125 parameter = new ConstantParameter(booleanValue); 126 assertSame(booleanValue, parameter.resolveInstance(picoContainer, null, Boolean.TYPE)); 127 assertSame(booleanValue, parameter.resolveInstance(picoContainer, null, Boolean .class)); 128 Character charValue = new Character ('x'); 129 parameter = new ConstantParameter(charValue); 130 assertSame(charValue, parameter.resolveInstance(picoContainer, null, Character.TYPE)); 131 assertSame(charValue, parameter.resolveInstance(picoContainer, null, Character .class)); 132 } 133 134 public void testConstantParameterWithPrimitivesRejectsUnexpectedType() throws PicoInitializationException, AssignabilityRegistrationException, NotConcreteRegistrationException, PicoIntrospectionException { 135 MutablePicoContainer picoContainer = new DefaultPicoContainer(); 136 Byte byteValue = new Byte ((byte) 5); 137 ConstantParameter parameter = new ConstantParameter(byteValue); 138 assertFalse(parameter.isResolvable(picoContainer, null, Integer.TYPE)); 139 Short shortValue = new Short ((short) 5); 140 parameter = new ConstantParameter(shortValue); 141 assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE)); 142 Integer intValue = new Integer (5); 143 parameter = new ConstantParameter(intValue); 144 assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE)); 145 Long longValue = new Long (5); 146 parameter = new ConstantParameter(longValue); 147 assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE)); 148 Float floatValue = new Float (5.5); 149 parameter = new ConstantParameter(floatValue); 150 assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE)); 151 Double doubleValue = new Double (5.5); 152 parameter = new ConstantParameter(doubleValue); 153 assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE)); 154 Boolean booleanValue = new Boolean (true); 155 parameter = new ConstantParameter(booleanValue); 156 assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE)); 157 Character charValue = new Character ('x'); 158 parameter = new ConstantParameter(charValue); 159 assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE)); 160 } 161 162 public void testKeyClashBug118() throws PicoRegistrationException, PicoInitializationException { 163 DefaultPicoContainer pico = new DefaultPicoContainer(); 164 pico.registerComponentImplementation("A", String .class, new Parameter[]{ 165 new ConstantParameter("A") 166 }); 167 pico.registerComponentImplementation("B", String .class, new Parameter[]{ 168 new ConstantParameter("A") 169 }); 170 new VerifyingVisitor().traverse(pico); 171 } 172 173 } 174 | Popular Tags |