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.jdk14;
18  
19  
20  import java.io.InputStream;
21  import java.util.Iterator;
22  import java.util.logging.Handler;
23  import java.util.logging.Level;
24  import java.util.logging.LogManager;
25  import java.util.logging.LogRecord;
26  import java.util.logging.Logger;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  
32  /***
33   * <p>TestCase for JDK 1.4 logging when running on a JDK 1.4 system with
34   * custom configuration, so that JDK 1.4 should be selected and an appropriate
35   * logger configured per the configuration properties.</p>
36   *
37   * @author Craig R. McClanahan
38   * @version $Revision: 1.9 $ $Date: 2004/02/28 21:46:45 $
39   */
40  
41  public class CustomConfigTestCase extends DefaultConfigTestCase {
42  
43  
44      // ----------------------------------------------------------- Constructors
45  
46  
47      /***
48       * <p>Construct a new instance of this test case.</p>
49       *
50       * @param name Name of the test case
51       */
52      public CustomConfigTestCase(String name) {
53          super(name);
54      }
55  
56  
57      // ----------------------------------------------------- Instance Variables
58  
59  
60      /***
61       * <p>The customized <code>Handler</code> we will be using.</p>
62       */
63      protected TestHandler handler = null;
64  
65  
66      /***
67       * <p>The underlying <code>Handler</code>s we will be using.</p>
68       */
69      protected Handler handlers[] = null;
70  
71  
72      /***
73       * <p>The underlying <code>Logger</code> we will be using.</p>
74       */
75      protected Logger logger = null;
76  
77  
78      /***
79       * <p>The underlying <code>LogManager</code> we will be using.</p>
80       */
81      protected LogManager manager = null;
82  
83  
84      /***
85       * <p>The message levels that should have been logged.</p>
86       */
87      protected Level testLevels[] =
88      { Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
89  
90  
91      /***
92       * <p>The message strings that should have been logged.</p>
93       */
94      protected String testMessages[] =
95      { "debug", "info", "warn", "error", "fatal" };
96  
97  
98      // ------------------------------------------- JUnit Infrastructure Methods
99  
100 
101     /***
102      * Set up instance variables required by this test case.
103      */
104     public void setUp() throws Exception {
105         setUpManager
106             ("org/apache/commons/logging/jdk14/CustomConfig.properties");
107         setUpLogger("TestLogger");
108         setUpHandlers();
109         setUpFactory();
110         setUpLog("TestLogger");
111     }
112 
113 
114     /***
115      * Return the tests included in this test suite.
116      */
117     public static Test suite() {
118         return (new TestSuite(CustomConfigTestCase.class));
119     }
120 
121     /***
122      * Tear down instance variables required by this test case.
123      */
124     public void tearDown() {
125         super.tearDown();
126         handlers = null;
127         logger = null;
128         manager = null;
129     }
130 
131 
132     // ----------------------------------------------------------- Test Methods
133 
134 
135     // Test logging message strings with exceptions
136     public void testExceptionMessages() throws Exception {
137 
138         logExceptionMessages();
139         checkLogRecords(true);
140 
141     }
142 
143 
144     // Test logging plain message strings
145     public void testPlainMessages() throws Exception {
146 
147         logPlainMessages();
148         checkLogRecords(false);
149 
150     }
151 
152 
153     // Test pristine Handlers instances
154     public void testPristineHandlers() {
155 
156         assertNotNull(handlers);
157         assertEquals(1, handlers.length);
158         assertTrue(handlers[0] instanceof TestHandler);
159         assertNotNull(handler);
160 
161     }
162 
163 
164     // Test pristine Logger instance
165     public void testPristineLogger() {
166 
167         assertNotNull("Logger exists", logger);
168         assertEquals("Logger name", "TestLogger", logger.getName());
169 
170         // Assert which logging levels have been enabled
171         assertTrue(logger.isLoggable(Level.SEVERE));
172         assertTrue(logger.isLoggable(Level.WARNING));
173         assertTrue(logger.isLoggable(Level.INFO));
174         assertTrue(logger.isLoggable(Level.CONFIG));
175         assertTrue(logger.isLoggable(Level.FINE));
176         assertTrue(!logger.isLoggable(Level.FINER));
177         assertTrue(!logger.isLoggable(Level.FINEST));
178 
179     }
180 
181 
182     // Test Serializability of Log instance
183     public void testSerializable() throws Exception {
184 
185         super.testSerializable();
186         testExceptionMessages();
187 
188     }
189 
190 
191     // -------------------------------------------------------- Support Methods
192 
193 
194     // Check the log instance
195     protected void checkLog() {
196 
197         assertNotNull("Log exists", log);
198         assertEquals("Log class",
199                      "org.apache.commons.logging.impl.Jdk14Logger",
200                      log.getClass().getName());
201 
202         // Assert which logging levels have been enabled
203         assertTrue(log.isFatalEnabled());
204         assertTrue(log.isErrorEnabled());
205         assertTrue(log.isWarnEnabled());
206         assertTrue(log.isInfoEnabled());
207         assertTrue(log.isDebugEnabled());
208         assertTrue(!log.isTraceEnabled());
209 
210     }
211 
212 
213     // Check the recorded messages
214     protected void checkLogRecords(boolean thrown) {
215         Iterator records = handler.records();
216         for (int i = 0; i < testMessages.length; i++) {
217             assertTrue(records.hasNext());
218             LogRecord record = (LogRecord) records.next();
219             assertEquals("LogRecord level",
220                          testLevels[i], record.getLevel());
221             assertEquals("LogRecord message",
222                          testMessages[i], record.getMessage());
223             assertEquals("LogRecord class",
224                          this.getClass().getName(),
225                          record.getSourceClassName());
226             if (thrown) {
227                 assertEquals("LogRecord method",
228                              "logExceptionMessages",
229                              record.getSourceMethodName());
230             } else {
231                 assertEquals("LogRecord method",
232                              "logPlainMessages",
233                              record.getSourceMethodName());
234             }
235             if (thrown) {
236                 assertNotNull("LogRecord thrown", record.getThrown());
237                 assertTrue("LogRecord thrown type",
238                            record.getThrown() instanceof IndexOutOfBoundsException);
239             } else {
240                 assertNull("LogRecord thrown",
241                            record.getThrown());
242             }
243         }
244         assertTrue(!records.hasNext());
245         handler.flush();
246     }
247 
248 
249     // Log the messages with exceptions
250     protected void logExceptionMessages() {
251         Throwable t = new IndexOutOfBoundsException();
252         log.trace("trace", t); // Should not actually get logged
253         log.debug("debug", t);
254         log.info("info", t);
255         log.warn("warn", t);
256         log.error("error", t);
257         log.fatal("fatal", t);
258     }
259 
260 
261     // Log the plain messages
262     protected void logPlainMessages() {
263         log.trace("trace"); // Should not actually get logged
264         log.debug("debug");
265         log.info("info");
266         log.warn("warn");
267         log.error("error");
268         log.fatal("fatal");
269     }
270 
271 
272     // Set up handlers instance
273     protected void setUpHandlers() throws Exception {
274         Logger parent = logger;
275         while (parent.getParent() != null) {
276             parent = parent.getParent();
277         }
278         handlers = parent.getHandlers();
279         if ((handlers != null) && (handlers.length == 1) &&
280             (handlers[0] instanceof TestHandler)) {
281             handler = (TestHandler) handlers[0];
282         }
283     }
284 
285 
286     // Set up logger instance
287     protected void setUpLogger(String name) throws Exception {
288         logger = Logger.getLogger(name);
289     }
290 
291 
292     // Set up LogManager instance
293     protected void setUpManager(String config) throws Exception {
294         manager = LogManager.getLogManager();
295         InputStream is =
296             this.getClass().getClassLoader().getResourceAsStream(config);
297         manager.readConfiguration(is);
298         is.close();
299     }
300 
301 
302 }