001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.logging.noop;
019
020 import java.io.ByteArrayInputStream;
021 import java.io.ByteArrayOutputStream;
022 import java.io.ObjectInputStream;
023 import java.io.ObjectOutputStream;
024
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027 import org.apache.commons.logging.impl.NoOpLog;
028 import org.apache.commons.logging.AbstractLogTest;
029
030 /**
031 * Tests for NoOpLog logging adapter.
032 * <p>
033 * This simply applies the tests defined in AbstractLogTest to this class.
034 */
035 public class NoOpLogTestCase extends AbstractLogTest
036 {
037 /**
038 * Set up instance variables required by this test case.
039 */
040 public void setUp() throws Exception {
041 LogFactory.releaseAll();
042
043 System.setProperty(
044 "org.apache.commons.logging.Log",
045 "org.apache.commons.logging.impl.NoOpLog");
046 }
047
048 /**
049 * Tear down instance variables required by this test case.
050 */
051 public void tearDown() {
052 LogFactory.releaseAll();
053 System.getProperties().remove("org.apache.commons.logging.Log");
054 }
055
056 /**
057 * Override the abstract method from the parent class so that the
058 * inherited tests can access the right Log object type.
059 */
060 public Log getLogObject()
061 {
062 return (Log) new NoOpLog(this.getClass().getName());
063 }
064
065 // Test Serializability of standard instance
066 public void testSerializable() throws Exception {
067 Log log = LogFactory.getLog(this.getClass().getName());
068 checkLog(log);
069
070 // Serialize and deserialize the instance
071 ByteArrayOutputStream baos = new ByteArrayOutputStream();
072 ObjectOutputStream oos = new ObjectOutputStream(baos);
073 oos.writeObject(log);
074 oos.close();
075 ByteArrayInputStream bais =
076 new ByteArrayInputStream(baos.toByteArray());
077 ObjectInputStream ois = new ObjectInputStream(bais);
078 log = (Log) ois.readObject();
079 ois.close();
080
081 checkLog(log);
082 }
083
084
085 // -------------------------------------------------------- Support Methods
086
087 private void checkLog(Log log) {
088
089 assertNotNull("Log exists", log);
090 assertEquals("Log class",
091 "org.apache.commons.logging.impl.NoOpLog",
092 log.getClass().getName());
093
094 // Can we call level checkers with no exceptions?
095 // Note that *everything* is permanently disabled for NoOpLog
096 assertFalse(log.isTraceEnabled());
097 assertFalse(log.isDebugEnabled());
098 assertFalse(log.isInfoEnabled());
099 assertFalse(log.isWarnEnabled());
100 assertFalse(log.isErrorEnabled());
101 assertFalse(log.isFatalEnabled());
102 }
103 }