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(),
040 new NullLifecycleStrategy(), " aa bb cc pogo dd "));
041 pico.addComponent(PogoStick.class, new PogoStick());
042 Helicopter chopper = pico.getComponent(Helicopter.class);
043 assertNotNull(chopper);
044 assertNotNull(chopper.pogo);
045 }
046
047 @Test public void testFieldInjectionByName() {
048 MutablePicoContainer pico = new DefaultPicoContainer();
049 pico.addAdapter(new NamedFieldInjector(Biplane.class, Biplane.class, null,
050 new NullComponentMonitor(),
051 new NullLifecycleStrategy(), " aa wing1 cc wing2 dd "));
052 pico.addConfig("wing1", "hello");
053 pico.addConfig("wing2", "goodbye");
054 Biplane biplane = pico.getComponent(Biplane.class);
055 assertNotNull(biplane);
056 assertNotNull(biplane.wing1);
057 assertEquals("hello", biplane.wing1);
058 assertNotNull(biplane.wing2);
059 assertEquals("goodbye", biplane.wing2);
060 }
061
062
063
064 }