Title: [waffle-scm] [658] trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar: WAFFLE-77: Added Registrar.getRegistered() method.

Diff

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/AbstractRegistrar.java (657 => 658)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/AbstractRegistrar.java	2008-04-29 12:49:56 UTC (rev 657)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/AbstractRegistrar.java	2008-04-29 14:14:50 UTC (rev 658)
@@ -15,6 +15,7 @@
  * for use in their applications.
  *
  * @author Michael Ward
+ * @author Mauro Talevi
  */
 public abstract class AbstractRegistrar implements Registrar {
     private final Registrar delegate;
@@ -32,6 +33,10 @@
         return delegate.isRegistered(typeOrInstance);
     }
     
+    public Object getRegistered(Object typeOrInstance){
+        return delegate.getRegistered(typeOrInstance);
+    }
+    
     public Registrar register(Class<?> type, Object... parameters) {
         delegate.register(type, parameters);
         return this;

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/Registrar.java (657 => 658)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/Registrar.java	2008-04-29 12:49:56 UTC (rev 657)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/Registrar.java	2008-04-29 14:14:50 UTC (rev 658)
@@ -42,6 +42,15 @@
      * @return A boolean flag, <code>true</code> if component is registered
      */
     boolean isRegistered(Object typeOrInstance);
+    
+    /**
+     * Returns a registered component 
+     * 
+     * @param typeOrInstance the component Class type or Object instance/key
+     * @return The registered component instance
+     * @throws RegistrarException if component not registered or more than one instance found
+     */
+    Object getRegistered(Object typeOrInstance);
 
     /**
      * Registers a component in the current context.

Added: trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/RegistrarException.java (0 => 658)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/RegistrarException.java	                        (rev 0)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/RegistrarException.java	2008-04-29 14:14:50 UTC (rev 658)
@@ -0,0 +1,24 @@
+/*****************************************************************************
+ * Copyright (c) 2005-2008 Michael Ward                                      *
+ * 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.                                                     *
+ *****************************************************************************/
+package org.codehaus.waffle.registrar;
+
+import org.codehaus.waffle.WaffleException;
+
[EMAIL PROTECTED]("serial")
+public class RegistrarException extends WaffleException {
+
+    public RegistrarException(String message) {
+        super(message);
+    }
+
+    public RegistrarException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+}

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/pico/PicoRegistrar.java (657 => 658)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/pico/PicoRegistrar.java	2008-04-29 12:49:56 UTC (rev 657)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/registrar/pico/PicoRegistrar.java	2008-04-29 14:14:50 UTC (rev 658)
@@ -10,8 +10,11 @@
  *****************************************************************************/
 package org.codehaus.waffle.registrar.pico;
 
+import java.util.List;
+
 import org.codehaus.waffle.monitor.RegistrarMonitor;
 import org.codehaus.waffle.registrar.Registrar;
+import org.codehaus.waffle.registrar.RegistrarException;
 import org.codehaus.waffle.registrar.RubyAwareRegistrar;
 import org.picocontainer.ComponentAdapter;
 import org.picocontainer.MutablePicoContainer;
@@ -57,6 +60,25 @@
         return picoContainer.getComponentInstance(typeOrInstance) != null;
     }
 
+    public Object getRegistered(Object typeOrInstance) {
+        if (typeOrInstance instanceof Class) {
+            Class<?> type = (Class<?>) typeOrInstance;
+            List<?> instances = picoContainer.getComponentInstancesOfType(type);
+            if ( instances.size() == 0 ){
+                throw new RegistrarException("No component instance registered for type "+type);
+            } else if ( instances.size() > 1 ){
+                throw new RegistrarException("More than one component instance registered for type "+type);
+            } else {
+                return instances.get(0);
+            }            
+        }
+        Object instance = picoContainer.getComponentInstance(typeOrInstance);
+        if ( instance == null ){
+            throw new RegistrarException("No component instance registered for type or instance "+typeOrInstance);
+        }
+        return instance;
+    }
+
     public Registrar register(Class<?> type, Object... parameters) {
         this.register(type, type, parameters);
         return this;

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/registrar/pico/PicoRegistrarTest.java (657 => 658)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/registrar/pico/PicoRegistrarTest.java	2008-04-29 12:49:56 UTC (rev 657)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/registrar/pico/PicoRegistrarTest.java	2008-04-29 14:14:50 UTC (rev 658)
@@ -15,6 +15,7 @@
 import org.codehaus.waffle.monitor.SilentMonitor;
 import org.codehaus.waffle.registrar.Registrar;
 import org.codehaus.waffle.registrar.ComponentReference;
+import org.codehaus.waffle.registrar.RegistrarException;
 import org.codehaus.waffle.testmodel.ComponentWithParameterDependencies;
 import org.codehaus.waffle.testmodel.ConstructorInjectionComponent;
 import org.codehaus.waffle.testmodel.FakeBean;
@@ -23,6 +24,8 @@
 import org.jmock.Expectations;
 import org.jmock.Mockery;
 import org.jmock.integration.junit4.JMock;
+
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
@@ -61,8 +64,8 @@
                 .register(type);
         assertTrue(registrar.isRegistered(type));
 
-        FakeController controllerOne = (FakeController) pico.getComponentInstance(FakeController.class);
-        FakeController controllerTwo = (FakeController) pico.getComponentInstance(FakeController.class);
+        FakeController controllerOne = (FakeController) registrar.getRegistered(FakeController.class);
+        FakeController controllerTwo = (FakeController) registrar.getRegistered(FakeController.class);
 
         assertSame(controllerOne, controllerTwo);
     }
