1 8 package org.picocontainer.gems.monitors; 9 10 import java.io.BufferedReader ; 11 import java.io.IOException ; 12 import java.io.Reader ; 13 import java.io.StringReader ; 14 import java.lang.reflect.Constructor ; 15 import java.lang.reflect.Method ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 19 import junit.framework.TestCase; 20 21 import org.picocontainer.ComponentMonitor; 22 import org.picocontainer.monitors.AbstractComponentMonitor; 23 24 31 public abstract class AbstractComponentMonitorTestCase extends TestCase { 32 private ComponentMonitor componentMonitor; 33 private Constructor constructor; 34 private Method method; 35 36 protected void setUp() throws Exception { 37 constructor = getConstructor(); 38 method = getMethod(); 39 componentMonitor = makeComponentMonitor(); 40 } 41 42 protected abstract ComponentMonitor makeComponentMonitor(); 43 44 protected abstract Constructor getConstructor() throws NoSuchMethodException ; 45 46 protected abstract Method getMethod() throws NoSuchMethodException ; 47 48 protected abstract String getLogPrefix(); 49 50 protected void tearDown() throws Exception { 51 ForTestSakeAppender.CONTENT = ""; 52 } 53 54 public void testShouldTraceInstantiating() throws Exception { 55 componentMonitor.instantiating(constructor); 56 assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INSTANTIATING, new Object []{AbstractComponentMonitor.toString(constructor)})); 57 } 58 59 public void testShouldTraceInstantiated() throws Exception { 60 componentMonitor.instantiated(constructor, 543); 61 String line = getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INSTANTIATED, new Object []{AbstractComponentMonitor.toString(constructor), new Long (543)}); 62 assertFileContent(line); 63 } 64 65 public void testShouldTraceInstantiatedWithInjected() throws Exception { 66 Object [] injected = new Object [0]; 67 Object instantiated = new Object (); 68 componentMonitor.instantiated(constructor, instantiated, injected, 543); 69 String s = AbstractComponentMonitor.format(AbstractComponentMonitor.INSTANTIATED2, new Object []{AbstractComponentMonitor.toString(constructor), new Long (543), instantiated.getClass().getName(), AbstractComponentMonitor.toString(injected)}); 70 assertFileContent(getLogPrefix() + s); 71 } 72 73 74 public void testShouldTraceInstantiationFailed() throws Exception { 75 componentMonitor.instantiationFailed(constructor, new RuntimeException ("doh")); 76 assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INSTANTIATION_FAILED, new Object []{AbstractComponentMonitor.toString(constructor), "doh"})); 77 } 78 79 public void testShouldTraceInvoking() throws Exception { 80 componentMonitor.invoking(method, this); 81 assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INVOKING, new Object []{AbstractComponentMonitor.toString(method), this})); 82 } 83 84 public void testShouldTraceInvoked() throws Exception { 85 componentMonitor.invoked(method, this, 543); 86 assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INVOKED, new Object []{AbstractComponentMonitor.toString(method), this, new Long (543)})); 87 } 88 89 public void testShouldTraceInvocatiationFailed() throws Exception { 90 componentMonitor.invocationFailed(method, this, new RuntimeException ("doh")); 91 assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INVOCATION_FAILED, new Object []{AbstractComponentMonitor.toString(method), this, "doh"})); 92 } 93 94 protected void assertFileContent(String line) throws IOException { 95 List lines = toLines( new StringReader ( ForTestSakeAppender.CONTENT ) ); 96 String s = lines.toString(); 97 assertTrue("Line '" + line + "' not found", s.indexOf(line) > 0); 98 } 99 100 protected List toLines(Reader resource) throws IOException { 101 BufferedReader br = new BufferedReader (resource); 102 List lines = new ArrayList (); 103 String line = br.readLine(); 104 while ( line != null) { 105 lines.add(line); 106 line = br.readLine(); 107 } 108 return lines; 109 } 110 111 } 112 | Popular Tags |