KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > logging > noop > NoOpLogTestCase


1 /*
2  * Copyright 2001-2006 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.noop;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.ObjectOutputStream JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.logging.impl.NoOpLog;
27 import org.apache.commons.logging.AbstractLogTest;
28
29 /**
30  * Tests for NoOpLog logging adapter.
31  * <p>
32  * This simply applies the tests defined in AbstractLogTest to this class.
33  */

34 public class NoOpLogTestCase extends AbstractLogTest
35 {
36     /**
37      * Set up instance variables required by this test case.
38      */

39     public void setUp() throws Exception JavaDoc {
40         LogFactory.releaseAll();
41
42         System.setProperty(
43                 "org.apache.commons.logging.Log",
44                 "org.apache.commons.logging.impl.NoOpLog");
45     }
46
47     /**
48      * Tear down instance variables required by this test case.
49      */

50     public void tearDown() {
51         LogFactory.releaseAll();
52     }
53     
54     /**
55      * Override the abstract method from the parent class so that the
56      * inherited tests can access the right Log object type.
57      */

58     public Log getLogObject()
59     {
60         return (Log) new NoOpLog(this.getClass().getName());
61     }
62
63     // Test Serializability of standard instance
64
public void testSerializable() throws Exception JavaDoc {
65         Log log = LogFactory.getLog(this.getClass().getName());
66         checkLog(log);
67
68         // Serialize and deserialize the instance
69
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
70         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
71         oos.writeObject(log);
72         oos.close();
73         ByteArrayInputStream JavaDoc bais =
74             new ByteArrayInputStream JavaDoc(baos.toByteArray());
75         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
76         log = (Log) ois.readObject();
77         ois.close();
78
79         checkLog(log);
80     }
81
82
83     // -------------------------------------------------------- Support Methods
84

85     private void checkLog(Log log) {
86
87         assertNotNull("Log exists", log);
88         assertEquals("Log class",
89                      "org.apache.commons.logging.impl.NoOpLog",
90                      log.getClass().getName());
91
92         // Can we call level checkers with no exceptions?
93
// Note that *everything* is permanently disabled for NoOpLog
94
assertFalse(log.isTraceEnabled());
95         assertFalse(log.isDebugEnabled());
96         assertFalse(log.isInfoEnabled());
97         assertFalse(log.isWarnEnabled());
98         assertFalse(log.isErrorEnabled());
99         assertFalse(log.isFatalEnabled());
100     }
101 }
102
Popular Tags