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.log4j2;
18  
19  import java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.commons.logging.impl.Log4jApiLogFactory;
24  import org.apache.logging.log4j.Level;
25  import org.apache.logging.log4j.Marker;
26  import org.apache.logging.log4j.MarkerManager;
27  import org.apache.logging.log4j.core.LogEvent;
28  import org.apache.logging.log4j.core.LoggerContext;
29  import org.apache.logging.log4j.core.config.Configuration;
30  import org.apache.logging.log4j.core.test.appender.ListAppender;
31  import org.apache.logging.log4j.message.ObjectMessage;
32  import org.apache.logging.log4j.message.SimpleMessage;
33  
34  import junit.framework.TestCase;
35  
36  public class CallerInformationTestCase extends TestCase {
37  
38      private static final Object OBJ = new Object();
39      private static final String STRING = "String";
40      private static final Throwable T = new RuntimeException();
41      private static final Marker MARKER = MarkerManager.getMarker("COMMONS-LOGGING");
42  
43      private static final Level[] levels = {Level.FATAL, Level.ERROR, Level.WARN, Level.INFO, Level.DEBUG, Level.TRACE};
44  
45      private LogFactory factory;
46      private Log log;
47      private ListAppender appender;
48  
49      @Override
50      public void setUp() {
51          factory = LogFactory.getFactory();
52          log = factory.getInstance(getClass());
53          final LoggerContext context = LoggerContext.getContext(false);
54          final Configuration config = context.getConfiguration();
55          appender = config.getAppender("LIST");
56          assertNotNull("Missing Log4j 2.x appender.", appender);
57      }
58  
59      public void testFactoryClassName() {
60          assertEquals(Log4jApiLogFactory.class, factory.getClass());
61      }
62  
63      public void testLocationInfo() {
64          appender.clear();
65          // The following value must match the line number
66          final int currentLineNumber = 66;
67          log.fatal(OBJ);
68          log.fatal(OBJ, T);
69          log.error(OBJ);
70          log.error(OBJ, T);
71          log.warn(OBJ);
72          log.warn(OBJ, T);
73          log.info(OBJ);
74          log.info(OBJ, T);
75          log.debug(OBJ);
76          log.debug(OBJ, T);
77          log.trace(OBJ);
78          log.trace(OBJ, T);
79          final ObjectMessage expectedMessage = new ObjectMessage(OBJ);
80          final List<LogEvent> events = appender.getEvents();
81          assertEquals("All events received.", levels.length * 2, events.size());
82          for (int lev = 0; lev < levels.length; lev++) {
83              for (int hasThrowable = 0; hasThrowable <= 1; hasThrowable++) {
84                  final LogEvent event = events.get(2 * lev + hasThrowable);
85                  assertEquals("Correct message.", expectedMessage, event.getMessage());
86                  assertEquals("Correct marker.", MARKER, event.getMarker());
87                  assertEquals("Level matches.", levels[lev], event.getLevel());
88                  final StackTraceElement location = event.getSource();
89                  assertNotNull("Has location", location);
90                  assertEquals("Correct source file.", "CallerInformationTestCase.java", location.getFileName());
91                  assertEquals("Correct method name.", "testLocationInfo", location.getMethodName());
92                  assertEquals("Correct location class.", getClass().getName(), location.getClassName());
93                  assertEquals("Correct location line.",
94                          currentLineNumber + 2 * lev + hasThrowable + 1,
95                          location.getLineNumber());
96                  assertEquals("Correct exception", hasThrowable > 0 ? T : null, event.getThrown());
97              }
98          }
99      }
100 
101     public void testMessageType() {
102         appender.clear();
103         log.info(OBJ);
104         log.info(STRING);
105         final List<LogEvent> events = appender.getEvents();
106         assertEquals("Correct number of messages.", 2, events.size());
107         assertEquals("Correct message type.", new ObjectMessage(OBJ), events.get(0).getMessage());
108         assertEquals("Correct message type.", new SimpleMessage(STRING), events.get(1).getMessage());
109     }
110 }