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