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.logkit;
019    
020    
021    import java.io.ByteArrayInputStream;
022    import java.io.ByteArrayOutputStream;
023    import java.io.ObjectInputStream;
024    import java.io.ObjectOutputStream;
025    
026    import junit.framework.Test;
027    
028    import org.apache.commons.logging.AbstractLogTest;
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    import org.apache.commons.logging.PathableClassLoader;
032    import org.apache.commons.logging.PathableTestSuite;
033    import org.apache.commons.logging.impl.LogKitLogger;
034    
035    /**
036     * Basic tests for Avalon LogKit logger adapter.
037     */
038    
039    public class StandardTestCase extends AbstractLogTest {
040    
041    
042        // ----------------------------------------------------- Instance Variables
043    
044    
045        /**
046         * <p>The {@link LogFactory} implementation we have selected.</p>
047         */
048        protected LogFactory factory = null;
049    
050    
051        /**
052         * <p>The {@link Log} implementation we have selected.</p>
053         */
054        protected Log log = null;
055    
056    
057        // ------------------------------------------- JUnit Infrastructure Methods
058    
059    
060        /**
061         * Return the tests included in this test suite.
062         */
063        public static Test suite() throws Exception {
064            Class thisClass = StandardTestCase.class;
065    
066            PathableClassLoader loader = new PathableClassLoader(null);
067            loader.useExplicitLoader("junit.", Test.class.getClassLoader());
068            loader.addLogicalLib("testclasses");
069            loader.addLogicalLib("commons-logging");
070            loader.addLogicalLib("logkit");
071    
072            Class testClass = loader.loadClass(thisClass.getName());
073            return new PathableTestSuite(testClass, loader);
074        }
075    
076        /**
077         * Set up instance variables required by this test case.
078         */
079        public void setUp() throws Exception {
080            LogFactory.releaseAll();
081    
082            System.setProperty(
083                    "org.apache.commons.logging.Log",
084                    "org.apache.commons.logging.impl.LogKitLogger");
085    
086            factory = LogFactory.getFactory();
087            log = LogFactory.getLog("TestLogger");
088        }
089    
090        /**
091         * Tear down instance variables required by this test case.
092         */
093        public void tearDown() {
094            log = null;
095            factory = null;
096            LogFactory.releaseAll();
097        }
098    
099        // ----------------------------------------------------------- Test Methods
100    
101        /**
102         * Override the abstract method from the parent class so that the
103         * inherited tests can access the right Log object type. 
104         */
105        public Log getLogObject()
106        {
107            return new LogKitLogger(this.getClass().getName());
108        }
109    
110        // Test pristine LogFactory instance
111        public void testPristineFactory() {
112    
113            assertNotNull("LogFactory exists", factory);
114            assertEquals("LogFactory class",
115                         "org.apache.commons.logging.impl.LogFactoryImpl",
116                         factory.getClass().getName());
117    
118            String names[] = factory.getAttributeNames();
119            assertNotNull("Names exists", names);
120            assertEquals("Names empty", 0, names.length);
121        }
122    
123        // Test pristine Log instance
124        public void testPristineLog() {
125            checkStandard();
126        }
127    
128        // Test Serializability of standard instance
129        public void testSerializable() throws Exception {
130            checkStandard();
131    
132            // Serialize and deserialize the instance
133            ByteArrayOutputStream baos = new ByteArrayOutputStream();
134            ObjectOutputStream oos = new ObjectOutputStream(baos);
135            oos.writeObject(log);
136            oos.close();
137            ByteArrayInputStream bais =
138                new ByteArrayInputStream(baos.toByteArray());
139            ObjectInputStream ois = new ObjectInputStream(bais);
140            log = (Log) ois.readObject();
141            ois.close();
142    
143            checkStandard();
144        }
145    
146    
147        // -------------------------------------------------------- Support Methods
148    
149        // Check the standard log instance
150        protected void checkStandard() {
151    
152            assertNotNull("Log exists", log);
153            assertEquals("Log class",
154                         "org.apache.commons.logging.impl.LogKitLogger",
155                         log.getClass().getName());
156    
157            // Can we call level checkers with no exceptions?
158            // Note that by default *everything* is enabled for LogKit
159            assertTrue(log.isTraceEnabled());
160            assertTrue(log.isDebugEnabled());
161            assertTrue(log.isInfoEnabled());
162            assertTrue(log.isWarnEnabled());
163            assertTrue(log.isErrorEnabled());
164            assertTrue(log.isFatalEnabled());
165        }
166    }