001: /*
002: * Copyright (C) The DNA Group. All rights reserved.
003: *
004: * This software is published under the terms of the DNA
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.dna.impl;
009:
010: import junit.framework.TestCase;
011: import org.apache.log4j.Appender;
012: import org.apache.log4j.Level;
013: import org.apache.log4j.Logger;
014: import org.codehaus.dna.impl.Log4JLogger;
015:
016: public class Log4JLoggerTestCase extends TestCase {
017: public void testLog4JLoggerEmptyCtor() throws Exception {
018: try {
019: new Log4JLogger(null);
020: } catch (NullPointerException npe) {
021: assertEquals("npe.getMessage()", "logger", npe.getMessage());
022: }
023: }
024:
025: public void testLog4JLoggerGetChildLogger() throws Exception {
026: final MockAppender target = new MockAppender();
027: final Log4JLogger logger = createLogger(target, Level.DEBUG);
028:
029: assertNotSame("logger.getChildLogger == logger", logger, logger
030: .getChildLogger("whatever"));
031: }
032:
033: public void testLog4JLoggerTraceEnabled() throws Exception {
034: final Level level = Level.DEBUG;
035: final Level type = Level.DEBUG;
036: final String message = "Meep!";
037: final Throwable throwable = null;
038: final boolean output = true;
039:
040: final MockAppender target = new MockAppender();
041: final Log4JLogger logger = createLogger(target, level);
042: logger.trace(message);
043: checkLogger(target, output, message, throwable, type);
044: }
045:
046: public void testLog4JLoggerTraceDisabled() throws Exception {
047: final Level level = Level.ERROR;
048: final String message = "Meep!";
049:
050: final MockAppender target = new MockAppender();
051: final Log4JLogger logger = createLogger(target, level);
052: logger.trace(message);
053: checkLogger(target, false, null, null, null);
054: }
055:
056: public void testLog4JLoggerTraceWithExceptionEnabled()
057: throws Exception {
058: final Level level = Level.DEBUG;
059: final Level type = Level.DEBUG;
060: final String message = "Meep!";
061: final Throwable throwable = new Throwable();
062: final boolean output = true;
063:
064: final MockAppender target = new MockAppender();
065: final Log4JLogger logger = createLogger(target, level);
066:
067: logger.trace(message, throwable);
068: checkLogger(target, output, message, throwable, type);
069: }
070:
071: public void testLog4JLoggerTraceWithExceptionDisabled()
072: throws Exception {
073: final Level level = Level.ERROR;
074: final String message = "Meep!";
075: final Throwable throwable = new Throwable();
076:
077: final MockAppender target = new MockAppender();
078: final Log4JLogger logger = createLogger(target, level);
079:
080: logger.trace(message, throwable);
081: checkLogger(target, false, null, null, null);
082: }
083:
084: public void testLog4JLoggerDebugEnabled() throws Exception {
085: final Level level = Level.DEBUG;
086: final Level type = Level.DEBUG;
087: final String message = "Meep!";
088: final Throwable throwable = null;
089: final boolean output = true;
090:
091: final MockAppender target = new MockAppender();
092: final Log4JLogger logger = createLogger(target, level);
093: logger.debug(message);
094: checkLogger(target, output, message, throwable, type);
095: }
096:
097: public void testLog4JLoggerDebugDisabled() throws Exception {
098: final Level level = Level.ERROR;
099: final String message = "Meep!";
100:
101: final MockAppender target = new MockAppender();
102: final Log4JLogger logger = createLogger(target, level);
103: logger.debug(message);
104: checkLogger(target, false, null, null, null);
105: }
106:
107: public void testLog4JLoggerDebugWithExceptionEnabled()
108: throws Exception {
109: final Level level = Level.DEBUG;
110: final Level type = Level.DEBUG;
111: final String message = "Meep!";
112: final Throwable throwable = new Throwable();
113: final boolean output = true;
114:
115: final MockAppender target = new MockAppender();
116: final Log4JLogger logger = createLogger(target, level);
117: logger.debug(message, throwable);
118: checkLogger(target, output, message, throwable, type);
119: }
120:
121: public void testLog4JLoggerDebugWithExceptionDisabled()
122: throws Exception {
123: final Level level = Level.ERROR;
124: final String message = "Meep!";
125: final Throwable throwable = new Throwable();
126:
127: final MockAppender target = new MockAppender();
128: final Log4JLogger logger = createLogger(target, level);
129: logger.debug(message, throwable);
130: checkLogger(target, false, null, null, null);
131: }
132:
133: public void testLog4JLoggerInfoEnabled() throws Exception {
134: final Level level = Level.DEBUG;
135: final Level type = Level.INFO;
136: final String message = "Meep!";
137: final Throwable throwable = null;
138: final boolean output = true;
139:
140: final MockAppender target = new MockAppender();
141: final Log4JLogger logger = createLogger(target, level);
142: logger.info(message);
143: checkLogger(target, output, message, throwable, type);
144: }
145:
146: public void testLog4JLoggerInfoDisabled() throws Exception {
147: final Level level = Level.ERROR;
148: final String message = "Meep!";
149:
150: final MockAppender target = new MockAppender();
151: final Log4JLogger logger = createLogger(target, level);
152: logger.info(message);
153: checkLogger(target, false, null, null, null);
154: }
155:
156: public void testLog4JLoggerInfoWithExceptionEnabled()
157: throws Exception {
158: final Level level = Level.DEBUG;
159: final Level type = Level.INFO;
160: final String message = "Meep!";
161: final Throwable throwable = new Throwable();
162: final boolean output = true;
163:
164: final MockAppender target = new MockAppender();
165: final Log4JLogger logger = createLogger(target, level);
166: logger.info(message, throwable);
167: checkLogger(target, output, message, throwable, type);
168: }
169:
170: public void testLog4JLoggerInfoWithExceptionDisabled()
171: throws Exception {
172: final Level level = Level.ERROR;
173: final String message = "Meep!";
174: final Throwable throwable = new Throwable();
175:
176: final MockAppender target = new MockAppender();
177: final Log4JLogger logger = createLogger(target, level);
178: logger.info(message, throwable);
179: checkLogger(target, false, null, null, null);
180: }
181:
182: public void testLog4JLoggerWarnEnabled() throws Exception {
183: final Level level = Level.DEBUG;
184: final Level type = Level.WARN;
185: final String message = "Meep!";
186: final Throwable throwable = null;
187: final boolean output = true;
188:
189: final MockAppender target = new MockAppender();
190: final Log4JLogger logger = createLogger(target, level);
191: logger.warn(message);
192: checkLogger(target, output, message, throwable, type);
193: }
194:
195: public void testLog4JLoggerWarnDisabled() throws Exception {
196: final Level level = Level.ERROR;
197: final String message = "Meep!";
198:
199: final MockAppender target = new MockAppender();
200: final Log4JLogger logger = createLogger(target, level);
201: logger.warn(message);
202: checkLogger(target, false, null, null, null);
203: }
204:
205: public void testLog4JLoggerWarnWithExceptionEnabled()
206: throws Exception {
207: final Level level = Level.DEBUG;
208: final Level type = Level.WARN;
209: final String message = "Meep!";
210: final Throwable throwable = new Throwable();
211: final boolean output = true;
212:
213: final MockAppender target = new MockAppender();
214: final Log4JLogger logger = createLogger(target, level);
215: logger.warn(message, throwable);
216: checkLogger(target, output, message, throwable, type);
217: }
218:
219: public void testLog4JLoggerWarnWithExceptionDisabled()
220: throws Exception {
221: final Level level = Level.ERROR;
222: final String message = "Meep!";
223: final Throwable throwable = new Throwable();
224:
225: final MockAppender target = new MockAppender();
226: final Log4JLogger logger = createLogger(target, level);
227: logger.warn(message, throwable);
228: checkLogger(target, false, null, null, null);
229: }
230:
231: public void testLog4JLoggerErrorEnabled() throws Exception {
232: final Level level = Level.DEBUG;
233: final Level type = Level.ERROR;
234: final String message = "Meep!";
235: final Throwable throwable = null;
236: final boolean output = true;
237:
238: final MockAppender target = new MockAppender();
239: final Log4JLogger logger = createLogger(target, level);
240: logger.error(message);
241: checkLogger(target, output, message, throwable, type);
242: }
243:
244: public void testLog4JLoggerErrorWithExceptionEnabled()
245: throws Exception {
246: final Level level = Level.DEBUG;
247: final Level type = Level.ERROR;
248: final String message = "Meep!";
249: final Throwable throwable = new Throwable();
250: final boolean output = true;
251:
252: final MockAppender target = new MockAppender();
253: final Log4JLogger logger = createLogger(target, level);
254: logger.error(message, throwable);
255: checkLogger(target, output, message, throwable, type);
256: }
257:
258: public void testConsoleLevelComparisonWithDebugEnabled()
259: throws Exception {
260: final MockAppender target = new MockAppender();
261: final Log4JLogger logger = createLogger(target, Level.DEBUG);
262:
263: assertEquals("logger.isTraceEnabled()", true, logger
264: .isTraceEnabled());
265: assertEquals("logger.isDebugEnabled()", true, logger
266: .isDebugEnabled());
267: assertEquals("logger.isInfoEnabled()", true, logger
268: .isInfoEnabled());
269: assertEquals("logger.isWarnEnabled()", true, logger
270: .isWarnEnabled());
271: assertEquals("logger.isErrorEnabled()", true, logger
272: .isErrorEnabled());
273: }
274:
275: public void testConsoleLevelComparisonWithInfoEnabled()
276: throws Exception {
277: final MockAppender target = new MockAppender();
278: final Log4JLogger logger = createLogger(target, Level.INFO);
279:
280: assertEquals("logger.isTraceEnabled()", false, logger
281: .isTraceEnabled());
282: assertEquals("logger.isDebugEnabled()", false, logger
283: .isDebugEnabled());
284: assertEquals("logger.isInfoEnabled()", true, logger
285: .isInfoEnabled());
286: assertEquals("logger.isWarnEnabled()", true, logger
287: .isWarnEnabled());
288: assertEquals("logger.isErrorEnabled()", true, logger
289: .isErrorEnabled());
290: }
291:
292: public void testConsoleLevelComparisonWithWarnEnabled()
293: throws Exception {
294: final MockAppender target = new MockAppender();
295: final Log4JLogger logger = createLogger(target, Level.WARN);
296:
297: assertEquals("logger.isTraceEnabled()", false, logger
298: .isTraceEnabled());
299: assertEquals("logger.isDebugEnabled()", false, logger
300: .isDebugEnabled());
301: assertEquals("logger.isInfoEnabled()", false, logger
302: .isInfoEnabled());
303: assertEquals("logger.isWarnEnabled()", true, logger
304: .isWarnEnabled());
305: assertEquals("logger.isErrorEnabled()", true, logger
306: .isErrorEnabled());
307: }
308:
309: public void testConsoleLevelComparisonWithErrorEnabled()
310: throws Exception {
311: final MockAppender target = new MockAppender();
312: final Log4JLogger logger = createLogger(target, Level.ERROR);
313:
314: assertEquals("logger.isTraceEnabled()", false, logger
315: .isTraceEnabled());
316: assertEquals("logger.isDebugEnabled()", false, logger
317: .isDebugEnabled());
318: assertEquals("logger.isInfoEnabled()", false, logger
319: .isInfoEnabled());
320: assertEquals("logger.isWarnEnabled()", false, logger
321: .isWarnEnabled());
322: assertEquals("logger.isErrorEnabled()", true, logger
323: .isErrorEnabled());
324: }
325:
326: private Log4JLogger createLogger(final Appender target,
327: final Level priority) {
328: final Logger log4jLogger = Logger.getLogger("test");
329: log4jLogger.removeAllAppenders();
330: log4jLogger.addAppender(target);
331: log4jLogger.setLevel(priority);
332: return new Log4JLogger(log4jLogger);
333: }
334:
335: private void checkLogger(final MockAppender target,
336: final boolean output, final String message,
337: final Throwable throwable, final Level priority) {
338: assertEquals("logger.m_message == message", message,
339: target.m_message);
340: assertEquals("logger.m_output == output", output,
341: target.m_output);
342: assertEquals("logger.m_throwable == null", throwable,
343: target.m_throwable);
344: assertEquals("logger.m_priority == null", priority,
345: target.m_priority);
346: }
347: }
|