    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     * Idea by Rachel Davies, Original code by Stacy Curl                        *
    009     *****************************************************************************/
    010
    011    package org.picocontainer.defaults;
    012
    013    import static org.junit.Assert.assertEquals;
    014    import static org.junit.Assert.assertFalse;
    015    import static org.junit.Assert.assertNull;
    016    import static org.junit.Assert.assertTrue;
    017
    018    import org.junit.Before;
    019    import org.junit.Test;
    020    import org.picocontainer.ComponentAdapter;
    021    import org.picocontainer.DefaultPicoContainer;
    022    import org.picocontainer.PicoCompositionException;
    023    import org.picocontainer.injectors.ConstructorInjector;
    024    import org.picocontainer.lifecycle.NullLifecycleStrategy;
    025    import org.picocontainer.monitors.NullComponentMonitor;
    026    import org.picocontainer.testmodel.AlternativeTouchable;
    027    import org.picocontainer.testmodel.SimpleTouchable;
    028    import org.picocontainer.testmodel.Touchable;
    029
    030    public class DefaultComponentRegistryTestCase {
    031        private DefaultPicoContainer picoContainer;
    032
    033        @Before
    034        public void setUp() throws Exception {
    035            picoContainer = new DefaultPicoContainer();
    036        }
    037
    038        @Test public void testRegisterComponent() throws PicoCompositionException {
    039            ComponentAdapter componentAdapter = createComponentAdapter();
    040            picoContainer.addAdapter(componentAdapter);
    041            assertTrue(picoContainer.getComponentAdapters().contains(componentAdapter));
    042        }
    043
    044        @Test public void testUnregisterComponent() throws PicoCompositionException {
    045            ComponentAdapter componentAdapter = createComponentAdapter();
    046            picoContainer.addAdapter(componentAdapter);
    047            picoContainer.removeComponent(Touchable.class);
    048            assertFalse(picoContainer.getComponentAdapters().contains(componentAdapter));
    049        }
    050
    051        @Test public void testCannotInstantiateAnUnregisteredComponent() throws PicoCompositionException {
    052            ComponentAdapter componentAdapter = createComponentAdapter();
    053            picoContainer.addAdapter(componentAdapter);
    054            picoContainer.getComponents();
    055            picoContainer.removeComponent(Touchable.class);
    056
    057            assertTrue(picoContainer.getComponents().isEmpty());
    058        }
    059
    060        @Test public void testCanInstantiateReplacedComponent() throws PicoCompositionException {
    061            ComponentAdapter componentAdapter = createComponentAdapter();
    062            picoContainer.addAdapter(componentAdapter);
    063            picoContainer.getComponents();
    064            picoContainer.removeComponent(Touchable.class);
    065
    066            picoContainer.addComponent(Touchable.class, AlternativeTouchable.class);
    067
    068            assertEquals("Container should container 1 addComponent",
    069                    1, picoContainer.getComponents().size());
    070        }
    071
    072        @Test public void testUnregisterAfterInstantiateComponents() throws PicoCompositionException {
    073            ComponentAdapter componentAdapter = createComponentAdapter();
    074            picoContainer.addAdapter(componentAdapter);
    075            picoContainer.getComponents();
    076            picoContainer.removeComponent(Touchable.class);
    077            assertNull(picoContainer.getComponent(Touchable.class));
    078        }
    079
    080        @Test public void testReplacedInstantiatedComponentHasCorrectClass() throws PicoCompositionException {
    081            ComponentAdapter componentAdapter = createComponentAdapter();
    082            picoContainer.addAdapter(componentAdapter);
    083            picoContainer.getComponents();
    084            picoContainer.removeComponent(Touchable.class);
    085
    086            picoContainer.addComponent(Touchable.class, AlternativeTouchable.class);
    087            Object component = picoContainer.getComponents().iterator().next();
    088
    089            assertEquals(AlternativeTouchable.class, component.getClass());
    090        }
    091
    092        private ComponentAdapter createComponentAdapter() throws PicoCompositionException {
    093            return new ConstructorInjector(Touchable.class, SimpleTouchable.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false);
    094        }
    095    }