osdir.com
mailing list archive

Subject: [picocontainer-commits] [CVS java] Add hot swapping use case from Alex Shneydermann. - msg#00098

List: java.picocontainer.cvs

Date: Prev Next Index Thread: Prev Next Index
Commit in java/picocontainer/src/test/org/picocontainer/defaults on MAIN UserQuestionTestCase.java+28-31.1 -> 1.2
Add hot swapping use case from Alex Shneydermann.
java/picocontainer/src/test/org/picocontainer/defaults
UserQuestionTestCase.java 1.1 -> 1.2
diff -u -r1.1 -r1.2
--- UserQuestionTestCase.java	24 Mar 2004 11:13:48 -0000	1.1
+++ UserQuestionTestCase.java	24 Mar 2004 11:48:14 -0000	1.2
@@ -13,7 +13,7 @@
  * Or to answer questions.
  *
  * @author Aslak Hellesøy
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
  */
 public class UserQuestionTestCase extends TestCase {
 
@@ -96,6 +96,31 @@
         assertEquals("Roquefort", flexibleOmelette.getCheese().getName());
     }
 
+    // From Alex Shneyderman 23/03/2004
+    public void testMultipleOmelettesCanHaveDifferentCheeseUsingSingleSwapping() {
+        MutablePicoContainer pico = new DefaultPicoContainer(
+                new CachingComponentAdapterFactory(
+                        new ImplementationHidingComponentAdapterFactory(
+                                new ConstructorInjectionComponentAdapterFactory(), false)));
+
+        pico.registerComponentImplementation("Omelette1", Omelette.class);
+        pico.registerComponentImplementation("Omelette2", Omelette.class);
+        pico.registerComponentImplementation(Cheese.class, Gouda.class);
+
+        Omelette flexibleOmelette1 = (Omelette) pico.getComponentInstance("Omelette1");
+        Omelette flexibleOmelette2 = (Omelette) pico.getComponentInstance("Omelette2");
+        assertNotSame(flexibleOmelette1, flexibleOmelette2);
+        assertSame(flexibleOmelette1.getCheese(), flexibleOmelette2.getCheese());
+
+        // Let's swap the cheese for all omelettes :)
+        Cheese cheese = (Cheese) pico.getComponentInstance(Cheese.class);
+        Swappable swappableCheese = (Swappable) cheese;
+        swappableCheese.hotswap(new Roquefort());
+        
+        assertEquals("Roquefort", flexibleOmelette1.getCheese().getName());
+        assertEquals("Roquefort", flexibleOmelette2.getCheese().getName());
+    }
+
     public static interface InterfaceX {
         String getIt();
     }
@@ -160,7 +185,7 @@
         assertEquals("Enabled", needsInterfaceX.getIt());
     }
 
-    // From Jon Tal 23/03/2004
+    // From John Tal 23/03/2004
     public static interface ABC {
     }
 
@@ -177,7 +202,7 @@
         }
     }
 
