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