    001    /*****************************************************************************
    002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Original code by Aslak Hellesoy and Paul Hammant                          *
    009     *****************************************************************************/
    010    package org.nanocontainer.script.xml;
    011
    012    import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase;
    013    import org.nanocontainer.testmodel.DefaultWebServerConfig;
    014    import org.nanocontainer.testmodel.ThingThatTakesParamsInConstructor;
    015    import org.nanocontainer.testmodel.WebServerImpl;
    016    import org.picocontainer.PicoContainer;
    017    import org.picocontainer.defaults.DecoratingComponentAdapter;
    018
    019    import java.io.Reader;
    020    import java.io.StringReader;
    021
    022    public class XStreamContainerBuilderTestCase extends AbstractScriptedContainerBuilderTestCase {
    023
    024        public void testContainerBuilding() {
    025
    026            Reader script = new StringReader("" +
    027                    "<container>" +
    028                    "    <instance key='foo'>" +
    029                    "       <string>foo bar</string>" +
    030                    "    </instance>" +
    031                    "    <instance key='bar'>" +
    032                    "       <int>239</int>" +
    033                    "    </instance>" +
    034                    "    <instance>" +
    035                    "       <org.nanocontainer.testmodel.DefaultWebServerConfig>" +
    036                    "                       <port>555</port>" +
    037                    "       </org.nanocontainer.testmodel.DefaultWebServerConfig>" +
    038                    "    </instance>" +
    039                    "        <implementation class='org.nanocontainer.testmodel.WebServerImpl'>" +
    040                    "               <dependency class='org.nanocontainer.testmodel.DefaultWebServerConfig'/>" +
    041                    "        </implementation>" +
    042                    "        <implementation key='konstantin needs beer' class='org.nanocontainer.testmodel.ThingThatTakesParamsInConstructor'>" +
    043                    "               <constant>" +
    044                    "                       <string>it's really late</string>" +
    045                    "               </constant>" +
    046                    "               <constant>" +
    047                    "                       <int>239</int>" +
    048                    "               </constant>" +
    049                    "        </implementation>" +
    050                    "</container>");
    051
    052            PicoContainer pico = buildContainer(new XStreamContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
    053            assertEquals(5, pico.getComponentInstances().size());
    054            assertEquals("foo bar", pico.getComponentInstance("foo"));
    055            assertEquals(new Integer(239), pico.getComponentInstance("bar"));
    056            assertEquals(555, ((DefaultWebServerConfig) pico.getComponentInstance(DefaultWebServerConfig.class)).getPort());
    057
    058            assertNotNull(pico.getComponentInstanceOfType(WebServerImpl.class));
    059            assertNotNull(pico.getComponentInstanceOfType(ThingThatTakesParamsInConstructor.class));
    060            assertSame(pico.getComponentInstance("konstantin needs beer"), pico.getComponentInstanceOfType(ThingThatTakesParamsInConstructor.class));
    061            assertEquals("it's really late239", ((ThingThatTakesParamsInConstructor) pico.getComponentInstance("konstantin needs beer")).getValue());
    062        }
    063
    064        public void testComponentAdapterInjection() throws Throwable {
    065            Reader script = new StringReader("<container>" +
    066                    "<adapter key='testAdapter'>" +
    067                    "<instance key='firstString'>" +
    068                    "<string>bla bla</string>" +
    069                    "</instance>" +
    070                    "<instance key='secondString' >" +
    071                    "<string>glarch</string>" +
    072                    "</instance>" +
    073                    "<instance key='justInt'>" +
    074                    "<int>777</int>" +
    075                    "</instance>" +
    076                    "<implementation key='testAdapter' class='org.nanocontainer.script.xml.TestComponentAdapter'>" +
    077                    "<dependency key='firstString'/>" +
    078                    "<dependency key='justInt'/>" +
    079                    "<dependency key='secondString'/>" +
    080                    "</implementation>" +
    081                    "</adapter>" +
    082                    "</container>");
    083
    084            PicoContainer pico = buildContainer(new XStreamContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
    085            TestComponentAdapter tca = (TestComponentAdapter) pico.getComponentAdapter(TestComponentAdapter.class);
    086            assertNotNull(tca);
    087        }
    088
    089        public void testInstantiationOfComponentsWithInstancesOfSameComponent() throws Exception {
    090            Reader script = new StringReader("" +
    091                    "<container>" +
    092                    "  <instance key='bean1'>" +
    093                    "       <org.nanocontainer.script.xml.TestBean>" +
    094                    "               <foo>10</foo>" +
    095                    "               <bar>hello1</bar>" +
    096                    "       </org.nanocontainer.script.xml.TestBean>" +
    097                    "  </instance>" +
    098                    "  <instance key='bean2'>" +
    099                    "       <org.nanocontainer.script.xml.TestBean>" +
    100                    "               <foo>10</foo>" +
    101                    "               <bar>hello2</bar>" +
    102                    "       </org.nanocontainer.script.xml.TestBean>" +
    103                    "  </instance>" +
    104                    "  <implementation class='org.nanocontainer.script.xml.TestBeanComposer'>" +
    105                    "               <dependency key='bean1'/>" +
    106                    "               <dependency key='bean2'/>" +
    107                    "  </implementation>" +
    108                    "</container>");
    109            PicoContainer pico = buildContainer(new XStreamContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
    110            assertNotNull(pico.getComponentInstance(TestBeanComposer.class));
    111            TestBeanComposer composer = (TestBeanComposer) pico.getComponentInstance(TestBeanComposer.class);
    112            assertEquals("bean1", "hello1", composer.getBean1().getBar());
    113            assertEquals("bean2", "hello2", composer.getBean2().getBar());
    114        }
    115
    116        // do not know how to extract parameters off adapter....
    117        public void testThatDependencyUsesClassAsKey() {
    118            Reader script = new StringReader("" +
    119            "<container>" +
    120            "   <implementation class='java.lang.String'/>" +
    121            "   <implementation key='foo' class='org.nanocontainer.script.xml.TestBean'>" +
    122            "       <dependency class='java.lang.String'/>" +
    123            "   </implementation>" +
    124            "</container>"
    125            );
    126
    127           PicoContainer pico = buildContainer(new XStreamContainerBuilder(script, getClass().getClassLoader()), null,null);
    128           DecoratingComponentAdapter adapter = (DecoratingComponentAdapter)pico.getComponentAdapter("foo");
    129           assertNotNull(adapter);
    130        }
    131
    132
    133        public void testDefaultContsructorRegistration() throws Exception {
    134
    135            Reader script = new StringReader(
    136            "<container>" +
    137            "   <implementation class='org.nanocontainer.script.xml.TestBean' constructor='default'/>" +
    138            "   <instance>" +
    139            "       <string>blurge</string>" +
    140            "   </instance>" +
    141            "</container>"
    142             );
    143
    144
    145            PicoContainer pico = buildContainer(new XStreamContainerBuilder(script, getClass().getClassLoader()), null,null);
    146            TestBean bean = (TestBean)pico.getComponentInstanceOfType(TestBean.class);
    147            assertEquals("default",bean.getConstructorCalled());
    148        }
    149    }
    150