KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > tck > AbstractMultipleConstructorTestCase


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.tck;
11
12 import junit.framework.TestCase;
13 import org.picocontainer.MutablePicoContainer;
14 import org.picocontainer.Parameter;
15 import org.picocontainer.PicoException;
16 import org.picocontainer.PicoRegistrationException;
17 import org.picocontainer.defaults.ComponentParameter;
18 import org.picocontainer.defaults.ConstantParameter;
19 import org.picocontainer.defaults.TooManySatisfiableConstructorsException;
20
21 /**
22  * @author Aslak Hellesøy
23  * @version $Revision: 1804 $
24  */

25 public abstract class AbstractMultipleConstructorTestCase extends TestCase {
26
27     protected abstract MutablePicoContainer createPicoContainer();
28
29     public static class Multi {
30         public String JavaDoc message;
31
32         public Multi(One one, Two two, Three three) {
33             message = "one two three";
34         }
35
36         public Multi(One one, Two two) {
37             message = "one two";
38         }
39
40         public Multi(Two two, One one) {
41             message = "two one";
42         }
43
44         public Multi(Two two, Three three) {
45             message = "two three";
46         }
47
48         public Multi(Three three, One one) {
49             message = "three one";
50         }
51
52         public Multi(One one, String JavaDoc string) {
53             message = "one string";
54         }
55
56         public Multi(One one, int i) {
57             message = "one int";
58         }
59
60         public Multi() {
61             message = "none";
62         }
63     }
64
65     public static class One {
66     }
67
68     public static class Two {
69     }
70
71     public static class Three {
72     }
73
74
75     public void testStringWorks() throws PicoException, PicoRegistrationException {
76         MutablePicoContainer pico = createPicoContainer();
77         pico.registerComponentImplementation(String JavaDoc.class);
78         assertEquals("", pico.getComponentInstance(String JavaDoc.class));
79     }
80
81     public void testMultiWithOnlySmallSatisfiedDependencyWorks() throws PicoException, PicoRegistrationException {
82         MutablePicoContainer pico = createPicoContainer();
83         pico.registerComponentImplementation(Multi.class);
84         pico.registerComponentImplementation(One.class);
85         pico.registerComponentImplementation(Three.class);
86
87         Multi multi = (Multi) pico.getComponentInstance(Multi.class);
88         assertEquals("three one", multi.message);
89     }
90
91     public void testMultiWithBothSatisfiedDependencyWorks() throws PicoException, PicoRegistrationException {
92         MutablePicoContainer pico = createPicoContainer();
93         pico.registerComponentImplementation(Multi.class);
94         pico.registerComponentImplementation(One.class);
95         pico.registerComponentImplementation(Two.class);
96         pico.registerComponentImplementation(Three.class);
97
98         Multi multi = (Multi) pico.getComponentInstance(Multi.class);
99         assertEquals("one two three", multi.message);
100     }
101
102     public void testMultiWithTwoEquallyBigSatisfiedDependenciesFails() throws PicoException, PicoRegistrationException {
103         MutablePicoContainer pico = createPicoContainer();
104         pico.registerComponentImplementation(Multi.class);
105         pico.registerComponentImplementation(One.class);
106         pico.registerComponentImplementation(Two.class);
107
108         try {
109             pico.getComponentInstance(Multi.class);
110             fail();
111         } catch (TooManySatisfiableConstructorsException e) {
112             assertTrue(e.getMessage().indexOf("Three") == -1);
113             assertEquals(3, e.getConstructors().size());
114             assertEquals(Multi.class, e.getForImplementationClass());
115         }
116     }
117
118     public void testMultiWithSatisfyingDependencyAndParametersWorks() throws PicoException, PicoRegistrationException {
119         MutablePicoContainer pico = createPicoContainer();
120         pico.registerComponentImplementation("MultiOneTwo", Multi.class, new Parameter[]{
121             ComponentParameter.DEFAULT,
122             new ComponentParameter("Two"),
123         });
124         pico.registerComponentImplementation("MultiTwoOne", Multi.class, new Parameter[]{
125             new ComponentParameter("Two"),
126             ComponentParameter.DEFAULT,
127         });
128         pico.registerComponentImplementation("MultiOneString", Multi.class, new Parameter[]{
129             ComponentParameter.DEFAULT,
130             new ConstantParameter(""),
131         });
132         pico.registerComponentImplementation("MultiOneInt", Multi.class, new Parameter[]{
133             ComponentParameter.DEFAULT,
134             new ConstantParameter(new Integer JavaDoc(5)),
135         });
136         pico.registerComponentImplementation("MultiNone", Multi.class, new Parameter[]{});
137         pico.registerComponentImplementation(One.class);
138         pico.registerComponentImplementation("Two", Two.class);
139
140         Multi multiOneTwo = (Multi) pico.getComponentInstance("MultiOneTwo");
141         assertEquals("one two", multiOneTwo.message);
142         Multi multiTwoOne = (Multi) pico.getComponentInstance("MultiTwoOne");
143         assertEquals("two one", multiTwoOne.message);
144         Multi multiOneString = (Multi) pico.getComponentInstance("MultiOneString");
145         assertEquals("one string", multiOneString.message);
146         Multi multiOneInt = (Multi) pico.getComponentInstance("MultiOneInt");
147         assertEquals("one int", multiOneInt.message);
148         Multi multiNone = (Multi) pico.getComponentInstance("MultiNone");
149         assertEquals("none", multiNone.message);
150     }
151 }
152
Popular Tags