KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > samples > TraceInterceptorTest


1 /*
2  * Nanning Aspects
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package com.tirsen.nanning.samples;
8
9 import com.tirsen.nanning.config.AspectSystem;
10 import com.tirsen.nanning.config.InterceptorAspect;
11 import com.tirsen.nanning.config.Introductor;
12 import junit.framework.TestCase;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.apache.commons.logging.impl.SimpleLog;
19 import org.apache.commons.logging.impl.NoOpLog;
20 import org.apache.commons.logging.Log;
21
22 /**
23  * TODO document TraceInterceptorTest
24  *
25  * <!-- $Id: TraceInterceptorTest.java,v 1.10 2003/05/26 05:39:32 tirsen Exp $ -->
26  *
27  * @author $Author: tirsen $
28  * @version $Revision: 1.10 $
29  */

30 public class TraceInterceptorTest extends TestCase {
31     public static class MockLogger implements Log {
32         private List JavaDoc expectMessages = new ArrayList JavaDoc();
33         private List JavaDoc actualMessages = new ArrayList JavaDoc();
34
35         public void error(Object JavaDoc message, Throwable JavaDoc throwable) {
36             actualMessages.add("ERROR " + message);
37         }
38
39         public void debug(Object JavaDoc message) {
40             actualMessages.add(String.valueOf(message));
41         }
42
43         public void expectAddMessage(String JavaDoc messageToExpect) {
44             expectMessages.add(messageToExpect);
45         }
46
47         public void verify() {
48             Iterator JavaDoc actualIterator = actualMessages.iterator();
49             Iterator JavaDoc expectIterator = expectMessages.iterator();
50             while (expectIterator.hasNext()) {
51                 String JavaDoc expectedMessage = (String JavaDoc) expectIterator.next();
52                 assertTrue("log output not as expected", actualIterator.hasNext());
53                 String JavaDoc actualMessage = (String JavaDoc) actualIterator.next();
54                 assertTrue("log output does not match: " + expectedMessage, actualMessage.startsWith(expectedMessage));
55             }
56         }
57
58         public void reset() {
59             actualMessages.clear();
60             expectMessages.clear();
61         }
62
63 ///CLOVER:OFF
64
public boolean isDebugEnabled() {
65             return true;
66         }
67
68         public boolean isErrorEnabled() {
69             return false;
70         }
71
72         public boolean isFatalEnabled() {
73             return false;
74         }
75
76         public boolean isInfoEnabled() {
77             return false;
78         }
79
80         public boolean isTraceEnabled() {
81             return false;
82         }
83
84         public boolean isWarnEnabled() {
85             return false;
86         }
87
88         public void trace(Object JavaDoc o) {
89         }
90
91         public void trace(Object JavaDoc o, Throwable JavaDoc throwable) {
92         }
93
94         public void debug(Object JavaDoc o, Throwable JavaDoc throwable) {
95         }
96
97         public void info(Object JavaDoc o) {
98         }
99
100         public void info(Object JavaDoc o, Throwable JavaDoc throwable) {
101         }
102
103         public void warn(Object JavaDoc o) {
104         }
105
106         public void warn(Object JavaDoc o, Throwable JavaDoc throwable) {
107         }
108
109         public void error(Object JavaDoc o) {
110         }
111
112         public void fatal(Object JavaDoc o) {
113         }
114
115         public void fatal(Object JavaDoc o, Throwable JavaDoc throwable) {
116         }
117         ///CLOVER:ON
118
}
119
120     public static interface Intf {
121         String JavaDoc call(String JavaDoc arg, String JavaDoc arg2);
122     }
123
124     public static class Impl implements Intf {
125         public String JavaDoc call(String JavaDoc arg, String JavaDoc arg2) {
126             return "hej tillbax!";
127         }
128     }
129
130     public static class ErrorImpl implements Intf {
131         public String JavaDoc call(String JavaDoc arg, String JavaDoc arg2) {
132             throw new RuntimeException JavaDoc("ERROR");
133         }
134     }
135
136     public void testLogInterceptor() throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
137         AspectSystem system = new AspectSystem();
138         MockLogger mockLogger = new MockLogger();
139         system.addAspect(new InterceptorAspect(new TraceInterceptor(mockLogger)));
140         system.addAspect(new Introductor(Intf.class, Impl.class));
141
142         mockLogger.expectAddMessage(">>> call(hej, svej)");
143         mockLogger.expectAddMessage("<<< call(hej, svej), took");
144         Intf intf = (Intf) system.newInstance(Intf.class);
145         intf.call("hej", "svej");
146         mockLogger.verify();
147     }
148 }
149
Popular Tags