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