KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > reflection > DefaultNanoContainerTestCase


1 /*****************************************************************************
2  * Copyright (C) NanoContainer 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 Aslak Hellesoy and Paul Hammant *
9  *****************************************************************************/

10
11 package org.nanocontainer.reflection;
12
13 import junit.framework.TestCase;
14 import org.nanocontainer.DefaultNanoContainer;
15 import org.nanocontainer.NanoContainer;
16 import org.nanocontainer.testmodel.ThingThatTakesParamsInConstructor;
17 import org.nanocontainer.testmodel.WebServerImpl;
18 import org.picocontainer.*;
19 import org.picocontainer.alternatives.AbstractDelegatingMutablePicoContainer;
20
21 import java.io.File JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 public class DefaultNanoContainerTestCase extends TestCase {
28
29     public void testBasic() throws PicoRegistrationException, PicoInitializationException, ClassNotFoundException JavaDoc {
30         NanoContainer nanoContainer = new DefaultNanoContainer();
31         nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.DefaultWebServerConfig");
32         nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.WebServer", "org.nanocontainer.testmodel.WebServerImpl");
33     }
34
35     public void testProvision() throws PicoException, PicoInitializationException, ClassNotFoundException JavaDoc {
36         NanoContainer nanoContainer = new DefaultNanoContainer();
37         nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.DefaultWebServerConfig");
38         nanoContainer.registerComponentImplementation("org.nanocontainer.testmodel.WebServerImpl");
39
40         assertNotNull("WebServerImpl should exist", nanoContainer.getPico().getComponentInstance(WebServerImpl.class));
41         assertTrue("WebServerImpl should exist", nanoContainer.getPico().getComponentInstance(WebServerImpl.class) instanceof WebServerImpl);
42     }
43
44     public void testNoGenerationRegistration() throws PicoRegistrationException, PicoIntrospectionException {
45         NanoContainer nanoContainer = new DefaultNanoContainer();
46         try {
47             nanoContainer.registerComponentImplementation("Ping");
48             fail("should have failed");
49         } catch (ClassNotFoundException JavaDoc e) {
50             // expected
51
}
52     }
53
54     public void testParametersCanBePassedInStringForm() throws ClassNotFoundException JavaDoc, PicoException, PicoInitializationException {
55         NanoContainer nanoContainer = new DefaultNanoContainer();
56         String JavaDoc className = ThingThatTakesParamsInConstructor.class.getName();
57
58         nanoContainer.registerComponentImplementation("thing",
59                 className,
60                 new String JavaDoc[]{
61                     "java.lang.String",
62                     "java.lang.Integer"
63                 },
64                 new String JavaDoc[]{
65                     "hello",
66                     "22"
67                 });
68
69         ThingThatTakesParamsInConstructor thing =
70                 (ThingThatTakesParamsInConstructor) nanoContainer.getPico().getComponentInstance("thing");
71         assertNotNull("component not present", thing);
72         assertEquals("hello22", thing.getValue());
73     }
74
75     public void testThatTestCompIsNotNaturallyInTheClassPathForTesting() {
76
77         // the following tests try to load the jar containing TestComp - it
78
// won't do to have the class already available in the classpath
79

80         try {
81             DefaultNanoContainer dfca = new DefaultNanoContainer();
82             dfca.registerComponentImplementation("foo", "TestComp");
83             Object JavaDoc o = dfca.getPico().getComponentInstance("foo");
84             System.out.println("");
85             fail("Should have failed. Class was loaded from " + o.getClass().getProtectionDomain().getCodeSource().getLocation());
86         } catch (ClassNotFoundException JavaDoc expected) {
87         }
88
89     }
90
91     public void testChildContainerAdapterCanRelyOnParentContainerAdapter() throws MalformedURLException JavaDoc, ClassNotFoundException JavaDoc {
92
93         String JavaDoc testcompJarFileName = System.getProperty("testcomp.jar", "src/test-comp/TestComp.jar");
94         // Paul's path to TestComp. PLEASE do not take out.
95
//testcompJarFileName = "D:/OSS/PN/java/nanocontainer/src/test-comp/TestComp.jar";
96
File JavaDoc testCompJar = new File JavaDoc(testcompJarFileName);
97         assertTrue("The testcomp.jar system property should point to java/nanocontainer/src/test-comp/TestComp.jar", testCompJar.isFile());
98
99         // Set up parent
100
NanoContainer parentContainer = new DefaultNanoContainer();
101         parentContainer.addClassLoaderURL(testCompJar.toURL());
102         parentContainer.registerComponentImplementation("parentTestComp", "TestComp");
103         parentContainer.registerComponentImplementation("java.lang.StringBuffer");
104
105         PicoContainer parentContainerAdapterPico = parentContainer.getPico();
106         Object JavaDoc parentTestComp = parentContainerAdapterPico.getComponentInstance("parentTestComp");
107         assertEquals("TestComp", parentTestComp.getClass().getName());
108
109         // Set up child
110
NanoContainer childContainer = new DefaultNanoContainer(parentContainer);
111         File JavaDoc testCompJar2 = new File JavaDoc(testCompJar.getParentFile(), "TestComp2.jar");
112         childContainer.addClassLoaderURL(testCompJar2.toURL());
113         childContainer.registerComponentImplementation("childTestComp", "TestComp2");
114
115         PicoContainer childContainerAdapterPico = childContainer.getPico();
116         Object JavaDoc childTestComp = childContainerAdapterPico.getComponentInstance("childTestComp");
117
118         assertEquals("TestComp2", childTestComp.getClass().getName());
119
120         assertNotSame(parentTestComp, childTestComp);
121
122         final ClassLoader JavaDoc parentCompClassLoader = parentTestComp.getClass().getClassLoader();
123         final ClassLoader JavaDoc childCompClassLoader = childTestComp.getClass().getClassLoader();
124         if(parentCompClassLoader != childCompClassLoader.getParent()) {
125             printClassLoader(parentCompClassLoader);
126             printClassLoader(childCompClassLoader);
127             fail("parentTestComp classloader should be parent of childTestComp classloader");
128         }
129         //PicoContainer.getParent() is now ImmutablePicoContainer
130
assertNotSame(parentContainerAdapterPico, childContainerAdapterPico.getParent());
131     }
132
133     private void printClassLoader(ClassLoader JavaDoc classLoader) {
134         while(classLoader != null) {
135             System.out.println(classLoader);
136             classLoader = classLoader.getParent();
137         }
138         System.out.println("--");
139     }
140
141     public static class AnotherFooComp {
142
143     }
144
145     public void testClassLoaderJugglingIsPossible() throws MalformedURLException JavaDoc, ClassNotFoundException JavaDoc {
146         NanoContainer parentContainer = new DefaultNanoContainer();
147
148         String JavaDoc testcompJarFileName = System.getProperty("testcomp.jar", "src/test-comp/TestComp.jar");
149         // Paul's path to TestComp. PLEASE do not take out.
150
//testcompJarFileName = "D:/OSS/PN/java/nanocontainer/src/test-comp/TestComp.jar";
151
File JavaDoc testCompJar = new File JavaDoc(testcompJarFileName);
152         assertTrue("The testcomp.jar system property should point to java/nanocontainer/src/test-comp/TestComp.jar", testCompJar.isFile());
153
154         parentContainer.registerComponentImplementation("foo", "org.nanocontainer.testmodel.DefaultWebServerConfig");
155
156         Object JavaDoc fooWebServerConfig = parentContainer.getPico().getComponentInstance("foo");
157         assertEquals("org.nanocontainer.testmodel.DefaultWebServerConfig", fooWebServerConfig.getClass().getName());
158
159         NanoContainer childContainer = new DefaultNanoContainer(parentContainer);
160         childContainer.addClassLoaderURL(testCompJar.toURL());
161         childContainer.registerComponentImplementation("bar", "TestComp");
162
163         Object JavaDoc barTestComp = childContainer.getPico().getComponentInstance("bar");
164         assertEquals("TestComp", barTestComp.getClass().getName());
165
166         assertNotSame(fooWebServerConfig.getClass().getClassLoader(), barTestComp.getClass().getClassLoader());
167
168         // This kludge is needed because IDEA, Eclipse and Maven have different numbers of
169
// classloaders in their hierachies for junit invocation.
170
ClassLoader JavaDoc fooCL = fooWebServerConfig.getClass().getClassLoader();
171         ClassLoader JavaDoc barCL1 = barTestComp.getClass().getClassLoader().getParent();
172         ClassLoader JavaDoc barCL2, barCL3;
173         if (barCL1 != null && barCL1 != fooCL) {
174             barCL2 = barCL1.getParent();
175             if (barCL2 != null && barCL2 != fooCL) {
176                 barCL3 = barCL2.getParent();
177                 if (barCL3 != null && barCL3 != fooCL) {
178                     fail("One of the parent classloaders of TestComp, should be that of DefaultWebServerConfig");
179                 }
180             }
181         }
182     }
183
184     public void TODO_testSecurityManagerCanPreventOperations() throws MalformedURLException JavaDoc, ClassNotFoundException JavaDoc {
185         NanoContainer parentContainer = new DefaultNanoContainer();
186
187         String JavaDoc testcompJarFileName = System.getProperty("testcomp.jar");
188         // Paul's path to TestComp. PLEASE do not take out.
189
//testcompJarFileName = "D:/OSS/PN/java/nanocontainer/src/test-comp/TestComp.jar";
190
assertNotNull("The testcomp.jar system property should point to nano/reflection/src/test-comp/TestComp.jar", testcompJarFileName);
191         File JavaDoc testCompJar = new File JavaDoc(testcompJarFileName);
192         assertTrue(testCompJar.isFile());
193
194         parentContainer.registerComponentImplementation("foo", "org.nanocontainer.testmodel.DefaultWebServerConfig");
195
196         Object JavaDoc fooWebServerConfig = parentContainer.getPico().getComponentInstance("foo");
197         assertEquals("org.nanocontainer.testmodel.DefaultWebServerConfig", fooWebServerConfig.getClass().getName());
198
199         NanoContainer childContainer = new DefaultNanoContainer(parentContainer);
200         childContainer.addClassLoaderURL(testCompJar.toURL());
201         //TODO childContainer.setPermission(some permission list, that includes the preventing of general file access);
202
// Or shoud this be done in the ctor for DRCA ?
203
// or should it a parameter in the addClassLoaderURL(..) method
204
childContainer.registerComponentImplementation("bar", "org.nanocontainer.testmodel.FileSystemUsing");
205
206         try {
207             parentContainer.getPico().getComponentInstance("bar");
208             fail("Should have barfed");
209         } catch (java.security.AccessControlException JavaDoc e) {
210             // expected
211
}
212     }
213
214
215     public void testChainOfDecoratingPicoContainersCanDoInterceptionOfMutablePicoContainerMethods() throws ClassNotFoundException JavaDoc {
216         NanoContainer nanoContainer = new DefaultNanoContainer();
217         MutablePicoContainer decorating = nanoContainer.addDecoratingPicoContainer(FooDecoratingPicoContainer.class);
218         assertTrue(decorating instanceof FooDecoratingPicoContainer);
219         MutablePicoContainer decorating2 = nanoContainer.addDecoratingPicoContainer(BarDecoratingPicoContainer.class);
220         assertTrue(decorating2 instanceof BarDecoratingPicoContainer);
221         nanoContainer.registerComponentImplementation("java.util.Vector");
222         // decorators are fairly dirty - they replace a very select implementation in this TestCase.
223
assertNotNull(nanoContainer.getComponentInstanceOfType("java.util.ArrayList"));
224         assertNull(nanoContainer.getComponentInstanceOfType("java.util.Vector"));
225         assertNotNull(nanoContainer.getPico().getComponentInstanceOfType(ArrayList JavaDoc.class));
226         assertNull(nanoContainer.getPico().getComponentInstanceOfType(Vector JavaDoc.class));
227     }
228
229     public static class FooDecoratingPicoContainer extends AbstractDelegatingMutablePicoContainer {
230         public FooDecoratingPicoContainer(MutablePicoContainer delegate) {
231             super(delegate);
232         }
233         public MutablePicoContainer makeChildContainer() {
234             return null;
235         }
236
237         public ComponentAdapter registerComponentImplementation(Class JavaDoc compImpl) throws PicoRegistrationException {
238             assertEquals(HashMap JavaDoc.class, compImpl);
239             return super.registerComponentImplementation(ArrayList JavaDoc.class);
240         }
241     }
242
243     public static class BarDecoratingPicoContainer extends AbstractDelegatingMutablePicoContainer {
244         public BarDecoratingPicoContainer(MutablePicoContainer delegate) {
245             super(delegate);
246         }
247         public MutablePicoContainer makeChildContainer() {
248             return null;
249         }
250         public ComponentAdapter registerComponentImplementation(Class JavaDoc compImpl) throws PicoRegistrationException {
251             assertEquals(Vector JavaDoc.class, compImpl);
252             return super.registerComponentImplementation(HashMap JavaDoc.class);
253         }
254     }
255
256
257 }
258
Popular Tags