geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r225645 - in /geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging: ./ log4j/ log4j/AbstractLog4jLogTest.java log4j/Foo.java log4j/MockLogger.java log4j/XLevelTest.java
Date Wed, 27 Jul 2005 22:20:40 GMT
Author: dain
Date: Wed Jul 27 15:20:37 2005
New Revision: 225645

URL: http://svn.apache.org/viewcvs?rev=225645&view=rev
Log:
GERONIMO-145 white box tests for log4j logging

Added:
    geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/
    geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/
    geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/AbstractLog4jLogTest.java
  (with props)
    geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/Foo.java
    geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/MockLogger.java
  (with props)
    geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/XLevelTest.java
  (with props)

Added: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/AbstractLog4jLogTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/AbstractLog4jLogTest.java?rev=225645&view=auto
==============================================================================
--- geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/AbstractLog4jLogTest.java
(added)
+++ geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/AbstractLog4jLogTest.java
Wed Jul 27 15:20:37 2005
@@ -0,0 +1,144 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.system.logging.log4j;
+
+import junit.framework.TestCase;
+import org.apache.log4j.Priority;
+import org.apache.log4j.Level;
+import org.apache.commons.logging.Log;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class AbstractLog4jLogTest extends TestCase {
+    protected final static Object MESSAGE = new Object();
+    protected final static Throwable THROWABLE = new Throwable();
+    protected MockLogger mockLogger;
+    protected Log log;
+    protected String logFQCN;
+
+    protected void doTestDelegatedToLog(Priority priority, Object object, Throwable throwable)
{
+        assertTrue("Method delegates to Logger.log",
+                mockLogger.logCalled
+                & mockLogger.calledWithString == logFQCN
+                & mockLogger.calledWithPriority == priority
+                & mockLogger.calledWithObject == object
+                & mockLogger.calledWithThrowable == throwable);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#isWarnEnabled()} method.
+     * Tests that it delegates to {@link org.apache.log4j.Category#isEnabledFor(Priority)}
+     * with {@link org.apache.log4j.Level#WARN} as an argument.
+     */
+    public void testIsWarnEnabled() {
+        log.isWarnEnabled();
+        assertTrue("Method delegates to Logger.isEnabledFor",
+                mockLogger.isEnabledForCalled
+                & mockLogger.calledWithPriority==Level.WARN);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#warn(Object) method.
+     * Tests that it delegates to
+     * {@link org.apache.log4j.Category#log(String, Priority, Object, Throwable)}
+     * with {@link org.apache.log4j.Level#WARN} level, specified message and <code>null</code>
throwable.
+     */
+    public void testWarnNull() {
+        log.warn(MESSAGE);
+        doTestDelegatedToLog(Level.WARN, MESSAGE, null);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#warn(Object,java.lang.Throwable throwable)
method.
+     * Tests that it delegates to
+     * {@link org.apache.log4j.Category#log(String, Priority, Object, Throwable)}
+     * with {@link org.apache.log4j.Level#WARN} level, specified message and throwable.
+     */
+    public void testWarnThrowable() {
+        log.warn(MESSAGE, THROWABLE);
+        doTestDelegatedToLog(Level.WARN, MESSAGE, THROWABLE);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#isErrorEnabled()} method.
+     * Tests that it delegates to {@link org.apache.log4j.Category#isEnabledFor(Priority)}
+     * with {@link org.apache.log4j.Level#ERROR} as an argument.
+     */
+    public void testIsErrorEnabled() {
+        log.isErrorEnabled();
+        assertTrue("Method delegates to Logger.isEnabledFor",
+                mockLogger.isEnabledForCalled
+                & mockLogger.calledWithPriority==Level.ERROR);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#error(Object) method.
+     * Tests that it delegates to
+     * {@link org.apache.log4j.Category#log(String, Priority, Object, Throwable)}
+     * with {@link org.apache.log4j.Level#ERROR} level, specified message and <code>null</code>
throwable.
+     */
+    public void testErrorNull() {
+        log.error(MESSAGE);
+        doTestDelegatedToLog(Level.ERROR, MESSAGE, null);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#error(Object,java.lang.Throwable throwable)
method.
+     * Tests that it delegates to
+     * {@link org.apache.log4j.Category#log(String, Priority, Object, Throwable)}
+     * with {@link org.apache.log4j.Level#ERROR} level, specified message and throwable.
+     */
+    public void testErrorThrowable() {
+        log.error(MESSAGE, THROWABLE);
+        doTestDelegatedToLog(Level.ERROR, MESSAGE, THROWABLE);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#isFatalEnabled()} method.
+     * Tests that it delegates to {@link org.apache.log4j.Category#isEnabledFor(Priority)}
+     * with {@link org.apache.log4j.Level#FATAL} as an argument.
+     */
+    public void testIsFatalEnabled() {
+        log.isFatalEnabled();
+        assertTrue("Method delegates to Logger.isEnabledFor",
+                mockLogger.isEnabledForCalled
+                & mockLogger.calledWithPriority==Level.FATAL);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#fatal(Object) method.
+     * Tests that it delegates to
+     * {@link org.apache.log4j.Category#log(String, Priority, Object, Throwable)}
+     * with {@link org.apache.log4j.Level#FATAL} level, specified message and <code>null</code>
throwable.
+     */
+    public void testFatalNull() {
+        log.fatal(MESSAGE);
+        doTestDelegatedToLog(Level.FATAL, MESSAGE, null);
+    }
+
+    /**
+     * Tests {@link org.apache.commons.logging.Log#fatal(Object,java.lang.Throwable throwable)
method.
+     * Tests that it delegates to
+     * {@link org.apache.log4j.Category#log(String, Priority, Object, Throwable)}
+     * with {@link org.apache.log4j.Level#FATAL} level, specified message and throwable.
+     */
+    public void testFatalThrowable() {
+        log.fatal(MESSAGE, THROWABLE);
+        doTestDelegatedToLog(Level.FATAL, MESSAGE, THROWABLE);
+    }
+}

Propchange: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/AbstractLog4jLogTest.java
------------------------------------------------------------------------------
    svn:executable = *

Added: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/Foo.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/Foo.java?rev=225645&view=auto
==============================================================================
--- geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/Foo.java
(added)
+++ geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/Foo.java
Wed Jul 27 15:20:37 2005
@@ -0,0 +1,23 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.system.logging.log4j;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class Foo {
+}

Added: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/MockLogger.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/MockLogger.java?rev=225645&view=auto
==============================================================================
--- geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/MockLogger.java
(added)
+++ geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/MockLogger.java
Wed Jul 27 15:20:37 2005
@@ -0,0 +1,88 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.system.logging.log4j;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.Priority;
+
+/**
+ * @version $Rev$ $Date$
+ */
+final class MockLogger extends Logger {
+    final static String MOCK_NAME = "MOCK_NAME";
+    boolean isEnabledForCalled = false;
+    Priority calledWithPriority = null;
+    boolean logCalled = false;
+    Object calledWithObject;
+    Throwable calledWithThrowable;
+    String calledWithString;
+    boolean isDebugEnabledCalled;
+    boolean isInfoEnabledCalled;
+    private boolean debugEnabled = false;
+    private boolean infoEnabled = false;
+    private boolean enabledFor = false;
+    final Object isEnabledForLock = new Object();
+
+    MockLogger(String str) {
+        super(str);
+    }
+
+    public boolean isEnabledFor(Priority priority) {
+        synchronized (isEnabledForLock) {
+            isEnabledForCalled = true;
+            calledWithPriority = priority;
+            if (isEnabledForCalled && isDebugEnabledCalled && isInfoEnabledCalled)
isEnabledForLock.notifyAll();
+            return enabledFor;
+        }
+    }
+
+    public boolean isDebugEnabled() {
+        synchronized (isEnabledForLock) {
+            isDebugEnabledCalled = true;
+            if (isEnabledForCalled && isDebugEnabledCalled && isInfoEnabledCalled)
isEnabledForLock.notifyAll();
+            return debugEnabled;
+        }
+    }
+
+    public boolean isInfoEnabled() {
+        synchronized (isEnabledForLock) {
+            isInfoEnabledCalled = true;
+            if (isEnabledForCalled && isDebugEnabledCalled && isInfoEnabledCalled)
isEnabledForLock.notifyAll();
+            return infoEnabled;
+        }
+    }
+
+    public void log(String s, Priority priority, Object o, Throwable throwable) {
+        logCalled = true;
+        calledWithString = s;
+        calledWithPriority = priority;
+        calledWithObject = o;
+        calledWithThrowable = throwable;
+    }
+
+    public void setEnabledFor(boolean enabledFor) {
+        this.enabledFor = enabledFor;
+    }
+
+    public void setDebugEnabled(boolean debugEnabled) {
+        this.debugEnabled = debugEnabled;
+    }
+
+    public void setInfoEnabled(boolean infoEnabled) {
+        this.infoEnabled = infoEnabled;
+    }
+}

Propchange: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/MockLogger.java
------------------------------------------------------------------------------
    svn:executable = *

Added: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/XLevelTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/XLevelTest.java?rev=225645&view=auto
==============================================================================
--- geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/XLevelTest.java
(added)
+++ geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/XLevelTest.java
Wed Jul 27 15:20:37 2005
@@ -0,0 +1,102 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.system.logging.log4j;
+
+import junit.framework.TestCase;
+import org.apache.log4j.Level;
+
+/**
+ * Tests the {@link XLevel} class.
+ * @version $Rev$ $Date$
+ */
+public final class XLevelTest extends TestCase {
+    private static final int TRACE_SYSLOG_EQ = 7;
+    private static final String TRACE_STRING = "TRACE";
+    private static final int TRACE_INT = Level.DEBUG_INT - 1;
+    private static final int MOCK_INT = TRACE_INT - 99;
+
+    private final static class MockLevel extends Level {
+        MockLevel() {
+            super(MOCK_INT, "MOCK", 99);
+        }
+    }
+
+    private final static Level MOCK = new MockLevel();
+
+    /**
+     * Tests the constants declared in the {@link XLevel}.
+     * The {@link XLevel#TRACE_INT} value is tested to be defined as {@link Level#DEBUG_INT}-1.
+     * The {@link XLevel#TRACE} value is tested to be an instance of {@link XLevel},
+     * have a name "TRACE" and syslog level equivalent of <code>7</code>.
+     */
+    public void testConstants() {
+        assertEquals("XLevel.TRACE_INT is defined as Level.DEBUG_INT-1", XLevel.TRACE_INT,
TRACE_INT);
+        final Object o = XLevel.TRACE;
+        assertTrue("XLevel.TRACE is an instance of XLevel", o instanceof XLevel);
+        final XLevel traceLevel = (XLevel) o;
+        assertEquals("XLevel.TRACE syslog equivalent is indeed " + TRACE_SYSLOG_EQ, traceLevel.getSyslogEquivalent(),
TRACE_SYSLOG_EQ);
+        assertEquals("XLevel.TRACE name is TRACE", traceLevel.toString(), TRACE_STRING);
+        assertEquals("XLevel.TRACE int level is " + TRACE_INT, traceLevel.toInt(), TRACE_INT);
+    }
+
+    /**
+     * Tests {@link XLevel#toLevel(java.lang.String, org.apache.log4j.Level) method.
+     * Tests that default value is returned if null value is passed.
+     * Tests that {@link XLevel#TRACE} is returned if the name is equal to "TRACE",
+     * irrespective of case.
+     * Tests that in all other cases the conversion is deferred to {@link Level#toLevel(java.lang.String,
org.apache.log4j.Level)}.
+     * Tests that if conversion fails the specified default value is returned.
+     */
+    public void testToLevelWithDefault() {
+        assertSame("Default value is indeed returned if null name is passed", XLevel.toLevel(null,
MOCK), MOCK);
+        assertSame("XLevel.TRACE is returned if the name is equal to \"TRACE\" irrespecctive
of case", XLevel.toLevel("trAce"), XLevel.TRACE);
+        Level levelResults = Level.toLevel("MOCK", MOCK);
+        Level xLevelResults = XLevel.toLevel("MOCK", MOCK);
+        assertSame("In all other cases conversion is deferred to Level", xLevelResults, levelResults);
+        assertSame("If conversion fails the default value is returned", xLevelResults, MOCK);
+    }
+
+    /**
+     * Tests {@link XLevel#toLevel(java.lang.String) method.
+     * Tests that this method simply delegates the work to
+     * {@link XLevel#toLevel(java.lang.String, org.apache.log4j.Level) with default value
+     * specified as {@link XLevel#TRACE}.
+     */
+    public void testToLevel() {
+        assertSame("XLevel.TRACE is returned if null name is passed", XLevel.toLevel(null),
XLevel.TRACE);
+        assertSame("XLevel.TRACE is returned if the name is equal to \"TRACE\" irrespective
of case", XLevel.toLevel("trAce"), XLevel.TRACE);
+        Level levelResults = Level.toLevel("MOCK", XLevel.TRACE);
+        Level xLevelResults = XLevel.toLevel("MOCK");
+        assertSame("In all other cases conversion is deferred to Level", xLevelResults, levelResults);
+        assertSame("If conversion fails XLevel.TRACE is returned", xLevelResults, XLevel.TRACE);
+    }
+
+    /**
+     * Tests {@link XLevel#toLevel(int) method.
+     * Tests that if level is equal to {@link XLevel#TRACE_INT} {@link XLevel#TRACE} is returned.
+     * Tests that in other cases the conversion is deferred to {@link Level#toLevel(int)}
+     * Tests that if conversion fails the {@link Level#DEBUG} is returned.
+     */
+    public void testToLevelInt() {
+        assertSame("XLevel.TRACE is returned is " + TRACE_INT + "is passed", XLevel.toLevel(TRACE_INT),
XLevel.TRACE);
+        Level levelResults = Level.toLevel(MOCK_INT);
+        Level xLevelResults = XLevel.toLevel(MOCK_INT);
+        assertSame("In all other cases conversion is deferred to Level", xLevelResults, levelResults);
+        assertSame("If conversion fails Level.DEBUG is returned", xLevelResults, Level.DEBUG);
+    }
+
+}

Propchange: geronimo/trunk/modules/system/src/test/org/apache/geronimo/system/logging/log4j/XLevelTest.java
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message