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