@@ -84,8 +87,8 @@
         registrar.register(key, type);
         assertTrue(registrar.isRegistered(type));
 
-        FakeController controllerOne = (FakeController) pico.getComponentInstance(key);
-        FakeController controllerTwo = (FakeController) pico.getComponentInstance(key);
+        FakeController controllerOne = (FakeController) registrar.getRegistered(key);
+        FakeController controllerTwo = (FakeController) registrar.getRegistered(key);
 
         assertSame(controllerOne, controllerTwo);
         assertSame(controllerOne, pico.getComponentInstanceOfType(type));
@@ -107,7 +110,7 @@
         registrar.registerInstance(fakeController);
         assertTrue(registrar.isRegistered(fakeController));
 
-        assertSame(fakeController, pico.getComponentInstanceOfType(FakeController.class));
+        assertSame(fakeController, registrar.getRegistered(FakeController.class));
     }
 
     @Test
@@ -127,7 +130,7 @@
         registrar.registerInstance(key, fakeController);
         assertTrue(registrar.isRegistered(key));
 
-        assertSame(fakeController, pico.getComponentInstance("foobar"));
+        assertSame(fakeController, registrar.getRegistered("foobar"));
         assertSame(fakeController, pico.getComponentInstanceOfType(FakeController.class));
     }
 
@@ -147,8 +150,8 @@
         registrar.registerNonCaching(type);
         assertTrue(registrar.isRegistered(type));
 
-        FakeController controllerOne = (FakeController) pico.getComponentInstance(FakeController.class);
-        FakeController controllerTwo = (FakeController) pico.getComponentInstance(FakeController.class);
+        FakeController controllerOne = (FakeController) registrar.getRegistered(FakeController.class);
+        FakeController controllerTwo = (FakeController) registrar.getRegistered(FakeController.class);
 
         assertNotSame(controllerOne, controllerTwo);
     }
@@ -170,8 +173,8 @@
         registrar.registerNonCaching(key, type);
         assertTrue(registrar.isRegistered(type));
 
-        FakeController controllerOne = (FakeController) pico.getComponentInstance("foobar");
-        FakeController controllerTwo = (FakeController) pico.getComponentInstance("foobar");
+        FakeController controllerOne = (FakeController) registrar.getRegistered("foobar");
+        FakeController controllerTwo = (FakeController) registrar.getRegistered("foobar");
 
         assertNotSame(controllerOne, controllerTwo);
     }
@@ -183,10 +186,10 @@
         ConstructorInjectionComponentAdapter componentAdapter
                 = new ConstructorInjectionComponentAdapter("a", FakeController.class);
 
-        PicoRegistrar picoRegistrar = new PicoRegistrar(pico, null, lifecycleStrategy, new SilentMonitor());
-        picoRegistrar.registerComponentAdapter(componentAdapter);
+        PicoRegistrar registrar = new PicoRegistrar(pico, null, lifecycleStrategy, new SilentMonitor());
+        registrar.registerComponentAdapter(componentAdapter);
 
-        FakeController controllerOne = (FakeController) pico.getComponentInstance("a");
+        FakeController controllerOne = (FakeController) registrar.getRegistered("a");
         FakeController controllerTwo = (FakeController) pico.getComponentInstanceOfType(FakeController.class);
 
         assertNotSame(controllerOne, controllerTwo);
