View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.logging.slf4j;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.logging.impl.Slf4jLogFactory;
28  import org.slf4j.LoggerFactory;
29  import org.slf4j.Marker;
30  import org.slf4j.MarkerFactory;
31  
32  import ch.qos.logback.classic.Level;
33  import ch.qos.logback.classic.Logger;
34  import ch.qos.logback.classic.LoggerContext;
35  import ch.qos.logback.classic.spi.ILoggingEvent;
36  import ch.qos.logback.classic.spi.ThrowableProxy;
37  import ch.qos.logback.core.filter.Filter;
38  import ch.qos.logback.core.read.ListAppender;
39  import ch.qos.logback.core.spi.FilterReply;
40  
41  public class CallerInformationTestCase extends TestCase {
42  
43      private static final String STRING = "String";
44      private static final Throwable T = new RuntimeException();
45      private static final List<Marker> MARKERS = Collections.singletonList(MarkerFactory.getMarker("COMMONS-LOGGING"));
46  
47      private static final Level[] levels = {Level.ERROR, // SLF4J has no FATAL level
48              Level.ERROR, Level.WARN, Level.INFO, Level.DEBUG, Level.TRACE};
49  
50      private LogFactory factory;
51      private Log log;
52      private ListAppender<ILoggingEvent> appender;
53  
54      @Override
55      public void setUp() {
56          factory = LogFactory.getFactory();
57          log = factory.getInstance(getClass());
58          final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
59          final Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
60          appender = (ListAppender) logger.getAppender("LIST");
61          appender.clearAllFilters();
62          appender.addFilter(new Filter<ILoggingEvent>() {
63              @Override
64              public FilterReply decide(final ILoggingEvent event) {
65                  // Force the registration of caller data
66                  event.getCallerData();
67                  return FilterReply.NEUTRAL;
68              }
69          });
70      }
71  
72      public void testFactoryClassName() {
73          assertEquals(Slf4jLogFactory.class, factory.getClass());
74      }
75  
76      public void testLocationInfo() {
77          appender.list.clear();
78          // The following value must match the line number
79          final int currentLineNumber = 79;
80          log.fatal(STRING);
81          log.fatal(STRING, T);
82          log.error(STRING);
83          log.error(STRING, T);
84          log.warn(STRING);
85          log.warn(STRING, T);
86          log.info(STRING);
87          log.info(STRING, T);
88          log.debug(STRING);
89          log.debug(STRING, T);
90          log.trace(STRING);
91          log.trace(STRING, T);
92          final List<ILoggingEvent> events = new ArrayList<>(appender.list);
93          assertEquals("All events received.", levels.length * 2, events.size());
94          for (int lev = 0; lev < levels.length; lev++) {
95              for (int hasThrowable = 0; hasThrowable <= 1; hasThrowable++) {
96                  final ILoggingEvent event = events.get(2 * lev + hasThrowable);
97                  assertEquals("Correct message.", STRING, event.getMessage());
98                  assertEquals("Correct marker.", MARKERS, event.getMarkerList());
99                  assertEquals("Level matches.", levels[lev], event.getLevel());
100                 final StackTraceElement[] callerData = event.getCallerData();
101                 assertTrue("Has location", callerData != null && callerData.length > 0);
102                 final StackTraceElement location = callerData[0];
103                 assertEquals("Correct location class.", getClass().getName(), location.getClassName());
104                 assertEquals("Correct location line.", currentLineNumber + 2 * lev + hasThrowable + 1, location.getLineNumber());
105                 final ThrowableProxy throwableProxy = (ThrowableProxy) event.getThrowableProxy();
106                 assertEquals("Correct exception", hasThrowable > 0 ? T : null, throwableProxy != null ? throwableProxy.getThrowable() : null);
107             }
108         }
109     }
110 }