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  import junit.framework.TestSuite;
26  
27  import org.apache.commons.logging.impl.SimpleLog;
28  
29  
30  /***
31   * <p>TestCase for simple logging when running with custom configuration
32   * properties.</p>
33   *
34   * @author Craig R. McClanahan
35   * @version $Revision: 1.6 $ $Date: 2004/05/30 10:32:25 $
36   */
37  public class CustomConfigTestCase extends DefaultConfigTestCase {
38  
39  
40      // ----------------------------------------------------------- Constructors
41  
42  
43      /***
44       * <p>Construct a new instance of this test case.</p>
45       *
46       * @param name Name of the test case
47       */
48      public CustomConfigTestCase(String name) {
49          super(name);
50      }
51  
52  
53      // ----------------------------------------------------- Instance Variables
54  
55  
56      /***
57       * <p>The expected log records.</p>
58       */
59      protected List expected;
60  
61  
62      /***
63       * <p>The message levels that should have been logged.</p>
64       */
65      /*
66      protected Level testLevels[] =
67      { Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
68      */
69  
70  
71      /***
72       * <p>The message strings that should have been logged.</p>
73       */
74      protected String testMessages[] =
75      { "debug", "info", "warn", "error", "fatal" };
76  
77  
78      // ------------------------------------------- JUnit Infrastructure Methods
79  
80  
81      /***
82       * Set up instance variables required by this test case.
83       */
84      public void setUp() throws Exception {
85          expected = new ArrayList();
86          setUpFactory();
87          setUpLog("DecoratedLogger");
88      }
89  
90  
91      /***
92       * Return the tests included in this test suite.
93       */
94      public static Test suite() {
95          return (new TestSuite(CustomConfigTestCase.class));
96      }
97  
98      /***
99       * Tear down instance variables required by this test case.
100      */
101     public void tearDown() {
102         super.tearDown();
103         expected = null;
104     }
105 
106 
107     // ----------------------------------------------------------- Test Methods
108 
109 
110     // Test logging message strings with exceptions
111     public void testExceptionMessages() throws Exception {
112 
113         ((DecoratedSimpleLog) log).clearCache();
114         logExceptionMessages();
115         checkExpected();
116 
117     }
118 
119 
120     // Test logging plain message strings
121     public void testPlainMessages() throws Exception {
122 
123         ((DecoratedSimpleLog) log).clearCache();
124         logPlainMessages();
125         checkExpected();
126 
127     }
128 
129 
130     // Test Serializability of standard instance
131     public void testSerializable() throws Exception {
132 
133         ((DecoratedSimpleLog) log).clearCache();
134         logPlainMessages();
135         super.testSerializable();
136         logExceptionMessages();
137         checkExpected();
138 
139     }
140 
141 
142     // -------------------------------------------------------- Support Methods
143 
144 
145     // Check the decorated log instance
146     protected void checkDecorated() {
147 
148         assertNotNull("Log exists", log);
149         assertEquals("Log class",
150                      "org.apache.commons.logging.simple.DecoratedSimpleLog",
151                      log.getClass().getName());
152 
153         // Can we call level checkers with no exceptions?
154         assertTrue(log.isDebugEnabled());
155         assertTrue(log.isErrorEnabled());
156         assertTrue(log.isFatalEnabled());
157         assertTrue(log.isInfoEnabled());
158         assertTrue(!log.isTraceEnabled());
159         assertTrue(log.isWarnEnabled());
160 
161         // Can we retrieve the current log level?
162         assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log).getLevel());
163 
164         // Can we validate the extra exposed properties?
165         checkDecoratedDateTime();
166         assertEquals("DecoratedLogger",
167                      ((DecoratedSimpleLog) log).getLogName());
168         checkShowDateTime();
169         assertTrue(((DecoratedSimpleLog) log).getShowShortName());
170 
171     }
172     
173     /*** Hook for subclassses */
174     protected void checkShowDateTime() {
175         assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
176     }
177     
178     /*** Hook for subclasses */
179     protected void checkDecoratedDateTime() {
180             assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
181                      ((DecoratedSimpleLog) log).getDateTimeFormat());
182     }
183     
184 
185 
186     // Check the actual log records against the expected ones
187     protected void checkExpected() {
188 
189         List acts = ((DecoratedSimpleLog) log).getCache();
190         Iterator exps = expected.iterator();
191         int n = 0;
192         while (exps.hasNext()) {
193             LogRecord exp = (LogRecord) exps.next();
194             LogRecord act = (LogRecord) acts.get(n++);
195             assertEquals("Row " + n + " type", exp.type, act.type);
196             assertEquals("Row " + n + " message", exp.message, act.message);
197             assertEquals("Row " + n + " throwable", exp.t, act.t);
198         }
199 
200     }
201 
202 
203     // Check the standard log instance
204     protected void checkStandard() {
205 
206         checkDecorated();
207 
208     }
209 
210 
211     // Log the messages with exceptions
212     protected void logExceptionMessages() {
213 
214         // Generate log records
215         Throwable t = new IndexOutOfBoundsException();
216         log.trace("trace", t); // Should not actually get logged
217         log.debug("debug", t);
218         log.info("info", t);
219         log.warn("warn", t);
220         log.error("error", t);
221         log.fatal("fatal", t);
222 
223         // Record the log records we expect
224         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", t));
225         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", t));
226         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", t));
227         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", t));
228         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", t));
229 
230     }
231 
232 
233     // Log the plain messages
234     protected void logPlainMessages() {
235 
236         // Generate log records
237         log.trace("trace"); // Should not actually get logged
238         log.debug("debug");
239         log.info("info");
240         log.warn("warn");
241         log.error("error");
242         log.fatal("fatal");
243 
244         // Record the log records we expect
245         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", null));
246         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", null));
247         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", null));
248         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", null));
249         expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", null));
250 
251     }
252 
253 
254 }