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.monitors; 011 012 import static org.junit.Assert.assertEquals; 013 import static org.picocontainer.monitors.ComponentMonitorHelper.ctorToString; 014 import static org.picocontainer.monitors.ComponentMonitorHelper.format; 015 import static org.picocontainer.monitors.ComponentMonitorHelper.methodToString; 016 import static org.picocontainer.monitors.ComponentMonitorHelper.parmsToString; 017 018 import java.io.StringWriter; 019 import java.io.Writer; 020 import java.lang.reflect.Constructor; 021 import java.lang.reflect.Method; 022 import java.lang.reflect.Type; 023 import java.util.HashMap; 024 import java.util.Map; 025 026 import org.junit.Assert; 027 import org.junit.Before; 028 import org.junit.Test; 029 import org.picocontainer.ComponentMonitor; 030 import org.picocontainer.PicoCompositionException; 031 import org.picocontainer.PicoContainer; 032 import org.picocontainer.PicoLifecycleException; 033 import org.picocontainer.adapters.AbstractAdapter; 034 import org.picocontainer.containers.TransientPicoContainer; 035 036 /** 037 * @author Aslak Hellesøy 038 * @author Mauro Talevi 039 */ 040 @SuppressWarnings("serial") 041 public class WriterComponentMonitorTestCase { 042 043 private Writer out; 044 private ComponentMonitor componentMonitor; 045 private static final String NL = System.getProperty("line.separator"); 046 private Constructor constructor; 047 private Method method; 048 049 @Before 050 public void setUp() throws Exception { 051 out = new StringWriter(); 052 constructor = getClass().getConstructor((Class[])null); 053 method = getClass().getDeclaredMethod("setUp", (Class[])null); 054 componentMonitor = new WriterComponentMonitor(out); 055 } 056 057 @SuppressWarnings("unchecked") 058 @Test public void testShouldTraceInstantiating() { 059 componentMonitor.instantiating(null, null, constructor); 060 assertEquals(format(ComponentMonitorHelper.INSTANTIATING, ctorToString(constructor)) +NL, out.toString()); 061 } 062 063 @SuppressWarnings("unchecked") 064 @Test public void testShouldTraceInstantiatedWithInjected() { 065 Object[] injected = new Object[0]; 066 Object instantiated = new Object(); 067 componentMonitor.instantiated(null, null, constructor, instantiated, injected, 543); 068 Assert.assertEquals(format(ComponentMonitorHelper.INSTANTIATED, 069 ctorToString(constructor), 070 (long)543, 071 instantiated.getClass().getName(), parmsToString(injected)) +NL, out.toString()); 072 } 073 074 @SuppressWarnings("unchecked") 075 @Test public void testShouldTraceInstantiationFailed() { 076 componentMonitor.instantiationFailed(null, null, constructor, new RuntimeException("doh")); 077 Assert.assertEquals(format(ComponentMonitorHelper.INSTANTIATION_FAILED, 078 ctorToString(constructor), "doh") +NL, out.toString()); 079 } 080 081 @Test public void testShouldTraceInvoking() { 082 componentMonitor.invoking(null, null, method, this, new Object[0]); 083 Assert.assertEquals(format(ComponentMonitorHelper.INVOKING, 084 methodToString(method), this) +NL, out.toString()); 085 } 086 087 @Test public void testShouldTraceInvoked() { 088 componentMonitor.invoked(null, null, method, this, 543, new Object[0], null); 089 Assert.assertEquals(format(ComponentMonitorHelper.INVOKED, 090 methodToString(method), this, 091 (long)543) +NL, out.toString()); 092 } 093 094 @Test public void testShouldTraceInvocatiationFailed() { 095 componentMonitor.invocationFailed(method, this, new RuntimeException("doh")); 096 Assert.assertEquals(format(ComponentMonitorHelper.INVOCATION_FAILED, 097 methodToString(method), this, "doh") +NL, out.toString()); 098 } 099 100 @SuppressWarnings("unchecked") 101 @Test public void testShouldTraceLifecycleInvocationFailed() { 102 try { 103 componentMonitor.lifecycleInvocationFailed(new TransientPicoContainer(), 104 new AbstractAdapter(Map.class, HashMap.class) { 105 public Object getComponentInstance(PicoContainer container) throws PicoCompositionException { 106 return getComponentInstance(container, null); 107 } 108 109 public Object getComponentInstance(PicoContainer container, Type into) 110 throws PicoCompositionException { 111 return "x"; 112 } 113 114 public void verify(PicoContainer container) 115 throws PicoCompositionException{ 116 } 117 118 public String getDescriptor() { 119 return null; 120 } 121 }, 122 method, 123 "fooooo", 124 new RuntimeException("doh")); 125 Assert.fail("should have barfed"); 126 } catch (PicoLifecycleException e) { 127 //expected 128 } 129 Assert.assertEquals(format(ComponentMonitorHelper.LIFECYCLE_INVOCATION_FAILED, 130 methodToString(method), "fooooo", "doh") + NL, 131 out.toString()); 132 } 133 134 @Test public void testNoComponent() { 135 136 componentMonitor.noComponentFound(new TransientPicoContainer(), "foo"); 137 Assert.assertEquals(format(ComponentMonitorHelper.NO_COMPONENT, 138 "foo") +NL, out.toString()); 139 } 140 141 142 }