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