commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skitch...@apache.org
Subject svn commit: r178062 - in /jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging: BasicTestCase.java ChildFirstClassLoader.java LogTester.java ParentFirstClassLoader.java TestConstants.java
Date Tue, 24 May 2005 02:41:05 GMT
Author: skitching
Date: Mon May 23 19:41:04 2005
New Revision: 178062

URL: http://svn.apache.org/viewcvs?rev=178062&view=rev
Log:
Initial testcase stuff.

Added:
    jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/BasicTestCase.java
  (with props)
    jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ChildFirstClassLoader.java
  (with props)
    jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/LogTester.java
  (with props)
    jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ParentFirstClassLoader.java
  (with props)
    jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/TestConstants.java
  (with props)

Added: jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/BasicTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/BasicTestCase.java?rev=178062&view=auto
==============================================================================
--- jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/BasicTestCase.java
(added)
+++ jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/BasicTestCase.java
Mon May 23 19:41:04 2005
@@ -0,0 +1,132 @@
+/* $Id$
+*
+* 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;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import java.net.URL;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.io.OutputStream;
+import java.lang.reflect.Method;
+
+/**
+ * A rough test case for the basic operations of JCL.
+ * <p>
+ * This is very much a work-in-progress!
+ */
+
+public class BasicTestCase extends TestCase {
+
+   public BasicTestCase(String name) {
+       super(name);
+   }
+
+   public static Test suite() {
+       return (new TestSuite(BasicTestCase.class));
+   }
+
+   /**
+    * Test a single classloader with core and test jars in classpath.
+    */
+   public void testDirect() throws Exception {
+       URL cwdDir = new URL("file:");
+       URL coreJarPath = new URL(cwdDir, 
+                   TestConstants.JAR_DIR + "/" + TestConstants.CORE_JAR);
+       URL testJarPath = new URL(cwdDir, 
+                   TestConstants.JAR_DIR + "/" + TestConstants.TEST_JAR);
+       URL testPath = new URL(cwdDir,
+                   TestConstants.JAR_DIR + "/" + "tests/");
+       
+       ParentFirstClassLoader loader = new ParentFirstClassLoader(null);
+       loader.addURL(coreJarPath);
+       loader.addURL(testJarPath);
+       loader.addURL(testPath);
+
+       String cwd = System.getProperty("user.dir");
+       System.out.println("Current working dir:" + cwd);
+       URL[] urls = loader.getURLs();
+       for(int i=0; i<urls.length; ++i) {
+           System.out.println("URL: " + urls[i]);
+       }
+       // create an in-memory stream that logging can output to.
+       OutputStream logBuffer = new ByteArrayOutputStream();
+       PrintStream logStream = new PrintStream(logBuffer);
+
+       // Check that the factory class that LogFactory binds to is in fact the
+       // test factory. Then set the destination stream for logging generated
+       // by the test factory loggers.
+       Class logFactoryClass = loader.loadClass(LogFactory.class.getName());
+       Method getFactoryMethod = logFactoryClass.getMethod("getFactory", (Class[]) null);
+       Object factory = getFactoryMethod.invoke(null, (Object[]) null);
+       assertTrue(
+               factory.getClass().getName()
+               .equals("org.apache.commons.logging.impl.TestFactory"));
+       Method setStreamMethod = factory.getClass().getMethod(
+               "setStream", new Class[] {PrintStream.class});
+       setStreamMethod.invoke(factory, new Object[] {logStream});
+
+       // ok, now get some code in the loader to execute some log calls
+       // and see if they appear in our in-memory buffer.
+       Class logTesterClass = loader.loadClass(LogTester.class.getName());
+       Object logTester = logTesterClass.newInstance();
+       
+       String logOutput = logBuffer.toString();
+       assertTrue("No log output", logOutput.length() > 0);
+       assertEquals("Unexpected log output", "INFO: LogTester test message\n", logOutput);
+   }
+   
+   /**
+    * Test what happens when the core jar is not in the classpath.
+    */
+   public void testNoCore() {
+   }
+   
+   /**
+    * Test what happens when no LogFactory implementation is in the classpath.
+    */
+   public void testNoLogFactory() {
+   }
+   
+   /**
+    * Test what happens when the core jar is deployed in both a parent
+    * and a child classloader. A warning should be reported....
+    */
+   public void testMultipleCore() {
+   }
+   
+   /**
+    * Given the possible factors:
+    * <ul>
+    * <li>spi is in parent or child</li>
+    * <li>adapter is in parent or child</li>
+    * <li>caller is in parent or child</li>
+    * <li>context is parent or child</li>
+    * <li>child classloader policy is child-first or parent-first</li>
+    * </ul>
+    * <p>
+    * Actually, we should iterate over all possible combinations of the
+    * above factors, and for each assert whether logging should have
+    * worked or not (and which logging got bound to!).
+    */
+   public void testClassloaderCombinations() {
+   }
+}

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

