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 org.picocontainer.annotations.Inject;
013 import org.picocontainer.MutablePicoContainer;
014 import org.picocontainer.DefaultPicoContainer;
015 import org.picocontainer.lifecycle.NullLifecycleStrategy;
016 import org.picocontainer.monitors.NullComponentMonitor;
017
018 import junit.framework.TestCase;
019
020 public class FieldAnnotationInjectorTestCase extends TestCase {
021
022 public static class Helicopter {
023 @Inject
024 private PogoStick pogo;
025
026 public Helicopter() {
027 }
028 }
029
030 public static class Helicopter2 {
031 private PogoStick pogo;
032
033 public Helicopter2() {
034 }
035 }
036
037 public static class PogoStick {
038 }
039
040 public void testFieldInjection() {
041 MutablePicoContainer pico = new DefaultPicoContainer();
042 pico.addAdapter(new FieldAnnotationInjector(Helicopter.class, Helicopter.class, null,
043 new NullComponentMonitor(), new NullLifecycleStrategy()));
044 pico.addComponent(PogoStick.class, new PogoStick());
045 Helicopter chopper = pico.getComponent(Helicopter.class);
046 assertNotNull(chopper);
047 assertNotNull(chopper.pogo);
048 }
049
050 public void testFieldInjectionWithoutAnnotationDoesNotWork() {
051 MutablePicoContainer pico = new DefaultPicoContainer();
052 pico.addAdapter(new FieldAnnotationInjector(Helicopter2.class, Helicopter2.class, null,
053 new NullComponentMonitor(), new NullLifecycleStrategy()));
054 pico.addComponent(PogoStick.class, new PogoStick());
055 Helicopter2 chopper = pico.getComponent(Helicopter2.class);
056 assertNotNull(chopper);
057 assertNull(chopper.pogo);
058 }
059
060 public void testFieldDeosNotHappenWithoutRightInjectorDoesNotWork() {
061 MutablePicoContainer pico = new DefaultPicoContainer();
062 pico.addAdapter(new SetterInjector(Helicopter.class, Helicopter.class, null,
063 new NullComponentMonitor(), new NullLifecycleStrategy()));
064 pico.addComponent(PogoStick.class, new PogoStick());
065 Helicopter chopper = pico.getComponent(Helicopter.class);
066 assertNotNull(chopper);
067 assertNull(chopper.pogo);
068 }
069
070
071
072
073 }