KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > bsh > BeanShellContainerBuilderTestCase


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
11 /**
12  * @author Aslak Hellesøy
13  * @author Michael Rimov
14  * @version $Revision: 3144 $
15  */

16 package org.nanocontainer.script.bsh;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24
25 import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase;
26 import org.picocontainer.PicoContainer;
27 import org.picocontainer.defaults.DefaultPicoContainer;
28
29 /**
30  * @author Aslak Hellesøy
31  * @version $Revision: 3144 $
32  */

33 public class BeanShellContainerBuilderTestCase extends AbstractScriptedContainerBuilderTestCase {
34
35     public void testContainerCanBeBuiltWithParent() {
36         Reader JavaDoc script = new StringReader JavaDoc("" +
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         //PicoContainer.getParent() is now ImmutablePicoContainer
42
assertNotSame(parent, pico.getParent());
43         assertEquals("BeanShell", pico.getComponentInstance("hello"));
44     }
45
46     /**
47      * The following test requires the next beta of beanshell to work.
48      * @todo Upgrade Maven1 Builds to Beanshell 2.0b4 so that this test case
49      * can run.
50      * @throws IOException
51      */

52     public void testWithParentClassPathPropagatesWithToBeanShellInterpreter()throws IOException JavaDoc {
53         Reader JavaDoc script = new StringReader JavaDoc("" +
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 JavaDoc testCompJar = new File JavaDoc(System.getProperty("testcomp.jar"));
67         URLClassLoader JavaDoc classLoader = new URLClassLoader JavaDoc(new URL JavaDoc[] {testCompJar.toURL()}, this.getClass().getClassLoader());
68         Class JavaDoc testComp = null;
69         PicoContainer parent = new DefaultPicoContainer();
70
71         try {
72             testComp = classLoader.loadClass("TestComp");
73         } catch (ClassNotFoundException JavaDoc 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 JavaDoc testCompInstance = pico.getComponentInstance(testComp);
81         assertNotNull(testCompInstance);
82         assertEquals(testCompInstance.getClass().getName(), testComp.getName());
83
84     }
85
86 }
87
Popular Tags