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.simple;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import junit.framework.Test;
24  
25  import org.apache.commons.logging.DummyException;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.logging.PathableClassLoader;
28  import org.apache.commons.logging.PathableTestSuite;
29  import org.apache.commons.logging.impl.SimpleLog;
30  
31  /**
32   * <p>TestCase for simple logging when running with custom configuration
33   * properties.</p>
34   */
35  public class CustomConfigTestCase extends DefaultConfigTestCase {
36  
37      /**
38       * Return the tests included in this test suite.
39       * <p>
40       * We need to use a PathableClassLoader here because the SimpleLog class
41       * is a pile of junk and chock-full of static variables. Any other test
42       * (like simple.CustomConfigTestCase) that has used the SimpleLog class
43       * will already have caused it to do once-only initialization that we
44       * can't reset, even by calling LogFactory.releaseAll, because of those
45       * ugly statics. The only clean solution is to load a clean copy of
46       * commons-logging including SimpleLog via a nice clean class loader.
47       * Or we could fix SimpleLog to be sane...
48       */
49      public static Test suite() throws Exception {
50          final Class<CustomConfigTestCase> thisClass = CustomConfigTestCase.class;
51  
52          final PathableClassLoader loader = new PathableClassLoader(null);
53          loader.useExplicitLoader("junit.", Test.class.getClassLoader());
54          loader.addLogicalLib("testclasses");
55          loader.addLogicalLib("commons-logging");
56  
57          final Class<?> testClass = loader.loadClass(thisClass.getName());
58          return new PathableTestSuite(testClass, loader);
59      }
60  
61      /**
62       * <p>The message levels that should have been logged.</p>
63       */
64      /*
65      protected Level[] testLevels =
66      { Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
67      */
68  
69      /**
70       * <p>The expected log records.</p>
71       */
72      protected List<LogRecord> expected;
73  
74      /**
75       * <p>The message strings that should have been logged.</p>
76       */
77      protected String[] testMessages =
78      { "debug", "info", "warn", "error", "fatal" };
79  
80      // Check the decorated log instance
81      @Override
82      protected void checkDecorated() {
83  
84          assertNotNull("Log exists", log);
85          assertEquals("Log class",
86                       "org.apache.commons.logging.simple.DecoratedSimpleLog",
87                       log.getClass().getName());
88  
89          // Can we call level checkers with no exceptions?
90          assertTrue(log.isDebugEnabled());
91          assertTrue(log.isErrorEnabled());
92          assertTrue(log.isFatalEnabled());
93          assertTrue(log.isInfoEnabled());
94          assertFalse(log.isTraceEnabled());
95          assertTrue(log.isWarnEnabled());
96  
97          // Can we retrieve the current log level?
98          assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log).getLevel());
99  
100         // Can we validate the extra exposed properties?
101         checkDecoratedDateTime();
102         assertEquals("DecoratedLogger",
103                      ((DecoratedSimpleLog) log).getLogName());
104         checkShowDateTime();
105         assertTrue(((DecoratedSimpleLog) log).getShowShortName());
106 
107     }
108 
109     /** Hook for subclasses */
110     protected void checkDecoratedDateTime() {
111         assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz", ((DecoratedSimpleLog) log).getDateTimeFormat());
112     }
113 
114     // Check the actual log records against the expected ones
115     protected void checkExpected() {
116         final List<LogRecord> acts = ((DecoratedSimpleLog) log).getCache();
117         int n = 0;
118         for (final LogRecord exp : expected) {
119             final LogRecord act = acts.get(n++);
120             assertEquals("Row " + n + " type", exp.type, act.type);
121             assertEquals("Row " + n + " message", exp.message, act.message);
122             assertEquals("Row " + n + " throwable", exp.t, act.t);
123         }
124     }
125 
126     /** Hook for subclassses */
127     protected void checkShowDateTime() {
128         assertFalse(((DecoratedSimpleLog) log).getShowDateTime());
129     }
130 
131     // Check the standard log instance
132     @Override
133     protected void checkStandard() {
134         checkDecorated();
135     }
136 
137     // Log the messages with exceptions
138     protected void logExceptionMessages() {
139         // Generate log records
140         final Throwable t = new DummyException();
141         log.trace("trace", t); // Should not actually get logged
142         log.debug("debug", t);
143         log.info("info", t);
144         log.warn("warn", t);
145         log.error("error", t);
146         log.fatal("fatal", t);
147 
148         // Record the log records we expect
149         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", t));
150         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", t));
151         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", t));
152         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", t));
153         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", t));
154     }
155 
156     // Log the plain messages
157     protected void logPlainMessages() {
158         // Generate log records
159         log.trace("trace"); // Should not actually get logged
160         log.debug("debug");
161         log.info("info");
162         log.warn("warn");
163         log.error("error");
164         log.fatal("fatal");
165 
166         // Record the log records we expect
167         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", null));
168         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", null));
169         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", null));
170         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", null));
171         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", null));
172     }
173 
174     /**
175      * Sets system properties that will control the LogFactory/Log objects
176      * when they are created. Subclasses can override this method to
177      * define properties that suit them.
178      */
179     @Override
180     public void setProperties() {
181         System.setProperty(
182             "org.apache.commons.logging.Log",
183             "org.apache.commons.logging.simple.DecoratedSimpleLog");
184         System.setProperty(
185             "org.apache.commons.logging.simplelog.defaultlog",
186             "debug");
187     }
188 
189     /**
190      * Sets up instance variables required by this test case.
191      */
192     @Override
193     public void setUp() throws Exception {
194         LogFactory.releaseAll();
195         setProperties();
196         expected = new ArrayList<>();
197         setUpFactory();
198         setUpLog("DecoratedLogger");
199     }
200 
201     /**
202      * Tear down instance variables required by this test case.
203      */
204     @Override
205     public void tearDown() {
206         super.tearDown();
207         expected = null;
208     }
209 
210     // Test logging message strings with exceptions
211     public void testExceptionMessages() throws Exception {
212         ((DecoratedSimpleLog) log).clearCache();
213         logExceptionMessages();
214         checkExpected();
215     }
216 
217     // Test logging plain message strings
218     public void testPlainMessages() throws Exception {
219         ((DecoratedSimpleLog) log).clearCache();
220         logPlainMessages();
221         checkExpected();
222     }
223 
224     // Test Serializability of standard instance
225     @Override
226     public void testSerializable() throws Exception {
227         ((DecoratedSimpleLog) log).clearCache();
228         logPlainMessages();
229         super.testSerializable();
230         logExceptionMessages();
231         checkExpected();
232     }
233 }