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  
18  package org.apache.commons.logging.log4j;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.commons.logging.DummyException;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   * Abstract set of tests that can be executed with various classpaths set.
35   * <p>
36   * The tests verify that when running on a system with Log4J present,
37   * Log4J is selected and that the logger basically works.
38   */
39  
40  public abstract class StandardTests extends TestCase {
41  
42      /**
43       * Simple structure to store information about messages that actually get
44       * logged by the underlying logging library.
45       */
46      public static class LogEvent {
47          public String msg;
48          public String level;
49          public Throwable throwable;
50      }
51  
52      /**
53       * Verify that the TestAppender has received the expected
54       * number of messages. This assumes that:
55       * <ul>
56       * <li>setUpTestAppender has been called
57       * <li>logPlainMessages or logExceptionMessages has been
58       * called to log a known number of messages at known levels.
59       * </ul>
60       *
61       * @param logEvents is the list of log events received.
62       * @param thrown False if logPlainMessages was called
63       * (ie the TestAppender is expected to have received
64       * logevents with no associated exception info). True if
65       * logExceptionMessages was called.
66       */
67      private void checkLoggingEvents(final List logEvents, final boolean thrown) {
68          LogEvent ev;
69  
70          assertEquals("Unexpected number of log events", 4, logEvents.size());
71  
72          ev = (LogEvent) logEvents.get(0);
73          assertEquals("Info message expected", "info", ev.msg);
74          assertEquals("Info level expected", "INFO", ev.level);
75          assertEquals("Exception data incorrect", ev.throwable!=null, thrown);
76  
77          ev = (LogEvent) logEvents.get(1);
78          assertEquals("Warn message expected", "warn", ev.msg);
79          assertEquals("Warn level expected", "WARN", ev.level);
80          assertEquals("Exception data incorrect", ev.throwable!=null, thrown);
81  
82          ev = (LogEvent) logEvents.get(2);
83          assertEquals("Error message expected", "error", ev.msg);
84          assertEquals("Error level expected", "ERROR", ev.level);
85          assertEquals("Exception data incorrect", ev.throwable!=null, thrown);
86  
87          ev = (LogEvent) logEvents.get(3);
88          assertEquals("Fatal message expected", "fatal", ev.msg);
89          assertEquals("Fatal level expected", "FATAL", ev.level);
90          assertEquals("Exception data incorrect", ev.throwable!=null, thrown);
91      }
92  
93      /**
94       * Log messages with exceptions
95       */
96      private void logExceptionMessages(final Log log) {
97          final Throwable t = new DummyException();
98          log.trace("trace", t); // Should not actually get logged
99          log.debug("debug", t); // Should not actually get logged
100         log.info("info", t);
101         log.warn("warn", t);
102         log.error("error", t);
103         log.fatal("fatal", t);
104     }
105 
106     /**
107      * Log plain messages.
108      */
109     private void logPlainMessages(final Log log) {
110         log.trace("trace"); // Should not actually get logged
111         log.debug("debug"); // Should not actually get logged
112         log.info("info");
113         log.warn("warn");
114         log.error("error");
115         log.fatal("fatal");
116     }
117 
118     /**
119      * Sets up instance variables required by this test case.
120      */
121     @Override
122     public void setUp() throws Exception {
123         LogFactory.releaseAll();
124     }
125 
126     /**
127      * Modify Log4j's setup so that all messages actually logged get redirected
128      * into the specified list.
129      * <p>
130      * This method also sets the logging level to INFO so that we
131      * can test whether messages are getting properly filtered.
132      */
133     public abstract void setUpTestAppender(List logEvents) throws Exception;
134 
135     /**
136      * Tear down instance variables required by this test case.
137      */
138     @Override
139     public void tearDown() {
140         LogFactory.releaseAll();
141     }
142 
143     /**
144      * Test that a LogFactory gets created as expected.
145      */
146     public void testCreateFactory() {
147         final LogFactory factory = LogFactory.getFactory();
148         assertNotNull("LogFactory exists", factory);
149         assertEquals("LogFactory class", "org.apache.commons.logging.impl.LogFactoryImpl", factory.getClass().getName());
150 
151         final String[] names = factory.getAttributeNames();
152         assertNotNull("Names exists", names);
153         assertEquals("Names empty", 0, names.length);
154     }
155 
156     /**
157      * Verify that we can log exception messages.
158      */
159     public void testExceptionMessages() throws Exception {
160         final List logEvents = new ArrayList();
161         setUpTestAppender(logEvents);
162         final Log log = LogFactory.getLog("test-category");
163         logExceptionMessages(log);
164         checkLoggingEvents(logEvents, true);
165     }
166 
167     /**
168      * Verify that we can log messages without exceptions.
169      */
170     public void testPlainMessages() throws Exception {
171         final List logEvents = new ArrayList();
172         setUpTestAppender(logEvents);
173         final Log log = LogFactory.getLog("test-category");
174         logPlainMessages(log);
175         checkLoggingEvents(logEvents, false);
176     }
177 
178     /**
179      * Test Serializability of Log instance
180      */
181     public void testSerializable() throws Exception {
182         final List logEvents = new ArrayList();
183         setUpTestAppender(logEvents);
184         final Log log = LogFactory.getLog("test-category");
185 
186         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
187         final ObjectOutputStream oos = new ObjectOutputStream(baos);
188         oos.writeObject(log);
189         oos.close();
190         final ByteArrayInputStream bais =
191             new ByteArrayInputStream(baos.toByteArray());
192         final ObjectInputStream ois = new ObjectInputStream(bais);
193         final Log newLog = (Log) ois.readObject();
194         ois.close();
195 
196         // Check the characteristics of the resulting object
197         logExceptionMessages(newLog);
198         checkLoggingEvents(logEvents, true);
199     }
200 }