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