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 static org.junit.Assert.assertEquals;
28  
29  import org.apache.log4j.spi.LocationInfo;
30  import org.apache.log4j.spi.LoggingEvent;
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.slf4j.ext.XLogger;
34  import org.slf4j.ext.XLoggerFactory;
35  
36  public class XLoggerTest {
37  
38      ListAppender listAppender;
39      org.apache.log4j.Logger log4jRoot;
40  
41      final static String EXPECTED_FILE_NAME = "XLoggerTest.java";
42  
43      @Before
44      public void setUp() throws Exception {
45  
46          // start from a clean slate for each test
47  
48          listAppender = new ListAppender();
49          listAppender.extractLocationInfo = true;
50          log4jRoot = org.apache.log4j.Logger.getRootLogger();
51          log4jRoot.addAppender(listAppender);
52          log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
53      }
54  
55      void verify(LoggingEvent le, String expectedMsg) {
56          assertEquals(expectedMsg, le.getMessage());
57          assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
58      }
59  
60      void verifyWithException(LoggingEvent le, String expectedMsg, Throwable t) {
61          verify(le, expectedMsg);
62          assertEquals(t.toString(), le.getThrowableStrRep()[0]);
63      }
64  
65      void verifyWithLevelAndException(LoggingEvent le, XLogger.Level level, String expectedMsg, Throwable t) {
66          verify(le, expectedMsg);
67          assertEquals(t.toString(), le.getThrowableStrRep()[0]);
68          assertEquals(le.getLevel().toString(), level.toString());
69      }
70  
71      @Test
72      public void testEntering() {
73          XLogger logger = XLoggerFactory.getXLogger("UnitTest");
74          logger.entry();
75          logger.entry(1);
76          logger.entry("test");
77          logger.entry("a", "b", "c", "d");
78          logger.entry("a", "b", "c", "d", "e");
79          logger.entry("a", "b", "c", "d", "e", "f");
80  
81          assertEquals(6, listAppender.list.size());
82          verify(listAppender.list.get(0), "entry");
83          verify(listAppender.list.get(1), "entry with (1)");
84          verify(listAppender.list.get(2), "entry with (test)");
85      }
86  
87      @Test
88      public void testExiting() {
89          XLogger logger = XLoggerFactory.getXLogger("UnitTest");
90          logger.exit();
91          assertEquals(Integer.valueOf(0), logger.exit(0));
92          assertEquals(Boolean.FALSE, logger.exit(false));
93  
94          assertEquals(3, listAppender.list.size());
95          verify(listAppender.list.get(0), "exit");
96          verify(listAppender.list.get(1), "exit with (0)");
97          verify(listAppender.list.get(2), "exit with (false)");
98      }
99  
100     @Test
101     public void testThrowing() {
102         XLogger logger = XLoggerFactory.getXLogger("UnitTest");
103         Throwable t = new UnsupportedOperationException("Test");
104         assertEquals(t, logger.throwing(t));
105         assertEquals(t, logger.throwing(XLogger.Level.DEBUG, t));
106         assertEquals(2, listAppender.list.size());
107         verifyWithException(listAppender.list.get(0), "throwing", t);
108         LoggingEvent event = listAppender.list.get(1);
109         verifyWithLevelAndException(event, XLogger.Level.DEBUG, "throwing", t);
110     }
111 
112     @Test
113     public void testCaught() {
114         XLogger logger = XLoggerFactory.getXLogger("UnitTest");
115         long x = 5;
116         Throwable t = null;
117         try {
118             @SuppressWarnings("unused")
119             long y = x / 0;
120         } catch (Exception ex) {
121             t = ex;
122             logger.catching(ex);
123             logger.catching(XLogger.Level.DEBUG, ex);
124         }
125         verifyWithException(listAppender.list.get(0), "catching", t);
126         verifyWithLevelAndException(listAppender.list.get(1), XLogger.Level.DEBUG, "catching", t);
127     }
128 
129     // See http://jira.qos.ch/browse/SLF4J-105
130     // formerly http://bugzilla.slf4j.org/show_bug.cgi?id=114
131     @Test
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     @Test
156     public void testNoDoubleSubstitution_Bug421() {
157         XLogger logger = XLoggerFactory.getXLogger("UnitTest");
158         logger.error("{},{}", "foo", "[{}]");
159         verify(listAppender.list.get(0), "foo,[{}]");
160         
161         logger.error("{},{}", "[{}]", "foo");
162         verify(listAppender.list.get(1), "[{}],foo");
163     }
164     
165     
166 }