01: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.self;
03:
04: import org.apache.log4j.AppenderSkeleton;
05: import org.apache.log4j.spi.LoggingEvent;
06:
07: import java.util.Iterator;
08: import java.util.List;
09: import java.util.Vector;
10:
11: /**
12: * Appender conserving the received log messages what allows test to ask him
13: * after execution of test code.
14: * @author Marc Guillemot
15: */
16: public class BufferingAppender extends AppenderSkeleton {
17: private List fEvents = new Vector();
18:
19: public boolean requiresLayout() {
20: return false;
21: }
22:
23: public void close() {
24: // nothing to do
25: }
26:
27: protected void append(final LoggingEvent event) {
28: fEvents.add(event);
29: }
30:
31: /**
32: * Gets the list of received events
33: * @return a list of {@link LoggingEvent}
34: */
35: public List getEvents() {
36: return fEvents;
37: }
38:
39: /**
40: * Gets a string containing all messages received.
41: */
42: public String allMessagesToString() {
43: StringBuffer sb = new StringBuffer();
44: for (Iterator iter = fEvents.iterator(); iter.hasNext();) {
45: LoggingEvent elt = (LoggingEvent) iter.next();
46: sb.append(elt.getMessage());
47: if (iter.hasNext())
48: sb.append("\n");
49: }
50: return sb.toString();
51: }
52:
53: /**
54: * Indicates if one of the received events has the given message
55: */
56: public boolean containsMessage(final String message) {
57: for (Iterator iter = fEvents.iterator(); iter.hasNext();) {
58: LoggingEvent elt = (LoggingEvent) iter.next();
59: if (message.equals(elt.getMessage()))
60: return true;
61: }
62: return false;
63: }
64: }
|