001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025
026package org.apache.commons.logging.test;
027
028import static org.junit.Assert.assertFalse;
029import static org.junit.Assert.assertTrue;
030import static org.junit.Assert.assertEquals;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034
035import org.junit.Test;
036
037public class InvokeJCLTest {
038
039    @Test
040    public void testIsEnabledAPI() {
041        // assume that we are running over slf4j-jdk14
042        Log log = LogFactory.getLog(InvokeJCLTest.class);
043        assertFalse(log.isTraceEnabled());
044        assertFalse(log.isDebugEnabled());
045        assertTrue(log.isInfoEnabled());
046        assertTrue(log.isWarnEnabled());
047        assertTrue(log.isErrorEnabled());
048        assertTrue(log.isFatalEnabled());
049    }
050
051    @Test
052    public void testPrintAPI() {
053        Log log = LogFactory.getLog(InvokeJCLTest.class);
054        Exception e = new Exception("just testing");
055
056        log.trace(null);
057        log.trace("trace message");
058
059        log.debug(null);
060        log.debug("debug message");
061
062        log.info(null);
063        log.info("info  message");
064
065        log.warn(null);
066        log.warn("warn message");
067
068        log.error(null);
069        log.error("error message");
070
071        log.fatal(null);
072        log.fatal("fatal message");
073
074        log.trace(null, e);
075        log.trace("trace message", e);
076
077        log.debug(null, e);
078        log.debug("debug message", e);
079
080        log.info(null, e);
081        log.info("info  message", e);
082
083        log.warn(null, e);
084        log.warn("warn message", e);
085
086        log.error(null, e);
087        log.error("error message", e);
088
089        log.fatal(null, e);
090        log.fatal("fatal message", e);
091    }
092
093    @Test
094    public void testAvoidConvertingObjectToString() {
095        Log log = LogFactory.getLog(InvokeJCLTest.class);
096        Exception e = new Exception("just testing");
097
098        TestMessage fatalMsg = new TestMessage("fatal msg");
099        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}