Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultJUnitAppender |
|
| 1.25;1.25 |
1 | package com.nexwerk.log4junit.log4j; | |
2 | ||
3 | import org.apache.log4j.Level; | |
4 | import org.apache.log4j.spi.LoggingEvent; | |
5 | import org.apache.log4j.varia.NullAppender; | |
6 | ||
7 | ||
8 | /** | |
9 | * The JUnitAppender will record the messages sent to a standard Log4J | |
10 | * LoggingEvent, so that these can be recorded and asserted at a later stage. | |
11 | * | |
12 | * @author Enrique Comba Riepenhausen | |
13 | * Date: Nov 1, 2007 | |
14 | * Time: 9:59:41 PM | |
15 | */ | |
16 | public class DefaultJUnitAppender extends NullAppender | |
17 | implements JUnitAppender { | |
18 | ||
19 | private StringBuffer log; | |
20 | private String logRecord; | |
21 | private boolean recording; | |
22 | 11 | private Level level = Level.ERROR; |
23 | 11 | private boolean threshholdReached = false; |
24 | ||
25 | /** | |
26 | * Null constructor... | |
27 | */ | |
28 | 11 | public DefaultJUnitAppender() { |
29 | 11 | } |
30 | ||
31 | /** | |
32 | * Start the log recording process. | |
33 | */ | |
34 | public void startRecording() { | |
35 | 12 | log = new StringBuffer(); |
36 | 12 | logRecord = ""; |
37 | 12 | recording = true; |
38 | 12 | } |
39 | ||
40 | /** | |
41 | * Returns the log messages collected so far. | |
42 | * | |
43 | * @return The log messages. | |
44 | */ | |
45 | public String getLogMessages() { | |
46 | 4 | return logRecord; |
47 | } | |
48 | ||
49 | /** | |
50 | * Sets the threshold level to check against to see if an error has occured. | |
51 | * | |
52 | * @param level The level threshold | |
53 | */ | |
54 | public void setLogLevelThreshold(Level level) { | |
55 | 7 | this.level = level; |
56 | 7 | } |
57 | ||
58 | /** | |
59 | * Checks if the threshold has been reached. | |
60 | * | |
61 | * @return True if the threshold has been reached. | |
62 | */ | |
63 | public boolean logLevelReached() { | |
64 | 12 | return threshholdReached; |
65 | } | |
66 | ||
67 | /** | |
68 | * Stop the recording process to be able to start asserting the log | |
69 | * without the log being cluttered with more logging information. | |
70 | */ | |
71 | public void stopRecording() { | |
72 | 12 | recording = false; |
73 | 12 | logRecord = log.toString(); |
74 | 12 | } |
75 | ||
76 | /** | |
77 | * Whenever there is a call to the log4j logging methods (debug, info, | |
78 | * warn, error or fatal) this method will be called internally if the | |
79 | * log level is reached. | |
80 | * | |
81 | * @param event The logging even containing the information (i.e. message | |
82 | * exceptions, etc). | |
83 | */ | |
84 | public void doAppend(LoggingEvent event) { | |
85 | 47 | if (recording) { |
86 | 7 | log.append(event.getMessage()); |
87 | ||
88 | 7 | checkLogLevel(event); |
89 | } | |
90 | 47 | } |
91 | ||
92 | private void checkLogLevel(LoggingEvent event) { | |
93 | 7 | if (recording && !threshholdReached) { |
94 | 6 | threshholdReached = level != null |
95 | && event.getLevel().isGreaterOrEqual(level); | |
96 | } | |
97 | 7 | } |
98 | } |