1 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 ; 22 import java.io.IOException ; 23 import java.io.Reader ; 24 import java.io.StringReader ; 25 26 public class JavascriptContainerBuilderTestCase extends AbstractScriptedContainerBuilderTestCase { 27 28 public void testInstantiateBasicScriptable() throws IOException , ClassNotFoundException , PicoCompositionException, JavaScriptException { 29 30 Reader script = new StringReader ("" + 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 , ClassNotFoundException , PicoCompositionException, JavaScriptException { 40 41 Reader script = new StringReader ("" + 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 wsc = pico.getComponentInstanceOfType(WebServerConfig.class); 49 Object ws1 = pico.getComponentInstanceOfType(WebServer.class); 50 Object 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 , IOException , PicoCompositionException, JavaScriptException { 58 59 File testCompJar = TestHelper.getTestCompJarFile(); 60 assertTrue(testCompJar.isFile()); 61 62 final String testCompJarPath = testCompJar.getCanonicalPath().replace('\\', '/'); 63 Reader script = new StringReader ( 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'))"); JavascriptContainerBuilder builder = new JavascriptContainerBuilder(script, getClass().getClassLoader()); 73 PicoContainer pico = buildContainer(builder, null, "SOME_SCOPE"); 74 75 Object parentComponent = pico.getComponentInstance("parentComponent"); 76 77 Object childComponent = pico.getComponentInstance("wayOfPassingSomethingToTestEnv"); 78 79 ClassLoader classLoader1 = parentComponent.getClass().getClassLoader(); 80 ClassLoader classLoader2 = childComponent.getClass().getClassLoader(); 81 assertNotSame(classLoader1, classLoader2); 82 87 assertSame(parentComponent.getClass().getClassLoader(), childComponent.getClass().getClassLoader().getParent()); 88 } 89 90 public void testRegisterComponentInstance() throws JavaScriptException, IOException { 91 Reader script = new StringReader ("" + 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 script = new StringReader ("" + 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 assertNotSame(parent, pico.getParent()); 113 } 114 } 115 | Popular Tags |