View Javadoc
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.test;
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.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import org.junit.Test;
36  
37  public class InvokeJCLTest {
38  
39      @Test
40      public void testIsEnabledAPI() {
41          // assume that we are running over slf4j-jdk14
42          Log log = LogFactory.getLog(InvokeJCLTest.class);
43          assertFalse(log.isTraceEnabled());
44          assertFalse(log.isDebugEnabled());
45          assertTrue(log.isInfoEnabled());
46          assertTrue(log.isWarnEnabled());
47          assertTrue(log.isErrorEnabled());
48          assertTrue(log.isFatalEnabled());
49      }
50  
51      @Test
52      public void testPrintAPI() {
53          Log log = LogFactory.getLog(InvokeJCLTest.class);
54          Exception e = new Exception("just testing");
55  
56          log.trace(null);
57          log.trace("trace message");
58  
59          log.debug(null);
60          log.debug("debug message");
61  
62          log.info(null);
63          log.info("info  message");
64  
65          log.warn(null);
66          log.warn("warn message");
67  
68          log.error(null);
69          log.error("error message");
70  
71          log.fatal(null);
72          log.fatal("fatal message");
73  
74          log.trace(null, e);
75          log.trace("trace message", e);
76  
77          log.debug(null, e);
78          log.debug("debug message", e);
79  
80          log.info(null, e);
81          log.info("info  message", e);
82  
83          log.warn(null, e);
84          log.warn("warn message", e);
85  
86          log.error(null, e);
87          log.error("error message", e);
88  
89          log.fatal(null, e);
90          log.fatal("fatal message", e);
91      }
92  
93      @Test
94      public void testAvoidConvertingObjectToString() {
95          Log log = LogFactory.getLog(InvokeJCLTest.class);
96          Exception e = new Exception("just testing");
97  
98          TestMessage fatalMsg = new TestMessage("fatal msg");
99          TestMessage errorMsg = new TestMessage("error msg");
100         TestMessage warnMsg = new TestMessage("warn msg");
101         TestMessage infoMsg = new TestMessage("info msg");
102         TestMessage debugMsg = new TestMessage("debug msg");
103         TestMessage traceMsg = new TestMessage("trace msg");
104 
105         log.fatal(fatalMsg);
106         log.fatal(fatalMsg, e);
107         assertEquals(2, fatalMsg.invokedCount);
108 
109         log.error(errorMsg);
110         log.error(errorMsg, e);
111         assertEquals(2, errorMsg.invokedCount);
112 
113         log.warn(warnMsg);
114         log.warn(warnMsg, e);
115         assertEquals(2, warnMsg.invokedCount);
116 
117         log.info(infoMsg);
118         log.info(infoMsg, e);
119         assertEquals(2, infoMsg.invokedCount);
120 
121         log.debug(debugMsg);
122         log.debug(debugMsg, e);
123         assertEquals(0, debugMsg.invokedCount);
124 
125         log.trace(traceMsg);
126         log.trace(traceMsg, e);
127         assertEquals(0, traceMsg.invokedCount);
128     }
129 
130     static class TestMessage {
131 
132         private final String msg;
133         int invokedCount = 0;
134 
135         TestMessage(String msg) {
136             this.msg = msg;
137         }
138 
139         @Override
140         public String toString() {
141             invokedCount++;
142             return msg;
143         }
144     }
145 
146 }