001package org.picocontainer.injectors;
002
003import junit.framework.Assert;
004import org.junit.Test;
005import org.picocontainer.DefaultPicoContainer;
006import org.picocontainer.Parameter;
007import org.picocontainer.monitors.NullComponentMonitor;
008
009import static junit.framework.Assert.fail;
010import static junit.framework.Assert.assertNull;
011import static org.junit.Assert.assertEquals;
012import static org.junit.Assert.assertNotNull;
013
014public class NamedMethodInjectorTestCase {
015
016    public static class Windmill {
017        private String wind;
018        public void setWind(String eeeeee) { // it is important to note here that 'eeeee' is not going to match any named comp
019            this.wind = eeeeee;
020        }
021    }
022
023    @Test
024    public void shouldMatchBasedOnMethodNameIfComponentAvailableAndNonOptional() {
025        final String expected = "use this one pico, its key matched the method name (ish)";
026        NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
027                new NullComponentMonitor(), false);
028        Windmill windmill = new DefaultPicoContainer()
029                .addAdapter(nmi)
030                .addConfig("attemptToConfusePicoContainer", "ha ha, confused you")
031                .addConfig("wind", expected) // matches setWind(..)
032                .addConfig("woo look here another string", "yup, really fooled you this time")
033                .getComponent(Windmill.class);
034        assertNotNull(windmill);
035        assertNotNull(windmill.wind);
036        assertEquals(expected, windmill.wind);
037    }
038    
039    @Test
040    public void shouldBeAmbigiousMultipleComponentAvailableOfRightTypeWithoutMatchingName() {
041        NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
042                new NullComponentMonitor());
043        try {
044            new DefaultPicoContainer()
045                    .addAdapter(nmi)
046                    .addConfig("attemptToConfusePicoContainer", "ha ha, confused you")
047                    .addConfig("woo look here another", "yup, really fooled you this time")
048                    .getComponent(Windmill.class);
049            fail("should have barfed");
050        } catch (AbstractInjector.AmbiguousComponentResolutionException e) {
051            Assert.assertEquals("NamedMethodInjection:" + Windmill.class + " needs a 'java.lang.String' injected via 'public void org.picocontainer.injectors.NamedMethodInjectorTestCase$Windmill.setWind(java.lang.String)', " +
052                    "but there are too many choices to inject. " +
053                    "These:[Instance-attemptToConfusePicoContainer, Instance-woo look here another], " +
054                    "refer http://picocontainer.org/ambiguous-injectable-help.html", e.getMessage());
055        }
056    }
057
058    @Test
059    public void shouldBeUnsatisfiedIfNoComponentAvailableOfTheRightTypeAndNonOptional() {
060        NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
061                new NullComponentMonitor(), false);
062        try {
063            new DefaultPicoContainer()
064                    .addAdapter(nmi)
065                    .addConfig("attemptToConfusePicoContainer", 123)
066                    .addConfig("woo look here another", 456)
067                    .getComponent(Windmill.class);
068            fail("should have barfed");
069        } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
070            // expected
071        }
072    }
073
074    @Test
075    public void withoutNameMatchWillBeOKTooIfOnlyOneOfRightTypeAndNonOptional() {
076        NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
077                new NullComponentMonitor(), false);
078        Windmill windmill = new DefaultPicoContainer()
079                .addAdapter(nmi)
080                .addConfig("anything", "hello")
081                .getComponent(Windmill.class);
082        assertNotNull(windmill);
083        assertNotNull(windmill.wind);
084        assertEquals("hello", windmill.wind);
085    }
086
087    @Test
088    public void withoutNameMatchWillBeOKTooIfNoneOfRightTypeAndOptional() {
089        NamedMethodInjector nmi = new NamedMethodInjector(Windmill.class, Windmill.class, Parameter.DEFAULT,
090                new NullComponentMonitor(), true);
091        Windmill windmill = new DefaultPicoContainer()
092                .addAdapter(nmi)
093                .getComponent(Windmill.class);
094        assertNotNull(windmill);
095        assertNull(windmill.wind);
096    }
097
098}