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  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      logger.exit(0);
96      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     logger.throwing(t);
108     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,
113         "throwing", t);
114   }
115 
116   public void testCaught() {
117     XLogger logger = XLoggerFactory.getXLogger("UnitTest");
118     long x = 5;
119     Throwable t = null;
120     try {
121       @SuppressWarnings("unused")
122       long y = x / 0;
123     } catch (Exception ex) {
124       t = ex;
125       logger.catching(ex);
126       logger.catching(XLogger.Level.DEBUG, ex);
127     }
128     verifyWithException((LoggingEvent) listAppender.list.get(0), "catching", t);
129     verifyWithLevelAndException((LoggingEvent) listAppender.list.get(1), XLogger.Level.DEBUG,
130         "catching", t);
131   }
132 
133   // See http://bugzilla.slf4j.org/show_bug.cgi?id=114
134   public void testLocationExtraction_Bug114() {
135     XLogger logger = XLoggerFactory.getXLogger("UnitTest");
136     int line = 137; // next line is line number 134
137     logger.exit(); 
138     logger.debug("hello");
139 
140     assertEquals(2, listAppender.list.size());
141 
142     {
143       LoggingEvent e = listAppender.list.get(0);
144       LocationInfo li = e.getLocationInformation();
145       assertEquals(this.getClass().getName(), li.getClassName());
146       assertEquals(""+line, li.getLineNumber());
147     }
148     
149     {
150       LoggingEvent e = listAppender.list.get(1);
151       LocationInfo li = e.getLocationInformation();
152       assertEquals(this.getClass().getName(), li.getClassName());
153       assertEquals(""+(line+1), li.getLineNumber());
154     }
155 
156   }
157 }