1 10 package org.nanocontainer.script.xml; 11 12 import org.nanocontainer.script.AbstractScriptedContainerBuilderTestCase; 13 import org.nanocontainer.testmodel.DefaultWebServerConfig; 14 import org.nanocontainer.testmodel.ThingThatTakesParamsInConstructor; 15 import org.nanocontainer.testmodel.WebServerImpl; 16 import org.picocontainer.PicoContainer; 17 import org.picocontainer.defaults.DecoratingComponentAdapter; 18 19 import java.io.Reader ; 20 import java.io.StringReader ; 21 22 public class XStreamContainerBuilderTestCase extends AbstractScriptedContainerBuilderTestCase { 23 24 public void testContainerBuilding() { 25 26 Reader script = new StringReader ("" + 27 "<container>" + 28 " <instance key='foo'>" + 29 " <string>foo bar</string>" + 30 " </instance>" + 31 " <instance key='bar'>" + 32 " <int>239</int>" + 33 " </instance>" + 34 " <instance>" + 35 " <org.nanocontainer.testmodel.DefaultWebServerConfig>" + 36 " <port>555</port>" + 37 " </org.nanocontainer.testmodel.DefaultWebServerConfig>" + 38 " </instance>" + 39 " <implementation class='org.nanocontainer.testmodel.WebServerImpl'>" + 40 " <dependency class='org.nanocontainer.testmodel.DefaultWebServerConfig'/>" + 41 " </implementation>" + 42 " <implementation key='konstantin needs beer' class='org.nanocontainer.testmodel.ThingThatTakesParamsInConstructor'>" + 43 " <constant>" + 44 " <string>it's really late</string>" + 45 " </constant>" + 46 " <constant>" + 47 " <int>239</int>" + 48 " </constant>" + 49 " </implementation>" + 50 "</container>"); 51 52 PicoContainer pico = buildContainer(new XStreamContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE"); 53 assertEquals(5, pico.getComponentInstances().size()); 54 assertEquals("foo bar", pico.getComponentInstance("foo")); 55 assertEquals(new Integer (239), pico.getComponentInstance("bar")); 56 assertEquals(555, ((DefaultWebServerConfig) pico.getComponentInstance(DefaultWebServerConfig.class)).getPort()); 57 58 assertNotNull(pico.getComponentInstanceOfType(WebServerImpl.class)); 59 assertNotNull(pico.getComponentInstanceOfType(ThingThatTakesParamsInConstructor.class)); 60 assertSame(pico.getComponentInstance("konstantin needs beer"), pico.getComponentInstanceOfType(ThingThatTakesParamsInConstructor.class)); 61 assertEquals("it's really late239", ((ThingThatTakesParamsInConstructor) pico.getComponentInstance("konstantin needs beer")).getValue()); 62 } 63 64 public void testComponentAdapterInjection() throws Throwable { 65 Reader script = new StringReader ("<container>" + 66 "<adapter key='testAdapter'>" + 67 "<instance key='firstString'>" + 68 "<string>bla bla</string>" + 69 "</instance>" + 70 "<instance key='secondString' >" + 71 "<string>glarch</string>" + 72 "</instance>" + 73 "<instance key='justInt'>" + 74 "<int>777</int>" + 75 "</instance>" + 76 "<implementation key='testAdapter' class='org.nanocontainer.script.xml.TestComponentAdapter'>" + 77 "<dependency key='firstString'/>" + 78 "<dependency key='justInt'/>" + 79 "<dependency key='secondString'/>" + 80 "</implementation>" + 81 "</adapter>" + 82 "</container>"); 83 84 PicoContainer pico = buildContainer(new XStreamContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE"); 85 TestComponentAdapter tca = (TestComponentAdapter) pico.getComponentAdapter(TestComponentAdapter.class); 86 assertNotNull(tca); 87 } 88 89 public void testInstantiationOfComponentsWithInstancesOfSameComponent() throws Exception { 90 Reader script = new StringReader ("" + 91 "<container>" + 92 " <instance key='bean1'>" + 93 " <org.nanocontainer.script.xml.TestBean>" + 94 " <foo>10</foo>" + 95 " <bar>hello1</bar>" + 96 " </org.nanocontainer.script.xml.TestBean>" + 97 " </instance>" + 98 " <instance key='bean2'>" + 99 " <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 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 151 | Popular Tags |