    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     * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
    009     *****************************************************************************/
    010    package org.picocontainer.injectors;
    011
    012    import static org.junit.Assert.assertEquals;
    013    import static org.junit.Assert.assertNotNull;
    014    import static org.picocontainer.Characteristics.USE_NAMES;
    015
    016    import java.util.Properties;
    017
    018    import org.junit.Before;
    019    import org.junit.Test;
    020    import org.picocontainer.ComponentFactory;
    021    import org.picocontainer.DefaultPicoContainer;
    022    import org.picocontainer.monitors.NullComponentMonitor;
    023    import org.picocontainer.parameters.ConstantParameter;
    024    import org.picocontainer.tck.AbstractComponentFactoryTest;
    025    import org.picocontainer.tck.AbstractComponentAdapterTest.RecordingLifecycleStrategy;
    026    import org.picocontainer.testmodel.NullLifecycle;
    027    import org.picocontainer.testmodel.RecordingLifecycle;
    028    import org.picocontainer.testmodel.RecordingLifecycle.One;
    029
    030    /**
    031     * @author Mauro Talevi
    032     */
    033    public class ConstructorInjectionTestCase extends AbstractComponentFactoryTest {
    034
    035            @Before
    036        public void setUp() throws Exception {
    037            picoContainer = new DefaultPicoContainer(createComponentFactory());
    038        }
    039
    040        protected ComponentFactory createComponentFactory() {
    041            return new ConstructorInjection();
    042        }
    043
    044        @Test public void testCustomLifecycleCanBeInjected() throws NoSuchMethodException {
    045            RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer());
    046            ConstructorInjection componentFactory =
    047                new ConstructorInjection();
    048            ConstructorInjector cica =  (ConstructorInjector)
    049            componentFactory.createComponentAdapter(new NullComponentMonitor(), strategy, new Properties(), NullLifecycle.class, NullLifecycle.class);
    050            One one = new RecordingLifecycle.One(new StringBuffer());
    051            cica.start(one);
    052            cica.stop(one);
    053            cica.dispose(one);
    054            assertEquals("<start<stop<dispose", strategy.recording());
    055        }
    056
    057        public static class ClassA {
    058            private int x;
    059            public ClassA(int x) {
    060                this.x = x;
    061            }
    062        }
    063        @Test public void testAutoConversionOfIntegerParam() {
    064            picoContainer.as(USE_NAMES).addComponent(ClassA.class);
    065            picoContainer.addComponent("x", "12");
    066            assertNotNull(picoContainer.getComponent(ClassA.class));
    067            assertEquals(12,picoContainer.getComponent(ClassA.class).x);
    068        }
    069
    070        public static class ClassB {
    071            private float x;
    072            public ClassB(float x) {
    073                this.x = x;
    074            }
    075        }
    076        @Test public void testAutoConversionOfFloatParam() {
    077            picoContainer.as(USE_NAMES).addComponent(ClassB.class);
    078            picoContainer.addComponent("x", "1.2");
    079            assertNotNull(picoContainer.getComponent(ClassB.class));
    080            assertEquals(1.2,picoContainer.getComponent(ClassB.class).x, 0.0001);
    081        }
    082
    083
    084        /**
    085         * Test class to verify the CICA can handle
    086         * a constant parameter class type.
    087         *
    088         */
    089        @SuppressWarnings({"unchecked", "unused"})
    090        public static class ClassAsConstructor {
    091                    private final Class type;
    092
    093                    public ClassAsConstructor(Class type) {
    094                            this.type = type;
    095            }
    096        }
    097
    098            @Test
    099        @SuppressWarnings("unchecked")
    100        public void allowClassTypesForComponentAdapter() {
    101            ConstructorInjection componentFactory = new ConstructorInjection();
    102
    103            ConstructorInjector cica =  (ConstructorInjector)
    104            componentFactory.createComponentAdapter(new NullComponentMonitor(), null, new Properties(), ClassAsConstructor.class, ClassAsConstructor.class, new ConstantParameter(String.class));
    105
    106            ClassAsConstructor instance = (ClassAsConstructor) cica.getComponentInstance(picoContainer);
    107            assertNotNull(instance);
    108
    109        }
    110
    111
    112    }