    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 Stacy Curl                        *
    009     *****************************************************************************/
    010
    011    package org.picocontainer.injectors;
    012
    013    import static org.junit.Assert.assertEquals;
    014    import static org.junit.Assert.assertNotNull;
    015    import static org.junit.Assert.assertTrue;
    016
    017    import java.util.HashMap;
    018    import java.util.Map;
    019    import java.util.Properties;
    020
    021    import org.junit.Before;
    022    import org.junit.Test;
    023    import org.picocontainer.Characteristics;
    024    import org.picocontainer.ComponentAdapter;
    025    import org.picocontainer.ComponentFactory;
    026    import org.picocontainer.DefaultPicoContainer;
    027    import org.picocontainer.Parameter;
    028    import org.picocontainer.PicoCompositionException;
    029    import org.picocontainer.lifecycle.NullLifecycleStrategy;
    030    import org.picocontainer.lifecycle.ReflectionLifecycleStrategy;
    031    import org.picocontainer.monitors.ConsoleComponentMonitor;
    032    import org.picocontainer.monitors.NullComponentMonitor;
    033    import org.picocontainer.tck.AbstractComponentFactoryTest;
    034    import org.picocontainer.testmodel.SimpleTouchable;
    035    import org.picocontainer.testmodel.Touchable;
    036
    037    import com.thoughtworks.xstream.XStream;
    038    import com.thoughtworks.xstream.converters.Converter;
    039    import com.thoughtworks.xstream.converters.MarshallingContext;
    040    import com.thoughtworks.xstream.converters.UnmarshallingContext;
    041    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
    042    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
    043
    044    public class AdaptingInjectionTestCase extends AbstractComponentFactoryTest {
    045
    046        XStream xs;
    047
    048        @Before
    049        public void setUp() throws Exception {
    050            super.setUp();
    051            xs = new XStream();
    052            xs.alias("RLS", ReflectionLifecycleStrategy.class);
    053            xs.alias("CCM", ConsoleComponentMonitor.class);
    054            xs.alias("Method-Injection", AnnotatedMethodInjector.class);
    055            xs.alias("Field-Injection", AnnotatedFieldInjector.class);
    056            xs.alias("Constructor-Injection", ConstructorInjector.class);
    057            //xs.alias("CCM", ConsoleComponentMonitor.class);
    058            xs.registerConverter(new Converter() {
    059                public boolean canConvert(Class aClass) {
    060                    return aClass.getName().equals("org.picocontainer.monitors.ConsoleComponentMonitor") ||
    061                           aClass.getName().equals("org.picocontainer.lifecycle.ReflectionLifecycleStrategy");
    062
    063                }
    064
    065                public void marshal(Object object,
    066                                    HierarchicalStreamWriter hierarchicalStreamWriter,
    067                                    MarshallingContext marshallingContext)
    068                {
    069                }
    070
    071                public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
    072                                        UnmarshallingContext unmarshallingContext)
    073                {
    074                    return null;
    075                }
    076            });
    077
    078        }
    079
    080        protected ComponentFactory createComponentFactory() {
    081            return new AdaptingInjection();
    082        }
    083
    084        @Test public void testInstantiateComponentWithNoDependencies() throws PicoCompositionException {
    085            ComponentAdapter componentAdapter =
    086                createComponentFactory().createComponentAdapter(new NullComponentMonitor(),
    087                                                                new NullLifecycleStrategy(),
    088                                                                new Properties(Characteristics.CDI),
    089                                                                Touchable.class,
    090                                                                SimpleTouchable.class,
    091                                                                (Parameter[])null);
    092
    093            Object comp = componentAdapter.getComponentInstance(new DefaultPicoContainer(), ComponentAdapter.NOTHING.class);
    094            assertNotNull(comp);
    095            assertTrue(comp instanceof SimpleTouchable);
    096        }
    097
    098        @Test public void testSingleUsecanBeInstantiatedByDefaultComponentAdapter() {
    099            ComponentAdapter componentAdapter = createComponentFactory().createComponentAdapter(new NullComponentMonitor(),
    100                                                                                                new NullLifecycleStrategy(),
    101                                                                                                new Properties(
    102                                                                                                    Characteristics.CDI),
    103                                                                                                "o",
    104                                                                                                Object.class,
    105                                                                                                (Parameter[])null);
    106            Object component = componentAdapter.getComponentInstance(new DefaultPicoContainer(), ComponentAdapter.NOTHING.class);
    107            assertNotNull(component);
    108        }
    109
    110
    111        @Test public void testFactoryMakesConstructorInjector() {
    112
    113            ComponentFactory cf = createComponentFactory();
    114
    115            ConsoleComponentMonitor cm = new ConsoleComponentMonitor();
    116            ComponentAdapter ca = cf.createComponentAdapter(cm, new ReflectionLifecycleStrategy(cm), new Properties(),
    117                                                            Map.class, HashMap.class, Parameter.DEFAULT);
    118
    119            String foo = xs.toXML(ca).replace("\"", "");
    120
    121            assertEquals("<Constructor-Injection>\n" +
    122                         "  <componentKey class=java-class>java.util.Map</componentKey>\n" +
    123                         "  <componentImplementation>java.util.HashMap</componentImplementation>\n" +
    124                         "  <componentMonitor class=CCM/>\n" +
    125                         "  <lifecycleStrategy class=RLS/>\n" +
    126                         "  <useNames>false</useNames>\n" +
    127                         "  <rememberChosenConstructor>true</rememberChosenConstructor>\n" +
    128                         "</Constructor-Injection>", foo);
    129
    130
    131        }
    132
    133        @Test public void testFactoryMakesFieldAnnotationInjector() {
    134
    135            ComponentFactory cf = createComponentFactory();
    136
    137            ConsoleComponentMonitor cm = new ConsoleComponentMonitor();
    138            ComponentAdapter ca = cf.createComponentAdapter(cm,
    139                                                            new ReflectionLifecycleStrategy(cm),
    140                                                            new Properties(),
    141                                                            AnnotatedFieldInjectorTestCase.Helicopter.class,
    142                                                            AnnotatedFieldInjectorTestCase.Helicopter.class,
    143                                                            Parameter.DEFAULT);
    144
    145            String foo = xs.toXML(ca).replace("\"", "");
    146
    147            assertEquals("<Field-Injection>\n" +
    148                         "  <componentKey class=java-class>org.picocontainer.injectors.AnnotatedFieldInjectorTestCase$Helicopter</componentKey>\n" +
    149                         "  <componentImplementation>org.picocontainer.injectors.AnnotatedFieldInjectorTestCase$Helicopter</componentImplementation>\n" +
    150                         "  <componentMonitor class=CCM/>\n" +
    151                    "  <lifecycleStrategy class=RLS/>\n" +
    152                    "  <useNames>false</useNames>\n" +
    153                    "  <injectionAnnotation>org.picocontainer.annotations.Inject</injectionAnnotation>\n" +
    154                         "</Field-Injection>", foo);
    155
    156
    157        }
    158
    159        @Test public void testFactoryMakesMethodAnnotationInjector() {
    160
    161            ComponentFactory cf = createComponentFactory();
    162
    163            ConsoleComponentMonitor cm = new ConsoleComponentMonitor();
    164            ComponentAdapter ca = cf.createComponentAdapter(cm,
    165                                                            new ReflectionLifecycleStrategy(cm),
    166                                                            new Properties(),
    167                                                            AnnotatedMethodInjectorTestCase.AnnotatedBurp.class,
    168                                                            AnnotatedMethodInjectorTestCase.AnnotatedBurp.class,
    169                                                            Parameter.DEFAULT);
    170
    171            String foo = xs.toXML(ca).replace("\"", "");
    172
    173            assertEquals("<Method-Injection>\n" +
    174                         "  <componentKey class=java-class>org.picocontainer.injectors.AnnotatedMethodInjectorTestCase$AnnotatedBurp</componentKey>\n" +
    175                         "  <componentImplementation>org.picocontainer.injectors.AnnotatedMethodInjectorTestCase$AnnotatedBurp</componentImplementation>\n" +
    176                         "  <componentMonitor class=CCM/>\n" +
    177                         "  <lifecycleStrategy class=RLS/>\n" +
    178                         "  <useNames>false</useNames>\n" +
    179                         "  <setterMethodPrefix></setterMethodPrefix>\n" +
    180                         "  <injectionAnnotation>org.picocontainer.annotations.Inject</injectionAnnotation>\n" +
    181                         "</Method-Injection>", foo);
    182
    183
    184        }
    185
    186
    187    }