commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skitch...@apache.org
Subject svn commit: r178064 - /jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestFactory.java /jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestLogger.java
Date Tue, 24 May 2005 02:46:49 GMT
Author: skitching
Date: Mon May 23 19:46:49 2005
New Revision: 178064

URL: http://svn.apache.org/viewcvs?rev=178064&view=rev
Log:
factory/logger combination for use in unit tests.

Added:
    jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestFactory.java
  (with props)
    jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestLogger.java
  (with props)

Added: jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestFactory.java?rev=178064&view=auto
==============================================================================
--- jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestFactory.java
(added)
+++ jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestFactory.java
Mon May 23 19:46:49 2005
@@ -0,0 +1,70 @@
+/*
+ * 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.commons.logging.impl;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.Factory;
+import java.io.PrintStream;
+
+/**
+ * A Factory that is intended to be used in unit tests. 
+ * <p>
+ * This factory allows the output to be directed to an explicit
+ * PrintStream object, so that the caller can intercept the messages
+ * logged. Because this facility allows code in a child classloader to
+ * modify configuration on a Factory object residing in a shared classloader,
+ * it is not safe for use in environments where code in child classloaders
+ * is not trusted; that's why this is called TestFactory and not
+ * RedirectablyFactory or somesuch name.
+ */
+public class TestFactory extends Factory {
+    private PrintStream stream = System.out;
+    private Log logger = null;
+    
+    public TestFactory() {
+        super();
+    }
+
+    // --------------------------------------------------------- Public Methods
+
+    /**
+     * Return a logger associated with the specified category name.
+     */
+    public Log getLog(String name) {
+        if (logger == null) {
+            // we only construct logger on demand so that users have a
+            // chance to call setStream before the logger is created.
+            logger = new TestLogger(stream);
+        }
+        return logger;
+    }
+
+    public void release(ClassLoader cl) {
+    }
+    
+    public void releaseAll() {
+    }
+
+    /**
+     * This method is not part of the Factory interface; in order to 
+     * access this method, call LogFactory.getFactory(), then downcast
+     * to TestFactory.
+     */
+    public void setStream(PrintStream stream) {
+        this.stream = stream;
+    }
+}

Propchange: jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestLogger.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestLogger.java?rev=178064&view=auto
==============================================================================
--- jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestLogger.java
(added)
+++ jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestLogger.java
Mon May 23 19:46:49 2005
@@ -0,0 +1,108 @@
+/*
+ * 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.commons.logging.impl;
+
+import org.apache.commons.logging.Log;
+import java.io.PrintStream; 
+
+/**
+ * A logger class that writes to a specified PrintStream.
+ */
+public class TestLogger implements Log {
+
+    private PrintStream stream;
+    
+    public TestLogger(PrintStream stream) {
+        this.stream = stream;
+    }
+
+    public boolean isTraceEnabled() {
+        return true;
+    }
+
+    public boolean isDebugEnabled() {
+        return true;
+    }
+
+    public boolean isInfoEnabled() {
+        return true;
+    }
+
+    public boolean isWarnEnabled() {
+        return true;
+    }
+
+    public boolean isErrorEnabled() {
+        return true;
+    }
+
+    public boolean isFatalEnabled() {
+        return true;
+    }
+
+    public void trace(Object message) {
+        log("TRACE: " + message);
+    }
+
+    public void trace(Object message, Throwable t) {
+        log("TRACE: " + message);
+    }
+
+    public void debug(Object message) {
+        log("DEBUG: " + message);
+    }
+
+    public void debug(Object message, Throwable t) {
+        log("DEBUG: " + message);
+    }
+
+    public void info(Object message) {
+        log("INFO: " + message);
+    }
+
+    public void info(Object message, Throwable t) {
+        log("INFO: " + message);
+    }
+
+    public void warn(Object message) {
+        log("WARN: " + message);
+    }
+
+    public void warn(Object message, Throwable t) {
+        log("WARN: " + message);
+    }
+
+    public void error(Object message) {
+        log("ERROR: " + message);
+    }
+
+    public void error(Object message, Throwable t) {
+        log("ERROR: " + message);
+    }
+
+    public void fatal(Object message) {
+        log("FATAL: " + message);
+    }
+
+    public void fatal(Object message, Throwable t) {
+        log("FATAL: " + message);
+    }
+
+    private void log(String msg) {
+        stream.println(msg);
+    }
+}

Propchange: jakarta/commons/proper/logging/branches/simon-1.1/src/java/org/apache/commons/logging/impl/TestLogger.java
------------------------------------------------------------------------------
    svn:keywords = Id



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message