? GenericCollectionComponentAdapter.patch Index: src/java/org/picocontainer/defaults/GenericCollectionComponentAdapter.java =================================================================== RCS file: /home/projects/picocontainer/scm/java/picocontainer/src/java/org/picocontainer/defaults/GenericCollectionComponentAdapter.java,v retrieving revision 1.6 diff -b -u -r1.6 GenericCollectionComponentAdapter.java --- src/java/org/picocontainer/defaults/GenericCollectionComponentAdapter.java 10 Jun 2004 00:04:39 -0000 1.6 +++ src/java/org/picocontainer/defaults/GenericCollectionComponentAdapter.java 4 Aug 2004 14:54:14 -0000 @@ -26,13 +26,13 @@ private final Class collectionClass; public GenericCollectionComponentAdapter(Object componentKey, Class keyType, Class valueType, Class collectionType) { - super(componentKey, HashMap.class); + super(componentKey, collectionType); this.keyType = keyType; this.valueType = valueType; this.collectionType = collectionType; // The order of tests are significant. The least generic types last. - if (Array.class.isAssignableFrom(collectionType)) { + if (Array.class.isAssignableFrom(collectionType) || collectionType.isArray()) { collectionClass = Array.class; // TODO: uncomment when we start supporting generics // } else if (List.class.isAssignableFrom(collectionType)) { Index: src/test/org/picocontainer/defaults/GenericCollectionComponentAdapterTestCase.java =================================================================== RCS file: /home/projects/picocontainer/scm/java/picocontainer/src/test/org/picocontainer/defaults/GenericCollectionComponentAdapterTestCase.java,v retrieving revision 1.3 diff -b -u -r1.3 GenericCollectionComponentAdapterTestCase.java --- src/test/org/picocontainer/defaults/GenericCollectionComponentAdapterTestCase.java 24 May 2004 21:28:22 -0000 1.3 +++ src/test/org/picocontainer/defaults/GenericCollectionComponentAdapterTestCase.java 4 Aug 2004 14:54:14 -0000 @@ -3,10 +3,14 @@ import org.jmock.Mock; import org.jmock.MockObjectTestCase; import org.picocontainer.ComponentAdapter; +import org.picocontainer.MutablePicoContainer; +import org.picocontainer.Parameter; import org.picocontainer.PicoContainer; +import org.picocontainer.alternatives.ImplementationHidingPicoContainer; import java.lang.reflect.Array; import java.util.Arrays; +import java.util.Collection; import java.util.List; /** @@ -15,7 +19,7 @@ */ public class GenericCollectionComponentAdapterTestCase extends MockObjectTestCase { public void testShouldInstantiateArrayOfStrings() { - GenericCollectionComponentAdapter ca = new GenericCollectionComponentAdapter("x", null, String.class, Array.class); + GenericCollectionComponentAdapter ca = new GenericCollectionComponentAdapter("x", null, String.class, String[].class); Mock containerMock = mock(PicoContainer.class); containerMock.expects(once()). @@ -30,6 +34,57 @@ List expected = Arrays.asList(new String[]{"Hello", "World"}); List actual = Arrays.asList((Object[]) ca.getComponentInstance()); assertEquals(expected, actual); + } + + public static interface Component {} + + public static class CompositeComponent implements Component { + Object[] children; + + public CompositeComponent(Object[] children) { + this.children = children; + } + public String toString() { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < children.length; i++) { + buf.append(children[i].toString()); + } + return buf.toString(); + } + } + + public static class LeafComponent implements Component { + String name; + public LeafComponent(String name) { + this.name = name; + } + public String toString() { + return name; + } + } + + public void testInstantiateCompositeDependingOnComponentArrayRequiresPreInstantiationAndCaching() { + GenericCollectionComponentAdapter ca = new GenericCollectionComponentAdapter("x", null, Component.class, Object[].class); + MutablePicoContainer pc = new DefaultPicoContainer(); + pc.registerComponentImplementation(LeafComponent.class); + pc.registerComponentInstance("apple"); + pc.registerComponent(new CachingComponentAdapter(ca)); + Collection adapters = pc.getComponentAdaptersOfType(Component.class); + assertEquals(1, adapters.size()); + + Object[] arr = (Object[]) pc.getComponentInstance("x"); + + assertNotNull(arr); + assertEquals(1, arr.length); + + pc.registerComponentImplementation(CompositeComponent.class, + CompositeComponent.class, + new Parameter[] {new ComponentParameter("x")}); + + Component cc = (Component) pc.getComponentInstance(CompositeComponent.class); + + assertNotNull(cc); + assertEquals("apple", cc.toString()); } // todo similar tests for generic collections