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.noop;
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.AbstractLogTest;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.commons.logging.impl.NoOpLog;
29  
30  /**
31   * Tests for NoOpLog logging adapter.
32   * <p>
33   * This simply applies the tests defined in AbstractLogTest to this class.
34   */
35  public class NoOpLogTestCase extends AbstractLogTest {
36      private void checkLog(final Log log) {
37  
38          assertNotNull("Log exists", log);
39          assertEquals("Log class",
40                       "org.apache.commons.logging.impl.NoOpLog",
41                       log.getClass().getName());
42  
43          // Can we call level checkers with no exceptions?
44          // Note that *everything* is permanently disabled for NoOpLog
45          assertFalse(log.isTraceEnabled());
46          assertFalse(log.isDebugEnabled());
47          assertFalse(log.isInfoEnabled());
48          assertFalse(log.isWarnEnabled());
49          assertFalse(log.isErrorEnabled());
50          assertFalse(log.isFatalEnabled());
51      }
52  
53      /**
54       * Override the abstract method from the parent class so that the
55       * inherited tests can access the right Log object type.
56       */
57      @Override
58      public Log getLogObject()
59      {
60          return new NoOpLog(this.getClass().getName());
61      }
62  
63      /**
64       * Sets up instance variables required by this test case.
65       */
66      @Override
67      public void setUp() throws Exception {
68          LogFactory.releaseAll();
69  
70          System.setProperty(
71                  "org.apache.commons.logging.Log",
72                  "org.apache.commons.logging.impl.NoOpLog");
73      }
74  
75      /**
76       * Tear down instance variables required by this test case.
77       */
78      @Override
79      public void tearDown() {
80          LogFactory.releaseAll();
81          System.getProperties().remove("org.apache.commons.logging.Log");
82      }
83  
84      // Test Serializability of standard instance
85      public void testSerializable() throws Exception {
86          Log log = LogFactory.getLog(this.getClass().getName());
87          checkLog(log);
88  
89          // Serialize and deserialize the instance
90          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
91          final ObjectOutputStream oos = new ObjectOutputStream(baos);
92          oos.writeObject(log);
93          oos.close();
94          final ByteArrayInputStream bais =
95              new ByteArrayInputStream(baos.toByteArray());
96          final ObjectInputStream ois = new ObjectInputStream(bais);
97          log = (Log) ois.readObject();
98          ois.close();
99  
100         checkLog(log);
101     }
102 }