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