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