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 org.junit.Assert.assertNotNull;
013    import static org.junit.Assert.assertEquals;
014    
015    import org.junit.Test;
016    import org.picocontainer.DefaultPicoContainer;
017    import org.picocontainer.MutablePicoContainer;
018    import org.picocontainer.lifecycle.NullLifecycleStrategy;
019    import org.picocontainer.monitors.NullComponentMonitor;
020    
021    public class NamedFieldInjectorTestCase {
022    
023        public static class Helicopter {
024            private PogoStick pogo;
025        }
026    
027        public static class Biplane {
028            private String wing1;
029            private String wing2;
030        }
031    
032    
033        public static class PogoStick {
034        }
035    
036        @Test public void testFieldInjectionByType() {
037            MutablePicoContainer pico = new DefaultPicoContainer();
038            pico.addAdapter(new NamedFieldInjector(Helicopter.class, Helicopter.class, null,
039                                                        new NullComponentMonitor(), " aa bb cc pogo dd "));
040            pico.addComponent(PogoStick.class, new PogoStick());
041            Helicopter chopper = pico.getComponent(Helicopter.class);
042            assertNotNull(chopper);
043            assertNotNull(chopper.pogo);
044        }
045    
046        @Test public void testFieldInjectionByName() {
047            MutablePicoContainer pico = new DefaultPicoContainer();
048            pico.addAdapter(new NamedFieldInjector(Biplane.class, Biplane.class, null,
049                                                        new NullComponentMonitor(), " aa wing1 cc wing2 dd "));
050            pico.addConfig("wing1", "hello");
051            pico.addConfig("wing2", "goodbye");
052            Biplane biplane = pico.getComponent(Biplane.class);
053            assertNotNull(biplane);
054            assertNotNull(biplane.wing1);
055            assertEquals("hello", biplane.wing1);
056            assertNotNull(biplane.wing2);
057            assertEquals("goodbye", biplane.wing2);
058        }
059    
060    
061    
062    }