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.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  
25  import org.apache.commons.logging.Log;
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  import junit.framework.Test;
32  import junit.framework.TestCase;
33  
34  /**
35   * <p>TestCase for simple logging when running with zero configuration
36   * other than selecting the SimpleLog implementation.</p>
37   */
38  
39  public class DefaultConfigTestCase extends TestCase {
40  
41      /**
42       * Return the tests included in this test suite.
43       * <p>
44       * We need to use a PathableClassLoader here because the SimpleLog class
45       * is a pile of junk and chock-full of static variables. Any other test
46       * (like simple.CustomConfigTestCase) that has used the SimpleLog class
47       * will already have caused it to do once-only initialization that we
48       * can't reset, even by calling LogFactory.releaseAll, because of those
49       * ugly statics. The only clean solution is to load a clean copy of
50       * commons-logging including SimpleLog via a nice clean class loader.
51       * Or we could fix SimpleLog to be sane...
52       */
53      public static Test suite() throws Exception {
54          final Class thisClass = DefaultConfigTestCase.class;
55  
56          final PathableClassLoader loader = new PathableClassLoader(null);
57          loader.useExplicitLoader("junit.", Test.class.getClassLoader());
58          loader.addLogicalLib("testclasses");
59          loader.addLogicalLib("commons-logging");
60  
61          final Class testClass = loader.loadClass(thisClass.getName());
62          return new PathableTestSuite(testClass, loader);
63      }
64  
65      /**
66       * <p>The {@link LogFactory} implementation we have selected.</p>
67       */
68      protected LogFactory factory;
69  
70      /**
71       * <p>The {@link Log} implementation we have selected.</p>
72       */
73      protected Log log;
74  
75      // Check the decorated log instance
76      protected void checkDecorated() {
77          assertNotNull("Log exists", log);
78          assertEquals("Log class",
79                       "org.apache.commons.logging.simple.DecoratedSimpleLog",
80                       log.getClass().getName());
81  
82          // Can we call level checkers with no exceptions?
83          assertFalse(log.isDebugEnabled());
84          assertTrue(log.isErrorEnabled());
85          assertTrue(log.isFatalEnabled());
86          assertTrue(log.isInfoEnabled());
87          assertFalse(log.isTraceEnabled());
88          assertTrue(log.isWarnEnabled());
89  
90          // Can we retrieve the current log level?
91          assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
92  
93          // Can we validate the extra exposed properties?
94          assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
95                       ((DecoratedSimpleLog) log).getDateTimeFormat());
96          assertEquals("DecoratedLogger",
97                       ((DecoratedSimpleLog) log).getLogName());
98          assertFalse(((DecoratedSimpleLog) log).getShowDateTime());
99          assertTrue(((DecoratedSimpleLog) log).getShowShortName());
100     }
101 
102     // Check the standard log instance
103     protected void checkStandard() {
104         assertNotNull("Log exists", log);
105         assertEquals("Log class",
106                      "org.apache.commons.logging.impl.SimpleLog",
107                      log.getClass().getName());
108 
109         // Can we call level checkers with no exceptions?
110         assertFalse(log.isDebugEnabled());
111         assertTrue(log.isErrorEnabled());
112         assertTrue(log.isFatalEnabled());
113         assertTrue(log.isInfoEnabled());
114         assertFalse(log.isTraceEnabled());
115         assertTrue(log.isWarnEnabled());
116 
117         // Can we retrieve the current log level?
118         assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
119     }
120 
121     /**
122      * Sets system properties that will control the LogFactory/Log objects
123      * when they are created. Subclasses can override this method to
124      * define properties that suit them.
125      */
126     public void setProperties() {
127         System.setProperty(
128             "org.apache.commons.logging.Log",
129             "org.apache.commons.logging.impl.SimpleLog");
130     }
131 
132     /**
133      * Sets up instance variables required by this test case.
134      */
135     @Override
136     public void setUp() throws Exception {
137         LogFactory.releaseAll();
138         setProperties();
139         setUpFactory();
140         setUpLog("TestLogger");
141     }
142 
143     // Set up decorated log instance
144     protected void setUpDecorated(final String name) {
145         log = new DecoratedSimpleLog(name);
146     }
147 
148     // Set up factory instance
149     protected void setUpFactory() throws Exception {
150         factory = LogFactory.getFactory();
151     }
152 
153     // Set up log instance
154     protected void setUpLog(final String name) throws Exception {
155         log = LogFactory.getLog(name);
156     }
157 
158     /**
159      * Tear down instance variables required by this test case.
160      */
161     @Override
162     public void tearDown() {
163         log = null;
164         factory = null;
165         LogFactory.releaseAll();
166     }
167 
168     // Test pristine DecoratedSimpleLog instance
169     public void testPristineDecorated() {
170         setUpDecorated("DecoratedLogger");
171         checkDecorated();
172     }
173 
174     // Test pristine LogFactory instance
175     public void testPristineFactory() {
176         assertNotNull("LogFactory exists", factory);
177         assertEquals("LogFactory class",
178                      "org.apache.commons.logging.impl.LogFactoryImpl",
179                      factory.getClass().getName());
180 
181         final String[] names = factory.getAttributeNames();
182         assertNotNull("Names exists", names);
183         assertEquals("Names empty", 0, names.length);
184     }
185 
186     // Test pristine Log instance
187     public void testPristineLog() {
188         checkStandard();
189     }
190 
191     // Test Serializability of standard instance
192     public void testSerializable() throws Exception {
193 
194         // Serialize and deserialize the instance
195         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
196         try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
197             oos.writeObject(log);
198         }
199         final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
200         try (ObjectInputStream ois = new ObjectInputStream(bais)) {
201             log = (Log) ois.readObject();
202         }
203 
204         // Check the characteristics of the resulting object
205         checkStandard();
206 
207     }
208 
209 }