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.assertSame;
013    
014    import java.lang.reflect.Constructor;
015    import java.lang.reflect.InvocationTargetException;
016    import java.lang.reflect.Member;
017    import java.lang.reflect.Type;
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.junit.Before;
022    import org.junit.Test;
023    import org.picocontainer.ComponentAdapter;
024    import org.picocontainer.ComponentMonitor;
025    import org.picocontainer.LifecycleStrategy;
026    import org.picocontainer.Parameter;
027    import org.picocontainer.PicoCompositionException;
028    import org.picocontainer.PicoContainer;
029    import org.picocontainer.containers.EmptyPicoContainer;
030    import org.picocontainer.lifecycle.NullLifecycleStrategy;
031    import org.picocontainer.monitors.NullComponentMonitor;
032    
033    public class AbstractInjectorTestCase {
034    
035        private AbstractInjector ai;
036        Constructor<HashMap> ctor;
037    
038        @Before
039        public void setUp() throws NoSuchMethodException {
040            ai = new MyAbstractInjector(Map.class, HashMap.class, new Parameter[0],
041                                                         new NullComponentMonitor(),
042                                                         new NullLifecycleStrategy(), false);
043            ctor = HashMap.class.getConstructor();
044        }
045    
046        @Test public void testCaughtIllegalAccessExceptionInvokesMonitorAndThrows() {
047            final EmptyPicoContainer epc = new EmptyPicoContainer();
048            final IllegalAccessException iae = new IllegalAccessException("foo");
049            NullComponentMonitor ncm = new NullComponentMonitor() {
050                public void instantiationFailed(PicoContainer container,
051                                                ComponentAdapter componentAdapter,
052                                                Constructor constructor,
053                                                Exception e) {
054                    assertSame(epc, container);
055                    assertSame(ai, componentAdapter);
056                    assertSame(ctor, constructor);
057                    assertSame(iae, e);
058                }
059            };
060            try {
061                ai.caughtIllegalAccessException(ncm, ctor, iae, epc);
062            } catch (PicoCompositionException e) {
063                assertSame(iae, e.getCause());
064            }
065        }
066    
067        @Test public void testCaughtInstantiationExceptionInvokesMonitorAndThrows() {
068            final EmptyPicoContainer epc = new EmptyPicoContainer();
069            final InstantiationException ie = new InstantiationException("foo");
070            NullComponentMonitor ncm = new NullComponentMonitor() {
071                public void instantiationFailed(PicoContainer container,
072                                                ComponentAdapter componentAdapter,
073                                                Constructor constructor,
074                                                Exception e) {
075                    assertSame(epc, container);
076                    assertSame(ai, componentAdapter);
077                    assertSame(ctor, constructor);
078                    assertSame(ie, e);
079                }
080            };
081            try {
082                ai.caughtInstantiationException(ncm, ctor, ie, epc);
083            } catch (PicoCompositionException e) {
084                assertSame("Should never get here", e.getMessage());
085            }
086        }
087    
088        @Test public void testCaughtInvocationTargetExceptionInvokesMonitorAndReThrowsRuntimeIfRuntimeInTheFirstPlace() {
089            final InvocationTargetException ite = new InvocationTargetException(new RuntimeException("foo"));
090            NullComponentMonitor ncm = new NullComponentMonitor() {
091                public void invocationFailed(Member member, Object instance, Exception e) {
092                    assertSame(ctor, member);
093                    assertSame("bar", instance);
094                    assertSame(ite, e);
095                }
096            };
097            try {
098                ai.caughtInvocationTargetException(ncm, ctor, "bar", ite);
099            } catch (RuntimeException e) {
100                assertSame("foo", e.getMessage());
101            }
102        }
103    
104        @Test public void testCaughtInvocationTargetExceptionInvokesMonitorAndReThrowsErrorIfErrorInTheFirstPlace() {
105            final InvocationTargetException ite = new InvocationTargetException(new Error("foo"));
106            NullComponentMonitor ncm = new NullComponentMonitor() {
107                public void invocationFailed(Member member, Object instance, Exception e) {
108                    assertSame(ctor, member);
109                    assertSame("bar", instance);
110                    assertSame(ite, e);
111                }
112            };
113            try {
114                ai.caughtInvocationTargetException(ncm, ctor, "bar", ite);
115            } catch (Error e) {
116                assertSame("foo", e.getMessage());
117            }
118        }
119    
120        @Test public void testCaughtInvocationTargetExceptionInvokesMonitorAndReThrowsAsCompositionIfNotRuntimeOrError() {
121            final InvocationTargetException ite = new InvocationTargetException(new Exception("foo"));
122            NullComponentMonitor ncm = new NullComponentMonitor() {
123                public void invocationFailed(Member member, Object instance, Exception e) {
124                    assertSame(ctor, member);
125                    assertSame("bar", instance);
126                    assertSame(ite, e);
127                }
128            };
129            try {
130                ai.caughtInvocationTargetException(ncm, ctor, "bar", ite);
131            } catch (PicoCompositionException e) {
132                assertSame("foo", e.getCause().getMessage());
133            }
134        }
135    
136    
137    
138        private static class MyAbstractInjector extends AbstractInjector {
139    
140            public MyAbstractInjector(Object componentKey,
141                                      Class componentImplementation,
142                                      Parameter[] parameters,
143                                      ComponentMonitor monitor,
144                                      LifecycleStrategy lifecycleStrategy,
145                                      boolean useNames) {
146                super(componentKey, componentImplementation, parameters, monitor, lifecycleStrategy, useNames);
147            }
148    
149            @Override
150            public void verify(PicoContainer container) throws PicoCompositionException {
151                    }
152    
153            public Object getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
154                return null;
155            }
156    
157            public void decorateComponentInstance(PicoContainer container, Type into, Object instance) {
158            }
159    
160            public String getDescriptor() {
161                return null;
162            }
163        }
164    }