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 java.io.PrintStream;
012: import java.io.ByteArrayOutputStream;
013:
014: import org.codehaus.dna.impl.ConsoleLogger;
015:
016: public class ConsoleLoggerTestCase extends TestCase {
017: public void testConsoleWithEmptyOutput() throws Exception {
018: try {
019: new ConsoleLogger(MockConsoleLogger.LEVEL_ALL, null);
020: } catch (final NullPointerException npe) {
021: assertEquals("npe.message", "output", npe.getMessage());
022: return;
023: }
024: fail("Expected to fail due to NPE in ctor");
025: }
026:
027: public void testMockConsoleEmptyCtor() throws Exception {
028: final MockConsoleLogger logger = new MockConsoleLogger();
029: assertEquals("logger.level", MockConsoleLogger.LEVEL_ALL,
030: logger.getLevel());
031: }
032:
033: public void testMockConsoleGetChildLogger() throws Exception {
034: final MockConsoleLogger logger = new MockConsoleLogger();
035: assertEquals("logger.getChildLogger == logger", logger, logger
036: .getChildLogger("whatever"));
037: }
038:
039: public void testMockConsoleOutputToConsole() throws Exception {
040: final ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream();
041: final PrintStream output = new PrintStream(arrayOutput);
042: final ConsoleLogger logger = new ConsoleLogger(
043: MockConsoleLogger.LEVEL_ALL, output);
044: logger.debug("ignore me!", null);
045: final String message = arrayOutput.toString();
046: final String expectedMessage = "[DEBUG] ignore me!"
047: + System.getProperty("line.separator");
048: assertEquals("message", expectedMessage, message);
049: }
050:
051: public void testMockConsoleOutputToConsoleWithException()
052: throws Exception {
053: final ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream();
054: final PrintStream output = new PrintStream(arrayOutput);
055: final ConsoleLogger logger = new ConsoleLogger(
056: MockConsoleLogger.LEVEL_ALL, output);
057: logger.debug("ignore me!", new Throwable("Ignore me aswell!"));
058: final String message = arrayOutput.toString();
059: final String expectedMessage = "[DEBUG] ignore me!"
060: + System.getProperty("line.separator");
061: assertTrue("message", message.startsWith(expectedMessage));
062: assertTrue("throwable message", -1 != message
063: .indexOf("Ignore me aswell!"));
064: assertTrue("throwable", -1 != message.indexOf(Throwable.class
065: .getName()));
066: }
067:
068: public void testMockConsoleTraceEnabled() throws Exception {
069: final int level = MockConsoleLogger.LEVEL_ALL;
070: final String message = "Meep!";
071: final String type = "TRACE";
072: final Throwable throwable = null;
073: final boolean output = true;
074:
075: final MockConsoleLogger logger = new MockConsoleLogger(level);
076: logger.trace(message);
077: checkLogger(logger, output, message, throwable, type);
078: }
079:
080: public void testMockConsoleTraceDisabled() throws Exception {
081: final int level = MockConsoleLogger.LEVEL_NONE;
082: final String message = "Meep!";
083: final MockConsoleLogger logger = new MockConsoleLogger(level);
084: logger.trace(message);
085: checkLogger(logger, false, null, null, null);
086: }
087:
088: public void testMockConsoleTraceWithExceptionEnabled()
089: throws Exception {
090: final int level = MockConsoleLogger.LEVEL_ALL;
091: final String message = "Meep!";
092: final String type = "TRACE";
093: final Throwable throwable = new Throwable();
094: final boolean output = true;
095:
096: final MockConsoleLogger logger = new MockConsoleLogger(level);
097: logger.trace(message, throwable);
098: checkLogger(logger, output, message, throwable, type);
099: }
100:
101: public void testMockConsoleTraceWithExceptionDisabled()
102: throws Exception {
103: final int level = MockConsoleLogger.LEVEL_NONE;
104: final String message = "Meep!";
105: final Throwable throwable = new Throwable();
106: final MockConsoleLogger logger = new MockConsoleLogger(level);
107: logger.trace(message, throwable);
108: checkLogger(logger, false, null, null, null);
109: }
110:
111: public void testMockConsoleDebugEnabled() throws Exception {
112: final int level = MockConsoleLogger.LEVEL_ALL;
113: final String message = "Meep!";
114: final String type = "DEBUG";
115: final Throwable throwable = null;
116: final boolean output = true;
117:
118: final MockConsoleLogger logger = new MockConsoleLogger(level);
119: logger.debug(message);
120: checkLogger(logger, output, message, throwable, type);
121: }
122:
123: public void testMockConsoleDebugDisabled() throws Exception {
124: final int level = MockConsoleLogger.LEVEL_NONE;
125: final String message = "Meep!";
126: final MockConsoleLogger logger = new MockConsoleLogger(level);
127: logger.debug(message);
128: checkLogger(logger, false, null, null, null);
129: }
130:
131: public void testMockConsoleDebugWithExceptionEnabled()
132: throws Exception {
133: final int level = MockConsoleLogger.LEVEL_ALL;
134: final String message = "Meep!";
135: final String type = "DEBUG";
136: final Throwable throwable = new Throwable();
137: final boolean output = true;
138:
139: final MockConsoleLogger logger = new MockConsoleLogger(level);
140: logger.debug(message, throwable);
141: checkLogger(logger, output, message, throwable, type);
142: }
143:
144: public void testMockConsoleDebugWithExceptionDisabled()
145: throws Exception {
146: final int level = MockConsoleLogger.LEVEL_NONE;
147: final String message = "Meep!";
148: final Throwable throwable = new Throwable();
149: final MockConsoleLogger logger = new MockConsoleLogger(level);
150: logger.debug(message, throwable);
151: checkLogger(logger, false, null, null, null);
152: }
153:
154: public void testMockConsoleInfoEnabled() throws Exception {
155: final int level = MockConsoleLogger.LEVEL_ALL;
156: final String message = "Meep!";
157: final String type = "INFO";
158: final Throwable throwable = null;
159: final boolean output = true;
160:
161: final MockConsoleLogger logger = new MockConsoleLogger(level);
162: logger.info(message);
163: checkLogger(logger, output, message, throwable, type);
164: }
165:
166: public void testMockConsoleInfoDisabled() throws Exception {
167: final int level = MockConsoleLogger.LEVEL_NONE;
168: final String message = "Meep!";
169: final MockConsoleLogger logger = new MockConsoleLogger(level);
170: logger.info(message);
171: checkLogger(logger, false, null, null, null);
172: }
173:
174: public void testMockConsoleInfoWithExceptionEnabled()
175: throws Exception {
176: final int level = MockConsoleLogger.LEVEL_ALL;
177: final String message = "Meep!";
178: final String type = "INFO";
179: final Throwable throwable = new Throwable();
180: final boolean output = true;
181:
182: final MockConsoleLogger logger = new MockConsoleLogger(level);
183: logger.info(message, throwable);
184: checkLogger(logger, output, message, throwable, type);
185: }
186:
187: public void testMockConsoleInfoWithExceptionDisabled()
188: throws Exception {
189: final int level = MockConsoleLogger.LEVEL_NONE;
190: final String message = "Meep!";
191: final Throwable throwable = new Throwable();
192: final MockConsoleLogger logger = new MockConsoleLogger(level);
193: logger.info(message, throwable);
194: checkLogger(logger, false, null, null, null);
195: }
196:
197: public void testMockConsoleWarnEnabled() throws Exception {
198: final int level = MockConsoleLogger.LEVEL_ALL;
199: final String message = "Meep!";
200: final String type = "WARN";
201: final Throwable throwable = null;
202: final boolean output = true;
203:
204: final MockConsoleLogger logger = new MockConsoleLogger(level);
205: logger.warn(message);
206: checkLogger(logger, output, message, throwable, type);
207: }
208:
209: public void testMockConsoleWarnDisabled() throws Exception {
210: final int level = MockConsoleLogger.LEVEL_NONE;
211: final String message = "Meep!";
212: final MockConsoleLogger logger = new MockConsoleLogger(level);
213: logger.warn(message);
214: checkLogger(logger, false, null, null, null);
215: }
216:
217: public void testMockConsoleWarnWithExceptionEnabled()
218: throws Exception {
219: final int level = MockConsoleLogger.LEVEL_ALL;
220: final String message = "Meep!";
221: final String type = "WARN";
222: final Throwable throwable = new Throwable();
223: final boolean output = true;
224:
225: final MockConsoleLogger logger = new MockConsoleLogger(level);
226: logger.warn(message, throwable);
227: checkLogger(logger, output, message, throwable, type);
228: }
229:
230: public void testMockConsoleWarnWithExceptionDisabled()
231: throws Exception {
232: final int level = MockConsoleLogger.LEVEL_NONE;
233: final String message = "Meep!";
234: final Throwable throwable = new Throwable();
235: final MockConsoleLogger logger = new MockConsoleLogger(level);
236: logger.warn(message, throwable);
237: checkLogger(logger, false, null, null, null);
238: }
239:
240: public void testMockConsoleErrorEnabled() throws Exception {
241: final int level = MockConsoleLogger.LEVEL_ALL;
242: final String message = "Meep!";
243: final String type = "ERROR";
244: final Throwable throwable = null;
245: final boolean output = true;
246:
247: final MockConsoleLogger logger = new MockConsoleLogger(level);
248: logger.error(message);
249: checkLogger(logger, output, message, throwable, type);
250: }
251:
252: public void testMockConsoleErrorDisabled() throws Exception {
253: final int level = MockConsoleLogger.LEVEL_NONE;
254: final String message = "Meep!";
255: final MockConsoleLogger logger = new MockConsoleLogger(level);
256: logger.error(message);
257: checkLogger(logger, false, null, null, null);
258: }
259:
260: public void testMockConsoleErrorWithExceptionEnabled()
261: throws Exception {
262: final int level = MockConsoleLogger.LEVEL_ALL;
263: final String message = "Meep!";
264: final String type = "ERROR";
265: final Throwable throwable = new Throwable();
266: final boolean output = true;
267:
268: final MockConsoleLogger logger = new MockConsoleLogger(level);
269: logger.error(message, throwable);
270: checkLogger(logger, output, message, throwable, type);
271: }
272:
273: public void testMockConsoleErrorWithExceptionDisabled()
274: throws Exception {
275: final int level = MockConsoleLogger.LEVEL_NONE;
276: final String message = "Meep!";
277: final Throwable throwable = new Throwable();
278: final MockConsoleLogger logger = new MockConsoleLogger(level);
279: logger.error(message, throwable);
280: checkLogger(logger, false, null, null, null);
281: }
282:
283: public void testConsoleLevelComparisonWithAll() throws Exception {
284: final MockConsoleLogger logger = new MockConsoleLogger(
285: MockConsoleLogger.LEVEL_ALL);
286: assertEquals("logger.isTraceEnabled()", true, logger
287: .isTraceEnabled());
288: assertEquals("logger.isDebugEnabled()", true, logger
289: .isDebugEnabled());
290: assertEquals("logger.isInfoEnabled()", true, logger
291: .isInfoEnabled());
292: assertEquals("logger.isWarnEnabled()", true, logger
293: .isWarnEnabled());
294: assertEquals("logger.isErrorEnabled()", true, logger
295: .isErrorEnabled());
296: }
297:
298: public void testConsoleLevelComparisonWithNone() throws Exception {
299: final MockConsoleLogger logger = new MockConsoleLogger(
300: MockConsoleLogger.LEVEL_NONE);
301: assertEquals("logger.isTraceEnabled()", false, logger
302: .isTraceEnabled());
303: assertEquals("logger.isDebugEnabled()", false, logger
304: .isDebugEnabled());
305: assertEquals("logger.isInfoEnabled()", false, logger
306: .isInfoEnabled());
307: assertEquals("logger.isWarnEnabled()", false, logger
308: .isWarnEnabled());
309: assertEquals("logger.isErrorEnabled()", false, logger
310: .isErrorEnabled());
311: }
312:
313: public void testConsoleLevelComparisonWithTraceEnabled()
314: throws Exception {
315: final MockConsoleLogger logger = new MockConsoleLogger(
316: MockConsoleLogger.LEVEL_TRACE);
317: assertEquals("logger.isTraceEnabled()", true, logger
318: .isTraceEnabled());
319: assertEquals("logger.isDebugEnabled()", true, logger
320: .isDebugEnabled());
321: assertEquals("logger.isInfoEnabled()", true, logger
322: .isInfoEnabled());
323: assertEquals("logger.isWarnEnabled()", true, logger
324: .isWarnEnabled());
325: assertEquals("logger.isErrorEnabled()", true, logger
326: .isErrorEnabled());
327: }
328:
329: public void testConsoleLevelComparisonWithDebugEnabled()
330: throws Exception {
331: final MockConsoleLogger logger = new MockConsoleLogger(
332: MockConsoleLogger.LEVEL_DEBUG);
333: assertEquals("logger.isTraceEnabled()", false, logger
334: .isTraceEnabled());
335: assertEquals("logger.isDebugEnabled()", true, logger
336: .isDebugEnabled());
337: assertEquals("logger.isInfoEnabled()", true, logger
338: .isInfoEnabled());
339: assertEquals("logger.isWarnEnabled()", true, logger
340: .isWarnEnabled());
341: assertEquals("logger.isErrorEnabled()", true, logger
342: .isErrorEnabled());
343: }
344:
345: public void testConsoleLevelComparisonWithInfoEnabled()
346: throws Exception {
347: final MockConsoleLogger logger = new MockConsoleLogger(
348: MockConsoleLogger.LEVEL_INFO);
349: assertEquals("logger.isTraceEnabled()", false, logger
350: .isTraceEnabled());
351: assertEquals("logger.isDebugEnabled()", false, logger
352: .isDebugEnabled());
353: assertEquals("logger.isInfoEnabled()", true, logger
354: .isInfoEnabled());
355: assertEquals("logger.isWarnEnabled()", true, logger
356: .isWarnEnabled());
357: assertEquals("logger.isErrorEnabled()", true, logger
358: .isErrorEnabled());
359: }
360:
361: public void testConsoleLevelComparisonWithWarnEnabled()
362: throws Exception {
363: final MockConsoleLogger logger = new MockConsoleLogger(
364: MockConsoleLogger.LEVEL_WARN);
365: assertEquals("logger.isTraceEnabled()", false, logger
366: .isTraceEnabled());
367: assertEquals("logger.isDebugEnabled()", false, logger
368: .isDebugEnabled());
369: assertEquals("logger.isInfoEnabled()", false, logger
370: .isInfoEnabled());
371: assertEquals("logger.isWarnEnabled()", true, logger
372: .isWarnEnabled());
373: assertEquals("logger.isErrorEnabled()", true, logger
374: .isErrorEnabled());
375: }
376:
377: public void testConsoleLevelComparisonWithErrorEnabled()
378: throws Exception {
379: final MockConsoleLogger logger = new MockConsoleLogger(
380: MockConsoleLogger.LEVEL_ERROR);
381: assertEquals("logger.isTraceEnabled()", false, logger
382: .isTraceEnabled());
383: assertEquals("logger.isDebugEnabled()", false, logger
384: .isDebugEnabled());
385: assertEquals("logger.isInfoEnabled()", false, logger
386: .isInfoEnabled());
387: assertEquals("logger.isWarnEnabled()", false, logger
388: .isWarnEnabled());
389: assertEquals("logger.isErrorEnabled()", true, logger
390: .isErrorEnabled());
391: }
392:
393: private void checkLogger(final MockConsoleLogger logger,
394: final boolean output, final String message,
395: final Throwable throwable, final String type) {
396: assertEquals("logger.m_message == message", message,
397: logger.m_message);
398: assertEquals("logger.m_output == true", output, logger.m_output);
399: assertEquals("logger.m_throwable == null", throwable,
400: logger.m_throwable);
401: assertEquals("logger.m_type == null", type, logger.m_type);
402: }
403: }
|