KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9  *****************************************************************************/

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 JavaDoc;
26 import java.util.List JavaDoc;
27
28 public class NoneOfTheseTestsAffectCoverageMeaningTheyCouldGoTestCase extends TestCase {
29
30     //TODO - move to AbstractComponentRegistryTestCase
31
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     //TODO move
42
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     //TODO - move
61
public void testRegistrationByName() throws Exception JavaDoc {
62         DefaultPicoContainer pico = new DefaultPicoContainer();
63
64         Webster one = new Webster(new ArrayList JavaDoc());
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 JavaDoc {
82         DefaultPicoContainer pico = new DefaultPicoContainer();
83
84         pico.registerComponentInstance(List JavaDoc.class, new ArrayList JavaDoc());
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             // expected
105
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 JavaDoc.class, SimpleTouchable.class);
116         } catch (AssignabilityRegistrationException e) {
117             // not worded in message
118
assertTrue(e.getMessage().indexOf(List JavaDoc.class.getName()) > 0);
119             assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) > 0);
120         }
121
122     }
123
124     interface Animal {
125
126         String JavaDoc getFood();
127     }
128
129     public static class Dino implements Animal {
130         String JavaDoc food;
131
132         public Dino(String JavaDoc food) {
133             this.food = food;
134         }
135
136         public String JavaDoc 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 JavaDoc a, String JavaDoc b) {
149             super(a + b);
150         }
151     }
152
153     public static class Dino4 extends Dino {
154         public Dino4(String JavaDoc a, int n, String JavaDoc b, Touchable Touchable) {
155             super(a + n + b + " " + Touchable.getClass().getName());
156         }
157     }
158
159     public void testParameterCanBePassedToConstructor() throws Exception JavaDoc {
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 JavaDoc {
173         DefaultPicoContainer pico = new DefaultPicoContainer();
174         pico.registerComponentImplementation(Animal.class, Dino2.class, new Parameter[]{new ConstantParameter(new Integer JavaDoc(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 JavaDoc {
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 JavaDoc {
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 JavaDoc(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