1 10 11 16 package org.nanocontainer.script.bsh; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.Reader ; 21 import java.io.StringReader ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 25 import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase; 26 import org.picocontainer.PicoContainer; 27 import org.picocontainer.defaults.DefaultPicoContainer; 28 29 33 public class BeanShellContainerBuilderTestCase extends AbstractScriptedContainerBuilderTestCase { 34 35 public void testContainerCanBeBuiltWithParent() { 36 Reader script = new StringReader ("" + 37 "pico = new org.nanocontainer.reflection.DefaultNanoPicoContainer(parent);\n" + 38 "pico.registerComponentInstance(\"hello\", \"BeanShell\");\n"); 39 PicoContainer parent = new DefaultPicoContainer(); 40 PicoContainer pico = buildContainer(new BeanShellContainerBuilder(script, getClass().getClassLoader()), parent, "SOME_SCOPE"); 41 assertNotSame(parent, pico.getParent()); 43 assertEquals("BeanShell", pico.getComponentInstance("hello")); 44 } 45 46 52 public void testWithParentClassPathPropagatesWithToBeanShellInterpreter()throws IOException { 53 Reader script = new StringReader ("" + 54 "try {\n" + 55 " getClass(\"TestComp\");\n" + 56 "} catch (ClassNotFoundException ex) {\n" + 57 " ClassLoader current = this.getClass().getClassLoader(); \n" + 58 " print(current.toString());\n" + 59 " print(current.getParent().toString());\n" + 60 " print(\"Failed ClassLoading: \");\n" + 61 " ex.printStackTrace();\n" + 62 "}\n" + 63 "pico = new org.nanocontainer.reflection.DefaultNanoPicoContainer(getClass(\"TestComp\").getClassLoader(), parent);\n" + 64 "pico.registerComponentImplementation( \"TestComp\" );\n"); 65 66 File testCompJar = new File (System.getProperty("testcomp.jar")); 67 URLClassLoader classLoader = new URLClassLoader (new URL [] {testCompJar.toURL()}, this.getClass().getClassLoader()); 68 Class testComp = null; 69 PicoContainer parent = new DefaultPicoContainer(); 70 71 try { 72 testComp = classLoader.loadClass("TestComp"); 73 } catch (ClassNotFoundException ex) { 74 ex.printStackTrace(); 75 fail("Unable to load test component from the jar using a url classloader"); 76 } 77 78 PicoContainer pico = buildContainer(new BeanShellContainerBuilder(script, classLoader), parent, "SOME_SCOPE"); 79 assertNotNull(pico); 80 Object testCompInstance = pico.getComponentInstance(testComp); 81 assertNotNull(testCompInstance); 82 assertEquals(testCompInstance.getClass().getName(), testComp.getName()); 83 84 } 85 86 } 87 | Popular Tags |