Debian Codegraph @

jcl-over-slf4j/src/test/java/org/apache/commons/logging/InvokeJCLTest.java

raw
1 /**
2 * Copyright (c) 2004-2011 QOS.ch
3 * All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 package org.apache.commons.logging;
27
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.assertEquals;
31
32 import org.junit.Test;
33
34 public class InvokeJCLTest {
35
36 @Test
37 public void testIsEnabledAPI() {
38 // assume that we are running over slf4j-jdk14
39 Log log = LogFactory.getLog(InvokeJCLTest.class);
40 assertFalse(log.isTraceEnabled());
41 assertFalse(log.isDebugEnabled());
42 assertTrue(log.isInfoEnabled());
43 assertTrue(log.isWarnEnabled());
44 assertTrue(log.isErrorEnabled());
45 assertTrue(log.isFatalEnabled());
46 }
47
48 @Test
49 public void testPrintAPI() {
50 Log log = LogFactory.getLog(InvokeJCLTest.class);
51 Exception e = new Exception("just testing");
52
53 log.trace(null);
54 log.trace("trace message");
55
56 log.debug(null);
57 log.debug("debug message");
58
59 log.info(null);
60 log.info("info message");
61
62 log.warn(null);
63 log.warn("warn message");
64
65 log.error(null);
66 log.error("error message");
67
68 log.fatal(null);
69 log.fatal("fatal message");
70
71 log.trace(null, e);
72 log.trace("trace message", e);
73
74 log.debug(null, e);
75 log.debug("debug message", e);
76
77 log.info(null, e);
78 log.info("info message", e);
79
80 log.warn(null, e);
81 log.warn("warn message", e);
82
83 log.error(null, e);
84 log.error("error message", e);
85
86 log.fatal(null, e);
87 log.fatal("fatal message", e);
88 }
89
90 @Test
91 public void testAvoidConvertingObjectToString() {
92 Log log = LogFactory.getLog(InvokeJCLTest.class);
93 Exception e = new Exception("just testing");
94
95 TestMessage fatalMsg = new TestMessage("fatal msg");
96 TestMessage errorMsg = new TestMessage("error msg");
97 TestMessage warnMsg = new TestMessage("warn msg");
98 TestMessage infoMsg = new TestMessage("info msg");
99 TestMessage debugMsg = new TestMessage("debug msg");
100 TestMessage traceMsg = new TestMessage("trace msg");
101
102 log.fatal(fatalMsg);
103 log.fatal(fatalMsg, e);
104 assertEquals(2, fatalMsg.invokedCount);
105
106 log.error(errorMsg);
107 log.error(errorMsg, e);
108 assertEquals(2, errorMsg.invokedCount);
109
110 log.warn(warnMsg);
111 log.warn(warnMsg, e);
112 assertEquals(2, warnMsg.invokedCount);
113
114 log.info(infoMsg);
115 log.info(infoMsg, e);
116 assertEquals(2, infoMsg.invokedCount);
117
118 log.debug(debugMsg);
119 log.debug(debugMsg, e);
120 assertEquals(0, debugMsg.invokedCount);
121
122 log.trace(traceMsg);
123 log.trace(traceMsg, e);
124 assertEquals(0, traceMsg.invokedCount);
125 }
126
127 static class TestMessage {
128
129 private final String msg;
130 int invokedCount = 0;
131
132 TestMessage(String msg) {this.msg = msg;}
133
134 @Override
135 public String toString() {
136 invokedCount++;
137 return msg;
138 }
139 }
140
141 }