1 package org.picocontainer.monitors; 2 3 import java.io.StringWriter ; 4 import java.io.Writer ; 5 import java.lang.reflect.Constructor ; 6 import java.lang.reflect.Method ; 7 8 import junit.framework.TestCase; 9 10 import org.picocontainer.ComponentMonitor; 11 12 17 public class WriterComponentMonitorTestCase extends TestCase { 18 private Writer out; 19 private ComponentMonitor componentMonitor; 20 private static final String NL = System.getProperty("line.separator"); 21 private Constructor constructor; 22 private Method method; 23 24 protected void setUp() throws Exception { 25 out = new StringWriter (); 26 constructor = getClass().getConstructor((Class [])null); 27 method = getClass().getDeclaredMethod("setUp", (Class [])null); 28 componentMonitor = new WriterComponentMonitor(out); 29 } 30 31 public void testShouldTraceInstantiating() { 32 componentMonitor.instantiating(constructor); 33 assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INSTANTIATING, new Object []{AbstractComponentMonitor.toString(constructor)}) +NL, out.toString()); 34 } 35 36 public void testShouldTraceInstantiated() { 37 componentMonitor.instantiated(constructor, 543); 38 assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INSTANTIATED, new Object []{AbstractComponentMonitor.toString(constructor), new Long (543)}) +NL, out.toString()); 39 } 40 41 public void testShouldTraceInstantiatedWithInjected() { 42 Object [] injected = new Object [0]; 43 Object instantiated = new Object (); 44 componentMonitor.instantiated(constructor, instantiated, injected, 543); 45 assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INSTANTIATED2, new Object []{AbstractComponentMonitor.toString(constructor), new Long (543), instantiated.getClass().getName(), WriterComponentMonitor.toString(injected)}) +NL, out.toString()); 46 } 47 48 49 public void testShouldTraceInstantiationFailed() { 50 componentMonitor.instantiationFailed(constructor, new RuntimeException ("doh")); 51 assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INSTANTIATION_FAILED, new Object []{AbstractComponentMonitor.toString(constructor), "doh"}) +NL, out.toString()); 52 } 53 54 public void testShouldTraceInvoking() { 55 componentMonitor.invoking(method, this); 56 assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INVOKING, new Object []{AbstractComponentMonitor.toString(method), this}) +NL, out.toString()); 57 } 58 59 public void testShouldTraceInvoked() { 60 componentMonitor.invoked(method, this, 543); 61 assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INVOKED, new Object []{AbstractComponentMonitor.toString(method), this, new Long (543)}) +NL, out.toString()); 62 } 63 64 public void testShouldTraceInvocatiationFailed() { 65 componentMonitor.invocationFailed(method, this, new RuntimeException ("doh")); 66 assertEquals(WriterComponentMonitor.format(WriterComponentMonitor.INVOCATION_FAILED, new Object []{AbstractComponentMonitor.toString(method), this, "doh"}) +NL, out.toString()); 67 } 68 69 } 70 | Popular Tags |