1   /*
2    * Copyright (c) 2004-2008 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    public void testEntering() {
72      XLogger logger = XLoggerFactory.getXLogger("UnitTest");
73      logger.entry();
74      logger.entry(1);
75      logger.entry("test");
76  
77      assertEquals(3, listAppender.list.size());
78      verify((LoggingEvent) listAppender.list.get(0), "entry");
79      verify((LoggingEvent) listAppender.list.get(1), "entry with (1)");
80      verify((LoggingEvent) listAppender.list.get(2), "entry with (test)");
81    }
82  
83    public void testExiting() {
84      XLogger logger = XLoggerFactory.getXLogger("UnitTest");
85      logger.exit();
86      logger.exit(0);
87      logger.exit(false);
88  
89      assertEquals(3, listAppender.list.size());
90      verify((LoggingEvent) listAppender.list.get(0), "exit");
91      verify((LoggingEvent) listAppender.list.get(1), "exit with (0)");
92      verify((LoggingEvent) listAppender.list.get(2), "exit with (false)");
93    }
94  
95    public void testThrowing() {
96      XLogger logger = XLoggerFactory.getXLogger("UnitTest");
97      Throwable t = new UnsupportedOperationException("Test");
98      logger.throwing(t);
99      assertEquals(1, listAppender.list.size());
100     verifyWithException((LoggingEvent) listAppender.list.get(0), "throwing", t);
101   }
102 
103   public void testCaught() {
104     XLogger logger = XLoggerFactory.getXLogger("UnitTest");
105     long x = 5;
106     Throwable t = null;
107     try {
108       @SuppressWarnings("unused")
109       long y = x / 0;
110     } catch (Exception ex) {
111       t = ex;
112       logger.catching(ex);
113     }
114     verifyWithException((LoggingEvent) listAppender.list.get(0), "catching", t);
115   }
116 
117   // See http://bugzilla.slf4j.org/show_bug.cgi?id=114
118   public void testLocationExtraction_Bug114() {
119     XLogger logger = XLoggerFactory.getXLogger("UnitTest");
120     int line = 121; // next line is line number 121
121     logger.exit(); 
122     logger.debug("hello");
123 
124     assertEquals(2, listAppender.list.size());
125 
126     {
127       LoggingEvent e = listAppender.list.get(0);
128       LocationInfo li = e.getLocationInformation();
129       assertEquals(this.getClass().getName(), li.getClassName());
130       assertEquals(""+line, li.getLineNumber());
131     }
132     
133     {
134       LoggingEvent e = listAppender.list.get(1);
135       LocationInfo li = e.getLocationInformation();
136       assertEquals(this.getClass().getName(), li.getClassName());
137       assertEquals(""+(line+1), li.getLineNumber());
138     }
139 
140   }
141 }