    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.parameters.ComponentParameter;
    018    import org.picocontainer.testmodel.DecoratedTouchable;
    019    import org.picocontainer.testmodel.DependsOnTouchable;
    020    import org.picocontainer.testmodel.SimpleTouchable;
    021    import org.picocontainer.testmodel.Touchable;
    022
    023    /**
    024     * @author Thomas Heller
    025     * @author Aslak Helles&oslash;y
    026     */
    027    public class ComponentKeysTestCase {
    028
    029        @Test public void testComponensRegisteredWithClassKeyTakePrecedenceOverOthersWhenThereAreMultipleImplementations() throws Exception {
    030            DefaultPicoContainer pico = new DefaultPicoContainer();
    031            pico.addComponent("default", SimpleTouchable.class);
    032
    033            /**
    034             * By using a class as key, this should take precedence over the other Touchable
    035             */
    036            pico.addComponent(Touchable.class, DecoratedTouchable.class, new ComponentParameter("default"));
    037
    038            Touchable touchable = pico.getComponent(Touchable.class);
    039            assertEquals(DecoratedTouchable.class, touchable.getClass());
    040        }
    041
    042        @Test public void testComponentAdapterResolutionIsFirstLookedForByClassKeyToTheTopOfTheContainerHierarchy() {
    043            DefaultPicoContainer pico = new DefaultPicoContainer();
    044            pico.addComponent("default", SimpleTouchable.class);
    045
    046            // Use the List variant instead, so we get better test coverage.
    047            pico.addComponent(Touchable.class, DecoratedTouchable.class, new ComponentParameter("default"));
    048
    049            DefaultPicoContainer grandChild = new DefaultPicoContainer(new DefaultPicoContainer(new DefaultPicoContainer(pico)));
    050
    051            Touchable touchable = grandChild.getComponent(Touchable.class);
    052            assertEquals(DecoratedTouchable.class, touchable.getClass());
    053
    054        }
    055
    056        @Test public void testComponentKeysFromParentCannotConfuseTheChild() throws Exception {
    057            DefaultPicoContainer pico = new DefaultPicoContainer();
    058            pico.addComponent("test", SimpleTouchable.class);
    059
    060            DefaultPicoContainer child = new DefaultPicoContainer(pico);
    061
    062            child.addComponent("test", DependsOnTouchable.class);
    063
    064            DependsOnTouchable dot = (DependsOnTouchable) child.getComponent("test");
    065
    066            assertNotNull(dot);
    067        }
    068
    069    }