Source code: junit/log4j/LoggedTestCase.java
1 package junit.log4j;
2 /*
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.<p>
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.<p>
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br>
16 * http://www.gnu.org/copyleft/lesser.html
17 */
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.util.Properties;
23 import junit.framework.TestCase;
24 import org.apache.log4j.Category;
25 import org.apache.log4j.Priority;
26 import org.apache.log4j.PropertyConfigurator;
27
28 // Configuration Management Information:
29 // -------------------------------------
30 // $Id: LoggedTestCase.java,v 1.1 2001/11/12 21:35:31 wreissen Exp $
31 //
32 // Version History:
33 // ----------------
34 // $Log: LoggedTestCase.java,v $
35 // Revision 1.1 2001/11/12 21:35:31 wreissen
36 // moved from openfuture.bugbase.test.common
37 //
38 //
39 // ***********************************************************************************
40 /**
41 * <p>Extension of <code>TestCase</code> integrating
42 * <a href="http://jakarta.apache.org/log4j/index.html">Log4J</a>.
43 * </p>
44 *
45 *
46 * Created: Fri Nov 02 14:21:45 2001
47 *
48 * @author <a href="mailto:wolfgang@openfuture.de">Wolfgang Reissenberger</a>
49 * @version $Revision: 1.1 $
50 */
51
52 public class LoggedTestCase extends TestCase {
53
54
55 protected Category category;
56 protected boolean isLog4jInClasspath;
57
58
59 /**
60 * <p>Creates a new <code>LoggedTestCase</code> instance. The
61 * <a href="http://jakarta.apache.org/log4j/index.html">Log4J</a>
62 * properties are read from the file <code>name</code> from the
63 * current directory. If the file does not exist, the default
64 * <a href="http://jakarta.apache.org/log4j/index.html">Log4J</a>
65 * properties are used.</p>
66 *
67 * If <a href="http://jakarta.apache.org/log4j/index.html">Log4J</a>
68 * is not in the classpath, all log messages will be written to
69 * <code>stdout</code>. This is to make it easy on user
70 * who do not want to have to download log4j and put it in their
71 * classpath !
72 *
73 * @param name name of the testcase
74 * @param propertyFile file name of the property file
75 * for the configuration
76 */
77 public LoggedTestCase(String name, String propertyFile) {
78 super(name);
79
80 // Check if Log4j is in the classpath. If not, use a dummy
81 // implementation that does nothing. This is to make it easy on user
82 // who do not want to have to download log4j and put it in their
83 // classpath !
84 this.isLog4jInClasspath = true;
85 try {
86 Class aClass =
87 Class.forName("org.apache.log4j.Category");
88
89 this.category = Category.getInstance(getClass().getName());
90
91 Properties properties = new Properties();
92
93 // load the property file from the current directory
94 try {
95 File file = new File(propertyFile);
96 FileInputStream is = new FileInputStream(file);
97 properties.load(is);
98 is.close();
99 } catch (IOException e) {
100 // ignore it
101 }
102 PropertyConfigurator.configure(properties);
103 debug("Log4J successfully instantiated.");
104 } catch (ClassNotFoundException e) {
105 this.isLog4jInClasspath = false;
106 debug("Log4J instantiation failed. Using stdout.");
107 }
108 }
109
110
111 /**
112 * Calls {@link LoggedTestCase#LoggedTestCase(java.lang.String,
113 * java.lang.String)
114 * LoggedTestCase(<code>name</code>,
115 * <code>log4j.properties</code>)}.
116 *
117 * @param name test case name
118 */
119 public LoggedTestCase(String name) {
120 this(name, "log4j.properties");
121 }
122
123 /**
124 * Setup of the test case. Simply writes "Set up started."
125 * to the logger with {@link #info(Object) priority INFO}.
126 *
127 * @exception Exception if an error occurs
128 */
129 public void setUp() throws Exception {
130 info("Set up started.");
131 }
132
133
134 /**
135 * Tear down of the test case. Simply writes "Tear down finished."
136 * to the logger with {@link #info(Object) priority INFO}.
137 *
138 * @exception Exception if an error occurs
139 */
140 public void tearDown() throws Exception {
141 info("Tear down finished.");
142 }
143
144
145 /**
146 * Redirection to {@link org.apache.log4j.Category#debug(Object) debug()}
147 * in the testcase {@link #category category}.
148 *
149 * @param message the message object to log
150 */
151 public void debug(Object message) {
152 if (isLog4jInClasspath) {
153 this.category.debug(message);
154 } else {
155 System.out.println("[debug]: " + message);
156 }
157 }
158
159
160 /**
161 * Redirection to
162 * {@link org.apache.log4j.Category#debug(Object, Throwable) debug()}
163 * in the testcase {@link #category category}.
164 *
165 * @param message the message object to log
166 * @param t the exception to log, including its stack trace
167 */
168 public void debug(Object message, Throwable t) {
169 if (isLog4jInClasspath) {
170 this.category.debug(message, t);
171 } else {
172 System.out.println("[debug]: " + message);
173 }
174 }
175
176
177
178 /**
179 * Redirection to {@link org.apache.log4j.Category#error(Object) error()}
180 * in the testcase {@link #category category}.
181 *
182 * @param message the message object to log
183 */
184 public void error(Object message) {
185 if (isLog4jInClasspath) {
186 this.category.error(message);
187 } else {
188 System.out.println("[error]: " + message);
189 }
190 }
191
192
193
194 /**
195 * Redirection to
196 * {@link org.apache.log4j.Category#error(Object, Throwable) error()}
197 * in the testcase {@link #category category}.
198 *
199 * @param message the message object to log
200 * @param t the exception to log, including its stack trace
201 */
202 public void error(Object message, Throwable t) {
203 if (isLog4jInClasspath) {
204 this.category.error(message, t);
205 } else {
206 System.out.println("[error]: " + message);
207 }
208 }
209
210
211
212 /**
213 * Redirection to {@link org.apache.log4j.Category#fatal(Object) fatal()}
214 * in the testcase {@link #category category}.
215 *
216 * @param message the message object to log
217 */
218 public void fatal(Object message) {
219 if (isLog4jInClasspath) {
220 this.category.fatal(message);
221 } else {
222 System.out.println("[fatal]: " + message);
223 }
224 }
225
226
227
228 /**
229 * Redirection to
230 * {@link org.apache.log4j.Category#fatal(Object, Throwable) fatal()}
231 * in the testcase {@link #category category}.
232 *
233 * @param message the message object to log
234 * @param t the exception to log, including its stack trace
235 */
236 public void fatal(Object message, Throwable t) {
237 if (isLog4jInClasspath) {
238 this.category.fatal(message, t);
239 } else {
240 System.out.println("[fatal]: " + message);
241 }
242 }
243
244
245
246 /**
247 * Redirection to {@link org.apache.log4j.Category#info(Object) info()}
248 * in the testcase {@link #category category}.
249 *
250 * @param message the message object to log
251 */
252 public void info(Object message) {
253 if (isLog4jInClasspath) {
254 this.category.info(message);
255 } else {
256 System.out.println("[info]: " + message);
257 }
258 }
259
260
261
262 /**
263 * Redirection to
264 * {@link org.apache.log4j.Category#info(Object, Throwable) info()}
265 * in the testcase {@link #category category}.
266 *
267 * @param message the message object to log
268 * @param t the exception to log, including its stack trace
269 */
270 public void info(Object message, Throwable t) {
271 if (isLog4jInClasspath) {
272 this.category.info(message, t);
273 } else {
274 System.out.println("[info]: " + message);
275 }
276 }
277
278
279
280 /**
281 * Redirection to {@link org.apache.log4j.Category#warn(Object) warn()}
282 * in the testcase {@link #category category}.
283 *
284 * @param message the message object to log
285 */
286 public void warn(Object message) {
287 if (isLog4jInClasspath) {
288 this.category.warn(message);
289 } else {
290 System.out.println("[warn]: " + message);
291 }
292 }
293
294
295
296 /**
297 * Redirection to
298 * {@link org.apache.log4j.Category#warn(Object, Throwable) warn()}
299 * in the testcase {@link #category category}.
300 *
301 * @param message the message object to log
302 * @param t the exception to log, including its stack trace
303 */
304 public void warn(Object message, Throwable t) {
305 if (isLog4jInClasspath) {
306 this.category.warn(message, t);
307 } else {
308 System.out.println("[warn]: " + message);
309 }
310 }
311
312
313 } // LoggedTestCase