KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > alternatives > ImmutablePicoContainerTestCase


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.alternatives;
12
13 import org.jmock.Mock;
14 import org.jmock.MockObjectTestCase;
15 import org.picocontainer.ComponentAdapter;
16 import org.picocontainer.Disposable;
17 import org.picocontainer.MutablePicoContainer;
18 import org.picocontainer.PicoContainer;
19 import org.picocontainer.PicoVerificationException;
20 import org.picocontainer.PicoVisitor;
21 import org.picocontainer.defaults.DefaultPicoContainer;
22 import org.picocontainer.defaults.VerifyingVisitor;
23
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 /**
32  * @author Paul Hammant
33  * @version $Revision: 2382 $
34  */

35 public class ImmutablePicoContainerTestCase extends MockObjectTestCase {
36
37     public void testImmutingofNullBarfs() {
38         try {
39             new ImmutablePicoContainer(null);
40             fail("Should have barfed");
41         } catch (NullPointerException JavaDoc e) {
42             //expected
43
}
44     }
45
46     public void testDelegationOfGettingComponentInstance() {
47         DefaultPicoContainer mpc = new DefaultPicoContainer();
48         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
49         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
50         Map JavaDoc map = (Map JavaDoc) ipc.getComponentInstance(Map JavaDoc.class);
51         assertNotNull(map);
52     }
53
54     public void testDelegationOfGettingComponentInstanceOfType() {
55         DefaultPicoContainer mpc = new DefaultPicoContainer();
56         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
57         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
58         Map JavaDoc map = (Map JavaDoc) ipc.getComponentInstanceOfType(Map JavaDoc.class);
59         assertNotNull(map);
60     }
61
62     public void testDelegationOfGettingComponentInstancesOfType() {
63         DefaultPicoContainer mpc = new DefaultPicoContainer();
64         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
65         mpc.registerComponentImplementation(Collection JavaDoc.class, Vector JavaDoc.class);
66         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
67         List JavaDoc list = ipc.getComponentInstancesOfType(Map JavaDoc.class);
68         assertNotNull(list);
69         assertEquals(1,list.size());
70     }
71
72     public void testDelegationOfGetComponentInstances() {
73         DefaultPicoContainer mpc = new DefaultPicoContainer();
74         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
75         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
76         List JavaDoc comps = ipc.getComponentInstances();
77         assertNotNull(comps);
78         assertEquals(1, comps.size());
79     }
80
81     public void testDelegationOfGetComponentAdapter() {
82         DefaultPicoContainer mpc = new DefaultPicoContainer();
83         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
84         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
85         ComponentAdapter ca = ipc.getComponentAdapter(Map JavaDoc.class);
86         assertNotNull(ca);
87
88     }
89
90     public void testDelegationOfGetComponentAdapterOfType() {
91         DefaultPicoContainer mpc = new DefaultPicoContainer();
92         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
93         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
94         ComponentAdapter ca = ipc.getComponentAdapterOfType(Map JavaDoc.class);
95         assertNotNull(ca);
96     }
97
98     public void testDelegationOfGetComponentAdapters() {
99         DefaultPicoContainer mpc = new DefaultPicoContainer();
100         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
101         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
102         Collection JavaDoc comps = ipc.getComponentAdapters();
103         assertNotNull(comps);
104         assertEquals(1, comps.size());
105     }
106
107     public void testDelegationOfGetComponentAdaptersOfType() {
108         DefaultPicoContainer mpc = new DefaultPicoContainer();
109         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
110         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
111         List JavaDoc comps = ipc.getComponentAdaptersOfType(Map JavaDoc.class);
112         assertNotNull(comps);
113         assertEquals(1, comps.size());
114     }
115
116     public static class UnsatisfiableIterator implements Iterator JavaDoc {
117
118         public UnsatisfiableIterator(Map JavaDoc map) {
119         }
120
121         public void remove() {
122         }
123
124         public boolean hasNext() {
125             return false;
126         }
127
128         public Object JavaDoc next() {
129             return null;
130         }
131     }
132
133     public void testDelegationOfVerify() {
134         DefaultPicoContainer mpc = new DefaultPicoContainer();
135         mpc.registerComponentImplementation(Iterator JavaDoc.class, UnsatisfiableIterator.class);
136         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
137         try {
138             new VerifyingVisitor().traverse(ipc);
139             fail("PicoVerificationException expected");
140         } catch (PicoVerificationException e) {
141             // expected
142
}
143         try {
144             ipc.verify();
145             fail("PicoVerificationException expected");
146         } catch (PicoVerificationException e) {
147             // expected
148
}
149     }
150
151     public void testGetParentForMutable() {
152         DefaultPicoContainer par = new DefaultPicoContainer();
153         DefaultPicoContainer mpc = new DefaultPicoContainer(par);
154         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
155         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
156         PicoContainer parent = ipc.getParent();
157         assertNotNull(parent);
158         assertNotSame(par, parent);
159         PicoContainer parent2 = ipc.getParent();
160         assertNotNull(parent2);
161         assertEquals(parent, parent2);
162     }
163
164     public void testGetParentForNonMutable() {
165         DefaultPicoContainer par = new DefaultPicoContainer();
166         ImmutablePicoContainer par2 = new ImmutablePicoContainer(par);
167         DefaultPicoContainer mpc = new DefaultPicoContainer(par2);
168         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
169         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
170         PicoContainer parent = ipc.getParent();
171         assertNotNull(parent);
172         assertNotSame(par, parent);
173         PicoContainer parent2 = ipc.getParent();
174         assertNotNull(parent2);
175         assertEquals(parent, parent2);
176     }
177
178     public void testStartBarfs() {
179         DefaultPicoContainer mpc = new DefaultPicoContainer();
180         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
181         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
182         try {
183             ipc.start();
184             fail("should have barfed");
185         } catch (UnsupportedOperationException JavaDoc e) {
186             // expected
187
}
188     }
189
190     public void testStopBarfs() {
191         DefaultPicoContainer mpc = new DefaultPicoContainer();
192         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
193         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
194         try {
195             ipc.stop();
196             fail("stop have barfed");
197         } catch (UnsupportedOperationException JavaDoc e) {
198             // expected
199
}
200     }
201
202     public void testDisposeBarfs() {
203         DefaultPicoContainer mpc = new DefaultPicoContainer();
204         mpc.registerComponentImplementation(Map JavaDoc.class, HashMap JavaDoc.class);
205         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
206         try {
207             ipc.dispose();
208             fail("should have barfed");
209         } catch (UnsupportedOperationException JavaDoc e) {
210             // expected
211
}
212     }
213
214     public static class MyDisposable implements Disposable {
215         public boolean disposed;
216
217         public void dispose() {
218             disposed = true;
219         }
220     }
221
222     public void testLifecycleGuardIsEasyToCircumventSoItMightAsWellBeDeleted() {
223         DefaultPicoContainer mpc = new DefaultPicoContainer();
224         mpc.registerComponentImplementation(MyDisposable.class);
225         ImmutablePicoContainer ipc = new ImmutablePicoContainer(mpc);
226         List JavaDoc componentInstances = ipc.getComponentInstances();
227         for (Iterator JavaDoc iterator = componentInstances.iterator(); iterator.hasNext();) {
228             Object JavaDoc o = iterator.next();
229             if(o instanceof Disposable) {
230                 ((Disposable) o).dispose();
231             }
232         }
233         MyDisposable disposable = (MyDisposable ) ipc.getComponentInstance(MyDisposable.class);
234         assertTrue(disposable.disposed);
235     }
236
237     public void testFacetiouslyThatLifeCycleGuardPreventsCyclingOfChildContainersAsComponentsAreNotTheOnlyThingsThatAreLifecycleable() {
238         DefaultPicoContainer parent = new DefaultPicoContainer();
239         MutablePicoContainer child = parent.makeChildContainer();
240         parent.registerComponentImplementation("foo", MyDisposable.class);
241         child.registerComponentImplementation("bar", MyDisposable.class);
242         ImmutablePicoContainer ipc = new ImmutablePicoContainer(parent);
243         try {
244             ipc.dispose();
245             fail("Should have barfed");
246         } catch (UnsupportedOperationException JavaDoc e) {
247             // expected
248
}
249
250         MyDisposable parentDisposable = (MyDisposable) parent.getComponentInstance("foo");
251         assertFalse(parentDisposable.disposed);
252
253         MyDisposable childDisposable = (MyDisposable) child.getComponentInstance("bar");
254         assertFalse(childDisposable.disposed);
255
256         // If this were on parent, it would cascade to child.
257
((Disposable) ipc.getComponentInstances().get(0)).dispose();
258
259         // I can live with this below....
260
assertTrue(parentDisposable.disposed);
261
262         // However, I'd be in favor of changing
263
// interface PicoContainer extends Startable, Disposable {}
264
// to
265
// interface PicoContainer {}
266
// AND
267
// interface MutablePicoContainer extends PicoContainer {}
268
// to
269
// interface MutablePicoContainer extends PicoContainer implements Startable, Disposable {}
270
// That despite breaking (marginally) backwards compatability.
271
// - Paul
272
}
273
274     public void testVisitingOfImmutableContainerWorks() {
275         DefaultPicoContainer pico = new DefaultPicoContainer();
276         Object JavaDoc foo = new Object JavaDoc();
277         ComponentAdapter componentAdapter = pico.registerComponentInstance(foo);
278
279         Mock fooVisitor = new Mock(PicoVisitor.class);
280         fooVisitor.expects(once()).method("visitContainer").with(eq(pico));
281         fooVisitor.expects(once()).method("visitComponentAdapter").with(eq(componentAdapter));
282
283         ImmutablePicoContainer ipc = new ImmutablePicoContainer(pico);
284
285         ipc.accept((PicoVisitor) fooVisitor.proxy());
286
287
288     }
289 }
290
Popular Tags