Home | History | Annotate | Download | only in dummyExt
      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 package org.slf4j.dummyExt;
     26 
     27 import junit.framework.TestCase;
     28 
     29 import org.apache.log4j.spi.LocationInfo;
     30 import org.apache.log4j.spi.LoggingEvent;
     31 import org.slf4j.ext.XLogger;
     32 import org.slf4j.ext.XLoggerFactory;
     33 
     34 public class XLoggerTest extends TestCase {
     35 
     36     ListAppender listAppender;
     37     org.apache.log4j.Logger log4jRoot;
     38 
     39     final static String EXPECTED_FILE_NAME = "XLoggerTest.java";
     40 
     41     public XLoggerTest(String name) {
     42         super(name);
     43     }
     44 
     45     public void setUp() throws Exception {
     46         super.setUp();
     47 
     48         // start from a clean slate for each test
     49 
     50         listAppender = new ListAppender();
     51         listAppender.extractLocationInfo = true;
     52         log4jRoot = org.apache.log4j.Logger.getRootLogger();
     53         log4jRoot.addAppender(listAppender);
     54         log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
     55     }
     56 
     57     public void tearDown() throws Exception {
     58         super.tearDown();
     59     }
     60 
     61     void verify(LoggingEvent le, String expectedMsg) {
     62         assertEquals(expectedMsg, le.getMessage());
     63         assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
     64     }
     65 
     66     void verifyWithException(LoggingEvent le, String expectedMsg, Throwable t) {
     67         verify(le, expectedMsg);
     68         assertEquals(t.toString(), le.getThrowableStrRep()[0]);
     69     }
     70 
     71     void verifyWithLevelAndException(LoggingEvent le, XLogger.Level level, String expectedMsg, Throwable t) {
     72         verify(le, expectedMsg);
     73         assertEquals(t.toString(), le.getThrowableStrRep()[0]);
     74         assertEquals(le.getLevel().toString(), level.toString());
     75     }
     76 
     77     public void testEntering() {
     78         XLogger logger = XLoggerFactory.getXLogger("UnitTest");
     79         logger.entry();
     80         logger.entry(1);
     81         logger.entry("test");
     82         logger.entry("a", "b", "c", "d");
     83         logger.entry("a", "b", "c", "d", "e");
     84         logger.entry("a", "b", "c", "d", "e", "f");
     85 
     86         assertEquals(6, listAppender.list.size());
     87         verify((LoggingEvent) listAppender.list.get(0), "entry");
     88         verify((LoggingEvent) listAppender.list.get(1), "entry with (1)");
     89         verify((LoggingEvent) listAppender.list.get(2), "entry with (test)");
     90     }
     91 
     92     public void testExiting() {
     93         XLogger logger = XLoggerFactory.getXLogger("UnitTest");
     94         logger.exit();
     95         assertEquals(Integer.valueOf(0), logger.exit(0));
     96         assertEquals(Boolean.FALSE, logger.exit(false));
     97 
     98         assertEquals(3, listAppender.list.size());
     99         verify((LoggingEvent) listAppender.list.get(0), "exit");
    100         verify((LoggingEvent) listAppender.list.get(1), "exit with (0)");
    101         verify((LoggingEvent) listAppender.list.get(2), "exit with (false)");
    102     }
    103 
    104     public void testThrowing() {
    105         XLogger logger = XLoggerFactory.getXLogger("UnitTest");
    106         Throwable t = new UnsupportedOperationException("Test");
    107         assertEquals(t, logger.throwing(t));
    108         assertEquals(t, logger.throwing(XLogger.Level.DEBUG, t));
    109         assertEquals(2, listAppender.list.size());
    110         verifyWithException((LoggingEvent) listAppender.list.get(0), "throwing", t);
    111         LoggingEvent event = (LoggingEvent) listAppender.list.get(1);
    112         verifyWithLevelAndException(event, XLogger.Level.DEBUG, "throwing", t);
    113     }
    114 
    115     public void testCaught() {
    116         XLogger logger = XLoggerFactory.getXLogger("UnitTest");
    117         long x = 5;
    118         Throwable t = null;
    119         try {
    120             @SuppressWarnings("unused")
    121             long y = x / 0;
    122         } catch (Exception ex) {
    123             t = ex;
    124             logger.catching(ex);
    125             logger.catching(XLogger.Level.DEBUG, ex);
    126         }
    127         verifyWithException((LoggingEvent) listAppender.list.get(0), "catching", t);
    128         verifyWithLevelAndException((LoggingEvent) listAppender.list.get(1), XLogger.Level.DEBUG, "catching", t);
    129     }
    130 
    131     // See http://bugzilla.slf4j.org/show_bug.cgi?id=114
    132     public void testLocationExtraction_Bug114() {
    133         XLogger logger = XLoggerFactory.getXLogger("UnitTest");
    134         int line = 135; // requires update if line numbers change
    135         logger.exit();
    136         logger.debug("hello");
    137 
    138         assertEquals(2, listAppender.list.size());
    139 
    140         {
    141             LoggingEvent e = listAppender.list.get(0);
    142             LocationInfo li = e.getLocationInformation();
    143             assertEquals(this.getClass().getName(), li.getClassName());
    144             assertEquals("" + line, li.getLineNumber());
    145         }
    146 
    147         {
    148             LoggingEvent e = listAppender.list.get(1);
    149             LocationInfo li = e.getLocationInformation();
    150             assertEquals(this.getClass().getName(), li.getClassName());
    151             assertEquals("" + (line + 1), li.getLineNumber());
    152         }
    153 
    154     }
    155 }
    156