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 *****************************************************************************/
010package org.picocontainer.injectors;
011
012import static org.junit.Assert.assertTrue;
013import static org.junit.Assert.assertEquals;
014
015import java.util.HashMap;
016import java.util.Map;
017import java.util.Properties;
018import org.junit.Test;
019import org.picocontainer.ComponentAdapter;
020import org.picocontainer.Parameter;
021import org.picocontainer.lifecycle.NullLifecycleStrategy;
022import org.picocontainer.lifecycle.ReflectionLifecycleStrategy;
023import org.picocontainer.monitors.ConsoleComponentMonitor;
024
025public class TypedFieldInjectionTestCase {
026    private static final String FIELD_TYPES = Integer.class.getName() + " " + PogoStick.class.getName() + " " + Float.class.getName();
027
028    public static class Helicopter {
029        private PogoStick pogo;
030    }
031
032    public static class PogoStick {
033    }
034
035
036    @Test public void testFactoryMakesNamedInjector() {
037
038        TypedFieldInjection injectionFactory = new TypedFieldInjection();
039
040        ConsoleComponentMonitor cm = new ConsoleComponentMonitor();
041        Properties props = new Properties();
042        props.setProperty("injectionFieldTypes", FIELD_TYPES);
043        ComponentAdapter ca = injectionFactory.createComponentAdapter(cm, new NullLifecycleStrategy(),
044                props, Map.class, HashMap.class, Parameter.DEFAULT);
045
046        assertTrue(ca instanceof TypedFieldInjector);
047
048        TypedFieldInjector tfi = (TypedFieldInjector) ca;
049
050        assertEquals(3, tfi.getInjectionFieldTypes().size());
051        assertEquals(Integer.class.getName(), tfi.getInjectionFieldTypes().get(0));
052        assertEquals(PogoStick.class.getName(), tfi.getInjectionFieldTypes().get(1));
053        assertEquals(Float.class.getName(), tfi.getInjectionFieldTypes().get(2));
054    }
055
056    @Test public void testPropertiesAreRight() {
057        Properties props = TypedFieldInjection.injectionFieldTypes(FIELD_TYPES);
058        assertEquals("java.lang.Integer org.picocontainer.injectors.TypedFieldInjectionTestCase$PogoStick java.lang.Float", props.getProperty("injectionFieldTypes"));
059        assertEquals(1, props.size());
060    }
061
062
063}