001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by                                                          *
009     *****************************************************************************/
010    package org.picocontainer.injectors;
011    
012    import static junit.framework.Assert.fail;
013    import static org.junit.Assert.assertNotNull;
014    import static org.junit.Assert.assertEquals;
015    
016    import junit.framework.Assert;
017    import org.junit.Test;
018    import org.picocontainer.DefaultPicoContainer;
019    import org.picocontainer.MutablePicoContainer;
020    import org.picocontainer.lifecycle.NullLifecycleStrategy;
021    import org.picocontainer.monitors.NullComponentMonitor;
022    
023    public class NamedFieldInjectorTestCase {
024    
025        public static class Helicopter {
026            private PogoStick pogo;
027        }
028    
029        public static class Biplane {
030            private String wing1;
031            private String wing2;
032        }
033    
034        public static class Monoplane {
035            private String wing1;
036        }
037    
038        public static class PogoStick {
039        }
040    
041        @Test public void testFieldInjectionByType() {
042            MutablePicoContainer pico = new DefaultPicoContainer();
043            pico.addAdapter(new NamedFieldInjector(Helicopter.class, Helicopter.class, null,
044                                                        new NullComponentMonitor(), " aa bb cc pogo dd "));
045            pico.addComponent(PogoStick.class, new PogoStick());
046            Helicopter chopper = pico.getComponent(Helicopter.class);
047            assertNotNull(chopper);
048            assertNotNull(chopper.pogo);
049        }
050    
051        @Test public void testFieldInjectionByName() {
052            MutablePicoContainer pico = new DefaultPicoContainer();
053            pico.addAdapter(new NamedFieldInjector(Biplane.class, Biplane.class, null,
054                                                        new NullComponentMonitor(), " aa wing1 cc wing2 dd "));
055            pico.addConfig("wing1", "hello");
056            pico.addConfig("wing2", "goodbye");
057            Biplane biplane = pico.getComponent(Biplane.class);
058            assertNotNull(biplane);
059            assertNotNull(biplane.wing1);
060            assertEquals("hello", biplane.wing1);
061            assertNotNull(biplane.wing2);
062            assertEquals("goodbye", biplane.wing2);
063        }
064    
065    
066        @Test public void testFieldInjectionByTypeWhereNoMatch() {
067            MutablePicoContainer pico = new DefaultPicoContainer();
068            pico.setName("parent");
069            pico.addAdapter(new NamedFieldInjector(Monoplane.class, Monoplane.class, null,
070                    new NullComponentMonitor(), " aa wing1 cc wing2 dd "));
071            try {
072                pico.getComponent(Monoplane.class);
073                fail("should have barfed");
074            } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
075                String expected = "Monoplane has unsatisfied dependency for fields [String.wing1] from parent:1<|";
076                String actual = e.getMessage().replace("java.lang.","");
077                actual = actual.replace(NamedFieldInjectorTestCase.class.getName() + "$", "");
078                Assert.assertEquals(expected, actual);
079            }
080        }
081    
082    }