[CVS nano] jython front front end to nanocontainer

Paul Hammant <paul-yCVjj/[email protected]> Sun, 16 Nov 2003 10:19:11 -0600
Newsgroups gmane.comp.java.nanocontainer.cvs
Message-ID <[email protected]>
Commit in nano/nanocontainer on MAIN

project.xml +7 1.11 -> 1.12

src/java/org/nanocontainer/JythonCompositionNanoContainer.java +65 added 1.1

src/test/org/nanocontainer/JythonCompositionNanoContainerTestCase.java +113 added 1.1

+185

2 added + 1 modified, total 3 files

- TODO - this works differently under Maven to IDEA

jython front front end to nanocontainer

----------

nano /nanocontainer

project.xml 1.11 -> 1.12

diff -u -r1.11 -r1.12
--- project.xml 4 Nov 2003 22:52:08 -0000 1.11
+++ project.xml 16 Nov 2003 16:19:10 -0000 1.12
@@ -68,6 +68,13 @@

<version>1.0</version>
</dependency>

+ <dependency>
+ <id>jython</id>
+ <version>2.1</version>
+ </dependency>
+
+
+

</dependencies>

</project>

----------

nano /nanocontainer /src /java /org /nanocontainer

JythonCompositionNanoContainer.java added at 1.1

diff -N JythonCompositionNanoContainer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ JythonCompositionNanoContainer.java 16 Nov 2003 16:19:10 -0000 1.1
@@ -0,0 +1,65 @@

+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.nanocontainer;
+
+import org.nanocontainer.reflection.DefaultReflectionFrontEnd;
+import org.nanocontainer.reflection.ReflectionFrontEnd;
+import org.picocontainer.PicoCompositionException;
+import org.python.util.PythonInterpreter;
+
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+
+/**
+ * @author Paul Hammant
+ * @author Mike Royle
+ */
+public class JythonCompositionNanoContainer extends NanoContainer {
+
+ private PythonInterpreter interpreter;
+ private ReflectionFrontEnd reflectionRootContainer;
+
+
+ public JythonCompositionNanoContainer(Reader script, NanoContainerMonitor monitor)
+ throws PicoCompositionException, ClassNotFoundException, IOException {
+ super(monitor);
+ interpreter = new PythonInterpreter();
+ interpreter.exec("from org.nanocontainer.reflection import DefaultReflectionFrontEnd");
+ reflectionRootContainer = new DefaultReflectionFrontEnd();
+ compose(script);
+ }
+
+ public JythonCompositionNanoContainer(Reader script)
+ throws PicoCompositionException, ClassNotFoundException, IOException {
+ this(script, new NullNanoContainerMonitor());
+ }
+
+ protected void compose(final Reader script) throws IOException, ClassNotFoundException, PicoCompositionException {
+ interpreter.set("rootContainer", reflectionRootContainer);
+ interpreter.execfile(new InputStream() {
+ public int read() throws IOException {
+ return script.read();
+ }
+ });
+ rootContainer = reflectionRootContainer.getPicoContainer();
+ instantiateComponentsBreadthFirst(rootContainer);
+ startComponentsBreadthFirst();
+ }
+
+ public static void main(String[] args) throws Exception {
+ String nanoContainerPy = args[0];
+ if (nanoContainerPy == null) {
+ nanoContainerPy = "composition/components.py";
+ }
+ NanoContainer nano = new JythonCompositionNanoContainer(new FileReader(nanoContainerPy));
+ addShutdownHook(nano);
+ }
+}

----------

nano /nanocontainer /src /test /org /nanocontainer

JythonCompositionNanoContainerTestCase.java added at 1.1

diff -N JythonCompositionNanoContainerTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ JythonCompositionNanoContainerTestCase.java 16 Nov 2003 16:19:10 -0000 1.1
@@ -0,0 +1,113 @@