-    public void testJohnTahlOne() {
+    public void testJohnTalOne() {
         MutablePicoContainer picoContainer = new DefaultPicoContainer();
 
         picoContainer.registerComponentImplementation("ABC",ABCImpl.class);
Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

[picocontainer-commits] [CVS java] test case for john tal's question

Commit in java/picocontainer/src/test/org/picocontainer/defaults on MAIN UserQuestionTestCase.java+189added 1.1 FarquharTestCase.java-1541.4 removed +189-154 1 added + 1 removed, total 2 files test case for john tal's question java/picocontainer/src/test/org/picocontainer/defaults UserQuestionTestCase.java added at 1.1 diff -N UserQuestionTestCase.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ UserQuestionTestCase.java 24 Mar 2004 11:13:48 -0000 1.1 @@ -0,0 +1,189 @@ +package org.picocontainer.defaults; + +import junit.framework.TestCase; +import org.picocontainer.PicoInitializationException; +import org.picocontainer.PicoIntrospectionException; +import org.picocontainer.MutablePicoContainer; + +import java.util.Map; +import java.util.HashMap; + +/** + * This class can be used to test out various things asked on the mailing list. + * Or to answer questions. + * + * @author Aslak Hellesøy + * @version $Revision: 1.1 $ + */ +public class UserQuestionTestCase extends TestCase { + + // From Scott Farquahsr + public static class CheeseComponentAdapter extends AbstractComponentAdapter { + private Map bla; + + public CheeseComponentAdapter(Object componentKey, Class componentImplementation, Map cheeseMap) throws AssignabilityRegistrationException, NotConcreteRegistrationException { + super(componentKey, componentImplementation); + this.bla = cheeseMap; + } + + public Object getComponentInstance() throws PicoInitializationException, PicoIntrospectionException { + return bla.get("cheese"); + } + + public void verify() throws UnsatisfiableDependenciesException { + + } + } + + 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; + } + } + + public void testOmeletteCanHaveDifferentCheeseWithAFunnyComponentAdapter() { + Map cheeseMap = new HashMap(); + + MutablePicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory()); + pico.registerComponentImplementation(Omelette.class); + pico.registerComponent(new CheeseComponentAdapter("scott", Gouda.class, cheeseMap)); + + Cheese gouda = new Gouda(); + cheeseMap.put("cheese", gouda); + Omelette goudaOmelette = (Omelette) pico.getComponentInstance(Omelette.class); + assertSame(gouda, goudaOmelette.getCheese()); + + Cheese roquefort = new Roquefort(); + cheeseMap.put("cheese", roquefort); + Omelette roquefortOmelette = (Omelette) pico.getComponentInstance(Omelette.class); + assertSame(roquefort, roquefortOmelette.getCheese()); + } + + public void testOmeletteCanHaveDifferentCheeseUsingSwapping() { + MutablePicoContainer pico = new DefaultPicoContainer(new ImplementationHidingComponentAdapterFactory(new DefaultComponentAdapterFactory(), false)); + + pico.registerComponentImplementation(Omelette.class); + pico.registerComponentImplementation(Cheese.class, Gouda.class); + + Omelette flexibleOmelette = (Omelette) pico.getComponentInstance(Omelette.class); + assertEquals("Gouda", flexibleOmelette.getCheese().getName()); + + // Let's swap the cheese without creating a new omelette + Swappable swappableCheese = (Swappable) flexibleOmelette.getCheese(); + swappableCheese.hotswap(new Roquefort()); + assertEquals("Roquefort", flexibleOmelette.getCheese().getName()); + } + + 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(); + } + } + + public void testMoreWeirdness() { + MutablePicoContainer pico = new DefaultPicoContainer(); + Map map = new HashMap(); + pico.registerComponentInstance(map); + // See class level javadoc in DefaultPicoContainer - about precedence. + pico.registerComponentImplementation(InterfaceX.class, Something.class); + pico.registerComponentImplementation(Disabled.class); + pico.registerComponentImplementation(Enabled.class); + pico.registerComponentImplementation(NeedsInterfaceX.class); + + NeedsInterfaceX needsInterfaceX = (NeedsInterfaceX) pico.getComponentInstance(NeedsInterfaceX.class); + assertEquals("Disabled", needsInterfaceX.getIt()); + map.put("enabled", "blah"); + assertEquals("Enabled", needsInterfaceX.getIt()); + } + + // From Jon 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() { + } + } + + public void testJohnTahlOne() { + MutablePicoContainer picoContainer = new DefaultPicoContainer(); + + picoContainer.registerComponentImplementation("ABC",ABCImpl.class); + picoContainer.registerComponentImplementation("DEF",DEFImpl.class); + + assertEquals(ABCImpl.class, picoContainer.getComponentInstance("ABC").getClass()); + } + +} \ No newline at end of file java/picocontainer/src/test/org/picocontainer/defaults FarquharTestCase.java removed after 1.4 diff -N FarquharTestCase.java --- FarquharTestCase.java 15 Mar 2004 20:58:50 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,154 +0,0 @@ -package org.picocontainer.defaults; - -import junit.framework.TestCase; -import org.picocontainer.PicoInitializationException; -import org.picocontainer.PicoIntrospectionException; -import org.picocontainer.MutablePicoContainer; - -import java.util.Map; -import java.util.HashMap; - -/** - * @author Aslak Hellesøy - * @version $Revision: 1.4 $ - */ -public class FarquharTestCase extends TestCase { - - public static class CheeseComponentAdapter extends AbstractComponentAdapter { - private Map bla; - - public CheeseComponentAdapter(Object componentKey, Class componentImplementation, Map cheeseMap) throws AssignabilityRegistrationException, NotConcreteRegistrationException { - super(componentKey, componentImplementation); - this.bla = cheeseMap; - } - - public Object getComponentInstance() throws PicoInitializationException, PicoIntrospectionException { - return bla.get("cheese"); - } - - public void verify() throws UnsatisfiableDependenciesException { - - } - } - - 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; - } - } - - public void testOmeletteCanHaveDifferentCheeseWithAFunnyComponentAdapter() { - Map cheeseMap = new HashMap(); - - MutablePicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory()); - pico.registerComponentImplementation(Omelette.class); - pico.registerComponent(new CheeseComponentAdapter("scott", Gouda.class, cheeseMap)); - - Cheese gouda = new Gouda(); - cheeseMap.put("cheese", gouda); - Omelette goudaOmelette = (Omelette) pico.getComponentInstance(Omelette.class); - assertSame(gouda, goudaOmelette.getCheese()); - - Cheese roquefort = new Roquefort(); - cheeseMap.put("cheese", roquefort); - Omelette roquefortOmelette = (Omelette) pico.getComponentInstance(Omelette.class); - assertSame(roquefort, roquefortOmelette.getCheese()); - } - - public void testOmeletteCanHaveDifferentCheeseUsingSwapping() { - MutablePicoContainer pico = new DefaultPicoContainer( - new ImplementationHidingComponentAdapterFactory( - new DefaultComponentAdapterFactory(),false)); - - pico.registerComponentImplementation(Omelette.class); - pico.registerComponentImplementation(Cheese.class, Gouda.class); - - Omelette flexibleOmelette = (Omelette) pico.getComponentInstance(Omelette.class); - assertEquals("Gouda", flexibleOmelette.getCheese().getName()); - - // Let's swap the cheese without creating a new omelette - Swappable swappableCheese = (Swappable) flexibleOmelette.getCheese(); - swappableCheese.hotswap(new Roquefort()); - assertEquals("Roquefort", flexibleOmelette.getCheese().getName()); - } - - 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(); - } - } - - public void testMoreWeirdness() { - MutablePicoContainer pico = new DefaultPicoContainer(); - Map map = new HashMap(); - pico.registerComponentInstance(map); - // See class level javadoc in DefaultPicoContainer - about precedence. - pico.registerComponentImplementation(InterfaceX.class, Something.class); - pico.registerComponentImplementation(Disabled.class); - pico.registerComponentImplementation(Enabled.class); - pico.registerComponentImplementation(NeedsInterfaceX.class); - - NeedsInterfaceX needsInterfaceX = (NeedsInterfaceX) pico.getComponentInstance(NeedsInterfaceX.class); - assertEquals("Disabled", needsInterfaceX.getIt()); - map.put("enabled", "blah"); - assertEquals("Enabled", needsInterfaceX.getIt()); - } - -} \ No newline at end of file

Next Message by Date: click to view message preview

[picocontainer-commits] [CVS java] Fixed PICO-157: http://jira.codehaus.org/secure/ViewIssue.jspa?key=PICO-157

Commit in java/picocontainer/src on MAIN java/org/picocontainer/defaults/ConstructorInjectionComponentAdapter.java+2-21.3 -> 1.4 test/org/picocontainer/tck/AbstractPicoContainerTestCase.java+3-91.26 -> 1.27 +5-11 2 modified files Fixed PICO-157: http://jira.codehaus.org/secure/ViewIssue.jspa?key=PICO-157 java/picocontainer/src/java/org/picocontainer/defaults ConstructorInjectionComponentAdapter.java 1.3 -> 1.4 diff -u -r1.3 -r1.4 --- ConstructorInjectionComponentAdapter.java 12 Mar 2004 11:26:19 -0000 1.3 +++ ConstructorInjectionComponentAdapter.java 24 Mar 2004 16:15:48 -0000 1.4 @@ -42,7 +42,7 @@ * @author Jon Tirsén * @author Zohar Melamed * @author Jörg Schaible - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ public class ConstructorInjectionComponentAdapter extends InstantiatingComponentAdapter { private transient boolean instantiating; @@ -103,7 +103,7 @@ ComponentAdapter adapter = currentParameters[j].resolveAdapter(getContainer(), parameterTypes[j]); if (adapter == null) { failedDependency = true; - unsatisfiableDependencyTypes.add(Arrays.asList(parameterTypes)); + unsatisfiableDependencyTypes.add(parameterTypes[j]); } else { // we can't depend on ourself if (adapter.equals(this)) { java/picocontainer/src/test/org/picocontainer/tck AbstractPicoContainerTestCase.java 1.26 -> 1.27 diff -u -r1.26 -r1.27 --- AbstractPicoContainerTestCase.java 9 Mar 2004 12:18:23 -0000 1.26 +++ AbstractPicoContainerTestCase.java 24 Mar 2004 16:15:49 -0000 1.27 @@ -44,12 +44,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.HashSet; /** * This test tests (at least it should) all the methods in MutablePicoContainer. @@ -126,7 +126,7 @@ // Touchable.class is now inside a List (the list of unsatisfied parameters) -- mparaz Object unstaisifed = unsatisfiableDependencies.iterator().next(); - assertEquals(Collections.singletonList(Touchable.class), unstaisifed); + assertEquals(Touchable.class, unstaisifed); } } @@ -197,18 +197,12 @@ // The set now contains a list containing the two dependencies in // order. Therefore, we can't use the original code. - mparaz - assertEquals(1, unsatisfiableDependencies.size()); final List expectedList = new ArrayList(2); expectedList.add(ComponentE.class); expectedList.add(ComponentB.class); - // Convert the Set to a List and assert that its first and only - // element is the expected list. This is a stronger check than - // contains(). - // - mparaz - assertEquals(new ArrayList(unsatisfiableDependencies).get(0), - expectedList); + assertEquals(new HashSet(expectedList), unsatisfiableDependencies); } }

Previous Message by Thread: click to view message preview

[picocontainer-commits] [CVS java] test case for john tal's question

Commit in java/picocontainer/src/test/org/picocontainer/defaults on MAIN UserQuestionTestCase.java+189added 1.1 FarquharTestCase.java-1541.4 removed +189-154 1 added + 1 removed, total 2 files test case for john tal's question java/picocontainer/src/test/org/picocontainer/defaults UserQuestionTestCase.java added at 1.1 diff -N UserQuestionTestCase.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ UserQuestionTestCase.java 24 Mar 2004 11:13:48 -0000 1.1 @@ -0,0 +1,189 @@ +package org.picocontainer.defaults; + +import junit.framework.TestCase; +import org.picocontainer.PicoInitializationException; +import org.picocontainer.PicoIntrospectionException; +import org.picocontainer.MutablePicoContainer; + +import java.util.Map; +import java.util.HashMap; + +/** + * This class can be used to test out various things asked on the mailing list. + * Or to answer questions. + * + * @author Aslak Hellesøy + * @version $Revision: 1.1 $ + */ +public class UserQuestionTestCase extends TestCase { + + // From Scott Farquahsr + public static class CheeseComponentAdapter extends AbstractComponentAdapter { + private Map bla; + + public CheeseComponentAdapter(Object componentKey, Class componentImplementation, Map cheeseMap) throws AssignabilityRegistrationException, NotConcreteRegistrationException { + super(componentKey, componentImplementation); + this.bla = cheeseMap; + } + + public Object getComponentInstance() throws PicoInitializationException, PicoIntrospectionException { + return bla.get("cheese"); + } + + public void verify() throws UnsatisfiableDependenciesException { + + } + } + + 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; + } + } + + public void testOmeletteCanHaveDifferentCheeseWithAFunnyComponentAdapter() { + Map cheeseMap = new HashMap(); + + MutablePicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory()); + pico.registerComponentImplementation(Omelette.class); + pico.registerComponent(new CheeseComponentAdapter("scott", Gouda.class, cheeseMap)); + + Cheese gouda = new Gouda(); + cheeseMap.put("cheese", gouda); + Omelette goudaOmelette = (Omelette) pico.getComponentInstance(Omelette.class); + assertSame(gouda, goudaOmelette.getCheese()); + + Cheese roquefort = new Roquefort(); + cheeseMap.put("cheese", roquefort); + Omelette roquefortOmelette = (Omelette) pico.getComponentInstance(Omelette.class); + assertSame(roquefort, roquefortOmelette.getCheese()); + } + + public void testOmeletteCanHaveDifferentCheeseUsingSwapping() { + MutablePicoContainer pico = new DefaultPicoContainer(new ImplementationHidingComponentAdapterFactory(new DefaultComponentAdapterFactory(), false)); + + pico.registerComponentImplementation(Omelette.class); + pico.registerComponentImplementation(Cheese.class, Gouda.class); + + Omelette flexibleOmelette = (Omelette) pico.getComponentInstance(Omelette.class); + assertEquals("Gouda", flexibleOmelette.getCheese().getName()); + + // Let's swap the cheese without creating a new omelette + Swappable swappableCheese = (Swappable) flexibleOmelette.getCheese(); + swappableCheese.hotswap(new Roquefort()); + assertEquals("Roquefort", flexibleOmelette.getCheese().getName()); + } + + 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(); + } + } + + public void testMoreWeirdness() { + MutablePicoContainer pico = new DefaultPicoContainer(); + Map map = new HashMap(); + pico.registerComponentInstance(map); + // See class level javadoc in DefaultPicoContainer - about precedence. + pico.registerComponentImplementation(InterfaceX.class, Something.class); + pico.registerComponentImplementation(Disabled.class); + pico.registerComponentImplementation(Enabled.class); + pico.registerComponentImplementation(NeedsInterfaceX.class); + + NeedsInterfaceX needsInterfaceX = (NeedsInterfaceX) pico.getComponentInstance(NeedsInterfaceX.class); + assertEquals("Disabled", needsInterfaceX.getIt()); + map.put("enabled", "blah"); + assertEquals("Enabled", needsInterfaceX.getIt()); + } + + // From Jon 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() { + } + } + + public void testJohnTahlOne() { + MutablePicoContainer picoContainer = new DefaultPicoContainer(); + + picoContainer.registerComponentImplementation("ABC",ABCImpl.class); + picoContainer.registerComponentImplementation("DEF",DEFImpl.class); + + assertEquals(ABCImpl.class, picoContainer.getComponentInstance("ABC").getClass()); + } + +} \ No newline at end of file java/picocontainer/src/test/org/picocontainer/defaults FarquharTestCase.java removed after 1.4 diff -N FarquharTestCase.java --- FarquharTestCase.java 15 Mar 2004 20:58:50 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,154 +0,0 @@ -package org.picocontainer.defaults; - -import junit.framework.TestCase; -import org.picocontainer.PicoInitializationException; -import org.picocontainer.PicoIntrospectionException; -import org.picocontainer.MutablePicoContainer; - -import java.util.Map; -import java.util.HashMap; - -/** - * @author Aslak Hellesøy - * @version $Revision: 1.4 $ - */ -public class FarquharTestCase extends TestCase { - - public static class CheeseComponentAdapter extends AbstractComponentAdapter { - private Map bla; - - public CheeseComponentAdapter(Object componentKey, Class componentImplementation, Map cheeseMap) throws AssignabilityRegistrationException, NotConcreteRegistrationException { - super(componentKey, componentImplementation); - this.bla = cheeseMap; - } - - public Object getComponentInstance() throws PicoInitializationException, PicoIntrospectionException { - return bla.get("cheese"); - } - - public void verify() throws UnsatisfiableDependenciesException { - - } - } - - 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; - } - } - - public void testOmeletteCanHaveDifferentCheeseWithAFunnyComponentAdapter() { - Map cheeseMap = new HashMap(); - - MutablePicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory()); - pico.registerComponentImplementation(Omelette.class); - pico.registerComponent(new CheeseComponentAdapter("scott", Gouda.class, cheeseMap)); - - Cheese gouda = new Gouda(); - cheeseMap.put("cheese", gouda); - Omelette goudaOmelette = (Omelette) pico.getComponentInstance(Omelette.class); - assertSame(gouda, goudaOmelette.getCheese()); - - Cheese roquefort = new Roquefort(); - cheeseMap.put("cheese", roquefort); - Omelette roquefortOmelette = (Omelette) pico.getComponentInstance(Omelette.class); - assertSame(roquefort, roquefortOmelette.getCheese()); - } - - public void testOmeletteCanHaveDifferentCheeseUsingSwapping() { - MutablePicoContainer pico = new DefaultPicoContainer( - new ImplementationHidingComponentAdapterFactory( - new DefaultComponentAdapterFactory(),false)); - - pico.registerComponentImplementation(Omelette.class); - pico.registerComponentImplementation(Cheese.class, Gouda.class); - - Omelette flexibleOmelette = (Omelette) pico.getComponentInstance(Omelette.class); - assertEquals("Gouda", flexibleOmelette.getCheese().getName()); - - // Let's swap the cheese without creating a new omelette - Swappable swappableCheese = (Swappable) flexibleOmelette.getCheese(); - swappableCheese.hotswap(new Roquefort()); - assertEquals("Roquefort", flexibleOmelette.getCheese().getName()); - } - - 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(); - } - } - - public void testMoreWeirdness() { - MutablePicoContainer pico = new DefaultPicoContainer(); - Map map = new HashMap(); - pico.registerComponentInstance(map); - // See class level javadoc in DefaultPicoContainer - about precedence. - pico.registerComponentImplementation(InterfaceX.class, Something.class); - pico.registerComponentImplementation(Disabled.class); - pico.registerComponentImplementation(Enabled.class); - pico.registerComponentImplementation(NeedsInterfaceX.class); - - NeedsInterfaceX needsInterfaceX = (NeedsInterfaceX) pico.getComponentInstance(NeedsInterfaceX.class); - assertEquals("Disabled", needsInterfaceX.getIt()); - map.put("enabled", "blah"); - assertEquals("Enabled", needsInterfaceX.getIt()); - } - -} \ No newline at end of file

Next Message by Thread: click to view message preview

[picocontainer-commits] [CVS java] Fixed PICO-157: http://jira.codehaus.org/secure/ViewIssue.jspa?key=PICO-157

Commit in java/picocontainer/src on MAIN java/org/picocontainer/defaults/ConstructorInjectionComponentAdapter.java+2-21.3 -> 1.4 test/org/picocontainer/tck/AbstractPicoContainerTestCase.java+3-91.26 -> 1.27 +5-11 2 modified files Fixed PICO-157: http://jira.codehaus.org/secure/ViewIssue.jspa?key=PICO-157 java/picocontainer/src/java/org/picocontainer/defaults ConstructorInjectionComponentAdapter.java 1.3 -> 1.4 diff -u -r1.3 -r1.4 --- ConstructorInjectionComponentAdapter.java 12 Mar 2004 11:26:19 -0000 1.3 +++ ConstructorInjectionComponentAdapter.java 24 Mar 2004 16:15:48 -0000 1.4 @@ -42,7 +42,7 @@ * @author Jon Tirsén * @author Zohar Melamed * @author Jörg Schaible - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ public class ConstructorInjectionComponentAdapter extends InstantiatingComponentAdapter { private transient boolean instantiating; @@ -103,7 +103,7 @@ ComponentAdapter adapter = currentParameters[j].resolveAdapter(getContainer(), parameterTypes[j]); if (adapter == null) { failedDependency = true; - unsatisfiableDependencyTypes.add(Arrays.asList(parameterTypes)); + unsatisfiableDependencyTypes.add(parameterTypes[j]); } else { // we can't depend on ourself if (adapter.equals(this)) { java/picocontainer/src/test/org/picocontainer/tck AbstractPicoContainerTestCase.java 1.26 -> 1.27 diff -u -r1.26 -r1.27 --- AbstractPicoContainerTestCase.java 9 Mar 2004 12:18:23 -0000 1.26 +++ AbstractPicoContainerTestCase.java 24 Mar 2004 16:15:49 -0000 1.27 @@ -44,12 +44,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.HashSet; /** * This test tests (at least it should) all the methods in MutablePicoContainer. @@ -126,7 +126,7 @@ // Touchable.class is now inside a List (the list of unsatisfied parameters) -- mparaz Object unstaisifed = unsatisfiableDependencies.iterator().next(); - assertEquals(Collections.singletonList(Touchable.class), unstaisifed); + assertEquals(Touchable.class, unstaisifed); } } @@ -197,18 +197,12 @@ // The set now contains a list containing the two dependencies in // order. Therefore, we can't use the original code. - mparaz - assertEquals(1, unsatisfiableDependencies.size()); final List expectedList = new ArrayList(2); expectedList.add(ComponentE.class); expectedList.add(ComponentB.class); - // Convert the Set to a List and assert that its first and only - // element is the expected list. This is a stronger check than - // contains(). - // - mparaz - assertEquals(new ArrayList(unsatisfiableDependencies).get(0), - expectedList); + assertEquals(new HashSet(expectedList), unsatisfiableDependencies); } }
Sign up for updates to this mailing list. email:
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by