Added: jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ChildFirstClassLoader.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ChildFirstClassLoader.java?rev=178062&view=auto
==============================================================================
--- jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ChildFirstClassLoader.java
(added)
+++ jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ChildFirstClassLoader.java
Mon May 23 19:41:04 2005
@@ -0,0 +1,54 @@
+/* $Id$
+*
+* 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;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+/**
+ * (Rather slack) implementation of a child first classloader.
+ * Should be fit for the purpose intended (which is demonstration)
+ * but a more complete and robust implementation should be
+ * preferred for more general purposes.
+ */
+public class ChildFirstClassLoader extends URLClassLoader {
+
+    public ChildFirstClassLoader(URL[] urls, ClassLoader parent) {
+        super(urls, parent);
+    }
+
+    protected synchronized Class loadClass(String name, boolean resolve)
+            throws ClassNotFoundException {
+        
+        // very basic implementation
+        Class result = findLoadedClass(name);
+        if (result == null) {
+            try {
+                result = findClass(name);
+                if (resolve) {
+                    resolveClass(result);
+                }
+            } catch (ClassNotFoundException e) {
+                result = super.loadClass(name, resolve);
+            }
+        } 
+        
+        return result;
+    }
+
+}

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

Added: jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/LogTester.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/LogTester.java?rev=178062&view=auto
==============================================================================
--- jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/LogTester.java
(added)
+++ jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/LogTester.java
Mon May 23 19:41:04 2005
@@ -0,0 +1,32 @@
+/* $Id$
+*
+* 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;
+
+/**
+ * @author simon
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+public class LogTester {
+
+    public LogTester() {
+        Log log = LogFactory.getLog("my.category");
+        log.info("LogTester test message");
+    }
+}

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

Added: jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ParentFirstClassLoader.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ParentFirstClassLoader.java?rev=178062&view=auto
==============================================================================
--- jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ParentFirstClassLoader.java
(added)
+++ jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/ParentFirstClassLoader.java
Mon May 23 19:41:04 2005
@@ -0,0 +1,33 @@
+/* $Id$
+*
+* 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;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+/**
+ * Trivial class to make addURL method of URLClassLoader public.
+ */
+public class ParentFirstClassLoader extends URLClassLoader {
+    public ParentFirstClassLoader(ClassLoader parent) {
+        super(new URL[] {}, parent);
+    }
+    public void addURL(URL url) {
+        super.addURL(url);
+    }
+}

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

Added: jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/TestConstants.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/TestConstants.java?rev=178062&view=auto
==============================================================================
--- jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/TestConstants.java
(added)
+++ jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/TestConstants.java
Mon May 23 19:41:04 2005
@@ -0,0 +1,35 @@
+/* $Id$
+*
+* 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;
+
+/**
+ * This class contains constants used by other TestCase classes.
+ */
+
+public class TestConstants {
+    /**
+     * The directory containing the various jar files.
+     */
+    public static final String JAR_DIR = "target";
+
+    /**
+     * The name of the jar file containing the"core" jcl classes.
+     */
+    public static final String CORE_JAR = "commons-logging-core.jar";
+    public static final String TEST_JAR = "commons-logging-test.jar";
+}

Propchange: jakarta/commons/proper/logging/branches/simon-1.1/src/test/org/apache/commons/logging/TestConstants.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