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                                                          *
009     *****************************************************************************/
010    package org.nanocontainer.integrationkit;
011    
012    import org.jmock.Mock;
013    import org.jmock.MockObjectTestCase;
014    import org.picocontainer.MutablePicoContainer;
015    import org.picocontainer.PicoContainer;
016    import org.picocontainer.Startable;
017    import org.picocontainer.defaults.DefaultPicoContainer;
018    import org.picocontainer.defaults.ObjectReference;
019    import org.picocontainer.defaults.SimpleReference;
020    
021    /**
022     * @author Aslak Hellesøy
023     * @version $Revision: 1631 $
024     */
025    public class LifecycleContainerBuilderTestCase extends MockObjectTestCase {
026        public void testBuildContainerCreatesANewChildContainerAndStartsItButNotTheParent() {
027            final Mock childStartable = mock(Startable.class);
028            childStartable.expects(once()).method("start").withNoArguments();
029            childStartable.expects(once()).method("stop").withNoArguments();
030    
031            ContainerComposer containerComposer = new ContainerComposer() {
032                public void composeContainer(MutablePicoContainer container, Object assemblyScope) {
033                    container.registerComponentInstance(childStartable.proxy());
034                }
035            };
036            LifecycleContainerBuilder builder = new DefaultLifecycleContainerBuilder(containerComposer);
037    
038            ObjectReference parentRef = new SimpleReference();
039            MutablePicoContainer parent = new DefaultPicoContainer();
040    
041            Mock parentStartable = mock(Startable.class);
042            parent.registerComponentInstance(parentStartable.proxy());
043            parentRef.set(parent);
044    
045            ObjectReference childRef = new SimpleReference();
046    
047            builder.buildContainer(childRef, parentRef, null, true);
048            PicoContainer childContainer = (PicoContainer) childRef.get();
049            //PicoContainer.getParent() is now ImmutablePicoContainer
050            assertNotSame(parent, childContainer.getParent());
051    
052            builder.killContainer(childRef);
053        }
054    
055    }