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