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