vmassol 2003/02/23 10:05:20
Modified: framework/src/java/share/org/apache/cactus
AbstractClientTestCase.java ServletTestCase.java
AbstractWebClientTestCase.java
AbstractWebServerTestCase.java
HttpServiceDefinition.java
samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit
TestShareAll.java
framework/src/java/share/org/apache/cactus/server
AbstractWebTestCaller.java
documentation/docs/xdocs changes.xml
Added: framework/src/java/share/org/apache/cactus
ServletTestSuite.java AbstractTestSuite.java
samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit
TestJUnitTestCaseWrapper.java
Log:
<action dev="VMA" type="add">
Added support for running pure JUnit TestCase on the server side
using Cactus. This is possible by using a new
<code>ServletTestSuite</code> Test Suite.
</action>
Example:
public class TestJUnitTestCaseWrapper extends TestCase
{
public TestJUnitTestCaseWrapper(String theName)
{
super(theName);
}
public static Test suite()
{
ServletTestSuite suite = new ServletTestSuite();
suite.addTestSuite(TestJUnitTestCaseWrapper.class);
return suite;
}
public void testXXX()
{
}
}
Revision Changes Path
1.3 +34 -4
jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractClientTestCase.java
Index: AbstractClientTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractClientTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AbstractClientTestCase.java 22 Feb 2003 11:31:49 -0000 1.2
+++ AbstractClientTestCase.java 23 Feb 2003 18:05:19 -0000 1.3
@@ -60,6 +60,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import junit.framework.Test;
import junit.framework.TestCase;
import org.apache.cactus.client.initialization.ClientInitializer;
@@ -141,14 +142,42 @@
private Configuration configuration;
/**
+ * JUnit Test Case to wrap. This is needed to support running pure
+ * JUnit Test Case using Cactus.
+ */
+ private Test wrappedTest;
+
+ /**
* Constructs a JUnit test case with the given name.
*
- * @param theName the name of the test case
+ * @param theName the name of the test
*/
public AbstractClientTestCase(String theName)
{
super(theName);
this.setCurrentTestMethod(JUnitVersionHelper.getTestCaseName(this));
+ this.wrappedTest = this;
+ }
+
+ /**
+ * Wraps a standard JUnit Test Case in a Cactus Test Case.
+ *
+ * @param theName the name of the test
+ * @param theTest the Test Case class to wrap
+ * @since 1.5
+ */
+ public AbstractClientTestCase(String theName, Test theTest)
+ {
+ this(theName);
+ this.wrappedTest = theTest;
+ }
+
+ /**
+ * @return the wrapped Test Case
+ */
+ protected Test getWrappedTest()
+ {
+ return this.wrappedTest;
}
/**
@@ -292,7 +321,7 @@
{
// First, verify if a begin method exist. If one is found, verify if
// it has the correct signature. If not, send a warning.
- Method[] methods = getClass().getMethods();
+ Method[] methods = getWrappedTest().getClass().getMethods();
for (int i = 0; i < methods.length; i++)
{
@@ -335,7 +364,8 @@
try
{
- methods[i].invoke(this, new Object[] {theRequest});
+ methods[i].invoke(getWrappedTest(),
+ new Object[] {theRequest});
break;
}
1.10 +15 -1
jakarta-cactus/framework/src/java/share/org/apache/cactus/ServletTestCase.java
Index: ServletTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/ServletTestCase.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- ServletTestCase.java 22 Feb 2003 12:01:51 -0000 1.9
+++ ServletTestCase.java 23 Feb 2003 18:05:19 -0000 1.10
@@ -59,6 +59,8 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
+import junit.framework.Test;
+
import org.apache.cactus.configuration.Configuration;
import org.apache.cactus.configuration.ServletConfiguration;
import org.apache.cactus.server.wrapper.ServletConfigWrapper;
@@ -120,6 +122,18 @@
super(theName);
}
+ /**
+ * Wraps a standard JUnit Test Case in a Cactus Test Case.
+ *
+ * @param theName the name of the test
+ * @param theTest the Test Case class to wrap
+ * @since 1.5
+ */
+ public ServletTestCase(String theName, Test theTest)
+ {
+ super(theName, theTest);
+ }
+
/**
* @see AbstractClientTestCase#createConfiguration()
*/
1.2 +18 -3
jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractWebClientTestCase.java
Index: AbstractWebClientTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractWebClientTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractWebClientTestCase.java 21 Feb 2003 10:49:17 -0000 1.1
+++ AbstractWebClientTestCase.java 23 Feb 2003 18:05:19 -0000 1.2
@@ -62,6 +62,8 @@
import java.net.HttpURLConnection;
+import junit.framework.Test;
+
import org.apache.cactus.client.ClientException;
import org.apache.cactus.client.WebResponseObjectFactory;
@@ -87,6 +89,18 @@
}
/**
+ * Wraps a standard JUnit Test Case in a Cactus Test Case.
+ *
+ * @param theName the name of the test
+ * @param theTest the Test Case class to wrap
+ * @since 1.5
+ */
+ public AbstractWebClientTestCase(String theName, Test theTest)
+ {
+ super(theName, theTest);
+ }
+
+ /**
* Call the global end method. This is the method that is called after
* each test if it exists. It is called on the client side only.
*
@@ -110,7 +124,7 @@
Method methodToCall = null;
Object paramObject = null;
- Method[] methods = getClass().getMethods();
+ Method[] methods = getWrappedTest().getClass().getMethods();
for (int i = 0; i < methods.length; i++)
{
@@ -177,7 +191,8 @@
{
try
{
- methodToCall.invoke(this, new Object[] {paramObject});
+ methodToCall.invoke(getWrappedTest(),
+ new Object[] {paramObject});
}
catch (InvocationTargetException e)
{
1.5 +29 -7
jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractWebServerTestCase.java
Index: AbstractWebServerTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractWebServerTestCase.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- AbstractWebServerTestCase.java 23 Feb 2003 09:21:17 -0000 1.4
+++ AbstractWebServerTestCase.java 23 Feb 2003 18:05:19 -0000 1.5
@@ -61,6 +61,8 @@
import java.lang.reflect.Modifier;
import java.net.HttpURLConnection;
+import junit.framework.Test;
+
import org.apache.cactus.client.connector.http.DefaultHttpClient;
import org.apache.cactus.configuration.WebConfiguration;
import org.apache.commons.logging.LogFactory;
@@ -88,6 +90,18 @@
}
/**
+ * Wraps a standard JUnit Test Case in a Cactus Test Case.
+ *
+ * @param theName the name of the test
+ * @param theTest the Test Case class to wrap
+ * @since 1.5
+ */
+ public AbstractWebServerTestCase(String theName, Test theTest)
+ {
+ super(theName, theTest);
+ }
+
+ /**
* Run the test that was specified in the constructor on the server side,
* calling <code>setUp()</code> and <code>tearDown()</code>.
*
@@ -102,7 +116,7 @@
// side, so we instanciate the log for server side here.
if (getLogger() == null)
{
- setLogger(LogFactory.getLog(this.getClass()));
+ setLogger(LogFactory.getLog(getWrappedTest().getClass()));
}
setUp();
@@ -133,14 +147,14 @@
// methods. getDeclaredMethods returns all
// methods of this class but excludes the
// inherited ones.
- runMethod = getClass().getMethod(this.getCurrentTestMethod(),
- new Class[0]);
+ runMethod = getWrappedTest().getClass().getMethod(
+ this.getCurrentTestMethod(), new Class[0]);
}
catch (NoSuchMethodException e)
{
fail("Method [" + this.getCurrentTestMethod()
- + "()] does not exist for class [" + this.getClass().getName()
- + "].");
+ + "()] does not exist for class ["
+ + getWrappedTest().getClass().getName() + "].");
}
if ((runMethod != null) && !Modifier.isPublic(runMethod.getModifiers()))
@@ -151,7 +165,7 @@
try
{
- runMethod.invoke(this, new Class[0]);
+ runMethod.invoke(getWrappedTest(), new Class[0]);
}
catch (InvocationTargetException e)
{
@@ -227,6 +241,14 @@
theRequest.getAutomaticSession() ? "true" : "false",
WebRequest.GET_METHOD);
+ // Add the wrapped test if it is not equal to our current instance
+ if (getWrappedTest() != this)
+ {
+ theRequest.addParameter(
+ HttpServiceDefinition.WRAPPED_CLASS_NAME_PARAM,
+ getWrappedTest().getClass().getName(), WebRequest.GET_METHOD);
+ }
+
// Add the simulated URL (if one has been defined)
if (theRequest.getURL() != null)
{
1.7 +7 -1
jakarta-cactus/framework/src/java/share/org/apache/cactus/HttpServiceDefinition.java
Index: HttpServiceDefinition.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/HttpServiceDefinition.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- HttpServiceDefinition.java 4 Jan 2003 15:07:47 -0000 1.6
+++ HttpServiceDefinition.java 23 Feb 2003 18:05:19 -0000 1.7
@@ -81,6 +81,12 @@
static String CLASS_NAME_PARAM = "Cactus_TestClass";
/**
+ * Name of the parameter in the HTTP request that represents an optional
+ * Test being wrapped by the class represented by CLASS_NAME_PARAM.
+ */
+ static String WRAPPED_CLASS_NAME_PARAM = "Cactus_WrappedTestClass";
+
+ /**
* Name of the parameter in the HTTP request that represents the name of the
* Test method to call. The name is voluntarily long so that it will not
* clash with a user-defined parameter.
1.1
jakarta-cactus/framework/src/java/share/org/apache/cactus/ServletTestSuite.java
Index: ServletTestSuite.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
* Foundation" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.cactus;
import junit.framework.Test;
/**
* [EMAIL PROTECTED] junit.framework.TestSuite} wrapper that wraps all the tests of
the
* suite in Cactus [EMAIL PROTECTED] ServletTestCase} objects.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
* @version $Id: ServletTestSuite.java,v 1.1 2003/02/23 18:05:19 vmassol Exp $
* @since 1.5
*/
public class ServletTestSuite extends AbstractTestSuite
{
/**
* @see AbstractTestSuite#AbstractTestSuite()
*/
public ServletTestSuite()
{
}
/**
* @see AbstractTestSuite#AbstractTestSuite(Class)
*/
public ServletTestSuite(final Class theClass)
{
super(theClass);
}
/**
* @see AbstractTestSuite#AbstractTestSuite(String)
*/
public ServletTestSuite(String theName)
{
super(theName);
}
/**
* @see AbstractTestSuite#createTestSuite(Class)
*/
protected Test createTestSuite(Class theTestClass)
{
return new ServletTestSuite(theTestClass);
}
/**
* @see AbstractTestSuite#createCactusTestCase(String, Test)
*/
protected Test createCactusTestCase(String theName, Test theTest)
{
return new ServletTestCase(theName, theTest);
}
}
1.1
jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractTestSuite.java
Index: AbstractTestSuite.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
* Foundation" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.cactus;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.Vector;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestResult;
/**
* Test Suite that wraps all the tests of the suite in Cactus Test Case
* objects so that pure JUnit tests can be run on the server side.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
* @version $Id: AbstractTestSuite.java,v 1.1 2003/02/23 18:05:19 vmassol Exp $
* @since 1.5
*/
public abstract class AbstractTestSuite implements Test
{
/**
* Lists of tests to execute (Test objects)
*/
private Vector tests = new Vector(10);
/**
* Name of the current test suite
*/
private String name;
/**
* @see TestSuite#TestSuite()
*/
public AbstractTestSuite()
{
}
/**
* @see TestSuite#TestSuite(Class)
*/
public AbstractTestSuite(final Class theClass)
{
setName(theClass.getName());
Constructor constructor;
try
{
// Avoid generating multiple error messages
constructor = theClass.getConstructor(
new Class[] { String.class });
}
catch (NoSuchMethodException e)
{
addTest(warning("Class " + theClass.getName()
+ " has no public constructor TestCase(String name)"));
return;
}
if (!Modifier.isPublic(theClass.getModifiers()))
{
addTest(warning("Class " + theClass.getName() + " is not public"));
return;
}
Class superClass = theClass;
Vector names = new Vector();
while (Test.class.isAssignableFrom(superClass))
{
Method[] methods = superClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++)
{
addTestMethod(methods[i], names, constructor);
}
superClass = superClass.getSuperclass();
}
if (this.tests.size() == 0)
{
addTest(warning("No tests found in " + theClass.getName()));
}
}
/**
* @see TestSuite#TestSuite(String)
*/
public AbstractTestSuite(String theName)
{
setName(theName);
}
/**
* @see junit.framework.TestSuite#addTest(Test)
*/
public void addTest(Test theTest)
{
this.tests.addElement(theTest);
}
/**
* @see junit.framework.TestSuite#addTestSuite(Class)
*/
public void addTestSuite(Class theTestClass)
{
addTest(createTestSuite(theTestClass));
}
/**
* @see junit.framework.TestSuite#addTestMethod(Method, Vector, Constructor)
*/
private void addTestMethod(Method theMethod, Vector theNames,
Constructor theConstructor)
{
String name = theMethod.getName();
if (theNames.contains(name))
{
return;
}
if (isPublicTestMethod(theMethod))
{
theNames.addElement(name);
Object[] args = new Object[] { name };
try
{
// Note: We wrap the Test in a Cactus Test Case
addTest(new ServletTestCase(name,
(Test) theConstructor.newInstance(args)));
}
catch (InstantiationException e)
{
addTest(warning("Cannot instantiate test case: " + name
+ " (" + exceptionToString(e) + ")"));
}
catch (InvocationTargetException e)
{
addTest(warning("Exception in constructor: " + name + " ("
+ exceptionToString(e.getTargetException()) + ")"));
}
catch (IllegalAccessException e)
{
addTest(warning("Cannot access test case: " + name + " ("
+ exceptionToString(e) + ")"));
}
}
else
{
// Almost a test method
if (isTestMethod(theMethod))
{
addTest(warning("Test method isn't public: "
+ theMethod.getName()));
}
}
}
/**
* @see junit.framework.TestSuite#exceptionToString(Throwable)
*/
private String exceptionToString(Throwable theThrowable)
{
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
theThrowable.printStackTrace(writer);
return stringWriter.toString();
}
/**
* @see junit.framework.TestSuite#countTestCases()
*/
public int countTestCases()
{
int count = 0;
for (Enumeration e = tests(); e.hasMoreElements();)
{
Test test = (Test) e.nextElement();
count = count + test.countTestCases();
}
return count;
}
/**
* @see junit.framework.TestSuite#isPublicTestMethod(Method)
*/
private boolean isPublicTestMethod(Method theMethod)
{
return isTestMethod(theMethod)
&& Modifier.isPublic(theMethod.getModifiers());
}
/**
* @see junit.framework.TestSuite#isTestMethod(Method)
*/
private boolean isTestMethod(Method theMethod)
{
String name = theMethod.getName();
Class[] parameters = theMethod.getParameterTypes();
Class returnType = theMethod.getReturnType();
return parameters.length == 0
&& name.startsWith("test")
&& returnType.equals(Void.TYPE);
}
/**
* @see junit.framework.TestSuite#run(TestResult)
*/
public void run(TestResult theResult)
{
for (Enumeration e = tests(); e.hasMoreElements();)
{
if (theResult.shouldStop())
{
break;
}
Test test = (Test) e.nextElement();
runTest(test, theResult);
}
}
/**
* @see junit.framework.TestSuite#runTest(Test, TestResult)
*/
public void runTest(Test theTest, TestResult theResult)
{
theTest.run(theResult);
}
/**
* @see junit.framework.TestSuite#testAt(int)
*/
public Test testAt(int theIndex)
{
return (Test) this.tests.elementAt(theIndex);
}
/**
* @see junit.framework.TestSuite#testCount()
*/
public int testCount()
{
return this.tests.size();
}
/**
* @see junit.framework.TestSuite#tests()
*/
public Enumeration tests()
{
return this.tests.elements();
}
/**
* @see junit.framework.TestSuite#toString()
*/
public String toString()
{
if (getName() != null)
{
return getName();
}
return super.toString();
}
/**
* @see junit.framework.TestSuite#setName()
*/
public void setName(String theName)
{
this.name = theName;
}
/**
* @see junit.framework.TestSuite#getName()
*/
public String getName()
{
return this.name;
}
/**
* @see junit.framework.TestSuite#warning(String)
*/
private static Test warning(final String theMessage)
{
return new TestCase("warning")
{
protected void runTest()
{
fail(theMessage);
}
};
}
protected abstract Test createTestSuite(Class theTestClass);
protected abstract Test createCactusTestCase(String theName, Test theTest);
}
1.2 +3 -2
jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestShareAll.java
Index: TestShareAll.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestShareAll.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestShareAll.java 8 Feb 2003 11:17:09 -0000 1.1
+++ TestShareAll.java 23 Feb 2003 18:05:20 -0000 1.2
@@ -109,7 +109,8 @@
suite.addTestSuite(TestHttpHeaders.class);
suite.addTestSuite(TestHttpRequest.class);
suite.addTestSuite(TestServletConfig.class);
-
+ suite.addTest(TestJUnitTestCaseWrapper.suite());
+
// JspTestCase related tests
suite.addTestSuite(TestJspOut.class);
1.1
jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestJUnitTestCaseWrapper.java
Index: TestJUnitTestCaseWrapper.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.cactus.sample.unit;
import org.apache.cactus.ServletTestSuite;
import junit.framework.Test;
import junit.framework.TestCase;
/**
* Pure JUnit Test Case that we run on the server side using Cactus, by
* using a [EMAIL PROTECTED] ServletTestSuite}.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
* @version $Id: TestJUnitTestCaseWrapper.java,v 1.1 2003/02/23 18:05:20 vmassol Exp
$
*/
public class TestJUnitTestCaseWrapper extends TestCase
{
/**
* Defines the testcase name for JUnit.
*
* @param theName the testcase's name.
*/
public TestJUnitTestCaseWrapper(String theName)
{
super(theName);
}
/**
* Runs this pure JUnit Test Case with Cactus, wrapping it in
* a Servlet Test Case.
*
* @return the test suite containing all tests to run
*/
public static Test suite()
{
ServletTestSuite suite = new ServletTestSuite();
suite.addTestSuite(TestJUnitTestCaseWrapper.class);
return suite;
}
//-------------------------------------------------------------------------
/**
* No-op test just to verify that pure JUnit tests can be executed on the
* server side using Cactus.
*/
public void setUp()
{
// This test is executed on the server side.
}
/**
* No-op test just to verify that pure JUnit tests can be executed on the
* server side using Cactus.
*/
public void testXXX()
{
// This test is executed on the server side.
}
/**
* No-op test just to verify that pure JUnit tests can be executed on the
* server side using Cactus.
*/
public void tearDown()
{
// This test is executed on the server side.
}
}
1.16 +103 -11
jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractWebTestCaller.java
Index: AbstractWebTestCaller.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/server/AbstractWebTestCaller.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- AbstractWebTestCaller.java 22 Feb 2003 11:31:50 -0000 1.15
+++ AbstractWebTestCaller.java 23 Feb 2003 18:05:20 -0000 1.16
@@ -63,6 +63,8 @@
import javax.servlet.ServletException;
+import junit.framework.Test;
+
import org.apache.cactus.AbstractWebServerTestCase;
import org.apache.cactus.HttpServiceDefinition;
import org.apache.cactus.ServiceEnumeration;
@@ -143,7 +145,8 @@
{
// Create an instance of the test class
AbstractWebServerTestCase testInstance = getTestClassInstance(
- getTestClassName(), getTestMethodName());
+ getTestClassName(), getWrappedTestClassName(),
+ getTestMethodName());
// Set its fields (implicit objects)
setTestCaseFields(testInstance);
@@ -277,7 +280,32 @@
throw new ServletException(message);
}
- LOGGER.debug("Class to call = " + className);
+ LOGGER.debug("Class to call = [" + className + "]");
+
+ return className;
+ }
+
+ /**
+ * @return the optional test class that is wrapped by a Cactus test case,
+ * extracted from the HTTP request
+ * @exception ServletException if the wrapped class name is missing from
+ * the HTTP request
+ */
+ protected String getWrappedTestClassName() throws ServletException
+ {
+ String queryString = this.webImplicitObjects.getHttpServletRequest()
+ .getQueryString();
+ String className = ServletUtil.getQueryStringParameter(queryString,
+ HttpServiceDefinition.WRAPPED_CLASS_NAME_PARAM);
+
+ if (className == null)
+ {
+ LOGGER.debug("No wrapped test class");
+ }
+ else
+ {
+ LOGGER.debug("Wrapped test class = [" + className + "]");
+ }
return className;
}
@@ -331,6 +359,8 @@
/**
* @param theClassName the name of the test class
+ * @param theWrappedClassName the name of the wrapped test class. Can be
+ * null if there is none
* @param theTestCaseName the name of the current test case
* @return an instance of the test class to call
* @exception ServletException if the test case instance for the current
@@ -338,24 +368,48 @@
* information is missing from the HTTP request)
*/
protected AbstractWebServerTestCase getTestClassInstance(
- String theClassName, String theTestCaseName) throws ServletException
+ String theClassName, String theWrappedClassName,
+ String theTestCaseName) throws ServletException
{
// Get the class to call and build an instance of it.
Class testClass = getTestClassClass(theClassName);
AbstractWebServerTestCase testInstance = null;
-
+ Constructor constructor;
+
try
{
- Constructor constructor = testClass.getConstructor(
- new Class[] {String.class});
-
- testInstance = (AbstractWebServerTestCase) constructor.newInstance(
- new Object[] {theTestCaseName});
+ if (theWrappedClassName == null)
+ {
+ constructor = testClass.getConstructor(
+ new Class[] {String.class});
+
+ testInstance =
+ (AbstractWebServerTestCase) constructor.newInstance(
+ new Object[] {theTestCaseName});
+ }
+ else
+ {
+ Class wrappedTestClass =
+ getWrappedTestClassClass(theWrappedClassName);
+ Constructor wrappedConstructor =
+ wrappedTestClass.getConstructor(
+ new Class[] {String.class});
+ Test wrappedTestInstance =
+ (Test) wrappedConstructor.newInstance(
+ new Object[] {theTestCaseName});
+
+ constructor = testClass.getConstructor(
+ new Class[] {String.class, Test.class});
+
+ testInstance =
+ (AbstractWebServerTestCase) constructor.newInstance(
+ new Object[] {theTestCaseName, wrappedTestInstance});
+ }
}
catch (Exception e)
{
- String message = "Error instantiating class [" + theClassName + "("
- + theTestCaseName + ")]";
+ String message = "Error instantiating class [" + theClassName + "(["
+ + theTestCaseName + "], [" + theWrappedClassName + "])]";
LOGGER.error(message, e);
throw new ServletException(message, e);
@@ -400,4 +454,42 @@
return testClass;
}
+
+ /**
+ * @param theWrappedClassName the name of the wrapped test class
+ * @return the class object to wrapped test class
+ * @exception ServletException if the class of the wrapped test case
+ * cannot be loaded in memory (i.e. it is not in the
+ * classpath)
+ */
+ protected Class getWrappedTestClassClass(String theWrappedClassName)
+ throws ServletException
+ {
+ // Get the class to call and build an instance of it.
+ Class testClass = null;
+
+ try
+ {
+ testClass = ClassLoaderUtils.loadClass(theWrappedClassName,
+ this.getClass());
+ }
+ catch (Exception e)
+ {
+ String message = "Error finding class [" + theWrappedClassName
+ + "] using both the Context classloader and the webapp "
+ + "classloader. Possible causes include:\r\n";
+
+ message += ("\t- Your webapp does not include your test "
+ + "classes,\r\n");
+ message += ("\t- The cactus.jar is not located in your "
+ + "WEB-INF/lib directory and your Container has not set the "
+ + "Context classloader to point to the webapp one");
+
+ LOGGER.error(message, e);
+ throw new ServletException(message, e);
+ }
+
+ return testClass;
+ }
+
}
1.82 +5 -0 jakarta-cactus/documentation/docs/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/changes.xml,v
retrieving revision 1.81
retrieving revision 1.82
diff -u -r1.81 -r1.82
--- changes.xml 22 Feb 2003 22:27:02 -0000 1.81
+++ changes.xml 23 Feb 2003 18:05:20 -0000 1.82
@@ -57,6 +57,11 @@
</devs>
<release version="1.5" date="- in CVS">
+ <action dev="VMA" type="add">
+ Added support for running pure JUnit TestCase on the server side
+ using Cactus. This is possible by using a new
+ <code>ServletTestSuite</code> Test Suite.
+ </action>
<action dev="VMA" type="fix" due-to="Helen Rehn" due-to-email="[EMAIL
PROTECTED]">
Fixed bug where a simulation URL would be used even when none has
been defined (Reminder: a simulation URL is defined by calling
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]