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.jdk14; 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 import junit.framework.TestCase; 028 import junit.framework.TestSuite; 029 030 import org.apache.commons.logging.Log; 031 import org.apache.commons.logging.LogFactory; 032 import org.apache.commons.logging.PathableClassLoader; 033 import org.apache.commons.logging.PathableTestSuite; 034 035 036 /** 037 * <p>TestCase for JDK 1.4 logging when running on a JDK 1.4 system with 038 * zero configuration, and with Log4J not present (so JDK 1.4 logging 039 * should be automatically configured.</p> 040 * 041 * @author Craig R. McClanahan 042 * @version $Revision: 427808 $ $Date: 2006-08-02 02:08:20 +0200 (on, 02 aug 2006) $ 043 */ 044 045 public class DefaultConfigTestCase extends TestCase { 046 047 048 // ----------------------------------------------------------- Constructors 049 050 051 /** 052 * <p>Construct a new instance of this test case.</p> 053 * 054 * @param name Name of the test case 055 */ 056 public DefaultConfigTestCase(String name) { 057 super(name); 058 } 059 060 061 // ----------------------------------------------------- Instance Variables 062 063 064 /** 065 * <p>The {@link LogFactory} implementation we have selected.</p> 066 */ 067 protected LogFactory factory = null; 068 069 070 /** 071 * <p>The {@link Log} implementation we have selected.</p> 072 */ 073 protected Log log = null; 074 075 076 // ------------------------------------------- JUnit Infrastructure Methods 077 078 079 /** 080 * Set up instance variables required by this test case. 081 */ 082 public void setUp() throws Exception { 083 setUpFactory(); 084 setUpLog("TestLogger"); 085 } 086 087 088 /** 089 * Return the tests included in this test suite. 090 */ 091 public static Test suite() throws Exception { 092 PathableClassLoader loader = new PathableClassLoader(null); 093 loader.useExplicitLoader("junit.", Test.class.getClassLoader()); 094 loader.addLogicalLib("testclasses"); 095 loader.addLogicalLib("commons-logging"); 096 097 Class testClass = loader.loadClass(DefaultConfigTestCase.class.getName()); 098 return new PathableTestSuite(testClass, loader); 099 } 100 101 /** 102 * Tear down instance variables required by this test case. 103 */ 104 public void tearDown() { 105 log = null; 106 factory = null; 107 LogFactory.releaseAll(); 108 } 109 110 111 // ----------------------------------------------------------- Test Methods 112 113 114 // Test pristine Log instance 115 public void testPristineLog() { 116 117 checkLog(); 118 119 } 120 121 122 // Test pristine LogFactory instance 123 public void testPristineFactory() { 124 125 assertNotNull("LogFactory exists", factory); 126 assertEquals("LogFactory class", 127 "org.apache.commons.logging.impl.LogFactoryImpl", 128 factory.getClass().getName()); 129 130 String names[] = factory.getAttributeNames(); 131 assertNotNull("Names exists", names); 132 assertEquals("Names empty", 0, names.length); 133 134 } 135 136 137 // Test Serializability of Log instance 138 public void testSerializable() throws Exception { 139 140 // Serialize and deserialize the instance 141 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 142 ObjectOutputStream oos = new ObjectOutputStream(baos); 143 oos.writeObject(log); 144 oos.close(); 145 ByteArrayInputStream bais = 146 new ByteArrayInputStream(baos.toByteArray()); 147 ObjectInputStream ois = new ObjectInputStream(bais); 148 log = (Log) ois.readObject(); 149 ois.close(); 150 151 // Check the characteristics of the resulting object 152 checkLog(); 153 154 } 155 156 157 // -------------------------------------------------------- Support Methods 158 159 160 161 // Check the log instance 162 protected void checkLog() { 163 164 assertNotNull("Log exists", log); 165 assertEquals("Log class", 166 "org.apache.commons.logging.impl.Jdk14Logger", 167 log.getClass().getName()); 168 169 // Can we call level checkers with no exceptions? 170 log.isDebugEnabled(); 171 log.isErrorEnabled(); 172 log.isFatalEnabled(); 173 log.isInfoEnabled(); 174 log.isTraceEnabled(); 175 log.isWarnEnabled(); 176 177 } 178 179 180 // Set up factory instance 181 protected void setUpFactory() throws Exception { 182 factory = LogFactory.getFactory(); 183 } 184 185 186 // Set up log instance 187 protected void setUpLog(String name) throws Exception { 188 log = LogFactory.getLog(name); 189 } 190 191 192 }