    001    /*****************************************************************************
    002     * Copyright (C) PicoContainer 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 Paul Hammant                                             *
    009     *****************************************************************************/
    010
    011    package org.picocontainer.classname;
    012
    013    import static org.junit.Assert.assertNotNull;
    014    import static org.junit.Assert.assertTrue;
    015
    016    import java.util.List;
    017    import java.util.Properties;
    018
    019    import org.jmock.Mockery;
    020    import org.jmock.integration.junit4.JMock;
    021    import org.jmock.integration.junit4.JUnit4Mockery;
    022    import org.junit.Test;
    023    import org.junit.runner.RunWith;
    024    import org.picocontainer.Characteristics;
    025    import org.picocontainer.DefaultPicoContainer;
    026    import org.picocontainer.MutablePicoContainer;
    027    import org.picocontainer.PicoContainer;
    028    import org.picocontainer.classname.ClassLoadingPicoContainer;
    029    import org.picocontainer.classname.DefaultClassLoadingPicoContainer;
    030    import org.picocontainer.behaviors.Caching;
    031    import org.picocontainer.monitors.ConsoleComponentMonitor;
    032    import org.picocontainer.tck.AbstractPicoContainerTest;
    033
    034    /**
    035     * @author Paul Hammant
    036     */
    037    @RunWith(JMock.class)
    038    public class DefaultClassLoadingPicoContainerTestCase extends AbstractPicoContainerTest {
    039
    040            private Mockery context = new JUnit4Mockery();
    041
    042
    043        protected MutablePicoContainer createPicoContainer(PicoContainer parent) {
    044            return new DefaultClassLoadingPicoContainer(this.getClass().getClassLoader(), new DefaultPicoContainer(new Caching(), parent));
    045        }
    046
    047        protected Properties[] getProperties() {
    048            return new Properties[] { Characteristics.NONE};
    049        }
    050
    051        @Test public void testNamedChildContainerIsAccessible()  {
    052            StringBuffer sb = new StringBuffer();
    053            final ClassLoadingPicoContainer parent = (ClassLoadingPicoContainer) createPicoContainer(null);
    054            parent.addComponent(sb);
    055            final ClassLoadingPicoContainer child = (ClassLoadingPicoContainer) parent.makeChildContainer("foo");
    056            child.addComponent(LifeCycleMonitoring.class,LifeCycleMonitoring.class);
    057            LifeCycleMonitoring o = (LifeCycleMonitoring) parent.getComponent((Object)("foo/*" + LifeCycleMonitoring.class.getName()));
    058            assertNotNull(o);
    059        }
    060
    061        @Test public void testNamedChildContainerIsAccessibleForStringKeys() {
    062            StringBuffer sb = new StringBuffer();
    063            final ClassLoadingPicoContainer parent = (ClassLoadingPicoContainer) createPicoContainer(null);
    064            parent.addComponent(sb);
    065            final MutablePicoContainer child = parent.makeChildContainer("foo");
    066            child.addComponent("lcm",LifeCycleMonitoring.class);
    067            Object o = parent.getComponent("foo/lcm");
    068            assertNotNull(o);
    069            assertTrue(sb.toString().indexOf("-instantiated") != -1);
    070        }
    071
    072        @Test public void testNamedChildContainerIsAccessibleForClassKeys() {
    073            StringBuffer sb = new StringBuffer();
    074            final ClassLoadingPicoContainer parent = (ClassLoadingPicoContainer) createPicoContainer(null);
    075            parent.addComponent(sb);
    076            final MutablePicoContainer child = parent.makeChildContainer("foo");
    077            child.addComponent(LifeCycleMonitoring.class,LifeCycleMonitoring.class);
    078            Object o = parent.getComponent("foo/*" + LifeCycleMonitoring.class.getName());
    079            assertNotNull(o);
    080            assertTrue(sb.toString().indexOf("-instantiated") != -1);
    081        }
    082
    083        @Test public void testMakeRemoveChildContainer() {
    084            final ClassLoadingPicoContainer parent = (ClassLoadingPicoContainer) createPicoContainer(null);
    085            parent.addComponent("java.lang.String", "This is a test");
    086            MutablePicoContainer pico = parent.makeChildContainer();
    087            // Verify they are indeed wired together.
    088            assertNotNull(pico.getComponent("java.lang.String"));
    089            boolean result = parent.removeChildContainer(pico);
    090            assertTrue(result);
    091        }
    092
    093        // test methods inherited. This container is otherwise fully compliant.
    094        @Test public void testAcceptImplementsBreadthFirstStrategy() {
    095            super.testAcceptImplementsBreadthFirstStrategy();
    096        }
    097
    098        @Test(expected=IllegalStateException.class)
    099        public void testSwapComponentMonitorWithNoComponentMonitorStrategyDelegateThrowsIllegalStateException() {
    100            MutablePicoContainer delegate = context.mock(MutablePicoContainer.class);
    101            //Delegate it twice for effect.
    102            DefaultClassLoadingPicoContainer pico = new DefaultClassLoadingPicoContainer(new DefaultClassLoadingPicoContainer(delegate));
    103            pico.changeMonitor(new ConsoleComponentMonitor());
    104        }
    105
    106        @Test(expected=IllegalStateException.class)
    107        public void testCurrentMonitorWithNoComponentMonitorStrategyDelegateThrowsIllegalStateException() {
    108            Mockery context = new JUnit4Mockery();
    109            MutablePicoContainer delegate = context.mock(MutablePicoContainer.class);
    110            //Delegate it twice for effect.
    111            DefaultClassLoadingPicoContainer pico = new DefaultClassLoadingPicoContainer(new DefaultClassLoadingPicoContainer(delegate));
    112            pico.currentMonitor();
    113        }
    114
    115        protected void addContainers(List expectedList) {
    116            expectedList.add(DefaultClassLoadingPicoContainer.class);
    117            expectedList.add(DefaultPicoContainer.class);
    118        }
    119
    120
    121        protected void addDefaultComponentFactories(List expectedList) {
    122            expectedList.add(Caching.class);
    123        }
    124
    125
    126    }