001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.logging.logkit;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.commons.logging.PathableClassLoader;
030: import org.apache.commons.logging.PathableTestSuite;
031: import org.apache.commons.logging.impl.LogKitLogger;
032: import org.apache.commons.logging.impl.NoOpLog;
033:
034: import org.apache.commons.logging.AbstractLogTest;
035:
036: /**
037: * Basic tests for Avalon LogKit logger adapter.
038: */
039:
040: public class StandardTestCase extends AbstractLogTest {
041:
042: // ----------------------------------------------------- Instance Variables
043:
044: /**
045: * <p>The {@link LogFactory} implementation we have selected.</p>
046: */
047: protected LogFactory factory = null;
048:
049: /**
050: * <p>The {@link Log} implementation we have selected.</p>
051: */
052: protected Log log = null;
053:
054: // ------------------------------------------- JUnit Infrastructure Methods
055:
056: /**
057: * Return the tests included in this test suite.
058: */
059: public static Test suite() throws Exception {
060: Class this Class = StandardTestCase.class;
061:
062: PathableClassLoader loader = new PathableClassLoader(null);
063: loader.useSystemLoader("junit.");
064: loader.addLogicalLib("testclasses");
065: loader.addLogicalLib("commons-logging");
066: loader.addLogicalLib("logkit");
067:
068: Class testClass = loader.loadClass(this Class.getName());
069: return new PathableTestSuite(testClass, loader);
070: }
071:
072: /**
073: * Set up instance variables required by this test case.
074: */
075: public void setUp() throws Exception {
076: LogFactory.releaseAll();
077:
078: System.setProperty("org.apache.commons.logging.Log",
079: "org.apache.commons.logging.impl.LogKitLogger");
080:
081: factory = LogFactory.getFactory();
082: log = LogFactory.getLog("TestLogger");
083: }
084:
085: /**
086: * Tear down instance variables required by this test case.
087: */
088: public void tearDown() {
089: log = null;
090: factory = null;
091: LogFactory.releaseAll();
092: }
093:
094: // ----------------------------------------------------------- Test Methods
095:
096: /**
097: * Override the abstract method from the parent class so that the
098: * inherited tests can access the right Log object type.
099: */
100: public Log getLogObject() {
101: return new LogKitLogger(this .getClass().getName());
102: }
103:
104: // Test pristine LogFactory instance
105: public void testPristineFactory() {
106:
107: assertNotNull("LogFactory exists", factory);
108: assertEquals("LogFactory class",
109: "org.apache.commons.logging.impl.LogFactoryImpl",
110: factory.getClass().getName());
111:
112: String names[] = factory.getAttributeNames();
113: assertNotNull("Names exists", names);
114: assertEquals("Names empty", 0, names.length);
115: }
116:
117: // Test pristine Log instance
118: public void testPristineLog() {
119: checkStandard();
120: }
121:
122: // Test Serializability of standard instance
123: public void testSerializable() throws Exception {
124: checkStandard();
125:
126: // Serialize and deserialize the instance
127: ByteArrayOutputStream baos = new ByteArrayOutputStream();
128: ObjectOutputStream oos = new ObjectOutputStream(baos);
129: oos.writeObject(log);
130: oos.close();
131: ByteArrayInputStream bais = new ByteArrayInputStream(baos
132: .toByteArray());
133: ObjectInputStream ois = new ObjectInputStream(bais);
134: log = (Log) ois.readObject();
135: ois.close();
136:
137: checkStandard();
138: }
139:
140: // -------------------------------------------------------- Support Methods
141:
142: // Check the standard log instance
143: protected void checkStandard() {
144:
145: assertNotNull("Log exists", log);
146: assertEquals("Log class",
147: "org.apache.commons.logging.impl.LogKitLogger", log
148: .getClass().getName());
149:
150: // Can we call level checkers with no exceptions?
151: // Note that by default *everything* is enabled for LogKit
152: assertTrue(log.isTraceEnabled());
153: assertTrue(log.isDebugEnabled());
154: assertTrue(log.isInfoEnabled());
155: assertTrue(log.isWarnEnabled());
156: assertTrue(log.isErrorEnabled());
157: assertTrue(log.isFatalEnabled());
158: }
159: }
|