001    package org.picocontainer.monitors;
002    
003    import java.io.StringWriter;
004    import java.io.Writer;
005    import java.lang.reflect.Constructor;
006    import java.lang.reflect.Method;
007    
008    import junit.framework.TestCase;
009    
010    import org.picocontainer.ComponentMonitor;
011    
012    /**
013     * @author Aslak Hellesøy
014     * @author Mauro Talevi
015     * @version $Revision: 2973 $
016     */
017    public class WriterComponentMonitorTestCase extends TestCase {
018        private Writer out;
019        private ComponentMonitor componentMonitor;
020        private static final String NL = System.getProperty("line.separator");
021        private Constructor constructor;
022        private Method method;
023    
024        protected void setUp() throws Exception {
025            out = new StringWriter();
026            constructor = getClass().getConstructor((Class[])null);
027            method = getClass().getDeclaredMethod("setUp", (Class[])null);
028            componentMonitor = new WriterComponentMonitor(out);
029        }
030    
031        public void testShouldTraceInstantiating() {
032            componentMonitor.instantiating(constructor);
033            assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INSTANTIATING, new Object[]{AbstractComponentMonitor.toString(constructor)}) +NL,  out.toString());
034        }
035    
036        public void testShouldTraceInstantiated() {
037            componentMonitor.instantiated(constructor, 543);
038            assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INSTANTIATED, new Object[]{AbstractComponentMonitor.toString(constructor), new Long(543)}) +NL,  out.toString());
039        }
040    
041        public void testShouldTraceInstantiatedWithInjected() {
042            Object[] injected = new Object[0];
043            Object instantiated = new Object();
044            componentMonitor.instantiated(constructor, instantiated, injected, 543);
045            assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INSTANTIATED2, new Object[]{AbstractComponentMonitor.toString(constructor), new Long(543), instantiated.getClass().getName(), WriterComponentMonitor.toString(injected)}) +NL,  out.toString());
046        }
047    
048    
049        public void testShouldTraceInstantiationFailed() {
050            componentMonitor.instantiationFailed(constructor, new RuntimeException("doh"));
051            assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INSTANTIATION_FAILED, new Object[]{AbstractComponentMonitor.toString(constructor), "doh"}) +NL,  out.toString());
052        }
053    
054        public void testShouldTraceInvoking() {
055            componentMonitor.invoking(method, this);
056            assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INVOKING, new Object[]{AbstractComponentMonitor.toString(method), this}) +NL,  out.toString());
057        }
058    
059        public void testShouldTraceInvoked() {
060            componentMonitor.invoked(method, this, 543);
061            assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INVOKED, new Object[]{AbstractComponentMonitor.toString(method), this, new Long(543)}) +NL,  out.toString());
062        }
063    
064        public void testShouldTraceInvocatiationFailed() {
065            componentMonitor.invocationFailed(method, this, new RuntimeException("doh"));
066            assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INVOCATION_FAILED, new Object[]{AbstractComponentMonitor.toString(method), this, "doh"}) +NL,  out.toString());
067        }
068    
069    }