[picocontainer-scm] [5600] java/2.x/trunk/pico/container/src/test/org/picocontainer/injectors: start of named method injection

paul-yCVjj/[email protected]
Newsgroups gmane.comp.java.picocontainer.cvs
Message-ID <[email protected]>
Revision 5600
Author paul
Date 2010-02-12 13:46:51 -0600 (Fri, 12 Feb 2010)

Log Message

start of named method injection

Added Paths

- java/2.x/trunk/pico/container/src/java/org/picocontainer/injectors/NamedMethodInjector.java

- java/2.x/trunk/pico/container/src/test/org/picocontainer/injectors/NamedMethodInjectorTestCase.java

Diff

Added: java/2.x/trunk/pico/container/src/java/org/picocontainer/injectors/NamedMethodInjector.java (0 => 5600)

--- java/2.x/trunk/pico/container/src/java/org/picocontainer/injectors/NamedMethodInjector.java (rev 0)
+++ java/2.x/trunk/pico/container/src/java/org/picocontainer/injectors/NamedMethodInjector.java 2010-02-12 19:46:51 UTC (rev 5600)
@@ -0,0 +1,63 @@
+/*****************************************************************************
+ * 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.picocontainer.injectors;
+
+import org.picocontainer.ComponentMonitor;
+import org.picocontainer.NameBinding;
+import org.picocontainer.Parameter;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+@SuppressWarnings("serial")
+public class NamedMethodInjector<T> extends SetterInjector<T> {
+
+ public NamedMethodInjector(Object key,
+ Class<?> impl,
+ Parameter[] parameters,
+ ComponentMonitor monitor) {
+ super(key, impl, parameters, monitor, "set", true);
+ }
+
+ public NamedMethodInjector(Object key,
+ Class<?> impl,
+ Parameter[] parameters,
+ ComponentMonitor monitor,
+ String prefix) {
+ super(key, impl, parameters, monitor, prefix, true);
+ }
+
+ @Override
+ protected Object injectIntoMember(AccessibleObject member, Object componentInstance, Object toInject)
+ throws IllegalAccessException, InvocationTargetException {
+ return ((Method)member).invoke(componentInstance, toInject);
+ }
+
+ @Override
+ protected NameBinding makeParameterNameImpl(final AccessibleObject member) {
+ return new NameBinding() {
+ public String getName() {
+ return NamedMethodInjector.this.getName((Method) member);
+ }
+ };
+ }
+
+ @Override
+ protected String getName(Method method) {
+ String name = method.getName().substring(prefix.length()); // string off 'set' or chosen prefix
+ return name.substring(0,1).toLowerCase() + name.substring(1);
+ }
+
+ public String toString() {
+ return "NamedMethodInjection-" + super.toString();
+ }
+
+}
\ No newline at end of file

Added: java/2.x/trunk/pico/container/src/test/org/picocontainer/injectors/NamedMethodInjectorTestCase.java (0 => 5600)

--- java/2.x/trunk/pico/container/src/test/org/picocontainer/injectors/NamedMethodInjectorTestCase.java (rev 0)
+++ java/2.x/trunk/pico/container/src/test/org/picocontainer/injectors/NamedMethodInjectorTestCase.java 2010-02-12 19:46:51 UTC (rev 5600)
@@ -0,0 +1,82 @@
+package org.picocontainer.injectors;
+
+import org.junit.Test;
+import org.picocontainer.DefaultPicoContainer;
+import org.picocontainer.Parameter;
+import org.picocontainer.monitors.NullComponentMonitor;
+
+import static com.sun.tools.internal.ws.wsdl.parser.Util.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class NamedMethodInjectorTestCase {
+
+ public static class Windmill {
+ private String wind;
+ public void setWind(String eeeeee) { // it is important to note here that 'eeeee' is not going to match any named comp
+ this.wind = eeeeee;
+ }
+ }
+
+ @Test
+ public void shouldMatchBasedOnMethodNameIfComponentAvailable() {
+ final String expected = "use this one pico, its key matched the method name (ish)";
+ NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
+ new NullComponentMonitor());
+ Windmill windmill = new DefaultPicoContainer()
+ .addAdapter(nmi)
+ .addConfig("attemptToConfusePicoContainer", "ha ha, confused you")
+ .addConfig("wind", expected) // matches setWind(..)
+ .addConfig("woo look here another string", "yup, really fooled you this time")
+ .getComponent(Windmill.class);
+ assertNotNull(windmill);
+ assertNotNull(windmill.wind);
+ assertEquals(expected, windmill.wind);
+ }
+
+ @Test
+ public void shouldBeAmbigiousMultipleComponentAvailableOfRightTypeWithoutMatchingName() {
+ NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
+ new NullComponentMonitor());
+ try {
+ new DefaultPicoContainer()
+ .addAdapter(nmi)
+ .addConfig("attemptToConfusePicoContainer", "ha ha, confused you")
+ .addConfig("woo look here another", "yup, really fooled you this time")
+ .getComponent(Windmill.class);
+ fail("should have barfed");
+ } catch (AbstractInjector.AmbiguousComponentResolutionException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void shouldBeUnsatisfiedIfNoComponentAvailableOfTheRightType() {
+ NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
+ new NullComponentMonitor());
+ try {
+ new DefaultPicoContainer()
+ .addAdapter(nmi)
+ .addConfig("attemptToConfusePicoContainer", 123)
+ .addConfig("woo look here another", 456)
+ .getComponent(Windmill.class);
+ fail("should have barfed");
+ } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void withoutNameMatchWillBeOKTooIfOnlyOneOfRightType() {
+ NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
+ new NullComponentMonitor());
+ Windmill windmill = new DefaultPicoContainer()
+ .addAdapter(nmi)
+ .addConfig("anything", "hello")
+ .getComponent(Windmill.class);
+ assertNotNull(windmill);
+ assertNotNull(windmill.wind);
+ assertEquals("hello", windmill.wind);
+ }
+
+}

----------

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.