KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > jython > JythonContainerBuilderTestCase


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.jython;
11
12 import org.nanocontainer.integrationkit.PicoCompositionException;
13 import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase;
14 import org.nanocontainer.testmodel.WebServer;
15 import org.nanocontainer.testmodel.WebServerImpl;
16 import org.picocontainer.PicoContainer;
17 import org.picocontainer.defaults.DefaultPicoContainer;
18 import org.picocontainer.defaults.UnsatisfiableDependenciesException;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.io.StringReader JavaDoc;
23
24 /**
25  * @author Aslak Hellesøy
26  * @version $Revision: 3144 $
27  */

28 public class JythonContainerBuilderTestCase extends AbstractScriptedContainerBuilderTestCase {
29
30     public void testSimpleConfigurationIsPossible() {
31         Reader JavaDoc script = new StringReader JavaDoc("from org.nanocontainer.testmodel import *\n" +
32                 "pico = DefaultNanoPicoContainer()\n" +
33                 "pico.registerComponentImplementation(WebServerImpl)\n" +
34                 "pico.registerComponentImplementation(DefaultWebServerConfig)\n");
35
36         PicoContainer pico = buildContainer(new JythonContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
37         assertNotNull(pico.getComponentInstanceOfType(WebServer.class));
38     }
39
40     public void testDependenciesAreUnsatisfiableByChildContainers() throws IOException JavaDoc, ClassNotFoundException JavaDoc, PicoCompositionException {
41         try {
42             Reader JavaDoc script = new StringReader JavaDoc("" +
43                     "from org.nanocontainer.testmodel import *\n" +
44                     "pico = DefaultNanoPicoContainer()\n" +
45                     "pico.registerComponentImplementation(WebServerImpl)\n" +
46                     "childContainer = DefaultNanoPicoContainer(pico)\n" +
47                     "childContainer.registerComponentImplementation(DefaultWebServerConfig)\n");
48             PicoContainer pico = buildContainer(new JythonContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
49             pico.getComponentInstanceOfType(WebServer.class);
50             fail();
51         } catch (UnsatisfiableDependenciesException expected) {
52         }
53     }
54
55     public void testDependenciesAreSatisfiableByParentContainer() throws IOException JavaDoc, ClassNotFoundException JavaDoc, PicoCompositionException {
56         Reader JavaDoc script = new StringReader JavaDoc("" +
57                 "from org.nanocontainer.testmodel import *\n" +
58                 "pico = DefaultNanoPicoContainer()\n" +
59                 "pico.registerComponentImplementation(DefaultWebServerConfig)\n" +
60                 "child = pico.makeChildContainer()\n" +
61                 "child.registerComponentImplementation(WebServerImpl)\n" +
62                 "pico.registerComponentInstance('wayOfPassingSomethingToTestEnv', child.getComponentInstance(WebServerImpl))");
63         PicoContainer pico = buildContainer(new JythonContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
64         WebServerImpl wsi = (WebServerImpl) pico.getComponentInstance("wayOfPassingSomethingToTestEnv");
65         assertNotNull(wsi);
66     }
67
68     public void testContainerCanBeBuiltWithParent() {
69         Reader JavaDoc script = new StringReader JavaDoc("" +
70                 "pico = DefaultNanoPicoContainer(parent)\n");
71         PicoContainer parent = new DefaultPicoContainer();
72         PicoContainer pico = buildContainer(new JythonContainerBuilder(script, getClass().getClassLoader()), parent, "SOME_SCOPE");
73         //pico.getParent() is now ImmutablePicoContainer
74
assertNotSame(parent, pico.getParent());
75     }
76
77 }
78
Popular Tags