KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > ParameterTestCase


1 /*****************************************************************************
2  * Copyright (C) PicoContainer 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 *
9  *****************************************************************************/

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 /**
26  * @author Jon Tirsen (tirsen@codehaus.org)
27  * @version $Revision: 1.7 $
28  */

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 JavaDoc value = new Object JavaDoc();
51         ConstantParameter parameter = new ConstantParameter(value);
52         MutablePicoContainer picoContainer = new DefaultPicoContainer();
53         assertSame(value, parameter.resolveInstance(picoContainer, null, Object JavaDoc.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 JavaDoc(239));
75         assertNotNull(adapter);
76         Parameter parameter = new ComponentParameter("glarch");
77         assertNotNull(parameter.resolveInstance(picoContainer,null,Integer.TYPE));
78         assertEquals(239, ((Integer JavaDoc)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 JavaDoc byteValue = new Byte JavaDoc((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 JavaDoc.class));
104         Short JavaDoc shortValue = new Short JavaDoc((short) 5);
105         parameter = new ConstantParameter(shortValue);
106         assertSame(shortValue, parameter.resolveInstance(picoContainer, null, Short.TYPE));
107         assertSame(shortValue, parameter.resolveInstance(picoContainer, null, Short JavaDoc.class));
108         Integer JavaDoc intValue = new Integer JavaDoc(5);
109         parameter = new ConstantParameter(intValue);
110         assertSame(intValue, parameter.resolveInstance(picoContainer, null, Integer.TYPE));
111         assertSame(intValue, parameter.resolveInstance(picoContainer, null, Integer JavaDoc.class));
112         Long JavaDoc longValue = new Long JavaDoc(5);
113         parameter = new ConstantParameter(longValue);
114         assertSame(longValue, parameter.resolveInstance(picoContainer, null, Long.TYPE));
115         assertSame(longValue, parameter.resolveInstance(picoContainer, null, Long JavaDoc.class));
116         Float JavaDoc floatValue = new Float JavaDoc(5.5);
117         parameter = new ConstantParameter(floatValue);
118         assertSame(floatValue, parameter.resolveInstance(picoContainer, null, Float.TYPE));
119         assertSame(floatValue, parameter.resolveInstance(picoContainer, null, Float JavaDoc.class));
120         Double JavaDoc doubleValue = new Double JavaDoc(5.5);
121         parameter = new ConstantParameter(doubleValue);
122         assertSame(doubleValue, parameter.resolveInstance(picoContainer, null, Double.TYPE));
123         assertSame(doubleValue, parameter.resolveInstance(picoContainer, null, Double JavaDoc.class));
124         Boolean JavaDoc booleanValue = new Boolean JavaDoc(true);
125         parameter = new ConstantParameter(booleanValue);
126         assertSame(booleanValue, parameter.resolveInstance(picoContainer, null, Boolean.TYPE));
127         assertSame(booleanValue, parameter.resolveInstance(picoContainer, null, Boolean JavaDoc.class));
128         Character JavaDoc charValue = new Character JavaDoc('x');
129         parameter = new ConstantParameter(charValue);
130         assertSame(charValue, parameter.resolveInstance(picoContainer, null, Character.TYPE));
131         assertSame(charValue, parameter.resolveInstance(picoContainer, null, Character JavaDoc.class));
132     }
133
134     public void testConstantParameterWithPrimitivesRejectsUnexpectedType() throws PicoInitializationException, AssignabilityRegistrationException, NotConcreteRegistrationException, PicoIntrospectionException {
135         MutablePicoContainer picoContainer = new DefaultPicoContainer();
136         Byte JavaDoc byteValue = new Byte JavaDoc((byte) 5);
137         ConstantParameter parameter = new ConstantParameter(byteValue);
138         assertFalse(parameter.isResolvable(picoContainer, null, Integer.TYPE));
139         Short JavaDoc shortValue = new Short JavaDoc((short) 5);
140         parameter = new ConstantParameter(shortValue);
141         assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE));
142         Integer JavaDoc intValue = new Integer JavaDoc(5);
143         parameter = new ConstantParameter(intValue);
144         assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE));
145         Long JavaDoc longValue = new Long JavaDoc(5);
146         parameter = new ConstantParameter(longValue);
147         assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE));
148         Float JavaDoc floatValue = new Float JavaDoc(5.5);
149         parameter = new ConstantParameter(floatValue);
150         assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE));
151         Double JavaDoc doubleValue = new Double JavaDoc(5.5);
152         parameter = new ConstantParameter(doubleValue);
153         assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE));
154         Boolean JavaDoc booleanValue = new Boolean JavaDoc(true);
155         parameter = new ConstantParameter(booleanValue);
156         assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE));
157         Character JavaDoc charValue = new Character JavaDoc('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 JavaDoc.class, new Parameter[]{
165             new ConstantParameter("A")
166         });
167         pico.registerComponentImplementation("B", String JavaDoc.class, new Parameter[]{
168             new ConstantParameter("A")
169         });
170         new VerifyingVisitor().traverse(pico);
171     }
172
173 }
174
Popular Tags