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