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