1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.logging.simple;
18  
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import junit.framework.Test;
25  
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  /***
33   * <p>TestCase for simple logging when running with custom configuration
34   * properties.</p>
35   *
36   * @author Craig R. McClanahan
37   * @version $Revision: 370012 $ $Date: 2006-01-18 02:34:05 +0000 (Wed, 18 Jan 2006) $
38   */
39  public class CustomConfigTestCase extends DefaultConfigTestCase {
40  
41  
42      // ----------------------------------------------------- Instance Variables
43  
44  
45      /***
46       * <p>The expected log records.</p>
47       */
48      protected List expected;
49  
50  
51      /***
52       * <p>The message levels that should have been logged.</p>
53       */
54      /*
55      protected Level testLevels[] =
56      { Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
57      */
58  
59  
60      /***
61       * <p>The message strings that should have been logged.</p>
62       */
63      protected String testMessages[] =
64      { "debug", "info", "warn", "error", "fatal" };
65  
66  
67      // ------------------------------------------- JUnit Infrastructure Methods
68  
69      /***
70       * Set system properties that will control the LogFactory/Log objects
71       * when they are created. Subclasses can override this method to
72       * define properties that suit them.
73       */
74      public void setProperties() {
75          System.setProperty(
76              "org.apache.commons.logging.Log",
77              "org.apache.commons.logging.simple.DecoratedSimpleLog");
78          System.setProperty(
79              "org.apache.commons.logging.simplelog.defaultlog",
80              "debug");
81      }
82  
83      /***
84       * Set up instance variables required by this test case.
85       */
86      public void setUp() throws Exception {
87          LogFactory.releaseAll();
88          setProperties();
89          expected = new ArrayList();
90          setUpFactory();
91          setUpLog("DecoratedLogger");
92      }
93  
94  
95      /***
96       * Return the tests included in this test suite.
97       * <p>
98       * We need to use a PathableClassLoader here because the SimpleLog class
99       * is a pile of junk and chock-full of static variables. Any other test
100      * (like simple.CustomConfigTestCase) that has used the SimpleLog class
101      * will already have caused it to do once-only initialisation that we
102      * can't reset, even by calling LogFactory.releaseAll, because of those
103      * ugly statics. The only clean solution is to load a clean copy of
104      * commons-logging including SimpleLog via a nice clean classloader.
105      * Or we could fix SimpleLog to be sane...
106      */
107     public static Test suite() throws Exception {
108         Class thisClass = CustomConfigTestCase.class;
109 
110         PathableClassLoader loader = new PathableClassLoader(null);
111         loader.useSystemLoader("junit.");
112         loader.addLogicalLib("testclasses");
113         loader.addLogicalLib("commons-logging");
114         
115         Class testClass = loader.loadClass(thisClass.getName());
116         return new PathableTestSuite(testClass, loader);
117     }
118 
119     /***
120      * Tear down instance variables required by this test case.
121      */
122     public void tearDown() {
123         super.tearDown();
124         expected = null;
125     }
126 
127 
128     // ----------------------------------------------------------- Test Methods
129 
130 
131     // Test logging message strings with exceptions
132     public void testExceptionMessages() throws Exception {
133 
134         ((DecoratedSimpleLog) log).clearCache();
135         logExceptionMessages();
136         checkExpected();
137 
138     }
139 
140 
141     // Test logging plain message strings
142     public void testPlainMessages() throws Exception {
143 
144         ((DecoratedSimpleLog) log).clearCache();
145         logPlainMessages();
146         checkExpected();
147 
148     }
149 
150 
151     // Test Serializability of standard instance
152     public void testSerializable() throws Exception {
153 
154         ((DecoratedSimpleLog) log).clearCache();
155         logPlainMessages();
156         super.testSerializable();
157         logExceptionMessages();
158         checkExpected();
159 
160     }
161 
162 
163     // -------------------------------------------------------- Support Methods
164 
165 
166     // Check the decorated log instance
167     protected void checkDecorated() {
168 
169         assertNotNull("Log exists", log);
170         assertEquals("Log class",
171                      "org.apache.commons.logging.simple.DecoratedSimpleLog",
172                      log.getClass().getName());
173 
174         // Can we call level checkers with no exceptions?
175         assertTrue(log.isDebugEnabled());
176         assertTrue(log.isErrorEnabled());
177         assertTrue(log.isFatalEnabled());
178         assertTrue(log.isInfoEnabled());
179         assertTrue(!log.isTraceEnabled());
180         assertTrue(log.isWarnEnabled());
181 
182         // Can we retrieve the current log level?
183         assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log).getLevel());
184 
185         // Can we validate the extra exposed properties?
186         checkDecoratedDateTime();
187         assertEquals("DecoratedLogger",
188                      ((DecoratedSimpleLog) log).getLogName());
189         checkShowDateTime();
190         assertTrue(((DecoratedSimpleLog) log).getShowShortName());
191 
192     }
193     
194     /*** Hook for subclassses */
195     protected void checkShowDateTime() {
196         assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
197     }
198     
199     /*** Hook for subclasses */
200     protected void checkDecoratedDateTime() {
201             assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
202                      ((DecoratedSimpleLog) log).getDateTimeFormat());
203     }
204     
205 
206 
207     // Check the actual log records against the expected ones
208     protected void checkExpected() {
209 
210         List acts = ((DecoratedSimpleLog) log).getCache();
211         Iterator exps = expected.iterator();
212         int n = 0;
213         while (exps.hasNext()) {
214             LogRecord exp = (LogRecord) exps.next();
215             LogRecord act = (LogRecord) acts.get(n++);
216             assertEquals("Row " + n + " type", exp.type, act.type);
217             assertEquals("Row " + n + " message", exp.message, act.message);
218             assertEquals("Row " + n + " throwable", exp.t, act.t);
219         }
220 
221     }
222 
223 
224     // Check the standard log instance
225     protected void checkStandard() {
226 
227         checkDecorated();
228 
229     }
230 
231 
232     // Log the messages with exceptions
233     protected void logExceptionMessages() {
234 
235         // Generate log records
236         Throwable t = new IndexOutOfBoundsException();
237         log.trace("trace", t); // Should not actually get logged
238         log.debug("debug", t);
239         log.info("info", t);
240         log.warn("warn", t);
241         log.error("error", t);
242         log.fatal("fatal", t);
243 
244         // Record the log records we expect
245         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", t));
246         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", t));
247         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", t));
248         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", t));
249         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", t));
250 
251     }
252 
253 
254     // Log the plain messages
255     protected void logPlainMessages() {
256 
257         // Generate log records
258         log.trace("trace"); // Should not actually get logged
259         log.debug("debug");
260         log.info("info");
261         log.warn("warn");
262         log.error("error");
263         log.fatal("fatal");
264 
265         // Record the log records we expect
266         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", null));
267         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", null));
268         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", null));
269         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", null));
270         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", null));
271 
272     }
273 
274 
275 }