    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 Paul Hammant                                             *
    009     *****************************************************************************/
    010
    011    package org.nanocontainer.reflection;
    012
    013    import org.nanocontainer.NanoPicoContainer;
    014    import org.picocontainer.MutablePicoContainer;
    015    import org.picocontainer.PicoContainer;
    016    import org.picocontainer.tck.AbstractPicoContainerTestCase;
    017
    018    /**
    019     * @author Paul Hammant
    020     * @version $Revision: 2962 $
    021     */
    022    public class DefaultNanoPicoContainerTestCase extends AbstractPicoContainerTestCase {
    023
    024        protected MutablePicoContainer createPicoContainer(PicoContainer parent) {
    025            return new DefaultNanoPicoContainer(this.getClass().getClassLoader(), parent);
    026        }
    027
    028        // TODO - go to a Nano TCK?
    029        public void testNamedChildContainerIsAccessible() {
    030            StringBuffer sb = new StringBuffer();
    031            final NanoPicoContainer parent = (NanoPicoContainer) createPicoContainer(null);
    032            parent.registerComponentInstance(sb);
    033            final NanoPicoContainer child = (NanoPicoContainer) parent.makeChildContainer("foo");
    034            child.registerComponentImplementation(LifeCycleMonitoring.class,LifeCycleMonitoring.class);
    035            LifeCycleMonitoring o = (LifeCycleMonitoring) parent.getComponentInstance("foo/*" + LifeCycleMonitoring.class.getName());
    036            assertNotNull(o);
    037        }
    038
    039        // TODO - go to a Nano TCK?
    040        public void testNamedChildContainerIsAccessibleForStringKeys() {
    041            StringBuffer sb = new StringBuffer();
    042            final NanoPicoContainer parent = (NanoPicoContainer) createPicoContainer(null);
    043            parent.registerComponentInstance(sb);
    044            final MutablePicoContainer child = parent.makeChildContainer("foo");
    045            child.registerComponentImplementation("lcm",LifeCycleMonitoring.class);
    046            Object o = parent.getComponentInstance("foo/lcm");
    047            assertNotNull(o);
    048            assertTrue(sb.toString().indexOf("-instantiated") != -1);
    049        }
    050
    051        // TODO - go to a Nano TCK?
    052        public void testNamedChildContainerIsAccessibleForClassKeys() {
    053            StringBuffer sb = new StringBuffer();
    054            final NanoPicoContainer parent = (NanoPicoContainer) createPicoContainer(null);
    055            parent.registerComponentInstance(sb);
    056            final MutablePicoContainer child = parent.makeChildContainer("foo");
    057            child.registerComponentImplementation(LifeCycleMonitoring.class,LifeCycleMonitoring.class);
    058            Object o = parent.getComponentInstance("foo/*" + LifeCycleMonitoring.class.getName());
    059            assertNotNull(o);
    060            assertTrue(sb.toString().indexOf("-instantiated") != -1);
    061        }
    062
    063        public void testMakeRemoveChildContainer() {
    064            final NanoPicoContainer parent = (NanoPicoContainer) createPicoContainer(null);
    065            parent.registerComponentInstance("java.lang.String", "This is a test");
    066            MutablePicoContainer pico = parent.makeChildContainer();
    067            // Verify they are indeed wired together.
    068            assertNotNull(pico.getComponentInstance("java.lang.String"));
    069            boolean result = parent.removeChildContainer(pico);
    070            assertTrue(result);
    071        }
    072
    073        // test methods inherited. This container is otherwise fully compliant.
    074
    075
    076    }