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 *****************************************************************************/
010package org.picocontainer.tck;
011
012import static org.junit.Assert.assertEquals;
013import static org.junit.Assert.assertFalse;
014import static org.junit.Assert.assertSame;
015import static org.junit.Assert.assertTrue;
016
017import java.util.Properties;
018
019import org.junit.Before;
020import org.junit.Test;
021import org.picocontainer.Characteristics;
022import org.picocontainer.ComponentAdapter;
023import org.picocontainer.ComponentFactory;
024import org.picocontainer.DefaultPicoContainer;
025import org.picocontainer.PicoCompositionException;
026import org.picocontainer.lifecycle.NullLifecycleStrategy;
027import org.picocontainer.monitors.NullComponentMonitor;
028import org.picocontainer.testmodel.SimpleTouchable;
029import org.picocontainer.testmodel.Touchable;
030
031/**
032 * @author Aslak Hellesøy
033 */
034public abstract class AbstractComponentFactoryTest {
035
036    protected DefaultPicoContainer picoContainer;
037
038    protected abstract ComponentFactory createComponentFactory();
039
040    @Before
041    public void setUp() throws Exception {
042        picoContainer = new DefaultPicoContainer();
043    }
044
045    @Test public void testEquals() throws PicoCompositionException {
046        ComponentAdapter componentAdapter = createComponentFactory().createComponentAdapter(new NullComponentMonitor(),
047                                                                                            new NullLifecycleStrategy(),
048                                                                                            new Properties(
049                                                                                                Characteristics
050                                                                                                    .CDI),
051                                                                                            Touchable.class,
052                                                                                            SimpleTouchable.class);
053
054        assertEquals(componentAdapter, componentAdapter);
055        assertTrue(!componentAdapter.equals("blah"));
056    }
057
058    @Test public void testRegisterComponent() throws PicoCompositionException {
059        ComponentAdapter componentAdapter =
060            createComponentFactory().createComponentAdapter(new NullComponentMonitor(),
061                                                            new NullLifecycleStrategy(),
062                                                            new Properties(Characteristics
063                                                                .CDI),
064                                                            Touchable.class,
065                                                            SimpleTouchable.class);
066
067        picoContainer.addAdapter(componentAdapter);
068
069        ComponentAdapter adapter = (ComponentAdapter)picoContainer.getComponentAdapters().toArray()[0];
070        assertSame(componentAdapter.getComponentKey(), adapter.getComponentKey());
071    }
072
073    @Test public void testUnregisterComponent() throws PicoCompositionException {
074        ComponentAdapter componentAdapter =
075            createComponentFactory().createComponentAdapter(new NullComponentMonitor(),
076                                                            new NullLifecycleStrategy(),
077                                                            new Properties(Characteristics
078                                                                .CDI),
079                                                            Touchable.class,
080                                                            SimpleTouchable.class);
081
082        picoContainer.addAdapter(componentAdapter);
083        picoContainer.removeComponent(Touchable.class);
084
085        assertFalse(picoContainer.getComponentAdapters().contains(componentAdapter));
086    }
087}