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