KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > rhino > JavascriptContainerBuilderTestCase


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 *
9  *****************************************************************************/

10 package org.nanocontainer.script.rhino;
11
12 import org.mozilla.javascript.JavaScriptException;
13 import org.nanocontainer.integrationkit.PicoCompositionException;
14 import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase;
15 import org.nanocontainer.testmodel.WebServer;
16 import org.nanocontainer.testmodel.WebServerConfig;
17 import org.nanocontainer.TestHelper;
18 import org.picocontainer.PicoContainer;
19 import org.picocontainer.defaults.DefaultPicoContainer;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.StringReader JavaDoc;
25
26 public class JavascriptContainerBuilderTestCase extends AbstractScriptedContainerBuilderTestCase {
27
28     public void testInstantiateBasicScriptable() throws IOException JavaDoc, ClassNotFoundException JavaDoc, PicoCompositionException, JavaScriptException {
29
30         Reader JavaDoc script = new StringReader JavaDoc("" +
31                 "var pico = new DefaultNanoPicoContainer()\n" +
32                 "pico.registerComponentImplementation(Packages.org.nanocontainer.testmodel.DefaultWebServerConfig)\n");
33
34         PicoContainer pico = buildContainer(new JavascriptContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
35
36         assertNotNull(pico.getComponentInstanceOfType(WebServerConfig.class).getClass());
37     }
38
39     public void testInstantiateWithBespokeComponentAdapter() throws IOException JavaDoc, ClassNotFoundException JavaDoc, PicoCompositionException, JavaScriptException {
40
41         Reader JavaDoc script = new StringReader JavaDoc("" +
42                 "var pico = new DefaultNanoPicoContainer(new ConstructorInjectionComponentAdapterFactory())\n" +
43                 "pico.registerComponentImplementation(Packages.org.nanocontainer.testmodel.DefaultWebServerConfig)\n" +
44                 "pico.registerComponentImplementation(Packages.org.nanocontainer.testmodel.WebServerImpl)\n");
45
46         PicoContainer pico = buildContainer(new JavascriptContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
47
48         Object JavaDoc wsc = pico.getComponentInstanceOfType(WebServerConfig.class);
49         Object JavaDoc ws1 = pico.getComponentInstanceOfType(WebServer.class);
50         Object JavaDoc ws2 = pico.getComponentInstanceOfType(WebServer.class);
51
52         assertNotSame(ws1, ws2);
53
54         assertEquals("ClassLoader should be the same for both components", ws1.getClass().getClassLoader(), wsc.getClass().getClassLoader());
55     }
56
57     public void testClassLoaderHierarchy() throws ClassNotFoundException JavaDoc, IOException JavaDoc, PicoCompositionException, JavaScriptException {
58
59         File JavaDoc testCompJar = TestHelper.getTestCompJarFile();
60         assertTrue(testCompJar.isFile());
61
62         final String JavaDoc testCompJarPath = testCompJar.getCanonicalPath().replace('\\', '/');
63         Reader JavaDoc script = new StringReader JavaDoc(
64                 "var pico = new DefaultNanoPicoContainer()\n" +
65                 "pico.registerComponentImplementation('parentComponent', Packages." + FooTestComp.class.getName() + ")\n" +
66                 "child = new DefaultNanoPicoContainer(pico)\n" +
67                 "pico.addChildContainer(child)\n" +
68                 "url = new File('" + testCompJarPath + "').toURL()\n" +
69                 "child.addClassLoaderURL(url)\n" +
70                 "child.registerComponentImplementation('childComponent','TestComp')\n" +
71                 "pico.registerComponentInstance('wayOfPassingSomethingToTestEnv', child.getComponentInstance('childComponent'))"); // ugly hack for testing
72
JavascriptContainerBuilder builder = new JavascriptContainerBuilder(script, getClass().getClassLoader());
73         PicoContainer pico = buildContainer(builder, null, "SOME_SCOPE");
74
75         Object JavaDoc parentComponent = pico.getComponentInstance("parentComponent");
76
77         Object JavaDoc childComponent = pico.getComponentInstance("wayOfPassingSomethingToTestEnv");
78
79         ClassLoader JavaDoc classLoader1 = parentComponent.getClass().getClassLoader();
80         ClassLoader JavaDoc classLoader2 = childComponent.getClass().getClassLoader();
81         assertNotSame(classLoader1, classLoader2);
82         /*
83         system cl -> loads FooTestComp
84           parent container cl
85             child container cl -> loads TestComp
86         */

87         assertSame(parentComponent.getClass().getClassLoader(), childComponent.getClass().getClassLoader().getParent());
88     }
89
90     public void testRegisterComponentInstance() throws JavaScriptException, IOException JavaDoc {
91         Reader JavaDoc script = new StringReader JavaDoc("" +
92                 "var pico = new DefaultNanoPicoContainer()\n" +
93                 "pico.registerComponentInstance( new Packages." + FooTestComp.class.getName() + "())\n" +
94                 "pico.registerComponentInstance( 'foo', new Packages." + FooTestComp.class.getName() + "())\n");
95
96         PicoContainer pico = buildContainer(new JavascriptContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
97
98         assertEquals(FooTestComp.class, pico.getComponentInstances().get(0).getClass());
99         assertEquals(FooTestComp.class, pico.getComponentInstances().get(1).getClass());
100     }
101
102     public static class FooTestComp {
103
104     }
105
106     public void testContainerCanBeBuiltWithParent() {
107         Reader JavaDoc script = new StringReader JavaDoc("" +
108                 "var pico = new DefaultNanoPicoContainer(parent)\n");
109         PicoContainer parent = new DefaultPicoContainer();
110         PicoContainer pico = buildContainer(new JavascriptContainerBuilder(script, getClass().getClassLoader()), parent, "SOME_SCOPE");
111         //PicoContainer.getParent() is now ImmutablePicoContainer
112
assertNotSame(parent, pico.getParent());
113     }
114 }
115
Popular Tags