incubator-composer-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hamm...@apache.org
Subject svn commit: r613775 [11/15] - in /incubator/composer: ./ core/ core/src/ core/src/java/ core/src/java/org/ core/src/java/org/apache/ core/src/java/org/apache/composer/ core/src/java/org/apache/composer/core/ core/src/java/org/apache/composer/core/adapt...
Date Mon, 21 Jan 2008 06:41:50 GMT
Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/CollectionComponentParameterTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/CollectionComponentParameterTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/CollectionComponentParameterTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/CollectionComponentParameterTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,398 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.defaults;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.apache.composer.core.tck.MockFactory.mockeryWithCountingNamingScheme;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.adapters.InstanceAdapter;
+import org.apache.composer.core.behaviors.Caching;
+import org.apache.composer.core.injectors.AbstractInjector;
+import org.apache.composer.core.injectors.ConstructorInjector;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+import org.apache.composer.core.parameters.CollectionComponentParameter;
+import org.apache.composer.core.parameters.ComponentParameter;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+
+/**
+ * @author Aslak Hellesøy
+ * @author Jörg Schaible
+ * @author Mauro Talevi
+ */
+@RunWith(JMock.class)
+public class CollectionComponentParameterTestCase {
+
+	private Mockery mockery = mockeryWithCountingNamingScheme();
+
+	@Test
+	public void testShouldInstantiateArrayOfStrings() {
+		CollectionComponentParameter ccp = new CollectionComponentParameter();
+		final ComponentAdapter componentAdapter = mockery
+				.mock(ComponentAdapter.class);
+		final Container picoContainer = mockery.mock(Container.class);
+		mockery.checking(new Expectations() {
+			{
+				atLeast(1).of(componentAdapter).getComponentKey();
+				will(returnValue("x"));
+				one(picoContainer).getComponentAdapters();
+				will(returnValue(new HashSet()));
+				one(picoContainer).getComponentAdapters(
+						with(equal(String.class)));
+				will(returnValue(Arrays.asList(new InstanceAdapter("y",
+						"Hello", new NullLifecycleStrategy(),
+						new NullComponentMonitor()), new InstanceAdapter("z",
+						"World", new NullLifecycleStrategy(),
+						new NullComponentMonitor()))));
+				one(picoContainer).getComponent(with(equal("z")));
+				will(returnValue("World"));
+				one(picoContainer).getComponent(with(equal("y")));
+				will(returnValue("Hello"));
+				one(picoContainer).getParent();
+				will(returnValue(null));
+			}
+		});
+		List expected = Arrays.asList("Hello", "World");
+		Collections.sort(expected);
+		List actual = Arrays.asList((Object[]) ccp.resolveInstance(
+				picoContainer, componentAdapter, String[].class,
+				null, false, null));
+		Collections.sort(actual);
+		assertEquals(expected, actual);
+	}
+
+	static public interface Fish {
+	}
+
+	static public class Cod implements Fish {
+		public String toString() {
+			return "Cod";
+		}
+	}
+
+	static public class Shark implements Fish {
+		public String toString() {
+			return "Shark";
+		}
+	}
+
+	static public class Bowl {
+		private final Cod[] cods;
+		private final Fish[] fishes;
+
+		public Bowl(Cod cods[], Fish fishes[]) {
+			this.cods = cods;
+			this.fishes = fishes;
+		}
+	}
+
+	private MutableContainer getDefaultPicoContainer() {
+		MutableContainer mpc = new DefaultContainer(new Caching());
+		mpc.addComponent(Bowl.class);
+		mpc.addComponent(Cod.class);
+		mpc.addComponent(Shark.class);
+		return mpc;
+	}
+
+	@Test
+	public void testNativeArrays() {
+		MutableContainer mpc = getDefaultPicoContainer();
+		Cod cod = mpc.getComponent(Cod.class);
+		Bowl bowl = mpc.getComponent(Bowl.class);
+		assertEquals(1, bowl.cods.length);
+		assertEquals(2, bowl.fishes.length);
+		assertSame(cod, bowl.cods[0]);
+		assertNotSame(bowl.fishes[0], bowl.fishes[1]);
+	}
+
+	@Test
+	public void testCollectionsAreGeneratedOnTheFly() {
+		MutableContainer mpc = new DefaultContainer();
+		mpc
+				.addAdapter(new ConstructorInjector(Bowl.class, Bowl.class,
+						null, new NullComponentMonitor(),
+						new NullLifecycleStrategy(), false));
+		mpc.addComponent(Cod.class);
+		Bowl bowl = mpc.getComponent(Bowl.class);
+		assertEquals(1, bowl.cods.length);
+		mpc.addComponent("Nemo", new Cod());
+		bowl = mpc.getComponent(Bowl.class);
+		assertEquals(2, bowl.cods.length);
+		assertNotSame(bowl.cods[0], bowl.cods[1]);
+	}
+
+	static public class CollectedBowl {
+		private final Cod[] cods;
+		private final Fish[] fishes;
+
+		public CollectedBowl(Collection cods, Collection fishes) {
+			this.cods = (Cod[]) cods.toArray(new Cod[cods.size()]);
+			this.fishes = (Fish[]) fishes.toArray(new Fish[fishes.size()]);
+		}
+	}
+
+	@Test
+	public void testCollections() {
+		MutableContainer mpc = new DefaultContainer(new Caching());
+		mpc.addComponent(CollectedBowl.class, CollectedBowl.class,
+				new ComponentParameter(Cod.class, false),
+				new ComponentParameter(Fish.class, false));
+		mpc.addComponent(Cod.class);
+		mpc.addComponent(Shark.class);
+		Cod cod = mpc.getComponent(Cod.class);
+		CollectedBowl bowl = mpc.getComponent(CollectedBowl.class);
+		assertEquals(1, bowl.cods.length);
+		assertEquals(2, bowl.fishes.length);
+		assertSame(cod, bowl.cods[0]);
+		assertNotSame(bowl.fishes[0], bowl.fishes[1]);
+	}
+
+	static public class MappedBowl {
+		private final Fish[] fishes;
+
+		public MappedBowl(Map map) {
+			Collection collection = map.values();
+			this.fishes = (Fish[]) collection.toArray(new Fish[collection
+					.size()]);
+		}
+	}
+
+	@Test
+	public void testMaps() {
+		MutableContainer mpc = new DefaultContainer();
+		mpc.addComponent(MappedBowl.class, MappedBowl.class,
+				new ComponentParameter(Fish.class, false));
+		mpc.addComponent(Cod.class);
+		mpc.addComponent(Shark.class);
+		MappedBowl bowl = mpc.getComponent(MappedBowl.class);
+		assertEquals(2, bowl.fishes.length);
+		assertNotSame(bowl.fishes[0], bowl.fishes[1]);
+	}
+
+	public static class UngenericCollectionBowl {
+		public UngenericCollectionBowl(Collection fish) {
+		}
+	}
+
+	@Test
+	public void testShouldNotInstantiateCollectionForUngenericCollectionParameters() {
+		MutableContainer pico = getDefaultPicoContainer();
+		pico.addComponent(UngenericCollectionBowl.class);
+		try {
+			pico.getComponent(UngenericCollectionBowl.class);
+			fail();
+		} catch (AbstractInjector.UnsatisfiableDependenciesException e) {
+			// expected
+		}
+	}
+
+	public static class AnotherGenericCollectionBowl {
+		private final String[] strings;
+
+		public AnotherGenericCollectionBowl(String[] strings) {
+			this.strings = strings;
+		}
+
+		public String[] getStrings() {
+			return strings;
+		}
+	}
+
+	@Test
+	public void testShouldFailWhenThereAreNoComponentsToPutInTheArray() {
+		MutableContainer pico = getDefaultPicoContainer();
+		pico.addComponent(AnotherGenericCollectionBowl.class);
+		try {
+			pico.getComponent(AnotherGenericCollectionBowl.class);
+			fail();
+		} catch (AbstractInjector.UnsatisfiableDependenciesException e) {
+			// expected
+		}
+	}
+
+	@Test
+	public void testAllowsEmptyArraysIfEspeciallySet() {
+		MutableContainer pico = getDefaultPicoContainer();
+		pico.addComponent(AnotherGenericCollectionBowl.class,
+				AnotherGenericCollectionBowl.class,
+				ComponentParameter.ARRAY_ALLOW_EMPTY);
+		AnotherGenericCollectionBowl bowl = pico
+				.getComponent(AnotherGenericCollectionBowl.class);
+		assertNotNull(bowl);
+		assertEquals(0, bowl.strings.length);
+	}
+
+	public static class TouchableObserver implements Touchable {
+		private final Touchable[] touchables;
+
+		public TouchableObserver(Touchable[] touchables) {
+			this.touchables = touchables;
+
+		}
+
+		public void touch() {
+			for (Touchable touchable : touchables) {
+				touchable.touch();
+			}
+		}
+	}
+
+	@Test
+	public void testWillOmitSelfFromCollection() {
+		MutableContainer pico = getDefaultPicoContainer();
+		pico.addComponent(SimpleTouchable.class);
+		pico.addComponent(TouchableObserver.class);
+		Touchable observer = pico.getComponent(TouchableObserver.class);
+		assertNotNull(observer);
+		observer.touch();
+		SimpleTouchable touchable = pico.getComponent(SimpleTouchable.class);
+		assertTrue(touchable.wasTouched);
+	}
+
+	@Test
+	public void testWillRemoveComponentsWithMatchingKeyFromParent() {
+		MutableContainer parent = new DefaultContainer();
+		parent.addComponent("Tom", Cod.class);
+		parent.addComponent("Dick", Cod.class);
+		parent.addComponent("Harry", Cod.class);
+		MutableContainer child = new DefaultContainer(parent);
+		child.addComponent("Dick", Shark.class);
+		child.addComponent(Bowl.class);
+		Bowl bowl = child.getComponent(Bowl.class);
+		assertEquals(3, bowl.fishes.length);
+		assertEquals(2, bowl.cods.length);
+	}
+
+	@Test
+	public void testBowlWithoutTom() {
+		MutableContainer mpc = new DefaultContainer();
+		mpc.addComponent("Tom", Cod.class);
+		mpc.addComponent("Dick", Cod.class);
+		mpc.addComponent("Harry", Cod.class);
+		mpc.addComponent(Shark.class);
+		mpc.addComponent(CollectedBowl.class, CollectedBowl.class,
+				new CollectionComponentParameter(Cod.class, false) {
+					protected boolean evaluate(ComponentAdapter adapter) {
+						return !"Tom".equals(adapter.getComponentKey());
+					}
+				}, new CollectionComponentParameter(Fish.class, false));
+		CollectedBowl bowl = mpc.getComponent(CollectedBowl.class);
+		Cod tom = (Cod) mpc.getComponent("Tom");
+		assertEquals(4, bowl.fishes.length);
+		assertEquals(2, bowl.cods.length);
+		assertFalse(Arrays.asList(bowl.cods).contains(tom));
+	}
+
+	public static class DependsOnAll {
+		public DependsOnAll(Set set, SortedSet sortedSet,
+				Collection collection, List list, SortedMap sortedMap, Map map
+		// , ConcurrentMap concurrentMap, Queue queue, BlockingQueue
+		// blockingQueue
+		) {
+			assertNotNull(set);
+			assertNotNull(sortedSet);
+			assertNotNull(collection);
+			assertNotNull(list);
+			assertNotNull(sortedMap);
+			assertNotNull(map);
+			// assertNotNull(concurrentMap);
+			// assertNotNull(queue);
+			// assertNotNull(blockingQueue);
+		}
+	}
+
+	@Test
+	public void testDifferentCollectiveTypesAreResolved() {
+		MutableContainer pico = new DefaultContainer();
+		CollectionComponentParameter parameter = new CollectionComponentParameter(
+				Fish.class, true);
+		pico.addComponent(DependsOnAll.class, DependsOnAll.class, parameter,
+				parameter, parameter, parameter, parameter, parameter);
+		assertNotNull(pico.getComponent(DependsOnAll.class));
+	}
+
+	@Test
+	public void testVerify() {
+		MutableContainer pico = new DefaultContainer();
+		CollectionComponentParameter parameterNonEmpty = CollectionComponentParameter.ARRAY;
+		pico.addComponent(Shark.class);
+		parameterNonEmpty.verify(pico, null, Fish[].class, null, false, null);
+		try {
+			parameterNonEmpty
+					.verify(pico, null, Cod[].class, null, false, null);
+			fail("(PicoCompositionException expected");
+		} catch (CompositionException e) {
+			assertTrue(e.getMessage().indexOf(Cod.class.getName()) > 0);
+		}
+		CollectionComponentParameter parameterEmpty = CollectionComponentParameter.ARRAY_ALLOW_EMPTY;
+		parameterEmpty.verify(pico, null, Fish[].class, null, false, null);
+		parameterEmpty.verify(pico, null, Cod[].class, null, false, null);
+	}
+
+	// PICO-243 : this test will fail if executed on jdk1.3 without
+	// commons-collections
+	@Test
+	public void testOrderOfElementsOfAnArrayDependencyIsPreserved() {
+		MutableContainer pico = new DefaultContainer();
+		pico.addComponent("first", "first");
+		pico.addComponent("second", "second");
+		pico.addComponent("third", "third");
+		pico.addComponent("fourth", "fourth");
+		pico.addComponent("fifth", "fifth");
+		pico.addComponent(Truc.class);
+
+		final List strings = pico.getComponents(String.class);
+		assertEquals("first", strings.get(0));
+		assertEquals("second", strings.get(1));
+		assertEquals("third", strings.get(2));
+		assertEquals("fourth", strings.get(3));
+		assertEquals("fifth", strings.get(4));
+
+		pico.getComponent(Truc.class);
+	}
+
+	public static final class Truc {
+		public Truc(String[] s) {
+			assertEquals("first", s[0]);
+			assertEquals("second", s[1]);
+			assertEquals("third", s[2]);
+			assertEquals("fourth", s[3]);
+			assertEquals("fifth", s[4]);
+		}
+	}
+
+}
\ No newline at end of file

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/ComponentKeysTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/ComponentKeysTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/ComponentKeysTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/ComponentKeysTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,69 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.defaults;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.parameters.ComponentParameter;
+import org.apache.composer.core.testmodel.DecoratedTouchable;
+import org.apache.composer.core.testmodel.DependsOnTouchable;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+
+/**
+ * @author Thomas Heller
+ * @author Aslak Hellesøy
+ */
+public class ComponentKeysTestCase {
+	
+    @Test public void testComponensRegisteredWithClassKeyTakePrecedenceOverOthersWhenThereAreMultipleImplementations() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent("default", SimpleTouchable.class);
+
+        /**
+         * By using a class as key, this should take precedence over the other Touchable
+         */
+        pico.addComponent(Touchable.class, DecoratedTouchable.class, new ComponentParameter("default"));
+
+        Touchable touchable = pico.getComponent(Touchable.class);
+        assertEquals(DecoratedTouchable.class, touchable.getClass());
+    }
+
+    @Test public void testComponentAdapterResolutionIsFirstLookedForByClassKeyToTheTopOfTheContainerHierarchy() {
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent("default", SimpleTouchable.class);
+
+        // Use the List variant instead, so we get better test coverage.
+        pico.addComponent(Touchable.class, DecoratedTouchable.class, new ComponentParameter("default"));
+
+        DefaultContainer grandChild = new DefaultContainer(new DefaultContainer(new DefaultContainer(pico)));
+
+        Touchable touchable = grandChild.getComponent(Touchable.class);
+        assertEquals(DecoratedTouchable.class, touchable.getClass());
+
+    }
+
+    @Test public void testComponentKeysFromParentCannotConfuseTheChild() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent("test", SimpleTouchable.class);
+
+        DefaultContainer child = new DefaultContainer(pico);
+
+        child.addComponent("test", DependsOnTouchable.class);
+
+        DependsOnTouchable dot = (DependsOnTouchable) child.getComponent("test");
+
+        assertNotNull(dot);
+    }
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/ComposerExceptionsTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/ComposerExceptionsTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/ComposerExceptionsTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/ComposerExceptionsTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,115 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by Joerg Schaible                                           *
+ *****************************************************************************/
+package org.apache.composer.core.defaults;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Test;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.ComposerException;
+import org.apache.composer.core.injectors.AbstractInjector;
+import org.apache.composer.core.injectors.ConstructorInjector;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.AbstractComponentMonitor;
+
+/**
+ * Unit tests for the several PicoException classes.
+ */
+public class ComposerExceptionsTestCase {
+
+    final static public String MESSAGE = "Message of the exception";
+    final static public Throwable THROWABLE = new Throwable();
+
+    @SuppressWarnings({ "unchecked" })
+    final void executeTestOfStandardException(final Class clazz) {
+        final ComponentAdapter componentAdapter = new ConstructorInjector(clazz, clazz, null, new AbstractComponentMonitor(),
+                                                                          new NullLifecycleStrategy(), false);
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent(MESSAGE);
+        try {
+            final Exception exception = (Exception) componentAdapter.getComponentInstance(pico);
+            assertEquals(MESSAGE, exception.getMessage());
+        } catch (final AbstractInjector.UnsatisfiableDependenciesException ex) {
+            final Set<Object> set = new HashSet<Object>();
+            for (Object o : ex.getUnsatisfiableDependencies()) {
+                final List<Object> list = (List<Object>)o;
+                set.addAll(list);
+            }
+            assertTrue(set.contains(Throwable.class));
+        }
+        pico = new DefaultContainer();
+        pico.addComponent(THROWABLE);
+        try {
+            final ComposerException exception = (ComposerException) componentAdapter.getComponentInstance(pico);
+            assertSame(THROWABLE, exception.getCause());
+        } catch (final AbstractInjector.UnsatisfiableDependenciesException ex) {
+            final Set<Object> set = new HashSet<Object>();
+            for (Object o : ex.getUnsatisfiableDependencies()) {
+                final List<Object> list = (List<Object>)o;
+                set.addAll(list);
+            }
+            assertTrue(set.contains(String.class));
+        }
+        pico.addComponent(MESSAGE);
+        final ComposerException exception = (ComposerException) componentAdapter.getComponentInstance(pico);
+        assertEquals(MESSAGE, exception.getMessage());
+        assertSame(THROWABLE, exception.getCause());
+    }
+
+    @Test public void testPicoInitializationException() {
+        executeTestOfStandardException(CompositionException.class);
+    }
+
+    @Test public void testPicoInitializationExceptionWithDefaultConstructor() {
+        TestException e = new TestException(null);
+        assertNull(e.getMessage());
+        assertNull(e.getCause());
+    }
+
+    private static class TestException extends CompositionException {
+        public TestException(final String message) {
+            super(message);
+        }
+    }
+
+    @Test public void testPrintStackTrace() throws IOException {
+        ComposerException nestedException = new ComposerException("Outer", new Exception("Inner")) {
+        };
+        ComposerException simpleException = new ComposerException("Outer") {
+        };
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        PrintStream printStream = new PrintStream(out);
+        nestedException.printStackTrace(printStream);
+        simpleException.printStackTrace(printStream);
+        out.close();
+        assertTrue(out.toString().indexOf("Caused by:") > 0);
+        out = new ByteArrayOutputStream();
+        PrintWriter writer = new PrintWriter(out);
+        nestedException.printStackTrace(writer);
+        simpleException.printStackTrace(writer);
+        writer.flush();
+        out.close();
+        assertTrue(out.toString().indexOf("Caused by:") > 0);
+        //simpleException.printStackTrace();
+    }
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultComponentRegistryTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultComponentRegistryTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultComponentRegistryTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultComponentRegistryTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,95 @@
+/*****************************************************************************
+ * Copyright (c) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Idea by Rachel Davies, Original code by Stacy Curl                        *
+ *****************************************************************************/
+
+package org.apache.composer.core.defaults;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.injectors.ConstructorInjector;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+import org.apache.composer.core.testmodel.AlternativeTouchable;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+
+public class DefaultComponentRegistryTestCase {
+    private DefaultContainer picoContainer;
+
+    @Before
+    public void setUp() throws Exception {
+        picoContainer = new DefaultContainer();
+    }
+
+    @Test public void testRegisterComponent() throws CompositionException {
+        ComponentAdapter componentAdapter = createComponentAdapter();
+        picoContainer.addAdapter(componentAdapter);
+        assertTrue(picoContainer.getComponentAdapters().contains(componentAdapter));
+    }
+
+    @Test public void testUnregisterComponent() throws CompositionException {
+        ComponentAdapter componentAdapter = createComponentAdapter();
+        picoContainer.addAdapter(componentAdapter);
+        picoContainer.removeComponent(Touchable.class);
+        assertFalse(picoContainer.getComponentAdapters().contains(componentAdapter));
+    }
+
+    @Test public void testCannotInstantiateAnUnregisteredComponent() throws CompositionException {
+        ComponentAdapter componentAdapter = createComponentAdapter();
+        picoContainer.addAdapter(componentAdapter);
+        picoContainer.getComponents();
+        picoContainer.removeComponent(Touchable.class);
+
+        assertTrue(picoContainer.getComponents().isEmpty());
+    }
+
+    @Test public void testCanInstantiateReplacedComponent() throws CompositionException {
+        ComponentAdapter componentAdapter = createComponentAdapter();
+        picoContainer.addAdapter(componentAdapter);
+        picoContainer.getComponents();
+        picoContainer.removeComponent(Touchable.class);
+
+        picoContainer.addComponent(Touchable.class, AlternativeTouchable.class);
+
+        assertEquals("Container should container 1 addComponent",
+                1, picoContainer.getComponents().size());
+    }
+
+    @Test public void testUnregisterAfterInstantiateComponents() throws CompositionException {
+        ComponentAdapter componentAdapter = createComponentAdapter();
+        picoContainer.addAdapter(componentAdapter);
+        picoContainer.getComponents();
+        picoContainer.removeComponent(Touchable.class);
+        assertNull(picoContainer.getComponent(Touchable.class));
+    }
+
+    @Test public void testReplacedInstantiatedComponentHasCorrectClass() throws CompositionException {
+        ComponentAdapter componentAdapter = createComponentAdapter();
+        picoContainer.addAdapter(componentAdapter);
+        picoContainer.getComponents();
+        picoContainer.removeComponent(Touchable.class);
+
+        picoContainer.addComponent(Touchable.class, AlternativeTouchable.class);
+        Object component = picoContainer.getComponents().iterator().next();
+
+        assertEquals(AlternativeTouchable.class, component.getClass());
+    }
+
+    private ComponentAdapter createComponentAdapter() throws CompositionException {
+        return new ConstructorInjector(Touchable.class, SimpleTouchable.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false);
+    }
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultContainerLifecycleTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultContainerLifecycleTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultContainerLifecycleTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultContainerLifecycleTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,534 @@
+/*****************************************************************************
+ * Copyright (c) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the license.html file.                                                    *
+ *                                                                           *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
+ *****************************************************************************/
+
+package org.apache.composer.core.defaults;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.apache.composer.core.tck.MockFactory.mockeryWithCountingNamingScheme;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.ComponentMonitor;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.LifecycleStrategy;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.LifecycleException;
+import org.apache.composer.core.Startable;
+import org.apache.composer.core.behaviors.Caching;
+import org.apache.composer.core.injectors.AbstractInjector;
+import org.apache.composer.core.injectors.AdaptingInjection;
+import org.apache.composer.core.injectors.ConstructorInjection;
+import org.apache.composer.core.monitors.LifecycleComponentMonitor;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+import org.apache.composer.core.monitors.LifecycleComponentMonitor.LifecycleFailuresException;
+import org.apache.composer.core.testmodel.RecordingLifecycle.FiveTriesToBeMalicious;
+import org.apache.composer.core.testmodel.RecordingLifecycle.Four;
+import org.apache.composer.core.testmodel.RecordingLifecycle.One;
+import org.apache.composer.core.testmodel.RecordingLifecycle.Three;
+import org.apache.composer.core.testmodel.RecordingLifecycle.Two;
+
+/**
+ * This class tests the lifecycle aspects of DefaultPicoContainer.
+ *
+ * @author Aslak Helles&oslash;y
+ * @author Paul Hammant
+ * @author Ward Cunningham
+ * @author Mauro Talevi
+ */
+@RunWith(JMock.class)
+public class DefaultContainerLifecycleTestCase {
+
+	private Mockery mockery = mockeryWithCountingNamingScheme();
+	
+    @Test public void testOrderOfInstantiationShouldBeDependencyOrder() throws Exception {
+
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent("recording", StringBuffer.class);
+        pico.addComponent(Four.class);
+        pico.addComponent(Two.class);
+        pico.addComponent(One.class);
+        pico.addComponent(Three.class);
+        final List componentInstances = pico.getComponents();
+
+        // instantiation - would be difficult to do these in the wrong order!!
+        assertEquals("Incorrect Order of Instantiation", One.class, componentInstances.get(1).getClass());
+        assertEquals("Incorrect Order of Instantiation", Two.class, componentInstances.get(2).getClass());
+        assertEquals("Incorrect Order of Instantiation", Three.class, componentInstances.get(3).getClass());
+        assertEquals("Incorrect Order of Instantiation", Four.class, componentInstances.get(4).getClass());
+    }
+
+    @Test public void testOrderOfStartShouldBeDependencyOrderAndStopAndDisposeTheOpposite() throws Exception {
+        DefaultContainer parent = new DefaultContainer(new Caching());
+        MutableContainer child = parent.makeChildContainer();
+
+        parent.addComponent("recording", StringBuffer.class);
+        child.addComponent(Four.class);
+        parent.addComponent(Two.class);
+        parent.addComponent(One.class);
+        child.addComponent(Three.class);
+
+        parent.start();
+        parent.stop();
+        parent.dispose();
+
+        assertEquals("<One<Two<Three<FourFour>Three>Two>One>!Four!Three!Two!One",
+                parent.getComponent("recording").toString());
+    }
+
+
+    @Test public void testLifecycleIsIgnoredIfAdaptersAreNotLifecycleManagers() {
+        DefaultContainer parent = new DefaultContainer(new ConstructorInjection());
+        MutableContainer child = parent.makeChildContainer();
+
+        parent.addComponent("recording", StringBuffer.class);
+        child.addComponent(Four.class);
+        parent.addComponent(Two.class);
+        parent.addComponent(One.class);
+        child.addComponent(Three.class);
+
+        parent.start();
+        parent.stop();
+        parent.dispose();
+
+        assertEquals("",
+                parent.getComponent("recording").toString());
+    }
+
+    @Test public void testStartStartShouldFail() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.start();
+        try {
+            pico.start();
+            fail("Should have failed");
+        } catch (IllegalStateException e) {
+            // expected;
+        }
+    }
+
+    @Test public void testStartStopStopShouldFail() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.start();
+        pico.stop();
+        try {
+            pico.stop();
+            fail("Should have failed");
+        } catch (IllegalStateException e) {
+            // expected;
+        }
+    }
+
+    @Test public void testStartStopDisposeDisposeShouldFail() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.start();
+        pico.stop();
+        pico.dispose();
+        try {
+            pico.dispose();
+            fail("Should have barfed");
+        } catch (IllegalStateException e) {
+            // expected;
+        }
+    }
+
+    public static class FooRunnable implements Runnable, Startable {
+        private int runCount;
+        private Thread thread = new Thread();
+        private boolean interrupted;
+
+        public FooRunnable() {
+        }
+
+        public int runCount() {
+            return runCount;
+        }
+
+        public boolean isInterrupted() {
+            return interrupted;
+        }
+
+        public void start() {
+            thread = new Thread(this);
+            thread.start();
+        }
+
+        public void stop() {
+            thread.interrupt();
+        }
+
+        // this would do something a bit more concrete
+        // than counting in real life !
+        public void run() {
+            runCount++;
+            try {
+                Thread.sleep(10000);
+            } catch (InterruptedException e) {
+                interrupted = true;
+            }
+        }
+    }
+
+    @Test public void testStartStopOfDaemonizedThread() throws Exception {
+        DefaultContainer pico = new DefaultContainer(new Caching());
+        pico.addComponent(FooRunnable.class);
+
+        pico.getComponents();
+        pico.start();
+        Thread.sleep(100);
+        pico.stop();
+
+        FooRunnable foo = pico.getComponent(FooRunnable.class);
+        assertEquals(1, foo.runCount());
+        pico.start();
+        Thread.sleep(100);
+        pico.stop();
+        assertEquals(2, foo.runCount());
+    }
+
+    @Test public void testGetComponentInstancesOnParentContainerHostedChildContainerDoesntReturnParentAdapter() {
+        MutableContainer parent = new DefaultContainer();
+        MutableContainer child = parent.makeChildContainer();
+        assertEquals(0, child.getComponents().size());
+    }
+
+    @Test public void testComponentsAreStartedBreadthFirstAndStoppedAndDisposedDepthFirst() {
+        MutableContainer parent = new DefaultContainer(new Caching());
+        parent.addComponent(Two.class);
+        parent.addComponent("recording", StringBuffer.class);
+        parent.addComponent(One.class);
+        MutableContainer child = parent.makeChildContainer();
+        child.addComponent(Three.class);
+        parent.start();
+        parent.stop();
+        parent.dispose();
+
+        assertEquals("<One<Two<ThreeThree>Two>One>!Three!Two!One", parent.getComponent("recording").toString());
+    }
+
+    @Test public void testMaliciousComponentCannotExistInAChildContainerAndSeeAnyElementOfContainerHierarchy() {
+        MutableContainer parent = new DefaultContainer(new Caching());
+        parent.addComponent(Two.class);
+        parent.addComponent("recording", StringBuffer.class);
+        parent.addComponent(One.class);
+        parent.addComponent(Three.class);
+        MutableContainer child = parent.makeChildContainer();
+        child.addComponent(FiveTriesToBeMalicious.class);
+        try {
+            parent.start();
+            fail("Thrown " + AbstractInjector.UnsatisfiableDependenciesException.class.getName() + " expected");
+        } catch ( AbstractInjector.UnsatisfiableDependenciesException e) {
+            // FiveTriesToBeMalicious can't get instantiated as there is no PicoContainer in any component set
+        }
+        String recording = parent.getComponent("recording").toString();
+        assertEquals("<One<Two<Three", recording);
+        try {
+            child.getComponent(FiveTriesToBeMalicious.class);
+            fail("Thrown " + AbstractInjector.UnsatisfiableDependenciesException.class.getName() + " expected");
+        } catch (final AbstractInjector.UnsatisfiableDependenciesException e) {
+            // can't get instantiated as there is no PicoContainer in any component set
+        }
+        recording = parent.getComponent("recording").toString();
+        assertEquals("<One<Two<Three", recording); // still the same
+    }
+
+
+    public static class NotStartable {
+         public void start(){
+            Assert.fail("start() should not get invoked on NonStartable");
+        }
+    }
+
+    @Test public void testOnlyStartableComponentsAreStartedOnStart() {
+        MutableContainer pico = new DefaultContainer(new Caching());
+        pico.addComponent("recording", StringBuffer.class);
+        pico.addComponent(One.class);
+        pico.addComponent(NotStartable.class);
+        pico.start();
+        pico.stop();
+        pico.dispose();
+        assertEquals("<OneOne>!One", pico.getComponent("recording").toString());
+    }
+
+    @Test public void testShouldFailOnStartAfterDispose() {
+        MutableContainer pico = new DefaultContainer();
+        pico.dispose();
+        try {
+            pico.start();
+            fail();
+        } catch (IllegalStateException expected) {
+        }
+    }
+
+    @Test public void testShouldFailOnStopAfterDispose() {
+        MutableContainer pico = new DefaultContainer();
+        pico.dispose();
+        try {
+            pico.stop();
+            fail();
+        } catch (IllegalStateException expected) {
+        }
+    }
+
+    @Test public void testShouldStackContainersLast() {
+        // this is merely a code coverage test - but it doesn't seem to cover the StackContainersAtEndComparator
+        // fully. oh well.
+        MutableContainer pico = new DefaultContainer(new Caching());
+        pico.addComponent(ArrayList.class);
+        pico.addComponent(DefaultContainer.class);
+        pico.addComponent(HashMap.class);
+        pico.start();
+        DefaultContainer childContainer = pico.getComponent(DefaultContainer.class);
+        // it should be started too
+        try {
+            childContainer.start();
+            fail();
+        } catch (IllegalStateException e) {
+        }
+    }
+
+    @Test public void testCanSpecifyLifeCycleStrategyForInstanceRegistrationWhenSpecifyingComponentFactory()
+        throws Exception
+    {
+        LifecycleStrategy strategy = new LifecycleStrategy() {
+            public void start(Object component) {
+                ((StringBuffer)component).append("start>");
+            }
+
+            public void stop(Object component) {
+                ((StringBuffer)component).append("stop>");
+            }
+
+            public void dispose(Object component) {
+                ((StringBuffer)component).append("dispose>");
+            }
+
+            public boolean hasLifecycle(Class type) {
+                return true;
+            }
+        };
+        MutableContainer pico = new DefaultContainer( new AdaptingInjection(), strategy, null );
+
+        StringBuffer sb = new StringBuffer();
+
+        pico.addComponent(sb);
+
+        pico.start();
+        pico.stop();
+        pico.dispose();
+
+        assertEquals("start>stop>dispose>", sb.toString());
+    }
+
+    @Test public void testLifeCycleStrategyForInstanceRegistrationPassedToChildContainers()
+        throws Exception
+    {
+        LifecycleStrategy strategy = new LifecycleStrategy() {
+            public void start(Object component) {
+                ((StringBuffer)component).append("start>");
+            }
+
+            public void stop(Object component) {
+                ((StringBuffer)component).append("stop>");
+            }
+
+            public void dispose(Object component) {
+                ((StringBuffer)component).append("dispose>");
+            }
+
+            public boolean hasLifecycle(Class type) {
+                return true;
+            }
+        };
+        MutableContainer parent = new DefaultContainer(strategy, null);
+        MutableContainer pico = parent.makeChildContainer();
+
+        StringBuffer sb = new StringBuffer();
+
+        pico.addComponent(sb);
+
+        pico.start();
+        pico.stop();
+        pico.dispose();
+
+        assertEquals("start>stop>dispose>", sb.toString());
+    }
+
+
+    @Test public void testLifecycleDoesNotRecoverWithNullComponentMonitor() {
+
+    	final Startable s1 = mockery.mock(Startable.class, "s1");
+        Startable s2 = mockery.mock(Startable.class, "s2");
+        mockery.checking(new Expectations(){{
+            one(s1).start();
+            will(throwException(new RuntimeException("I do not want to start myself")));
+        }});
+ 
+        DefaultContainer dpc = new DefaultContainer();
+        dpc.addComponent("foo", s1);
+        dpc.addComponent("bar", s2);
+        try {
+            dpc.start();
+            fail("PicoLifecylceException expected");
+        } catch (LifecycleException e) {
+            assertEquals("I do not want to start myself", e.getCause().getMessage());
+        }
+        dpc.stop();
+    }
+
+    @Test public void testLifecycleCanRecoverWithCustomComponentMonitor() throws NoSuchMethodException {
+
+    	final Startable s1 = mockery.mock(Startable.class, "s1");
+        final Startable s2 = mockery.mock(Startable.class, "s2");
+        final ComponentMonitor cm = mockery.mock(ComponentMonitor.class);
+    	mockery.checking(new Expectations(){{
+            one(s1).start();
+            will(throwException(new RuntimeException("I do not want to start myself")));
+            one(s1).stop();
+            one(s2).start();
+            one(s2).stop();
+            // s1 expectations
+            one(cm).invoking(with(aNull(Container.class)), with(aNull(ComponentAdapter.class)), with(equal(Startable.class.getMethod("start", (Class[])null))), with(same(s1)));
+            one(cm).lifecycleInvocationFailed(with(aNull(MutableContainer.class)), with(aNull(ComponentAdapter.class)), with(any(Method.class)), with(same(s1)), with(any(RuntimeException.class)));
+            one(cm).invoking(with(aNull(Container.class)), with(aNull(ComponentAdapter.class)), with(equal(Startable.class.getMethod("stop", (Class[])null))), with(same(s1)));
+            one(cm).invoked(with(aNull(Container.class)), with(aNull(ComponentAdapter.class)), with(equal(Startable.class.getMethod("stop", (Class[])null))), with(same(s1)), with(any(Long.class)));
+
+            // s2 expectations
+            one(cm).invoking(with(aNull(Container.class)), with(aNull(ComponentAdapter.class)), with(equal(Startable.class.getMethod("start", (Class[])null))), with(same(s2)));
+            one(cm).invoked(with(aNull(Container.class)), with(aNull(ComponentAdapter.class)), with(equal(Startable.class.getMethod("start", (Class[])null))), with(same(s2)), with(any(Long.class)));
+            one(cm).invoking(with(aNull(Container.class)), with(aNull(ComponentAdapter.class)), with(equal(Startable.class.getMethod("stop", (Class[])null))), with(same(s2)));
+            one(cm).invoked(with(aNull(Container.class)), with(aNull(ComponentAdapter.class)), with(equal(Startable.class.getMethod("stop", (Class[])null))), with(same(s2)), with(any(Long.class)));
+    	}});
+
+        DefaultContainer dpc = new DefaultContainer(cm);
+        dpc.addComponent("foo", s1);
+        dpc.addComponent("bar", s2);
+        dpc.start();
+        dpc.stop();
+    }
+
+    @Test public void testLifecycleFailuresCanBePickedUpAfterTheEvent() {
+    	final Startable s1 = mockery.mock(Startable.class, "s1");
+        final Startable s2 = mockery.mock(Startable.class, "s2");
+        final Startable s3 = mockery.mock(Startable.class, "s3");
+        mockery.checking(new Expectations(){{
+            one(s1).start();
+            will(throwException(new RuntimeException("I do not want to start myself")));
+            one(s1).stop();
+            one(s2).start();
+            one(s2).stop();
+            one(s3).start();
+            will(throwException(new RuntimeException("I also do not want to start myself")));
+            one(s3).stop();
+        }});
+        
+        LifecycleComponentMonitor lifecycleComponentMonitor = new LifecycleComponentMonitor(new NullComponentMonitor());
+
+        DefaultContainer dpc = new DefaultContainer(lifecycleComponentMonitor);
+        dpc.addComponent("one", s1);
+        dpc.addComponent("two", s2);
+        dpc.addComponent("three", s3);
+
+        dpc.start();
+
+        try {
+            lifecycleComponentMonitor.rethrowLifecycleFailuresException();
+            fail("LifecycleFailuresException expected");
+        } catch (LifecycleFailuresException e) {
+            assertEquals("I do not want to start myself;  I also do not want to start myself;", e.getMessage().trim());
+            dpc.stop();
+            assertEquals(2, e.getFailures().size());
+        }
+
+    }
+
+    @Test public void testStartedComponentsCanBeStoppedIfSomeComponentsFailToStart() {
+
+    	final Startable s1 = mockery.mock(Startable.class, "s1");
+        final Startable s2 = mockery.mock(Startable.class, "s2");
+        mockery.checking(new Expectations(){{
+            one(s1).start();
+            one(s1).stop();
+            one(s2).start();
+            will(throwException(new RuntimeException("I do not want to start myself")));
+         // s2 does not expect stop().
+        }});
+        
+        DefaultContainer dpc = new DefaultContainer();
+        dpc.addComponent("foo", s1);
+        dpc.addComponent("bar", s2);
+
+        try {
+            dpc.start();
+            fail("PicoLifecylceException expected");
+        } catch (RuntimeException e) {
+            dpc.stop();
+        }
+
+    }
+
+    @Test public void testStartedComponentsCanBeStoppedIfSomeComponentsFailToStartEvenInAPicoHierarchy() {
+
+    	final Startable s1 = mockery.mock(Startable.class, "s1");
+        final Startable s2 = mockery.mock(Startable.class, "s2");
+        mockery.checking(new Expectations(){{
+            one(s1).start();
+            one(s1).stop();
+            one(s2).start();
+            will(throwException(new RuntimeException("I do not want to start myself")));
+         // s2 does not expect stop().
+        }});
+        
+        DefaultContainer dpc = new DefaultContainer();
+        dpc.addComponent("foo", s1);
+        dpc.addComponent("bar", s2);
+        dpc.addChildContainer(new DefaultContainer(dpc));
+
+        try {
+            dpc.start();
+            fail("PicoLifecylceException expected");
+        } catch (RuntimeException e) {
+            dpc.stop();
+        }
+
+    }
+
+    @Test public void testChildContainerIsStoppedWhenStartedIndependentlyOfParent() throws Exception {
+
+        DefaultContainer parent = new DefaultContainer();
+
+        parent.start();
+
+        MutableContainer child = parent.makeChildContainer();
+
+        final Startable s1 = mockery.mock(Startable.class, "s1");
+        mockery.checking(new Expectations(){{
+            one(s1).start();
+            one(s1).stop();
+        }});
+        
+        child.addComponent(s1);
+
+        child.start();
+        parent.stop();
+
+    }
+}
\ No newline at end of file

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultContainerTreeSerializationTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultContainerTreeSerializationTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultContainerTreeSerializationTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultContainerTreeSerializationTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,59 @@
+/*****************************************************************************
+ * Copyright (c) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
+ *****************************************************************************/
+
+package org.apache.composer.core.defaults;
+
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Properties;
+
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.ComposerException;
+import org.apache.composer.core.tck.AbstractContainerTest;
+
+
+/**
+ * @author Thomas Heller
+ * @author Paul Hammant
+ */
+public class DefaultContainerTreeSerializationTestCase extends AbstractContainerTest {
+    protected MutableContainer createPicoContainer(Container parent) {
+        return new DefaultContainer(parent);
+    }
+
+    protected Properties[] getProperties() {
+        return new Properties[0];
+    }
+    
+    @Test public void testContainerIsDeserializableWithParent() throws ComposerException,
+                                                                 IOException, ClassNotFoundException {
+
+        Container parent = createPicoContainer(null);
+        MutableContainer child = createPicoContainer(parent);
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+
+        oos.writeObject(child);
+
+        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
+        child = (MutableContainer) ois.readObject();
+        assertNotNull(child.getParent());
+    }
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultLazyInstantiationTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultLazyInstantiationTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultLazyInstantiationTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultLazyInstantiationTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,23 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.defaults;
+
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.tck.AbstractLazyInstantiationTest;
+
+/**
+ * @author Aslak Helles&oslash;y
+ */
+public class DefaultLazyInstantiationTestCase extends AbstractLazyInstantiationTest {
+    protected MutableContainer createPicoContainer() {
+        return new DefaultContainer();
+    }
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultMultipleConstructorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultMultipleConstructorTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultMultipleConstructorTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/DefaultMultipleConstructorTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,29 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.defaults;
+
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.tck.AbstractMultipleConstructorTest;
+
+/**
+ * @author Aslak Helles&oslash;y
+ */
+public class DefaultMultipleConstructorTestCase extends AbstractMultipleConstructorTest {
+    protected MutableContainer createPicoContainer() {
+        return new DefaultContainer();
+    }
+    
+    // Eclipse need at least *one* fixture as direct class member ...
+    @Test public void testEclipseDummy() {
+    }
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/DelegatingContainerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/DelegatingContainerTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/DelegatingContainerTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/DelegatingContainerTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,64 @@
+/*****************************************************************************
+ * Copyright (c) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
+ *****************************************************************************/
+
+package org.apache.composer.core.defaults;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.fail;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.injectors.AbstractInjector;
+import org.apache.composer.core.testmodel.DependsOnTouchable;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+
+public class DelegatingContainerTestCase {
+    private MutableContainer parent;
+    private DefaultContainer child;
+
+    @Before
+    public void setUp() throws CompositionException {
+        parent = new DefaultContainer();
+        child = new DefaultContainer(parent);
+    }
+
+    @Test public void testChildGetsFromParent() {
+        parent.addComponent(SimpleTouchable.class);
+        child.addComponent(DependsOnTouchable.class);
+        DependsOnTouchable dependsOnTouchable = child.getComponent(DependsOnTouchable.class);
+
+        assertNotNull(dependsOnTouchable);
+    }
+
+    @Test public void testParentDoesntGetFromChild() {
+        child.addComponent(SimpleTouchable.class);
+        parent.addComponent(DependsOnTouchable.class);
+        try {
+            parent.getComponent(DependsOnTouchable.class);
+            fail();
+        } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
+        }
+    }
+
+    @Test public void testChildOverridesParent() {
+        parent.addComponent(SimpleTouchable.class);
+        child.addComponent(SimpleTouchable.class);
+
+        SimpleTouchable parentTouchable = parent.getComponent(SimpleTouchable.class);
+        SimpleTouchable childTouchable = child.getComponent(SimpleTouchable.class);
+        assertEquals(1, child.getComponents().size());
+        assertNotSame(parentTouchable, childTouchable);
+    }
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/GenericsTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/GenericsTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/GenericsTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/GenericsTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,155 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.defaults;
+
+import org.junit.Test;
+
+/**
+ * Uncomment all the tests in this class (as well as the obvious places in
+ * ConstructorInjectionComponentAdapter) in order to run with generics support
+ * Requires JDK 1.5 with generics enabled.
+ * 
+ * @author Aslak Helles&oslash;y
+ */
+public class GenericsTestCase {
+    @Test public void testDummy() {
+
+    }
+
+    /*
+    private MutablePicoContainer pico;
+    private Shark shark;
+    private Cod cod;
+    private Bowl bowl;
+
+    protected void setUp() throws Exception {
+        pico = new DefaultPicoContainer();
+
+        shark = new Shark();
+        cod = new Cod();
+
+        pico.addAdapter("shark", shark);
+        pico.addAdapter(cod);
+        pico.addAdapter(Bowl.class);
+
+        bowl = (Bowl) pico.getComponent(Bowl.class);
+    }
+
+    public static interface Fish {
+    }
+
+    public static class Cod implements Fish{
+    }
+
+    public static class Shark implements Fish{
+    }
+
+    public static class Bowl {
+        private final Collection<Fish> fishes;
+        private final Set<Cod> cods;
+        private final Map<String, Fish> stringFishMap;
+        private final Map<Object, Shark> objectSharkMap;
+
+        public Bowl(Collection<Fish> fishes, Set<Cod> cods, Map<String,Fish> stringFishMap, Map<Object,Shark> objectSharkMap) {
+            this.fishes = fishes;
+            this.cods = cods;
+            this.stringFishMap = stringFishMap;
+            this.objectSharkMap = objectSharkMap;
+        }
+
+        public Collection<Fish> getFishes() {
+            return fishes;
+        }
+
+        public Set<Cod> getCods() {
+            return cods;
+        }
+
+        public Map<String,Fish> getStringFishMap() {
+            return stringFishMap;
+        }
+
+        public Map<Object, Shark> getObjectSharkMap() {
+            return objectSharkMap;
+        }
+    }
+
+    @Test public void testShouldCreateBowlWithFishCollection() {
+        Collection<Fish> fishes = bowl.getFishes();
+        assertEquals(2, fishes.size());
+        assertTrue(fishes.contains(shark));
+        assertTrue(fishes.contains(cod));
+
+        Set<Cod> cods = bowl.getCods();
+        assertEquals(1, cods.size());
+        assertTrue(cods.contains(cod));
+    }
+
+    @Test public void testShouldFilterMapByKeyType() {
+        Map<String, Fish> fishMap = bowl.getStringFishMap();
+        assertEquals(1, fishMap.size());
+        assertSame(shark, fishMap.get("shark"));
+    }
+
+    @Test public void testShouldFilterMapByValueType() {
+        Map<Object, Shark> fishMap = bowl.getObjectSharkMap();
+        assertEquals(1, fishMap.size());
+        assertSame(shark, fishMap.get("shark"));
+    }
+
+    public static class UngenericCollectionBowl {
+        public UngenericCollectionBowl(Collection fish) {
+        }
+    }
+
+    @Test public void testShouldNotInstantiateCollectionForUngenericCollectionParameters() {
+        pico.addAdapter(UngenericCollectionBowl.class);
+        try {
+            pico.getComponent(UngenericCollectionBowl.class);
+            fail();
+        } catch (UnsatisfiableDependenciesException e) {
+            // expected
+        }
+    }
+
+    public static class UngenericMapBowl {
+        public UngenericMapBowl(Map fish) {
+        }
+    }
+
+    @Test public void testShouldNotInstantiateMapForUngenericMapParameters() {
+        pico.addAdapter(UngenericMapBowl.class);
+        try {
+            pico.getComponent(UngenericMapBowl.class);
+            fail();
+        } catch (UnsatisfiableDependenciesException e) {
+            // expected
+        }
+    }
+
+    public static class AnotherGenericCollectionBowl {
+        private final Collection<String> strings;
+
+        public AnotherGenericCollectionBowl(Collection<String> strings) {
+            this.strings = strings;
+        }
+
+        public Collection<String> getStrings() {
+            return strings;
+        }
+    }
+
+    @Test public void testShouldInstantiateAmptyCollectionForAnotherGenericCollection() {
+        pico.addAdapter(AnotherGenericCollectionBowl.class);
+        AnotherGenericCollectionBowl anotherGenericCollectionBowl = (AnotherGenericCollectionBowl) pico.getComponent(AnotherGenericCollectionBowl.class);
+        assertEquals(0, anotherGenericCollectionBowl.getStrings().size());
+    }
+*/
+}
\ No newline at end of file

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/NoneOfTheseTestsAffectCoverageMeaningTheyCouldGoTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/NoneOfTheseTestsAffectCoverageMeaningTheyCouldGoTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/NoneOfTheseTestsAffectCoverageMeaningTheyCouldGoTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/NoneOfTheseTestsAffectCoverageMeaningTheyCouldGoTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,212 @@
+/*****************************************************************************
+ * Copyright (c) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
+ *****************************************************************************/
+
+package org.apache.composer.core.defaults;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Test;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.NameBinding;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.parameters.ComponentParameter;
+import org.apache.composer.core.parameters.ConstantParameter;
+import org.apache.composer.core.testmodel.DependsOnTouchable;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+import org.apache.composer.core.testmodel.Webster;
+
+public final class NoneOfTheseTestsAffectCoverageMeaningTheyCouldGoTestCase {
+
+    //TODO - move to AbstractComponentRegistryTestCase
+    @Test public void testGetComponentSpecification() throws CompositionException {
+        DefaultContainer pico = new DefaultContainer();
+
+        assertNull(pico.getComponentAdapter(Touchable.class, (NameBinding) null));
+        pico.addComponent(SimpleTouchable.class);
+        assertNotNull(pico.getComponentAdapter(SimpleTouchable.class, (NameBinding) null));
+        assertNotNull(pico.getComponentAdapter(Touchable.class,(NameBinding)  null));
+    }
+
+
+    //TODO move
+    @Test public void testMultipleImplementationsAccessedThroughKey()
+            throws CompositionException
+    {
+        SimpleTouchable Touchable1 = new SimpleTouchable();
+        SimpleTouchable Touchable2 = new SimpleTouchable();
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent("Touchable1", Touchable1);
+        pico.addComponent("Touchable2", Touchable2);
+        pico.addComponent("fred1", DependsOnTouchable.class, new ComponentParameter("Touchable1"));
+        pico.addComponent("fred2", DependsOnTouchable.class, new ComponentParameter("Touchable2"));
+
+        DependsOnTouchable fred1 = (DependsOnTouchable) pico.getComponent("fred1");
+        DependsOnTouchable fred2 = (DependsOnTouchable) pico.getComponent("fred2");
+
+        assertFalse(fred1 == fred2);
+        assertSame(Touchable1, fred1.getTouchable());
+        assertSame(Touchable2, fred2.getTouchable());
+    }
+
+    //TODO - move
+    @Test public void testRegistrationByName() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+
+        Webster one = new Webster(new ArrayList());
+        Touchable two = new SimpleTouchable();
+
+        pico.addComponent("one", one);
+        pico.addComponent("two", two);
+
+        assertEquals("Wrong number of comps in the internals", 2, pico.getComponents().size());
+
+        assertEquals("Looking up one Touchable", one, pico.getComponent("one"));
+        assertEquals("Looking up two Touchable", two, pico.getComponent("two"));
+
+        assertTrue("Object one the same", one == pico.getComponent("one"));
+        assertTrue("Object two the same", two == pico.getComponent("two"));
+
+        assertEquals("Lookup of unknown key should return null", null, pico.getComponent("unknown"));
+    }
+
+    @Test public void testRegistrationByNameAndClassWithResolving() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+
+        pico.addComponent(List.class, new ArrayList());
+        pico.addComponent("one", Webster.class);
+        pico.addComponent("two", SimpleTouchable.class);
+
+        assertEquals("Wrong number of comps in the internals", 3, pico.getComponents().size());
+
+        assertNotNull("Object one the same", pico.getComponent("one"));
+        assertNotNull("Object two the same", pico.getComponent("two"));
+
+        assertNull("Lookup of unknown key should return null", pico.getComponent("unknown"));
+    }
+
+    @Test public void testDuplicateRegistrationWithTypeAndObject() throws CompositionException {
+        DefaultContainer pico = new DefaultContainer();
+
+        pico.addComponent(SimpleTouchable.class);
+        try {
+            pico.addComponent(SimpleTouchable.class, new SimpleTouchable());
+            fail("Should have barfed with dupe registration");
+        } catch (CompositionException e) {
+            // expected
+            assertTrue(e.getMessage().startsWith("Duplicate"));
+            assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) > 0);
+        }
+    }
+
+
+    @Test public void testComponentRegistrationMismatch() throws CompositionException {
+        MutableContainer pico = new DefaultContainer();
+
+        try {
+            pico.addComponent(List.class, SimpleTouchable.class);
+        } catch (ClassCastException e) {
+            // not worded in message
+            assertTrue(e.getMessage().indexOf(List.class.getName()) > 0);
+            assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) == 0);
+        }
+
+    }
+
+    interface Animal {
+
+        String getFood();
+    }
+
+    public static class Dino implements Animal {
+        final String food;
+
+        public Dino(String food) {
+            this.food = food;
+        }
+
+        public String getFood() {
+            return food;
+        }
+    }
+
+    public static class Dino2 extends Dino {
+        public Dino2(int number) {
+            super(String.valueOf(number));
+        }
+    }
+
+    public static class Dino3 extends Dino {
+        public Dino3(String a, String b) {
+            super(a + b);
+        }
+    }
+
+    public static class Dino4 extends Dino {
+        public Dino4(String a, int n, String b, Touchable Touchable) {
+            super(a + n + b + " " + Touchable.getClass().getName());
+        }
+    }
+
+    @Test public void testParameterCanBePassedToConstructor() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent(Animal.class,
+                Dino.class,
+                new ConstantParameter("bones"));
+
+        Animal animal = pico.getComponent(Animal.class);
+        assertNotNull("Component not null", animal);
+        assertEquals("bones", animal.getFood());
+    }
+
+    @Test public void testParameterCanBePrimitive() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent(Animal.class, Dino2.class, new ConstantParameter(22));
+
+        Animal animal = pico.getComponent(Animal.class);
+        assertNotNull("Component not null", animal);
+        assertEquals("22", animal.getFood());
+    }
+
+    @Test public void testMultipleParametersCanBePassed() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent(Animal.class, Dino3.class, new ConstantParameter("a"),
+                new ConstantParameter("b"));
+
+        Animal animal = pico.getComponent(Animal.class);
+        assertNotNull("Component not null", animal);
+        assertEquals("ab", animal.getFood());
+
+    }
+
+    @Test public void testParametersCanBeMixedWithComponentsCanBePassed() throws Exception {
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent(Touchable.class, SimpleTouchable.class);
+        pico.addComponent(Animal.class, Dino4.class, new ConstantParameter("a"),
+                new ConstantParameter(3),
+                new ConstantParameter("b"),
+                ComponentParameter.DEFAULT);
+
+        Animal animal = pico.getComponent(Animal.class);
+        assertNotNull("Component not null", animal);
+        assertEquals("a3b org.apache.composer.core.testmodel.SimpleTouchable", animal.getFood());
+    }
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/SetterIntrospectorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/SetterIntrospectorTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/SetterIntrospectorTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/SetterIntrospectorTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,75 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.defaults;
+
+import java.beans.IntrospectionException;
+
+import org.junit.Test;
+
+/**
+ * @author Aslak Helles&oslash;y
+ */
+public class SetterIntrospectorTestCase {
+    public static class TestBean {
+        public void setPublicMethod(int i) {
+        }
+
+        public void setPublicMETHODAgain(int i) {
+        }
+
+        public void setMOOky(int i) {
+        }
+
+        public void setFOOBAR(int i) {
+        }
+
+        public void set(int i) {
+        }
+
+        public void sets(int i) {
+        }
+
+        public void fooBar(int i) {
+        }
+
+        public void setX(int i) {
+        }
+
+        public static void setStaticMethod(int i) {
+        }
+
+        public static void setMany() {
+        }
+
+        protected void setProtectedMethod(int i) {
+        }
+
+        private void setPrivateMethod(int i) {
+        }
+    }
+
+    @Test public void testShouldConvertPropertyNamesInSameWayAsBeanInfo() throws IntrospectionException {
+
+// TODO - to test via SetterInjectionComponentAdaptor with mock/expects.
+
+//        BeanInfo beanInfo = Introspector.getBeanInfo(TestBean.class);
+//        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
+//
+//        Map setters = getSetters(TestBean.class);
+//        assertEquals(propertyDescriptors.length, setters.size());
+//
+//        for (int i = 0; i < propertyDescriptors.length; i++) {
+//            PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
+//            String expectedPropertyName = propertyDescriptor.getName();
+//            assertEquals("No property found for " + expectedPropertyName, propertyDescriptor.getWriteMethod(), setters.get(expectedPropertyName));
+//        }
+    }
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/defaults/UserQuestionTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/defaults/UserQuestionTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/defaults/UserQuestionTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/defaults/UserQuestionTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,255 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.defaults;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.apache.composer.core.Characteristics;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.adapters.AbstractAdapter;
+import org.apache.composer.core.injectors.ConstructorInjection;
+
+/**
+ * This class can be used to test out various things asked on the mailing list.
+ * Or to answer questions.
+ *
+ * @author Aslak Helles&oslash;y
+ */
+public final class UserQuestionTestCase {
+
+    // From Scott Farquahsr
+    public static final class CheeseAdapter extends AbstractAdapter {
+        private final Map bla;
+
+        public CheeseAdapter(Object componentKey, Class componentImplementation, Map cheeseMap) throws CompositionException {
+            super(componentKey, componentImplementation);
+            this.bla = cheeseMap;
+        }
+
+        public Object getComponentInstance(Container pico) throws CompositionException {
+            return bla.get("cheese");
+        }
+
+        public void verify(Container pico) {
+        }
+
+        public String getDescriptor() {
+            return null;
+        }
+    }
+
+    public static interface Cheese {
+        String getName();
+    }
+
+    public static class Gouda implements Cheese {
+        public String getName() {
+            return "Gouda";
+        }
+    }
+
+    public static class Roquefort implements Cheese {
+        public String getName() {
+            return "Roquefort";
+        }
+    }
+
+    public static class Omelette {
+        private final Cheese cheese;
+
+        public Omelette(Cheese cheese) {
+            this.cheese = cheese;
+        }
+
+        public Cheese getCheese() {
+            return cheese;
+        }
+    }
+
+    @Test public void testOmeletteCanHaveDifferentCheeseWithAFunnyComponentAdapter() {
+        Map<String,Cheese> cheeseMap = new HashMap<String,Cheese>();
+
+        MutableContainer pico = new DefaultContainer(new ConstructorInjection());
+        pico.addComponent(Omelette.class);
+        pico.addAdapter(new CheeseAdapter("scott", Gouda.class, cheeseMap));
+
+        Cheese gouda = new Gouda();
+        cheeseMap.put("cheese", gouda);
+        Omelette goudaOmelette = pico.getComponent(Omelette.class);
+        assertSame(gouda, goudaOmelette.getCheese());
+
+        Cheese roquefort = new Roquefort();
+        cheeseMap.put("cheese", roquefort);
+        Omelette roquefortOmelette = pico.getComponent(Omelette.class);
+        assertSame(roquefort, roquefortOmelette.getCheese());
+    }
+
+    public static interface InterfaceX {
+        String getIt();
+    }
+
+    public static class Enabled implements InterfaceX {
+        public String getIt() {
+            return "Enabled";
+        }
+    }
+
+    public static class Disabled implements InterfaceX {
+        public String getIt() {
+            return "Disabled";
+        }
+    }
+
+    public static class Something implements InterfaceX {
+        private final Disabled disabled;
+        private final Enabled enabled;
+        private final Map map;
+
+        public Something(Disabled disabled, Enabled enabled, Map map) {
+            this.disabled = disabled;
+            this.enabled = enabled;
+            this.map = map;
+        }
+
+        public String getIt() {
+            if (map.get("enabled") == null) {
+                return disabled.getIt();
+            } else {
+                return enabled.getIt();
+            }
+        }
+    }
+
+    public static class NeedsInterfaceX {
+        private final InterfaceX interfaceX;
+
+        public NeedsInterfaceX(InterfaceX interfaceX) {
+            this.interfaceX = interfaceX;
+        }
+
+        public String getIt() {
+            return interfaceX.getIt();
+        }
+    }
+
+    @Test public void testMoreWeirdness() {
+        MutableContainer pico = new DefaultContainer();
+        Map<String,String> map = new HashMap<String,String>();
+        pico.addComponent(map);
+        // See class level javadoc in DefaultPicoContainer - about precedence. 
+        pico.addComponent(InterfaceX.class, Something.class);
+        pico.addComponent(Disabled.class);
+        pico.addComponent(Enabled.class);
+        pico.addComponent(NeedsInterfaceX.class);
+
+        NeedsInterfaceX needsInterfaceX = pico.getComponent(NeedsInterfaceX.class);
+        assertEquals("Disabled", needsInterfaceX.getIt());
+        map.put("enabled", "blah");
+        assertEquals("Enabled", needsInterfaceX.getIt());
+    }
+
+    // From John Tal 23/03/2004
+    public static interface ABC {
+    }
+
+    public static interface DEF {
+    }
+
+    public static class ABCImpl implements ABC {
+        public ABCImpl(DEF def) {
+        }
+    }
+
+    public static class DEFImpl implements DEF {
+        public DEFImpl() {
+        }
+    }
+
+    @Test public void testJohnTalOne() {
+        MutableContainer picoContainer = new DefaultContainer();
+
+        picoContainer.addComponent("ABC", ABCImpl.class);
+        picoContainer.addComponent("DEF", DEFImpl.class);
+
+        assertEquals(ABCImpl.class, picoContainer.getComponent("ABC").getClass());
+    }
+
+    public static interface Foo {
+    }
+
+    public static interface Bar {
+    }
+
+    public static class FooBar implements Foo, Bar {
+    }
+
+    public static class NeedsFoo {
+        private final Foo foo;
+
+        public NeedsFoo(Foo foo) {
+            this.foo = foo;
+        }
+
+        public Foo getFoo() {
+            return foo;
+        }
+    }
+
+    public static class NeedsBar {
+        private final Bar bar;
+
+        public NeedsBar(Bar bar) {
+            this.bar = bar;
+        }
+
+        public Bar getBar() {
+            return bar;
+        }
+    }
+
+    @Test public void testShouldBeAbleShareSameReferenceForDifferentTypes() {
+        MutableContainer pico = new DefaultContainer();
+        pico.as(Characteristics.CACHE).addComponent(FooBar.class);
+        pico.addComponent(NeedsFoo.class);
+        pico.addComponent(NeedsBar.class);
+        NeedsFoo needsFoo = pico.getComponent(NeedsFoo.class);
+        NeedsBar needsBar = pico.getComponent(NeedsBar.class);
+        assertSame(needsFoo.getFoo(), needsBar.getBar());
+    }
+
+    @Test public void testSeveralDifferentInstancesCanBeCreatedWithOnePreconfiguredContainer() {
+        // create a container that doesn't cache instances
+        MutableContainer container = new DefaultContainer(new ConstructorInjection());
+        container.addComponent(NeedsBar.class);
+
+        Bar barOne = new FooBar();
+        container.addComponent(Bar.class, barOne);
+        NeedsBar needsBarOne = container.getComponent(NeedsBar.class);
+        assertSame(barOne, needsBarOne.getBar());
+
+        // reuse the same container - just flip out the existing foo.
+        Bar barTwo = new FooBar();
+        container.removeComponent(Bar.class);
+        container.addComponent(Bar.class, barTwo);
+        NeedsBar needsBarTwo = container.getComponent(NeedsBar.class);
+        assertSame(barTwo, needsBarTwo.getBar());
+
+        assertNotSame(needsBarOne, needsBarTwo);
+    }
+}
\ No newline at end of file



Mime
View raw message