@@ -196,9 +199,9 @@
     public void canSwitchInjectionType() {
         FakeBean fakeBean = new FakeBean();
         MutablePicoContainer pico = new DefaultPicoContainer();
-        PicoRegistrar picoRegistrar = new PicoRegistrar(pico, null, lifecycleStrategy, new SilentMonitor());
+        PicoRegistrar registrar = new PicoRegistrar(pico, null, lifecycleStrategy, new SilentMonitor());
 
-        picoRegistrar.registerInstance(fakeBean)
+        registrar.registerInstance(fakeBean)
                 .register(ConstructorInjectionComponent.class)
                 .useInjection(Registrar.Injection.SETTER)
                 .register(SetterInjectionComponent.class);
@@ -215,11 +218,11 @@
     public void canRegisterComponentWithConstantParameters() {
         MutablePicoContainer pico = new DefaultPicoContainer();
         ParameterResolver parameterResolver = new DefaultParameterResolver(null);
-        PicoRegistrar picoRegistrar = new PicoRegistrar(pico, parameterResolver, lifecycleStrategy, new SilentMonitor());
+        PicoRegistrar registrar = new PicoRegistrar(pico, parameterResolver, lifecycleStrategy, new SilentMonitor());
 
-        picoRegistrar.register("component", ComponentWithParameterDependencies.class, "foo", "bar");
+        registrar.register("component", ComponentWithParameterDependencies.class, "foo", "bar");
 
-        ComponentWithParameterDependencies component = (ComponentWithParameterDependencies) pico.getComponentInstance("component");
+        ComponentWithParameterDependencies component = (ComponentWithParameterDependencies) registrar.getRegistered("component");
 
         // ensure both constructed correctly
         assertSame("foo", component.getValueOne());
@@ -230,16 +233,76 @@
     public void canRegisterComponentWithNamedDependency() {
         MutablePicoContainer pico = new DefaultPicoContainer();
         ParameterResolver parameterResolver = new DefaultParameterResolver(null);
-        PicoRegistrar picoRegistrar = new PicoRegistrar(pico, parameterResolver, lifecycleStrategy, new SilentMonitor());
+        PicoRegistrar registrar = new PicoRegistrar(pico, parameterResolver, lifecycleStrategy, new SilentMonitor());
 
-        picoRegistrar.registerInstance("one", "foo")
+        registrar.registerInstance("one", "foo")
                 .registerInstance("two", "bar")
                 .register("component", ComponentWithParameterDependencies.class, new ComponentReference("one"), new ComponentReference("two"));
 
-        ComponentWithParameterDependencies component = (ComponentWithParameterDependencies) pico.getComponentInstance("component");
+        ComponentWithParameterDependencies component = (ComponentWithParameterDependencies) registrar.getRegistered("component");
 
         // ensure both constructed correctly
         assertSame("foo", component.getValueOne());
         assertSame("bar", component.getValueTwo());
     }
+
+    @Test(expected=RegistrarException.class)
+    public void cannotGetRegistedComponentWithUnknownKey() {
+
+        MutablePicoContainer pico = new DefaultPicoContainer();
+
+        // Mock RegistrarMonitor
+        final RegistrarMonitor registrarMonitor = mockery.mock(RegistrarMonitor.class);
+        final Class<?> type = FakeController.class;
+        mockery.checking(new Expectations() {
+            {
+                one(registrarMonitor).componentRegistered(type, type, new Object[]{});
+            }
+        });
+
+        Registrar registrar = new PicoRegistrar(pico, null, lifecycleStrategy, registrarMonitor)
+                .register(type);
+        assertTrue(registrar.isRegistered(type));
+        assertFalse(registrar.isRegistered("unknownKey"));
+        registrar.getRegistered("unknownKey");
+    }
+
+    @Test(expected=RegistrarException.class)
+    public void cannotGetRegistedComponentThatHasNotBeenRegistered() {
+
+        MutablePicoContainer pico = new DefaultPicoContainer();
+
+        // Mock RegistrarMonitor
+        final RegistrarMonitor registrarMonitor = mockery.mock(RegistrarMonitor.class);
+        final Class<?> type = FakeController.class;
+        Registrar registrar = new PicoRegistrar(pico, null, lifecycleStrategy, registrarMonitor);
+        assertFalse(registrar.isRegistered(type));
+        registrar.getRegistered(type);
+    }
+
+    @Test(expected=RegistrarException.class)
+    public void cannotGetRegistedComponentThatHasMultipleInstancesRegistered() {
+
+        MutablePicoContainer pico = new DefaultPicoContainer();
+
+        // Mock RegistrarMonitor
+        final RegistrarMonitor registrarMonitor = mockery.mock(RegistrarMonitor.class);
+        final Class<?> type = FakeController.class;
+        final Class<?> subType = SubFakeController.class;
+        mockery.checking(new Expectations() {
+            {
+                one(registrarMonitor).componentRegistered(type, type, new Object[]{});
+                one(registrarMonitor).componentRegistered(subType, subType, new Object[]{});
+            }
+        });
+        Registrar registrar = new PicoRegistrar(pico, null, lifecycleStrategy, registrarMonitor);
+        registrar.register(type);
+        registrar.register(subType);
+        assertTrue(registrar.isRegistered(type));
+        registrar.getRegistered(type);
+    }
+
+    public static final class SubFakeController extends FakeController {
+        
+    }
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to