+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.nanocontainer;
+
+import junit.framework.ComparisonFailure;
+import junit.framework.TestCase;
+import org.nanocontainer.testmodel.WebServerConfig;
+import org.picocontainer.PicoCompositionException;
+import org.picocontainer.defaults.NoSatisfiableConstructorsException;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+
+/**
+ * @author Aslak Helles&oslash;y
+ * @author Paul Hammant
+ * @author Ward Cunningham
+ * @author Mike Royle
+ * @version $Revision: 1.1 $
+ */
+public class JythonCompositionNanoContainerTestCase extends TestCase {
+
+ protected void setUp() throws Exception {
+ MockMonitor.monitorRecorder = "";
+ MockMonitor.allComps = new ArrayList();
+ Xxx.componentRecorder = "";
+ }
+
+ public void testInstantiateBasicRhinoScriptable() throws IOException, ClassNotFoundException, PicoCompositionException {
+
+ NanoContainer nano = new JythonCompositionNanoContainer(new StringReader("" +
+ "rootContainer.registerComponentImplementation('org.nanocontainer.Xxx$A')\n"
+ ), new MockMonitor());
+ nano.stopComponentsDepthFirst();
+ nano.disposeComponentsDepthFirst();
+
+ assertEquals("Should match the expression", "<AA>!A", Xxx.componentRecorder);
+ }
+
+ public void testInstantiateWithChildContainer() throws IOException, ClassNotFoundException, PicoCompositionException {
+
+ // A and C have no no dependancies. B Depends on A.
+
+ NanoContainer nano = new JythonCompositionNanoContainer(new StringReader("" +
+ "rootContainer.registerComponentImplementation('org.nanocontainer.Xxx$A')\n" +
+ "childContainer = DefaultReflectionFrontEnd()\n" +
+ "rootContainer.getPicoContainer().addChild(childContainer.getPicoContainer())\n" +
+ "childContainer.registerComponentImplementation('org.nanocontainer.Xxx$B')\n" +
+ "rootContainer.registerComponentImplementation('org.nanocontainer.Xxx$C')\n"
+ ), new MockMonitor());
+ nano.stopComponentsDepthFirst();
+ nano.disposeComponentsDepthFirst();
+
+ //TODO - this works differently under Maven to IDEA
+ // needs attention!
+
+ try {
+ assertEquals("Should match the expression", "<A<C<BB>C>A>!B!C!A", Xxx.componentRecorder);
+ } catch (ComparisonFailure e) {
+ assertEquals("Should match the expression", "<C<A<BB>A>C>!B!A!C", Xxx.componentRecorder);
+ }
+
+ try {
+ assertEquals("Should match the expression", "*A*B+A_started+B_started+B_stopped+A_stopped+B_disposed+A_disposed", MockMonitor.monitorRecorder);
+ } catch (ComparisonFailure e) {
+ assertEquals("Should match the expression", "*C*B+C_started+B_started+B_stopped+C_stopped+B_disposed+C_disposed", MockMonitor.monitorRecorder);
+ }
+
+
+ }
+
+ public void testInstantiateWithImpossibleComponentDependanciesConsideringTheHierarchy() throws IOException, ClassNotFoundException, PicoCompositionException {
+
+ // A and C have no no dependancies. B Depends on A.
+
+ try {
+ new JythonCompositionNanoContainer(new StringReader("" +
+ "rootContainer.registerComponentImplementation('org.nanocontainer.Xxx$B')\n" +
+ "childContainer = DefaultReflectionFrontEnd()\n" +
+ "rootContainer.getPicoContainer().addChild(childContainer.getPicoContainer())\n" +
+ "childContainer.registerComponentImplementation('org.nanocontainer.Xxx$A')\n" +
+ "rootContainer.registerComponentImplementation('org.nanocontainer.Xxx$C')\n"
+ ), new MockMonitor());
+ fail("Should not have been able to instansiate component tree due to visibility/parent reasons.");
+ } catch (NoSatisfiableConstructorsException e) {
+ }
+ }
+
+ public void testInstantiateWithInlineConfiguration() throws IOException, ClassNotFoundException, PicoCompositionException {
+
+ NanoContainer nano = new JythonCompositionNanoContainer(new StringReader("" +
+ "from org.nanocontainer.testmodel import WebServerConfigBean\n" +
+ "wsc = WebServerConfigBean()\n" +
+ "wsc.setHost('foobar.com')\n" +
+ "wsc.setPort(4321)\n" +
+ "rootContainer.getPicoContainer().registerComponentInstance(wsc)\n" +
+ "rootContainer.registerComponentWithClassKey('org.nanocontainer.testmodel.WebServer','" + XmlCompositionNanoContainerTestCase.OverriddenWebServerImpl.class.getName() + "')\n"
+ ), new MockMonitor());
+
+ assertEquals("WebServerConfigBean and WebServerImpl expected", 2, nano.getRootContainer().getComponentInstances().size());
+ WebServerConfig wsc = (WebServerConfig) nano.getRootContainer().getComponentInstance(WebServerConfig.class);
+ assertEquals("foobar.com", wsc.getHost());
+ assertEquals(4321, wsc.getPort());
+ }
+
+}