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