    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.assertEquals;
    013
    014    import java.util.HashMap;
    015    import java.util.Map;
    016    import java.util.Properties;
    017
    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    import com.thoughtworks.xstream.XStream;
    025    import com.thoughtworks.xstream.converters.Converter;
    026    import com.thoughtworks.xstream.converters.MarshallingContext;
    027    import com.thoughtworks.xstream.converters.UnmarshallingContext;
    028    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
    029    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
    030
    031    public class AnnotatedFieldInjectionTestCase {
    032
    033        @Test public void testFactoryMakesAnnotationInjector() {
    034
    035            AnnotatedFieldInjection injectionFactory = new AnnotatedFieldInjection();
    036
    037            ConsoleComponentMonitor cm = new ConsoleComponentMonitor();
    038            ComponentAdapter ca = injectionFactory.createComponentAdapter(cm, new ReflectionLifecycleStrategy(cm), new Properties(), Map.class, HashMap.class, Parameter.DEFAULT);
    039
    040            XStream xs = new XStream();
    041            //xs.alias("CCM", ConsoleComponentMonitor.class);
    042            xs.registerConverter(new Converter() {
    043                public boolean canConvert(Class aClass) {
    044                    return aClass.getName().equals("org.picocontainer.monitors.ConsoleComponentMonitor") ||
    045                           aClass.getName().equals("org.picocontainer.lifecycle.ReflectionLifecycleStrategy");
    046
    047                }
    048
    049                public void marshal(Object object,
    050                                    HierarchicalStreamWriter hierarchicalStreamWriter,
    051                                    MarshallingContext marshallingContext)
    052                {
    053                }
    054
    055                public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
    056                                        UnmarshallingContext unmarshallingContext)
    057                {
    058                    return null;
    059                }
    060            });
    061
    062            String foo = xs.toXML(ca);
    063
    064            assertEquals("<org.picocontainer.injectors.AnnotatedFieldInjector>\n" +
    065                         "  <componentKey class=\"java-class\">java.util.Map</componentKey>\n" +
    066                         "  <componentImplementation>java.util.HashMap</componentImplementation>\n" +
    067                         "  <componentMonitor class=\"org.picocontainer.monitors.ConsoleComponentMonitor\"/>\n" +
    068                         "  <lifecycleStrategy class=\"org.picocontainer.lifecycle.ReflectionLifecycleStrategy\"/>\n" +
    069                         "  <useNames>false</useNames>\n" +
    070                         "  <injectionAnnotation>org.picocontainer.annotations.Inject</injectionAnnotation>\n" +
    071                         "</org.picocontainer.injectors.AnnotatedFieldInjector>", foo);
    072
    073
    074        }
    075
    076
    077    }