All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.picocontainer.script.xml.XMLContainerBuilderTestCase Maven / Gradle / Ivy

/*****************************************************************************
 * Copyright (C) PicoContainer 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.                                                     *
 *                                                                           *
 * Original code by Aslak Hellesoy and Paul Hammant                          *
 *****************************************************************************/

package org.picocontainer.script.xml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.Collection;

import javax.swing.JButton;

import org.junit.Ignore;
import org.junit.Test;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentFactory;
import org.picocontainer.DefaultPicoContainer;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoBuilder;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoException;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.LifecycleStrategy;
import org.picocontainer.Parameter;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.classname.DefaultClassLoadingPicoContainer;
import org.picocontainer.behaviors.AbstractBehaviorFactory;
import org.picocontainer.behaviors.Caching;
import org.picocontainer.behaviors.Locked;
import org.picocontainer.behaviors.Cached;
import org.picocontainer.containers.EmptyPicoContainer;
import org.picocontainer.injectors.AdaptingInjection;
import org.picocontainer.injectors.ConstructorInjection;
import org.picocontainer.injectors.ConstructorInjector;
import org.picocontainer.injectors.SetterInjection;
import org.picocontainer.injectors.SetterInjector;
import org.picocontainer.lifecycle.StartableLifecycleStrategy;
import org.picocontainer.monitors.NullComponentMonitor;
import org.picocontainer.monitors.WriterComponentMonitor;
import org.picocontainer.script.AbstractScriptedContainerBuilderTestCase;
import org.picocontainer.script.ScriptedPicoContainerMarkupException;
import org.picocontainer.script.TestHelper;
import org.picocontainer.script.testmodel.CustomerEntityImpl;
import org.picocontainer.script.testmodel.DefaultWebServerConfig;
import org.picocontainer.script.testmodel.Entity;
import org.picocontainer.script.testmodel.ListSupport;
import org.picocontainer.script.testmodel.MapSupport;
import org.picocontainer.script.testmodel.OrderEntityImpl;
import org.picocontainer.script.testmodel.WebServerConfig;
import org.picocontainer.script.testmodel.WebServerConfigComp;
import org.picocontainer.script.xml.XMLComponentInstanceFactory;
import org.picocontainer.script.xml.XMLContainerBuilder;
import org.picocontainer.testmodel.SimpleTouchable;
import org.picocontainer.testmodel.Touchable;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import com.thoughtworks.xstream.XStream;

/**
 * @author Paul Hammant
 * @author Aslak Hellesøy
 * @author Jeppe Cramon
 * @author Mauro Talevi
 */
public final class XMLContainerBuilderTestCase extends AbstractScriptedContainerBuilderTestCase {

    //TODO some tests for XMLContainerBuilder that use a classloader that is retrieved at testtime.
    // i.e. not a programatic consequence of this.getClass().getClassLoader()

