    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                                                          *
    009     *****************************************************************************/
    010    package org.picocontainer.defaults;
    011
    012    import static org.junit.Assert.assertEquals;
    013    import static org.junit.Assert.assertNotNull;
    014
    015    import org.junit.Test;
    016    import org.picocontainer.DefaultPicoContainer;
    017    import org.picocontainer.MutablePicoContainer;
    018    import org.picocontainer.parameters.ComponentParameter;
    019    import org.picocontainer.testmodel.AlternativeTouchable;
    020    import org.picocontainer.testmodel.DependsOnTouchable;
    021    import org.picocontainer.testmodel.SimpleTouchable;
    022    import org.picocontainer.testmodel.Touchable;
    023
    024    /**
    025     * @author Paul Hammant
    026     * @author Aslak Helles&oslash;y
    027     * @author Rafal Krzewski
    028     */
    029    public class ChildContainerTestCase {
    030
    031        @Test public void testParentContainerWithComponentWithEqualKeyShouldBeShadowedByChild() throws Exception {
    032            DefaultPicoContainer parent = new DefaultPicoContainer();
    033            DefaultPicoContainer child = new DefaultPicoContainer(parent);
    034
    035            parent.addComponent("key", AlternativeTouchable.class);
    036            child.addComponent("key", SimpleTouchable.class);
    037            child.addComponent(DependsOnTouchable.class);
    038
    039            DependsOnTouchable dot = child.getComponent(DependsOnTouchable.class);
    040            assertEquals(SimpleTouchable.class, dot.getTouchable().getClass());
    041        }
    042
    043        @Test public void testParentComponentRegisteredAsClassShouldBePreffered() throws Exception {
    044            DefaultPicoContainer parent = new DefaultPicoContainer();
    045            DefaultPicoContainer child = new DefaultPicoContainer(parent);
    046
    047            parent.addComponent(Touchable.class, AlternativeTouchable.class);
    048            child.addComponent("key", SimpleTouchable.class);
    049            child.addComponent(DependsOnTouchable.class);
    050
    051            DependsOnTouchable dot = child.getComponent(DependsOnTouchable.class);
    052            assertEquals(AlternativeTouchable.class, dot.getTouchable().getClass());
    053        }
    054
    055        @Test public void testResolveFromParentByType() {
    056            MutablePicoContainer parent = new DefaultPicoContainer();
    057            parent.addComponent(Touchable.class, SimpleTouchable.class);
    058
    059            MutablePicoContainer child = new DefaultPicoContainer(parent);
    060            child.addComponent(DependsOnTouchable.class);
    061
    062            assertNotNull(child.getComponent(DependsOnTouchable.class));
    063        }
    064
    065        @Test public void testResolveFromParentByKey() {
    066            MutablePicoContainer parent = new DefaultPicoContainer();
    067            parent.addComponent(Touchable.class, SimpleTouchable.class);
    068
    069            MutablePicoContainer child = new DefaultPicoContainer(parent);
    070            child.addComponent(DependsOnTouchable.class, DependsOnTouchable.class,
    071                               new ComponentParameter(Touchable.class));
    072
    073            assertNotNull(child.getComponent(DependsOnTouchable.class));
    074        }
    075
    076        @Test public void testResolveFromGrandParentByType() {
    077            MutablePicoContainer grandParent = new DefaultPicoContainer();
    078            grandParent.addComponent(Touchable.class, SimpleTouchable.class);
    079
    080            MutablePicoContainer parent = new DefaultPicoContainer(grandParent);
    081
    082            MutablePicoContainer child = new DefaultPicoContainer(parent);
    083            child.addComponent(DependsOnTouchable.class);
    084
    085            assertNotNull(child.getComponent(DependsOnTouchable.class));
    086        }
    087
    088        @Test public void testResolveFromGrandParentByKey() {
    089            MutablePicoContainer grandParent = new DefaultPicoContainer();
    090            grandParent.addComponent(Touchable.class, SimpleTouchable.class);
    091
    092            MutablePicoContainer parent = new DefaultPicoContainer(grandParent);
    093
    094            MutablePicoContainer child = new DefaultPicoContainer(parent);
    095            child.addComponent(DependsOnTouchable.class, DependsOnTouchable.class,
    096                               new ComponentParameter(Touchable.class));
    097
    098            assertNotNull(child.getComponent(DependsOnTouchable.class));
    099        }
    100    }