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