    @Test public void testCreateSimpleContainer() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "  " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        assertEquals(3, pico.getComponents().size());
        assertNotNull(pico.getComponent(StringBuffer.class));
        assertNotNull(pico.getComponent(DefaultWebServerConfig.class));
        assertNotNull(pico.getComponent("org.picocontainer.script.testmodel.WebServer"));
    }

    @Test public void testCreateSimpleContainerWithExplicitKeysAndParameters() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "  " +
                "  " +
                " 		" +
                " 		" +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        assertEquals(3, pico.getComponents().size());
        assertNotNull(pico.getComponent("aBuffer"));
        assertNotNull(pico.getComponent("org.picocontainer.script.testmodel.WebServerConfig"));
        assertNotNull(pico.getComponent("org.picocontainer.script.testmodel.WebServer"));
    }

    @Test public void testCreateSimpleContainerWithExplicitKeysAndImplicitParameter() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "  " +
                "  " +
                "       " +
                "       " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        assertEquals(3, pico.getComponents().size());
        assertNotNull(pico.getComponent("aBuffer"));
        assertNotNull(pico.getComponent("org.picocontainer.script.testmodel.WebServerConfig"));
        assertNotNull(pico.getComponent("org.picocontainer.script.testmodel.WebServer"));
    }

    @Test public void testNonParameterElementsAreIgnoredInComponentImplementation() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "  " +
                "  " +
                " 		" +
                " 		" +
                " 		" +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        assertEquals(3, pico.getComponents().size());
        assertNotNull(pico.getComponent("aBuffer"));
        assertNotNull(pico.getComponent("org.picocontainer.script.testmodel.WebServerConfig"));
        assertNotNull(pico.getComponent("org.picocontainer.script.testmodel.WebServer"));
    }

    @Test public void testContainerCanHostAChild() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "  " +
                "  " +
                "    " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        assertNotNull(pico.getComponent(DefaultWebServerConfig.class));

        StringBuffer sb = pico.getComponent(StringBuffer.class);
        assertTrue(sb.toString().indexOf("-WebServerImpl") != -1);
    }

    @Test public void testClassLoaderHierarchy() throws IOException {
        File testCompJar = TestHelper.getTestCompJarFile();
        File testCompJar2 = new File(testCompJar.getParentFile(), "TestComp2.jar");
        File notStartableJar = new File(testCompJar.getParentFile(), "NotStartable.jar");

        assertTrue(testCompJar.isFile());
        assertTrue(testCompJar2.isFile());

        Reader script = new StringReader("" +
                "" +
                "  " +
                "    " +
                "      " +
                "    " +
                "  " +
                "  " +
                "  " +
                "    " +
                "      " +
                "      " +
                "    " +
                "    " +
                "    " +
                "  " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object fooTestComp = pico.getComponent("foo");
        assertNotNull("Container should have a 'foo' component", fooTestComp);

        StringBuffer sb = pico.getComponent(StringBuffer.class);
        assertTrue("Container should have instantiated a 'TestComp2' component because it is Startable", sb.toString().indexOf("-TestComp2") != -1);
        // We are using the DefaultLifecycleManager, which only instantiates Startable components, and not non-Startable components.
        assertTrue("Container should NOT have instantiated a 'NotStartable' component because it is NOT Startable", sb.toString().indexOf("-NotStartable") == -1);
    }

    @Test public void testUnknownclassThrowsPicoContainerMarkupException() {
        try {
            Reader script = new StringReader("" +
                    "" +
                    "  " +
                    "");
            buildContainer(script);
            fail();
        } catch (ScriptedPicoContainerMarkupException expected) {
            assertTrue(expected.getCause() instanceof ClassNotFoundException);
        }
    }

    @Test public void testNoImplementationClassThrowsPicoContainerMarkupException() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "");
        try {
            buildContainer(script);
        } catch (ScriptedPicoContainerMarkupException expected) {
            assertEquals("'class' attribute not specified for component-implementation", expected.getMessage());
        }
    }

    @Test public void testConstantParameterWithNoChildElementThrowsPicoContainerMarkupException() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                " 		" +
                "		" +
                "  " +
                "");

        try {
            buildContainer(script);
        } catch (ScriptedPicoContainerMarkupException e) {
            assertEquals("parameter needs a child element", e.getMessage());
        }
    }

    @Test public void testEmptyScriptDoesNotThrowsEmptyCompositionException() {
        Reader script = new StringReader("");
        buildContainer(script);
    }

    @Test public void testCreateContainerFromScriptThrowsSAXException() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                ""); // open instead of close
        try {
            buildContainer(script);
        } catch (ScriptedPicoContainerMarkupException e) {
            assertTrue("SAXException", e.getCause() instanceof SAXException);
        }
    }

    @Test public void testCreateContainerFromNullScriptThrowsNullPointerException() {
        Reader script = null;
        try {
            buildContainer(script);
            fail("NullPointerException expected");
        } catch (NullPointerException expected) {
        }
    }

    @Test public void testShouldThrowExceptionForNonExistantCafClass() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "");
        try {
            buildContainer(script);
            fail();
        } catch (ScriptedPicoContainerMarkupException expected) {
            assertTrue("Message of exception does not contain missing class", expected.getMessage().indexOf("org.picocontainer.script.SomeInexistantFactory") > 0);
        }
    }

    @Test public void testComponentInstanceWithNoChildElementThrowsPicoContainerMarkupException() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "  " +
                "");

        try {
            buildContainer(script);
            fail();
        } catch (ScriptedPicoContainerMarkupException expected) {
            assertEquals("component-instance needs a child element", expected.getMessage());
        }
    }

    @Test public void testComponentInstanceWithFactoryCanBeUsed() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object instance = pico.getComponents().get(0);
        assertNotNull(instance);
        assertTrue(instance instanceof String);
        assertEquals("Hello", instance.toString());
    }

    @Test public void testComponentInstanceWithDefaultFactory() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "	" +
                "		10" +
                "		hello" +
                "	" +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object instance = pico.getComponents().get(0);
        assertNotNull(instance);
        assertTrue(instance instanceof TestBean);
        assertEquals(10, ((TestBean) instance).getFoo());
        assertEquals("hello", ((TestBean) instance).getBar());
    }

    @Test public void testComponentInstanceWithBeanFactory() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "	" +
                "		10" +
                "		hello" +
                "	" +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object instance = pico.getComponents().get(0);
        assertNotNull(instance);
        assertTrue(instance instanceof TestBean);
        assertEquals(10, ((TestBean) instance).getFoo());
        assertEquals("hello", ((TestBean) instance).getBar());
    }

    @Test public void testComponentInstanceWithBeanFactoryAndInstanceThatIsDefinedInContainer() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    " +
                "       " +
                "    " +
                "  " +
                "  " +
                "    " +
                "       false" +
                "       date" +
                "    " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object instance = pico.getComponent(SimpleDateFormat.class);
        assertNotNull(instance);
        assertTrue(instance instanceof SimpleDateFormat);
        SimpleDateFormat format = ((SimpleDateFormat) instance);
        assertFalse(format.isLenient());
        assertEquals(new Date(0), format.get2DigitYearStart());
    }

    @Test public void testComponentInstanceWithKey() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    Hello" +
                "  " +
                "" +
                "  " +
                "    22" +
                "  " +
                "" +
                "  " +
                "    " +
                "      Hello" +
                "      0.88" +
                "    " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        assertEquals("Hello", pico.getComponent("aString"));
        assertEquals((long)22, pico.getComponent("aLong"));
        JButton button = (JButton) pico.getComponent("aButton");
        assertEquals("Hello", button.getText());
        assertEquals(0.88, button.getAlignmentX(), 0.01);
    }

    @Test public void testComponentInstanceWithClassKey() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    " +
                "      " +
                "    " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Map map = pico.getComponent(Map.class);
        assertNotNull(map);
        assertEquals("bar", map.get("foo"));
    }

    @Test public void testComponentInstanceWithFactoryAndKey() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object instance = pico.getComponent("aKey");
        assertNotNull(instance);
        assertTrue(instance instanceof String);
        assertEquals("Hello", instance.toString());
    }

    @Test public void testComponentInstanceWithContainerFactoryAndKey() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    " +
                "  " +
                "  " +
                "    " +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object first = pico.getComponent("firstKey");
        assertNotNull(first);
        assertTrue(first instanceof String);
        assertEquals("Hello", first.toString());
        Object second = pico.getComponent("secondKey");
        assertNotNull(second);
        assertTrue(second instanceof String);
        assertEquals("ContainerHello", second.toString());
    }

    public static class TestFactory implements XMLComponentInstanceFactory {
        public Object makeInstance(PicoContainer pico, Element elem, ClassLoader classLoader) {
            return "Hello";
        }
    }

    public static class ContainerTestFactory implements XMLComponentInstanceFactory {
        public Object makeInstance(PicoContainer pico, Element elem, ClassLoader classLoader) {
            return "ContainerHello";
        }
    }

    @Test public void testInstantiationOfComponentsWithParams() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    localhost" +
                "    8080" +
                "  " +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);
        assertNotNull(pico.getComponent(WebServerConfigComp.class));
        WebServerConfigComp config = pico.getComponent(WebServerConfigComp.class);
        assertEquals("localhost", config.getHost());
        assertEquals(8080, config.getPort());
    }

    @Test public void testInstantiationOfComponentsWithParameterInstancesOfSameComponent() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                " 		" +
                "			" +
                "				10" +
                "				hello1" +
                "			" +
                "		" +
                " 		" +
                "			" +
                "				10" +
                "				hello2" +
                "			" +
                "		" +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);
        assertNotNull(pico.getComponent(TestBeanComposer.class));
        TestBeanComposer composer = pico.getComponent(TestBeanComposer.class);
        assertEquals("bean1", "hello1", composer.getBean1().getBar());
        assertEquals("bean2", "hello2", composer.getBean2().getBar());
    }

    @Test public void testInstantiationOfComponentsWithParameterInstancesOfSameComponentAndBeanFactory() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                " 		" +
                "			" +
                "				10" +
                "				hello1" +
                "			" +
                "		" +
                " 		" +
                "			" +
                "				10" +
                "				hello2" +
                "			" +
                "		" +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);
        assertNotNull(pico.getComponent(TestBeanComposer.class));
        TestBeanComposer composer = pico.getComponent(TestBeanComposer.class);
        assertEquals("bean1", "hello1", composer.getBean1().getBar());
        assertEquals("bean2", "hello2", composer.getBean2().getBar());
    }

    @Test public void testInstantiationOfComponentsWithParameterKeys() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "	" +
                "		10" +
                "		hello1" +
                "	" +
                "  " +
                "  " +
                "	" +
                "		10" +
                "		hello2" +
                "	" +
                "  " +
                "  " +
                " 		" +
                " 		" +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);
        assertNotNull(pico.getComponent(TestBeanComposer.class));
        TestBeanComposer composer = pico.getComponent(TestBeanComposer.class);
        assertEquals("bean1", "hello1", composer.getBean1().getBar());
        assertEquals("bean2", "hello2", composer.getBean2().getBar());
    }

    @Test public void testInstantiationOfComponentsWithComponentAdapter() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "	" +
                "		10" +
                "		hello1" +
                "	" +
                "  " +
                "  " +
                "	" +
                "		10" +
                "		hello2" +
                "	" +
                "  " +
                "  " +
                " 		" +
                " 		" +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);
        assertNotNull(pico.getComponent("beanKey"));
        TestBeanComposer composer = (TestBeanComposer) pico.getComponent("beanKey");
        assertEquals("bean1", "hello1", composer.getBean1().getBar());
        assertEquals("bean2", "hello2", composer.getBean2().getBar());
    }

    @Test public void testComponentAdapterWithSpecifiedFactory() throws IOException {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "	" +
                "		10" +
                "		hello1" +
                "	" +
                "  " +
                "  " +
                "	" +
                "		10" +
                "		hello2" +
                "	" +
                "  " +
                "  " +
                " 		" +
                " 		" +
                "  " +
                "");
        MutablePicoContainer pico = (MutablePicoContainer)buildContainer(script);


        assertNotNull(pico.getComponent("beanKey"));
        TestBeanComposer composer = (TestBeanComposer) pico.getComponent("beanKey");
        assertEquals("bean1", "hello1", composer.getBean1().getBar());
        assertEquals("bean2", "hello2", composer.getBean2().getBar());
    }

    @Test public void testComponentAdapterWithNoKeyUsesTypeAsKey() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);
        ComponentAdapter adapter = pico.getComponentAdapters().iterator().next();
        assertSame(TestBeanComposer.class, adapter.getComponentImplementation());
    }

    @Test public void testComponentAdapterWithNoClassThrowsPicoContainerMarkupException() {
        Reader script = new StringReader("" +
                "" +
                "   " +
                "");
        try {
            buildContainer(script);
            fail();
        } catch (ScriptedPicoContainerMarkupException expected) {
            assertEquals("'class' attribute not specified for component-adapter", expected.getMessage());
        }
    }

    @Test public void testCachingCanBeUnsetAtContainerLevel() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object wsc1 = pico.getComponent(WebServerConfig.class);
        Object wsc2 = pico.getComponent(WebServerConfig.class);

        assertNotSame(wsc1, wsc2);
    }

    @Test public void testCachingCanBeSetRedunadantlyAtContainerLevel() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        Object wsc1 = pico.getComponent(WebServerConfig.class);
        Object wsc2 = pico.getComponent(WebServerConfig.class);

        assertSame(wsc1, wsc2);
    }

    @Test public void testCustomInjectionFactory() throws IOException {
        Reader script = new StringReader("" +
                "" +
                "");

        MutablePicoContainer pico = (MutablePicoContainer) buildContainer(script);

        pico.addComponent(String.class);

        Collection> foo = pico.getComponentAdapters();
        assertEquals(1, foo.size());
        ComponentAdapter o = (ComponentAdapter) foo.toArray()[0];
        assertTrue(o instanceof Cached);
        o = o.findAdapterOfType(SetterInjector.class);
        assertNotNull(o);
        assertTrue(o instanceof SetterInjector);

    }


    @Test public void testComponentMonitorCanBeSpecified() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        pico.getComponent(WebServerConfig.class);
        assertTrue(StaticWriterComponentMonitor.WRITER.toString().length() > 0);
    }

    @Test public void testComponentMonitorCanBeSpecifiedIfCAFIsSpecified() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "");

        PicoContainer pico = buildContainer(script);
        pico.getComponent(WebServerConfig.class);
        assertTrue(StaticWriterComponentMonitor.WRITER.toString().length() > 0);
    }

    @Test public void testAdaptersAlsoUseBehaviorFactory() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);
        WebServerConfig cfg1 = pico.getComponent(WebServerConfig.class);
        WebServerConfig cfg2 = pico.getComponent(WebServerConfig.class);
        assertNotSame("Instances for components registered with a CICA must not be the same", cfg1, cfg2);
    }


    @SuppressWarnings("serial")
    public static class MyComponentFactory extends ConstructorInjection {
        public MyComponentFactory() {
            super();
        }
    }
    @SuppressWarnings("serial")
    public static class MyComponentFactory2 extends AbstractBehaviorFactory {
        public MyComponentFactory2(ComponentFactory delegate) {
            wrap(delegate);
        }
    }
    @SuppressWarnings("serial")
    public static class MyComponentFactory3 extends AbstractBehaviorFactory {
        public MyComponentFactory3(ComponentFactory delegate) {
            wrap(delegate);
        }
    }

    public void BROKEN_testNestedCAFLooksRightinXml() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    " +
                "      " +
                "    " +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);

        String xml = new XStream().toXML(pico).replace('\"','\'');

        assertEquals("\n" +
                "  \n" +
                "  \n" +
                "    \n" +
                "    \n" +
                "      \n" +
                "        \n" +
                "        \n" +
                "      \n" +
                "    \n" +
                "    \n" +
                "    \n" +
                "    \n" +
                "    true\n" +
                "    false\n" +
                "    \n" +
                "    \n" +
                "      \n" +
                "    \n" +
                "    \n" +
                "      \n" +
                "      \n" +
                "    \n" +
                "    \n" +
                "  \n" +
                "", xml);

        // This test suggests that testComponentCanUsePredefinedNestedCAF() is not testing what it hopes to test
    }

    public static class Hello {
    }

    @Test
    public void testComponentCanUsePredefinedNestedCAF() {
        Reader script = new StringReader("" +
                "" +
                "  " +
                "    " +
                "  " +
                "  " +
                "  " +
                "");
        PicoContainer pico = buildContainer(script);
        WebServerConfig cfg1 = pico.getComponent(WebServerConfig.class);
        WebServerConfig cfg2 = pico.getComponent(WebServerConfig.class);
        assertNotSame("Instances for components registered with a CICA must not be the same", cfg1, cfg2);
        assertFalse("Instance exposes only interface", cfg1 instanceof DefaultWebServerConfig);
        Hello hello = pico.getComponent(Hello.class);
        assertNotNull(hello);
    }

    @Test public void testChainOfWrappedComponents() {

       Reader script = new StringReader("" +
                "\n" +
               "   " +
               "   " +
                "");

        PicoContainer pico = buildContainer(script);

        // decorators are fairly dirty - they replace a very select implementation in this TestCase.
        assertNotNull(pico.getComponent(Touchable.class));
    }
    
    @Test
    public void testInheritanceOfBehaviorsFromParentContainer() {
    	Reader comparison = new StringReader("" +
        		"\n" +
                "  " +
                ""
        	);    	

    	MutablePicoContainer parent = new PicoBuilder().withLocking().build();
    	PicoContainer comparisonPico = buildContainer(new XMLContainerBuilder(comparison, getClass().getClassLoader()), parent, "SOME_SCOPE");
    	//Verify not locking by default
    	//assertTrue(comparisonPico.getComponent(DefaultWebServerConfig.class) != comparisonPico.getComponent(DefaultWebServerConfig.class));
    	assertNull(comparisonPico.getComponentAdapter(DefaultWebServerConfig.class).findAdapterOfType(Locked.class));
    	
    	//Verify parent caching propagates to child.
    	Reader script = new StringReader("" +
    		"\n" +
            "  " +
            ""
    	);
    	
    	parent = new PicoBuilder().withLocking().build();
    	PicoContainer pico = buildContainer(new XMLContainerBuilder(script, getClass().getClassLoader()), parent, "SOME_SCOPE");
    	
    	assertNotNull(pico.getComponentAdapter(DefaultWebServerConfig.class).findAdapterOfType(Locked.class));
    }
    
    
    @Test public void testListSupport() {

        Reader script = new StringReader("" +
                 "\n" +
                "   " +
                "       " +
                "   " +               
                "   " +
                "   " +
                 "");

         PicoContainer pico = buildContainer(script);
         
         ListSupport listSupport = pico.getComponent(ListSupport.class);

         assertNotNull(listSupport);
         assertNotNull(listSupport.getAListOfEntityObjects());
         assertEquals(2, listSupport.getAListOfEntityObjects().size());

         Entity entity1 = listSupport.getAListOfEntityObjects().get(0);
         Entity entity2 = listSupport.getAListOfEntityObjects().get(1);
         
         assertNotNull(entity1);
         assertEquals(CustomerEntityImpl.class, entity1.getClass());
         
         assertNotNull(entity2);
         assertEquals(OrderEntityImpl.class, entity2.getClass());
     }
    
    @Test public void testMapSupport() {
        
        Reader script = new StringReader("" +
                "\n" +
               "   " +
               "       " +
               "   " +               
               "   " +
               "   " +
                "");
        
        PicoContainer pico = buildContainer(script);
        
        MapSupport mapSupport = pico.getComponent(MapSupport.class);

        assertNotNull(mapSupport);
        assertNotNull(mapSupport.getAMapOfEntities());
        assertEquals(2, mapSupport.getAMapOfEntities().size());

        Map aMapOfEntities = mapSupport.getAMapOfEntities();
        
        Entity entity1 = aMapOfEntities.get("customer");
        Entity entity2 = aMapOfEntities.get("order");
        
        assertNotNull(entity1);
        assertEquals(CustomerEntityImpl.class, entity1.getClass());
        
        assertNotNull(entity2);
        assertEquals(OrderEntityImpl.class, entity2.getClass()); 
    }
    
    @Test public void testNoEmptyCollectionWithComponentKeyTypeFailure() {

        Reader script = new StringReader("" +
                 "\n" +
                "   " +
                "       " +
                "   " +               
                "   " +
                "   " +
                 "");

        try {
            buildContainer(script);
            fail("Thrown " + PicoException.class.getName() + " expected");
        } catch (final PicoException e) {
            assertTrue(e.getMessage().indexOf("one or both of the emptyCollection")>0);
        }
     }
    
    @Test public void testNoComponentValueTypeWithComponentKeyTypeFailure() {

        Reader script = new StringReader("" +
                 "\n" +
                "   " +
                "       " +
                "   " +               
                "   " +
                "   " +
                 "");

        try {
            buildContainer(script);
            fail("Thrown " + PicoException.class.getName() + " expected");
        } catch (final PicoException e) {
            assertTrue(e.getMessage().indexOf("but one or both of the emptyCollection")>0);
        }
     }   
    
    @Test public void testNoEmptyCollectionWithComponentValueTypeFailure() {

        Reader script = new StringReader("" +
                 "\n" +
                "   " +
                "       " +
                "   " +               
                "   " +
                "   " +
                 "");

        try {
            buildContainer(script);
            fail("Thrown " + PicoException.class.getName() + " expected");
        } catch (final PicoException e) {
            System.out.println(e);
            
            assertTrue(e.getMessage().indexOf("but the emptyCollection () was empty or null")>0);
        }
     }
    
    @Test
    public void testParameterZero() {
    	Reader script = new StringReader("" + 
                "\n" +
	    			" \n" +	
	    			"    \n" +
	    			" \n" +
	    			" \n" +
	    			"    \n" +
	    			"\n" +
                "\n"
    	);
    	PicoContainer pico = buildContainer(script);
    	assertNotNull(pico.getComponent(java.util.List.class));
    	assertNotNull(pico.getComponent(java.util.Set.class));
    }

    private PicoContainer buildContainer(Reader script) {
        return buildContainer(new XMLContainerBuilder(script, getClass().getClassLoader()), null, "SOME_SCOPE");
    }

    static public final class StaticWriterComponentMonitor extends WriterComponentMonitor {
        static final Writer WRITER = new StringWriter();

        public StaticWriterComponentMonitor() {
            super(WRITER);
        }

    }

    // TODO: Move this into pico-tck 
    public static class WrapsTouchable implements Touchable {
        private final Touchable wrapped;
        
        public WrapsTouchable(final Touchable wrapped) {
            this.wrapped = wrapped;
        }

        public void touch() {
            this.wrapped.touch();
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy