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.log.Hierarchy;
012: import org.apache.log.LogTarget;
013: import org.apache.log.Logger;
014: import org.apache.log.Priority;
015: import org.codehaus.dna.impl.LogkitLogger;
016:
017: public class LogkitLoggerTestCase extends TestCase {
018: public void testLogkitLoggerEmptyCtor() throws Exception {
019: try {
020: new LogkitLogger(null);
021: } catch (NullPointerException npe) {
022: assertEquals("npe.getMessage()", "logger", npe.getMessage());
023: }
024: }
025:
026: public void testLogkitLoggerGetChildLogger() throws Exception {
027: final MockLogTarget target = new MockLogTarget();
028: final LogkitLogger logger = createLogger(target, Priority.DEBUG);
029:
030: assertNotSame("logger.getChildLogger == logger", logger, logger
031: .getChildLogger("whatever"));
032: }
033:
034: public void testLogkitLoggerTraceEnabled() throws Exception {
035: final Priority level = Priority.DEBUG;
036: final Priority type = Priority.DEBUG;
037: final String message = "Meep!";
038: final Throwable throwable = null;
039: final boolean output = true;
040:
041: final MockLogTarget target = new MockLogTarget();
042: final LogkitLogger logger = createLogger(target, level);
043: logger.trace(message);
044: checkLogger(target, output, message, throwable, type);
045: }
046:
047: public void testLogkitLoggerTraceDisabled() throws Exception {
048: final Priority level = Priority.ERROR;
049: final String message = "Meep!";
050:
051: final MockLogTarget target = new MockLogTarget();
052: final LogkitLogger logger = createLogger(target, level);
053: logger.trace(message);
054: checkLogger(target, false, null, null, null);
055: }
056:
057: public void testLogkitLoggerTraceWithExceptionEnabled()
058: throws Exception {
059: final Priority level = Priority.DEBUG;
060: final Priority type = Priority.DEBUG;
061: final String message = "Meep!";
062: final Throwable throwable = new Throwable();
063: final boolean output = true;
064:
065: final MockLogTarget target = new MockLogTarget();
066: final LogkitLogger logger = createLogger(target, level);
067:
068: logger.trace(message, throwable);
069: checkLogger(target, output, message, throwable, type);
070: }
071:
072: public void testLogkitLoggerTraceWithExceptionDisabled()
073: throws Exception {
074: final Priority level = Priority.ERROR;
075: final String message = "Meep!";
076: final Throwable throwable = new Throwable();
077:
078: final MockLogTarget target = new MockLogTarget();
079: final LogkitLogger logger = createLogger(target, level);
080:
081: logger.trace(message, throwable);
082: checkLogger(target, false, null, null, null);
083: }
084:
085: public void testLogkitLoggerDebugEnabled() throws Exception {
086: final Priority level = Priority.DEBUG;
087: final Priority type = Priority.DEBUG;
088: final String message = "Meep!";
089: final Throwable throwable = null;
090: final boolean output = true;
091:
092: final MockLogTarget target = new MockLogTarget();
093: final LogkitLogger logger = createLogger(target, level);
094: logger.debug(message);
095: checkLogger(target, output, message, throwable, type);
096: }
097:
098: public void testLogkitLoggerDebugDisabled() throws Exception {
099: final Priority level = Priority.ERROR;
100: final String message = "Meep!";
101:
102: final MockLogTarget target = new MockLogTarget();
103: final LogkitLogger logger = createLogger(target, level);
104: logger.debug(message);
105: checkLogger(target, false, null, null, null);
106: }
107:
108: public void testLogkitLoggerDebugWithExceptionEnabled()
109: throws Exception {
110: final Priority level = Priority.DEBUG;
111: final Priority type = Priority.DEBUG;
112: final String message = "Meep!";
113: final Throwable throwable = new Throwable();
114: final boolean output = true;
115:
116: final MockLogTarget target = new MockLogTarget();
117: final LogkitLogger logger = createLogger(target, level);
118: logger.debug(message, throwable);
119: checkLogger(target, output, message, throwable, type);
120: }
121:
122: public void testLogkitLoggerDebugWithExceptionDisabled()
123: throws Exception {
124: final Priority level = Priority.ERROR;
125: final String message = "Meep!";
126: final Throwable throwable = new Throwable();
127:
128: final MockLogTarget target = new MockLogTarget();
129: final LogkitLogger logger = createLogger(target, level);
130: logger.debug(message, throwable);
131: checkLogger(target, false, null, null, null);
132: }
133:
134: public void testLogkitLoggerInfoEnabled() throws Exception {
135: final Priority level = Priority.DEBUG;
136: final Priority type = Priority.INFO;
137: final String message = "Meep!";
138: final Throwable throwable = null;
139: final boolean output = true;
140:
141: final MockLogTarget target = new MockLogTarget();
142: final LogkitLogger logger = createLogger(target, level);
143: logger.info(message);
144: checkLogger(target, output, message, throwable, type);
145: }
146:
147: public void testLogkitLoggerInfoDisabled() throws Exception {
148: final Priority level = Priority.ERROR;
149: final String message = "Meep!";
150:
151: final MockLogTarget target = new MockLogTarget();
152: final LogkitLogger logger = createLogger(target, level);
153: logger.info(message);
154: checkLogger(target, false, null, null, null);
155: }
156:
157: public void testLogkitLoggerInfoWithExceptionEnabled()
158: throws Exception {
159: final Priority level = Priority.DEBUG;
160: final Priority type = Priority.INFO;
161: final String message = "Meep!";
162: final Throwable throwable = new Throwable();
163: final boolean output = true;
164:
165: final MockLogTarget target = new MockLogTarget();
166: final LogkitLogger logger = createLogger(target, level);
167: logger.info(message, throwable);
168: checkLogger(target, output, message, throwable, type);
169: }
170:
171: public void testLogkitLoggerInfoWithExceptionDisabled()
172: throws Exception {
173: final Priority level = Priority.ERROR;
174: final String message = "Meep!";
175: final Throwable throwable = new Throwable();
176:
177: final MockLogTarget target = new MockLogTarget();
178: final LogkitLogger logger = createLogger(target, level);
179: logger.info(message, throwable);
180: checkLogger(target, false, null, null, null);
181: }
182:
183: public void testLogkitLoggerWarnEnabled() throws Exception {
184: final Priority level = Priority.DEBUG;
185: final Priority type = Priority.WARN;
186: final String message = "Meep!";
187: final Throwable throwable = null;
188: final boolean output = true;
189:
190: final MockLogTarget target = new MockLogTarget();
191: final LogkitLogger logger = createLogger(target, level);
192: logger.warn(message);
193: checkLogger(target, output, message, throwable, type);
194: }
195:
196: public void testLogkitLoggerWarnDisabled() throws Exception {
197: final Priority level = Priority.ERROR;
198: final String message = "Meep!";
199:
200: final MockLogTarget target = new MockLogTarget();
201: final LogkitLogger logger = createLogger(target, level);
202: logger.warn(message);
203: checkLogger(target, false, null, null, null);
204: }
205:
206: public void testLogkitLoggerWarnWithExceptionEnabled()
207: throws Exception {
208: final Priority level = Priority.DEBUG;
209: final Priority type = Priority.WARN;
210: final String message = "Meep!";
211: final Throwable throwable = new Throwable();
212: final boolean output = true;
213:
214: final MockLogTarget target = new MockLogTarget();
215: final LogkitLogger logger = createLogger(target, level);
216: logger.warn(message, throwable);
217: checkLogger(target, output, message, throwable, type);
218: }
219:
220: public void testLogkitLoggerWarnWithExceptionDisabled()
221: throws Exception {
222: final Priority level = Priority.ERROR;
223: final String message = "Meep!";
224: final Throwable throwable = new Throwable();
225:
226: final MockLogTarget target = new MockLogTarget();
227: final LogkitLogger logger = createLogger(target, level);
228: logger.warn(message, throwable);
229: checkLogger(target, false, null, null, null);
230: }
231:
232: public void testLogkitLoggerErrorEnabled() throws Exception {
233: final Priority level = Priority.DEBUG;
234: final Priority type = Priority.ERROR;
235: final String message = "Meep!";
236: final Throwable throwable = null;
237: final boolean output = true;
238:
239: final MockLogTarget target = new MockLogTarget();
240: final LogkitLogger logger = createLogger(target, level);
241: logger.error(message);
242: checkLogger(target, output, message, throwable, type);
243: }
244:
245: public void testLogkitLoggerErrorWithExceptionEnabled()
246: throws Exception {
247: final Priority level = Priority.DEBUG;
248: final Priority type = Priority.ERROR;
249: final String message = "Meep!";
250: final Throwable throwable = new Throwable();
251: final boolean output = true;
252:
253: final MockLogTarget target = new MockLogTarget();
254: final LogkitLogger logger = createLogger(target, level);
255: logger.error(message, throwable);
256: checkLogger(target, output, message, throwable, type);
257: }
258:
259: public void testConsoleLevelComparisonWithDebugEnabled()
260: throws Exception {
261: final MockLogTarget target = new MockLogTarget();
262: final LogkitLogger logger = createLogger(target, Priority.DEBUG);
263:
264: assertEquals("logger.isTraceEnabled()", true, logger
265: .isTraceEnabled());
266: assertEquals("logger.isDebugEnabled()", true, logger
267: .isDebugEnabled());
268: assertEquals("logger.isInfoEnabled()", true, logger
269: .isInfoEnabled());
270: assertEquals("logger.isWarnEnabled()", true, logger
271: .isWarnEnabled());
272: assertEquals("logger.isErrorEnabled()", true, logger
273: .isErrorEnabled());
274: }
275:
276: public void testConsoleLevelComparisonWithInfoEnabled()
277: throws Exception {
278: final MockLogTarget target = new MockLogTarget();
279: final LogkitLogger logger = createLogger(target, Priority.INFO);
280:
281: assertEquals("logger.isTraceEnabled()", false, logger
282: .isTraceEnabled());
283: assertEquals("logger.isDebugEnabled()", false, logger
284: .isDebugEnabled());
285: assertEquals("logger.isInfoEnabled()", true, logger
286: .isInfoEnabled());
287: assertEquals("logger.isWarnEnabled()", true, logger
288: .isWarnEnabled());
289: assertEquals("logger.isErrorEnabled()", true, logger
290: .isErrorEnabled());
291: }
292:
293: public void testConsoleLevelComparisonWithWarnEnabled()
294: throws Exception {
295: final MockLogTarget target = new MockLogTarget();
296: final LogkitLogger logger = createLogger(target, Priority.WARN);
297:
298: assertEquals("logger.isTraceEnabled()", false, logger
299: .isTraceEnabled());
300: assertEquals("logger.isDebugEnabled()", false, logger
301: .isDebugEnabled());
302: assertEquals("logger.isInfoEnabled()", false, logger
303: .isInfoEnabled());
304: assertEquals("logger.isWarnEnabled()", true, logger
305: .isWarnEnabled());
306: assertEquals("logger.isErrorEnabled()", true, logger
307: .isErrorEnabled());
308: }
309:
310: public void testConsoleLevelComparisonWithErrorEnabled()
311: throws Exception {
312: final MockLogTarget target = new MockLogTarget();
313: final LogkitLogger logger = createLogger(target, Priority.ERROR);
314:
315: assertEquals("logger.isTraceEnabled()", false, logger
316: .isTraceEnabled());
317: assertEquals("logger.isDebugEnabled()", false, logger
318: .isDebugEnabled());
319: assertEquals("logger.isInfoEnabled()", false, logger
320: .isInfoEnabled());
321: assertEquals("logger.isWarnEnabled()", false, logger
322: .isWarnEnabled());
323: assertEquals("logger.isErrorEnabled()", true, logger
324: .isErrorEnabled());
325: }
326:
327: private LogkitLogger createLogger(final MockLogTarget target,
328: final Priority priority) {
329: final Hierarchy hierarchy = new Hierarchy();
330: final Logger logkitLogger = hierarchy.getLoggerFor("test");
331: logkitLogger.setLogTargets(new LogTarget[] { target });
332: logkitLogger.setPriority(priority);
333: final LogkitLogger logger = new LogkitLogger(logkitLogger);
334: return logger;
335: }
336:
337: private void checkLogger(final MockLogTarget target,
338: final boolean output, final String message,
339: final Throwable throwable, final Priority priority) {
340: assertEquals("logger.m_message == message", message,
341: target.m_message);
342: assertEquals("logger.m_output == output", output,
343: target.m_output);
344: assertEquals("logger.m_throwable == null", throwable,
345: target.m_throwable);
346: assertEquals("logger.m_priority == null", priority,
347: target.m_priority);
348: }
349: }
|