KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > monitors > AbstractComponentMonitorTestCase


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 package org.picocontainer.gems.monitors;
9
10 import java.io.BufferedReader JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.Reader JavaDoc;
13 import java.io.StringReader JavaDoc;
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import junit.framework.TestCase;
20
21 import org.picocontainer.ComponentMonitor;
22 import org.picocontainer.monitors.AbstractComponentMonitor;
23
24 /**
25  * @author Paul Hammant
26  * @author Aslak Hellesøy
27  * @author Mauro Talevi
28  * @author Juze Peleteiro
29  * @version $Revision: 2024 $
30  */

31 public abstract class AbstractComponentMonitorTestCase extends TestCase {
32     private ComponentMonitor componentMonitor;
33     private Constructor JavaDoc constructor;
34     private Method JavaDoc method;
35     
36     protected void setUp() throws Exception JavaDoc {
37         constructor = getConstructor();
38         method = getMethod();
39         componentMonitor = makeComponentMonitor();
40     }
41
42     protected abstract ComponentMonitor makeComponentMonitor();
43     
44     protected abstract Constructor JavaDoc getConstructor() throws NoSuchMethodException JavaDoc;
45
46     protected abstract Method JavaDoc getMethod() throws NoSuchMethodException JavaDoc;
47     
48     protected abstract String JavaDoc getLogPrefix();
49
50     protected void tearDown() throws Exception JavaDoc {
51         ForTestSakeAppender.CONTENT = "";
52     }
53
54     public void testShouldTraceInstantiating() throws Exception JavaDoc {
55         componentMonitor.instantiating(constructor);
56         assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INSTANTIATING, new Object JavaDoc[]{AbstractComponentMonitor.toString(constructor)}));
57     }
58
59     public void testShouldTraceInstantiated() throws Exception JavaDoc {
60         componentMonitor.instantiated(constructor, 543);
61         String JavaDoc line = getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INSTANTIATED, new Object JavaDoc[]{AbstractComponentMonitor.toString(constructor), new Long JavaDoc(543)});
62         assertFileContent(line);
63     }
64
65     public void testShouldTraceInstantiatedWithInjected() throws Exception JavaDoc {
66         Object JavaDoc[] injected = new Object JavaDoc[0];
67         Object JavaDoc instantiated = new Object JavaDoc();
68         componentMonitor.instantiated(constructor, instantiated, injected, 543);
69         String JavaDoc s = AbstractComponentMonitor.format(AbstractComponentMonitor.INSTANTIATED2, new Object JavaDoc[]{AbstractComponentMonitor.toString(constructor), new Long JavaDoc(543), instantiated.getClass().getName(), AbstractComponentMonitor.toString(injected)});
70         assertFileContent(getLogPrefix() + s);
71     }
72
73
74     public void testShouldTraceInstantiationFailed() throws Exception JavaDoc {
75         componentMonitor.instantiationFailed(constructor, new RuntimeException JavaDoc("doh"));
76         assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INSTANTIATION_FAILED, new Object JavaDoc[]{AbstractComponentMonitor.toString(constructor), "doh"}));
77     }
78
79     public void testShouldTraceInvoking() throws Exception JavaDoc {
80         componentMonitor.invoking(method, this);
81         assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INVOKING, new Object JavaDoc[]{AbstractComponentMonitor.toString(method), this}));
82     }
83
84     public void testShouldTraceInvoked() throws Exception JavaDoc {
85         componentMonitor.invoked(method, this, 543);
86         assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INVOKED, new Object JavaDoc[]{AbstractComponentMonitor.toString(method), this, new Long JavaDoc(543)}));
87     }
88
89     public void testShouldTraceInvocatiationFailed() throws Exception JavaDoc {
90         componentMonitor.invocationFailed(method, this, new RuntimeException JavaDoc("doh"));
91         assertFileContent(getLogPrefix() + AbstractComponentMonitor.format(AbstractComponentMonitor.INVOCATION_FAILED, new Object JavaDoc[]{AbstractComponentMonitor.toString(method), this, "doh"}));
92     }
93
94     protected void assertFileContent(String JavaDoc line) throws IOException JavaDoc{
95         List JavaDoc lines = toLines( new StringReader JavaDoc( ForTestSakeAppender.CONTENT ) );
96         String JavaDoc s = lines.toString();
97         assertTrue("Line '" + line + "' not found", s.indexOf(line) > 0);
98     }
99     
100     protected List JavaDoc toLines(Reader JavaDoc resource) throws IOException JavaDoc {
101         BufferedReader JavaDoc br = new BufferedReader JavaDoc(resource);
102         List JavaDoc lines = new ArrayList JavaDoc();
103         String JavaDoc 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