vmassol 2003/07/12 12:31:41
Modified: framework/src/test/share/org/apache/cactus
TestAbstractTestCaseInterceptorTestCase.java
framework/src/java/share/org/apache/cactus
ServletTestCase.java JspTestCase.java
framework/src/java/j2ee13/org/apache/cactus
FilterTestCase.java
framework/src/java/share/org/apache/cactus/server
AbstractWebTestCaller.java
samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit
TestGlobalBeginEnd.java
Added: framework/src/java/share/org/apache/cactus/internal/client
WebClientTestCaseDelegate.java
ClientTestCaseDelegate.java
framework/src/java/share/org/apache/cactus/internal/server
ServerTestCaseDelegate.java
Removed: framework/src/java/share/org/apache/cactus/internal/client
ClientTestCaseDelegator.java
WebClientTestCaseDelegator.java
framework/src/java/share/org/apache/cactus/internal/server
ServerTestCaseWrapper.java
Log:
- Renamed *Delegator in *Delegate
- Last bout of refactoring to support delegates instead of class hierarchies.
Everything is working fine on my machine now. Back to normal.
Revision Changes Path
1.1
jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/client/WebClientTestCaseDelegate.java
Index: WebClientTestCaseDelegate.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.internal.client;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.HttpURLConnection;
import junit.framework.Test;
import org.apache.cactus.RequestDirectives;
import org.apache.cactus.WebRequest;
import org.apache.cactus.client.ClientException;
import org.apache.cactus.client.WebResponseObjectFactory;
import org.apache.cactus.client.connector.http.DefaultHttpClient;
import org.apache.cactus.configuration.Configuration;
import org.apache.cactus.configuration.WebConfiguration;
/**
* Delegator extension to support test cases using the HTTP protocol. It adds
* support for end methods (as they are dependent on the protocol used, which
* is HTTP here).
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
* @version $Id: WebClientTestCaseDelegate.java,v 1.1 2003/07/12 19:31:40 vmassol
Exp $
*/
public class WebClientTestCaseDelegate extends ClientTestCaseDelegate
{
/**
* @param theDelegatedTest the test we are delegating for
* @param theWrappedTest the test being wrapped by this delegator (or null
* if none)
* @param theConfiguration the configuration to use
*/
public WebClientTestCaseDelegate(Test theDelegatedTest,
Test theWrappedTest, Configuration theConfiguration)
{
super(theDelegatedTest, theWrappedTest, theConfiguration);
}
/**
* 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.
*
* @param theRequest the request data that were used to open the
* connection.
* @param theConnection the <code>HttpURLConnection</code> that was used
* to open the connection to the redirection servlet. The response
* codes, headers, cookies can be checked using the get methods of
* this object.
* @param theMethodName the name of the end method to call
* @param theResponse the Response object if it exists. Can be null in
* which case it is created from the HttpURLConnection
* @return the created WebReponse object (either Cactus or HttpClient)
* @exception Throwable any error that occurred when calling the end method
* for the current test case.
*/
private Object callGenericEndMethod(WebRequest theRequest,
HttpURLConnection theConnection, String theMethodName,
Object theResponse) throws Throwable
{
Method methodToCall = null;
Object paramObject = null;
Method[] methods = getWrappedTest().getClass().getMethods();
for (int i = 0; i < methods.length; i++)
{
if (methods[i].getName().equals(theMethodName))
{
// Check return type
if (!methods[i].getReturnType().getName().equals("void"))
{
fail("The method [" + methods[i].getName()
+ "] should return void and not ["
+ methods[i].getReturnType().getName() + "]");
}
// Check if method is public
if (!Modifier.isPublic(methods[i].getModifiers()))
{
fail("Method [" + methods[i].getName()
+ "] should be declared public");
}
// Check parameters
Class[] parameters = methods[i].getParameterTypes();
// Verify only one parameter is defined
if (parameters.length != 1)
{
fail("The method [" + methods[i].getName()
+ "] must only have a single parameter");
}
paramObject = theResponse;
if (paramObject == null)
{
try
{
paramObject = new WebResponseObjectFactory()
.getResponseObject(parameters[0].getName(),
theRequest, theConnection);
}
catch (ClientException e)
{
throw new ClientException("The method ["
+ methods[i].getName()
+ "] has a bad parameter of type ["
+ parameters[0].getName() + "]", e);
}
}
// Has a method to call already been found ?
if (methodToCall != null)
{
fail("There can only be one method ["
+ methods[i].getName() + "] per test case. "
+ "Test case [" + this.getCurrentTestName()
+ "] has two at least !");
}
methodToCall = methods[i];
}
}
if (methodToCall != null)
{
try
{
methodToCall.invoke(getWrappedTest(),
new Object[] {paramObject});
}
catch (InvocationTargetException e)
{
e.fillInStackTrace();
throw e.getTargetException();
}
catch (IllegalAccessException e)
{
e.fillInStackTrace();
throw e;
}
}
return paramObject;
}
/**
* Call the client tear down up method if it exists.
*
* @param theRequest the request data that were used to open the
* connection.
* @param theConnection the <code>HttpURLConnection</code> that was used
* to open the connection to the redirection servlet. The response
* codes, headers, cookies can be checked using the get methods of
* this object.
* @param theResponse the Response object if it exists. Can be null in
* which case it is created from the HttpURLConnection
* @exception Throwable any error that occurred when calling the method
*/
protected void callClientGlobalEnd(WebRequest theRequest,
HttpURLConnection theConnection, Object theResponse) throws Throwable
{
callGenericEndMethod(theRequest, theConnection,
CLIENT_GLOBAL_END_METHOD, theResponse);
}
/**
* Call the test case end method
*
* @param theRequest the request data that were used to open the
* connection.
* @param theConnection the <code>HttpURLConnection</code> that was used
* to open the connection to the redirection servlet. The response
* codes, headers, cookies can be checked using the get methods of
* this object.
* @return the created WebReponse object (either Cactus or HttpClient)
* @exception Throwable any error that occurred when calling the end method
* for the current test case.
*/
public Object callEndMethod(WebRequest theRequest,
HttpURLConnection theConnection) throws Throwable
{
return callGenericEndMethod(theRequest, theConnection,
getEndMethodName(), null);
}
/**
* Runs a test case. This method is overriden from the JUnit
* <code>TestCase</code> class in order to seamlessly call the
* Cactus redirection servlet.
*
* @exception Throwable if any error happens during the execution of
* the test
*/
public void runTest() throws Throwable
{
runGenericTest(new DefaultHttpClient(
(WebConfiguration) getConfiguration()));
}
/**
* Execute the test case begin method, then connect to the server proxy
* redirector (where the test case test method is executed) and then
* executes the test case end method.
*
* @param theHttpClient the HTTP client class to use to connect to the
* proxy redirector.
* @exception Throwable any error that occurred when calling the test method
* for the current test case.
*/
protected void runGenericTest(DefaultHttpClient theHttpClient)
throws Throwable
{
WebRequest request = new WebRequest(
(WebConfiguration) getConfiguration());
// Call the set up and begin methods to fill the request object
callClientGlobalBegin(request);
callBeginMethod(request);
// Run the web test
HttpURLConnection connection = runWebTest(request, theHttpClient);
// Call the end method
Object response = callEndMethod(request, connection);
// call the tear down method
callClientGlobalEnd(request, connection, response);
// Close the input stream (just in the case the user has not done it
// in it's endXXX method (or if he has no endXXX method) ....
connection.getInputStream().close();
}
/**
* Run the web test by connecting to the server proxy
* redirector (where the test case test method is executed).
*
* @param theRequest the request object which will contain data that will
* be used to connect to the Cactus server side redirectors.
* @param theHttpClient the HTTP client class to use to connect to the
* proxy redirector.
* @return the HTTP connection object that was used to call the server side
* @exception Throwable any error that occurred when calling the test method
* for the current test case.
*/
private HttpURLConnection runWebTest(
WebRequest theRequest,
DefaultHttpClient theHttpClient)
throws Throwable
{
// Add the class name, the method name, to the request to simulate and
// automatic session creation flag to the request
RequestDirectives directives = new RequestDirectives(theRequest);
directives.setClassName(getDelegatedTest().getClass().getName());
directives.setMethodName(getCurrentTestName());
directives.setAutoSession(
theRequest.getAutomaticSession() ? "true" : "false");
// Add the wrapped test if it is not equal to our current instance
if (isWrappingATest())
{
directives.setWrappedTestName(getWrappedTestName());
}
// Add the simulated URL (if one has been defined)
if (theRequest.getURL() != null)
{
theRequest.getURL().saveToRequest(theRequest);
}
// Open the HTTP connection to the servlet redirector
// and manage errors that could be returned in the
// HTTP response.
HttpURLConnection connection = theHttpClient.doTest(theRequest);
return connection;
}
}
1.1
jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/client/ClientTestCaseDelegate.java
Index: ClientTestCaseDelegate.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.internal.client;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import junit.framework.Assert;
import junit.framework.Test;
import org.apache.cactus.Request;
import org.apache.cactus.WebRequest;
import org.apache.cactus.client.initialization.ClientInitializer;
import org.apache.cactus.configuration.Configuration;
import org.apache.cactus.util.JUnitVersionHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Delegate class that provides useful methods for the Cactus
* <code>XXXTestCase</code> classes. All the methods provided are independent
* of any communication protocol between client side and server side (HTTP,
* JMS, etc). Subclasses will define additional behaviour that depends on the
* protocol.
*
* It provides the ability to run common code before each test on the client
* side (note that calling common tear down code is delegated to child classes
* as the method signature depends on the protocol used).
*
* In addition it provides the ability to execute some one time (per-JVM)
* initialisation code (a pity this is not provided in JUnit). It can be
* useful to start an embedded server for example. Note: In the future this
* should be refatored and provided using a custom JUnit TestSuite.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
* @version $Id: ClientTestCaseDelegate.java,v 1.1 2003/07/12 19:31:40 vmassol Exp $
*/
public class ClientTestCaseDelegate extends Assert
{
/**
* The prefix of a test method.
*/
protected static final String TEST_METHOD_PREFIX = "test";
/**
* The prefix of a begin test method.
*/
protected static final String BEGIN_METHOD_PREFIX = "begin";
/**
* The prefix of an end test method.
*/
protected static final String END_METHOD_PREFIX = "end";
/**
* The name of the method that is called before each test on the client
* side (if it exists).
*/
protected static final String CLIENT_GLOBAL_BEGIN_METHOD = "begin";
/**
* The name of the method that is called after each test on the client
* side (if it exists).
*/
protected static final String CLIENT_GLOBAL_END_METHOD = "end";
/**
* Flag used to verify if client initialization has already been performed
* for the current test suite or not.
*/
private static boolean isClientInitialized;
/**
* The logger (only used on the client side).
*/
private Log logger;
/**
* The Cactus configuration.
*/
private Configuration configuration;
/**
* Pure JUnit Test Case that we are wrapping (if any)
*/
private Test wrappedTest;
/**
* The test we are delegating for.
*/
private Test delegatedTest;
/**
* @param theDelegatedTest the test we are delegating for
* @param theWrappedTest the test being wrapped by this delegate (or null
* if none)
* @param theConfiguration the configuration to use
*/
public ClientTestCaseDelegate(Test theDelegatedTest,
Test theWrappedTest, Configuration theConfiguration)
{
if (theDelegatedTest == null)
{
throw new IllegalStateException(
"The test object passed must not be null");
}
setDelegatedTest(theDelegatedTest);
setWrappedTest(theWrappedTest);
setConfiguration(theConfiguration);
}
/**
* @param theWrappedTest the pure JUnit test that we need to wrap
*/
public void setWrappedTest(Test theWrappedTest)
{
this.wrappedTest = theWrappedTest;
}
/**
* @param theDelegatedTest the test we are delegating for
*/
public void setDelegatedTest(Test theDelegatedTest)
{
this.delegatedTest = theDelegatedTest;
}
/**
* @return the wrapped JUnit test
*/
public Test getWrappedTest()
{
return this.wrappedTest;
}
/**
* @return the test we are delegating for
*/
public Test getDelegatedTest()
{
return this.delegatedTest;
}
/**
* @return The logger used by the <code>TestCase</code> class and
* subclasses to perform logging.
*/
public final Log getLogger()
{
return this.logger;
}
/**
* @param theLogger the logger to use
*/
protected void setLogger(Log theLogger)
{
this.logger = theLogger;
}
/**
* @return the Cactus configuration
*/
public Configuration getConfiguration()
{
return this.configuration;
}
/**
* Sets the Cactus configuration
*
* @param theConfiguration the Cactus configuration
*/
public void setConfiguration(Configuration theConfiguration)
{
this.configuration = theConfiguration;
}
/**
* @return the name of the test method to call without the
* TEST_METHOD_PREFIX prefix
*/
private String getBaseMethodName()
{
// Sanity check
if (!getCurrentTestName().startsWith(TEST_METHOD_PREFIX))
{
throw new RuntimeException("bad name ["
+ getCurrentTestName()
+ "]. It should start with ["
+ TEST_METHOD_PREFIX + "].");
}
return getCurrentTestName().substring(
TEST_METHOD_PREFIX.length());
}
/**
* @return the name of the test begin method to call that initialize the
* test by initializing the <code>WebRequest</code> object
* for the test case.
*/
protected String getBeginMethodName()
{
return BEGIN_METHOD_PREFIX + getBaseMethodName();
}
/**
* @return the name of the test end method to call when the test has been
* run on the server. It can be used to verify returned headers,
* cookies, ...
*/
protected String getEndMethodName()
{
return END_METHOD_PREFIX + getBaseMethodName();
}
/**
* Perform client side initializations before each test, such as
* re-initializating the logger and printing some logging information.
*/
public void runBareInit()
{
// We make sure we reinitialize The logger with the name of the
// current extending class so that log statements will contain the
// actual class name (that's why the logged instance is not static).
this.logger = LogFactory.getLog(this.getClass());
// Initialize client side configuration
if (!isClientInitialized)
{
// Call client side initializer (if defined). It will be called
// only once per JVM.
ClientInitializer.initialize(getConfiguration());
}
// Mark beginning of test on client side
getLogger().debug("------------- Test: "
+ this.getCurrentTestName());
}
/**
* Call a begin method which takes Cactus WebRequest as parameter
*
* @param theRequest the request object which will contain data that will
* be used to connect to the Cactus server side redirectors.
* @param theMethodName the name of the begin method to call
* @exception Throwable any error that occurred when calling the method
*/
private void callGenericBeginMethod(Request theRequest,
String theMethodName) throws Throwable
{
// 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 = getWrappedTest().getClass().getMethods();
for (int i = 0; i < methods.length; i++)
{
if (methods[i].getName().equals(theMethodName))
{
// Check return type
if (!methods[i].getReturnType().getName().equals("void"))
{
fail("The method [" + methods[i].getName()
+ "] should return void and not ["
+ methods[i].getReturnType().getName() + "]");
}
// Check if method is public
if (!Modifier.isPublic(methods[i].getModifiers()))
{
fail("Method [" + methods[i].getName()
+ "] should be declared public");
}
// Check parameters
Class[] parameters = methods[i].getParameterTypes();
if (parameters.length != 1)
{
fail("The method [" + methods[i].getName()
+ "] must accept a single parameter derived from "
+ "class [" + WebRequest.class.getName() + "], "
+ "but " + parameters.length
+ " parameters were found");
}
else if (!theRequest.getClass().isAssignableFrom(parameters[0]))
{
fail("The method [" + methods[i].getName()
+ "] must accept a single parameter derived from "
+ "class [" + theRequest.getClass().getName() + "], "
+ "but found a [" + parameters[0].getName() + "] "
+ "parameter instead");
}
try
{
methods[i].invoke(getWrappedTest(),
new Object[] {theRequest});
break;
}
catch (InvocationTargetException e)
{
e.fillInStackTrace();
throw e.getTargetException();
}
catch (IllegalAccessException e)
{
e.fillInStackTrace();
throw e;
}
}
}
}
/**
* Call the global begin method. This is the method that is called before
* each test if it exists. It is called on the client side only.
*
* @param theRequest the request object which will contain data that will
* be used to connect to the Cactus server side redirectors.
* @exception Throwable any error that occurred when calling the method
*/
protected void callClientGlobalBegin(Request theRequest) throws Throwable
{
callGenericBeginMethod(theRequest, CLIENT_GLOBAL_BEGIN_METHOD);
}
/**
* Call the test case begin method.
*
* @param theRequest the request object to pass to the begin method.
* @exception Throwable any error that occurred when calling the begin
* method for the current test case.
*/
public void callBeginMethod(Request theRequest) throws Throwable
{
callGenericBeginMethod(theRequest, getBeginMethodName());
}
/**
* @see #getCurrentTestName()
* @deprecated Use [EMAIL PROTECTED] #getCurrentTestName()} instead
*/
protected String getCurrentTestMethod()
{
return getCurrentTestName();
}
/**
* @return the name of the current test case being executed (it corresponds
* to the name of the test method with the "test" prefix removed.
* For example, for "testSomeTestOk" would return "someTestOk".
*/
protected String getCurrentTestName()
{
return JUnitVersionHelper.getTestCaseName(getDelegatedTest());
}
/**
* @return The wrapped test name, if any (null otherwise).
*/
public String getWrappedTestName()
{
if (isWrappingATest())
{
return getWrappedTest().getClass().getName();
}
return null;
}
/**
* @return whether this test case wraps another
*/
public boolean isWrappingATest()
{
return getWrappedTest() != getDelegatedTest();
}
}
1.16 +3 -3
jakarta-cactus/framework/src/test/share/org/apache/cactus/TestAbstractTestCaseInterceptorTestCase.java
Index: TestAbstractTestCaseInterceptorTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/test/share/org/apache/cactus/TestAbstractTestCaseInterceptorTestCase.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- TestAbstractTestCaseInterceptorTestCase.java 12 Jul 2003 16:08:13 -0000
1.15
+++ TestAbstractTestCaseInterceptorTestCase.java 12 Jul 2003 19:31:41 -0000
1.16
@@ -66,7 +66,7 @@
import org.apache.cactus.client.ClientException;
import org.apache.cactus.configuration.ServletConfiguration;
import org.apache.cactus.configuration.WebConfiguration;
-import org.apache.cactus.internal.client.WebClientTestCaseDelegator;
+import org.apache.cactus.internal.client.WebClientTestCaseDelegate;
import org.apache.cactus.mock.MockHttpURLConnection;
import org.apache.cactus.util.JUnitVersionHelper;
@@ -103,7 +103,7 @@
*/
protected void runTest() throws Throwable
{
- WebClientTestCaseDelegator delegator = new WebClientTestCaseDelegator(
+ WebClientTestCaseDelegate delegator = new WebClientTestCaseDelegate(
this, this, new ServletConfiguration());
try
1.1
jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/server/ServerTestCaseDelegate.java
Index: ServerTestCaseDelegate.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.internal.server;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import junit.framework.Assert;
import junit.framework.Test;
import org.apache.cactus.util.JUnitVersionHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Delegate class that allows executing JUnit test on the server side by
* calling <code>setUp()</code>, <code>testXXX()</code> and
* <code>tearDown()</code>.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
* @version $Id: ServerTestCaseDelegate.java,v 1.1 2003/07/12 19:31:41 vmassol Exp $
*/
public class ServerTestCaseDelegate extends Assert
{
/**
* The logger.
*/
private Log logger;
/**
* The test we are delegating for.
*/
private Test delegatedTest;
/**
* Pure JUnit Test Case that we are wrapping (if any)
*/
private Test wrappedTest;
/**
* @param theDelegatedTest the test we are delegating for
* @param theWrappedTest the test being wrapped by this delegate (or null
* if none)
*/
public ServerTestCaseDelegate(Test theDelegatedTest, Test theWrappedTest)
{
if (theDelegatedTest == null)
{
throw new IllegalStateException(
"The test object passed must not be null");
}
setDelegatedTest(theDelegatedTest);
setWrappedTest(theWrappedTest);
}
/**
* @param theWrappedTest the pure JUnit test that we need to wrap
*/
public void setWrappedTest(Test theWrappedTest)
{
this.wrappedTest = theWrappedTest;
}
/**
* @return the wrapped JUnit test
*/
public Test getWrappedTest()
{
return this.wrappedTest;
}
/**
* @param theDelegatedTest the test we are delegating for
*/
public void setDelegatedTest(Test theDelegatedTest)
{
this.delegatedTest = theDelegatedTest;
}
/**
* @return the test we are delegating for
*/
public Test getDelegatedTest()
{
return this.delegatedTest;
}
/**
*/
public void runBareInit()
{
// Initialize the logging system. As this class is instanciated both
// on the server side and on the client side, we need to differentiate
// the logging initialisation. This method is only called on the server
// side, so we instanciate the log for server side here.
if (getLogger() == null)
{
setLogger(LogFactory.getLog(getDelegatedTest().getClass()));
}
}
/**
* @return the logger pointing to the wrapped test case that use to perform
* logging on behalf of the wrapped test.
*/
private final Log getLogger()
{
return this.logger;
}
/**
* @param theLogger the logger to use
*/
private void setLogger(Log theLogger)
{
this.logger = theLogger;
}
/**
* Run the test that was specified in the constructor on the server side
* by executing the testXXX method.
*
* @exception Throwable any error that occurred when calling the test method
* for the current test case, on the server side.
*/
public void runServerTest() throws Throwable
{
Method runMethod = null;
try
{
// Use getMethod to get all public inherited
// methods. getDeclaredMethods returns all
// methods of this class but excludes the
// inherited ones.
runMethod = getWrappedTest().getClass().getMethod(
JUnitVersionHelper.getTestCaseName(getWrappedTest()),
new Class[0]);
}
catch (NoSuchMethodException e)
{
fail("Method ["
+ JUnitVersionHelper.getTestCaseName(getWrappedTest())
+ "()] does not exist for class ["
+ getWrappedTest().getClass().getName() + "].");
}
if ((runMethod != null) && !Modifier.isPublic(runMethod.getModifiers()))
{
fail("Method ["
+ JUnitVersionHelper.getTestCaseName(getWrappedTest())
+ "()] should be public");
}
try
{
runMethod.invoke(getWrappedTest(), new Class[0]);
}
catch (InvocationTargetException e)
{
e.fillInStackTrace();
throw e.getTargetException();
}
catch (IllegalAccessException e)
{
e.fillInStackTrace();
throw e;
}
}
}
1.15 +89 -21
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.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- ServletTestCase.java 12 Jul 2003 16:08:12 -0000 1.14
+++ ServletTestCase.java 12 Jul 2003 19:31:41 -0000 1.15
@@ -63,7 +63,8 @@
import junit.framework.TestCase;
import org.apache.cactus.configuration.ServletConfiguration;
-import org.apache.cactus.internal.client.WebClientTestCaseDelegator;
+import org.apache.cactus.internal.client.WebClientTestCaseDelegate;
+import org.apache.cactus.internal.server.ServerTestCaseDelegate;
import org.apache.cactus.server.ServletConfigWrapper;
/**
@@ -114,11 +115,18 @@
public ServletConfigWrapper config;
/**
- * Delegator that provides all Cactus related test case logic. We are using
- * a delegator in order to hide non public API to the users and thus to be
- * able to easily change the implementation.
+ * Delegate that provides all client side Cactus related test case logic.
+ * We are using a delegate in order to hide non public API to the users
+ * and thus to be able to easily change the implementation.
*/
- private WebClientTestCaseDelegator delegator;
+ private WebClientTestCaseDelegate clientDelegate;
+
+ /**
+ * Delegate that provides all server side Cactus related test case logic.
+ * We are using a delegate in order to hide non public API to the users
+ * and thus to be able to easily change the implementation.
+ */
+ private ServerTestCaseDelegate serverDelegate;
/**
* Default constructor defined in order to allow creating Test Case
@@ -163,38 +171,78 @@
*/
void init(Test theTest)
{
- setDelegator(new WebClientTestCaseDelegator(
+ setClientDelegate(new WebClientTestCaseDelegate(
this, theTest, new ServletConfiguration()));
+ setServerDelegate(new ServerTestCaseDelegate(this, theTest));
+ }
+
+ /**
+ * @param theDelegate the client test case delegate
+ */
+ void setClientDelegate(WebClientTestCaseDelegate theDelegate)
+ {
+ this.clientDelegate = theDelegate;
}
/**
- * @param theDelegator the client test case delegator
+ * @param theDelegate the client test case delegate
*/
- void setDelegator(WebClientTestCaseDelegator theDelegator)
+ void setServerDelegate(ServerTestCaseDelegate theDelegate)
{
- this.delegator = theDelegator;
+ this.serverDelegate = theDelegate;
}
/**
- * @return the client test case delegator
+ * @return the client test case delegate
*/
- WebClientTestCaseDelegator getDelegator()
+ WebClientTestCaseDelegate getClientDelegate()
{
- return this.delegator;
+ return this.clientDelegate;
}
/**
- * Runs the bare test. This method is overridden from the JUnit
- * [EMAIL PROTECTED] TestCase} class in order to prevent the latter to call the
- * <code>setUp()</code> and <code>tearDown()</code> methods which, in
- * our case, need to be executed on the server side.
+ * @return the server test case delegate
+ */
+ private ServerTestCaseDelegate getServerDelegate()
+ {
+ return this.serverDelegate;
+ }
+
+ /**
+ * @return true if this test class has been instanciated on the server
+ * side or false otherwise
+ */
+ private boolean isServerSide()
+ {
+ boolean result = false;
+
+ if (this.request != null)
+ {
+ result = true;
+ }
+ return result;
+ }
+
+ /**
+ * Runs the bare test (either on the client side or on the server side).
+ * This method is overridden from the JUnit
+ * [EMAIL PROTECTED] TestCase} class in order to prevent the latter to
immediatly
+ * call the <code>setUp()</code> and <code>tearDown()</code> methods
+ * which, in our case, need to be executed on the server side.
*
* @exception Throwable if any exception is thrown during the test. Any
* exception will be displayed by the JUnit Test Runner
*/
public void runBare() throws Throwable
{
- getDelegator().runBareInit();
+ if (isServerSide())
+ {
+ getServerDelegate().runBareInit();
+ }
+ else
+ {
+ getClientDelegate().runBareInit();
+ }
// Catch the exception just to have a chance to log it
try
@@ -204,7 +252,10 @@
}
catch (Throwable t)
{
- getDelegator().getLogger().debug("Exception in test", t);
+ if (!isServerSide())
+ {
+ getClientDelegate().getLogger().debug("Exception in test", t);
+ }
throw t;
}
}
@@ -212,13 +263,30 @@
/**
* Runs a test case. This method is overriden from the JUnit
* [EMAIL PROTECTED] TestCase} class in order to seamlessly call the
- * Cactus redirector.
+ * Cactus redirector on the client side and the test on the server
+ * side.
*
* @exception Throwable if any error happens during the execution of
* the test
*/
protected void runTest() throws Throwable
{
- getDelegator().runTest();
+ if (isServerSide())
+ {
+ setUp();
+
+ try
+ {
+ getServerDelegate().runServerTest();
+ }
+ finally
+ {
+ tearDown();
+ }
+ }
+ else
+ {
+ getClientDelegate().runTest();
+ }
}
}
1.13 +5 -3
jakarta-cactus/framework/src/java/share/org/apache/cactus/JspTestCase.java
Index: JspTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/JspTestCase.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- JspTestCase.java 12 Jul 2003 16:08:12 -0000 1.12
+++ JspTestCase.java 12 Jul 2003 19:31:41 -0000 1.13
@@ -61,7 +61,8 @@
import junit.framework.Test;
import org.apache.cactus.configuration.JspConfiguration;
-import org.apache.cactus.internal.client.WebClientTestCaseDelegator;
+import org.apache.cactus.internal.client.WebClientTestCaseDelegate;
+import org.apache.cactus.internal.server.ServerTestCaseDelegate;
import org.apache.cactus.server.PageContextWrapper;
/**
@@ -100,8 +101,9 @@
*/
void init(Test theTest)
{
- setDelegator(new WebClientTestCaseDelegator(
+ setClientDelegate(new WebClientTestCaseDelegate(
this, theTest, new JspConfiguration()));
+ setServerDelegate(new ServerTestCaseDelegate(this, theTest));
}
/**
1.16 +91 -21
jakarta-cactus/framework/src/java/j2ee13/org/apache/cactus/FilterTestCase.java
Index: FilterTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/framework/src/java/j2ee13/org/apache/cactus/FilterTestCase.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- FilterTestCase.java 12 Jul 2003 16:08:13 -0000 1.15
+++ FilterTestCase.java 12 Jul 2003 19:31:41 -0000 1.16
@@ -63,7 +63,8 @@
import junit.framework.TestCase;
import org.apache.cactus.configuration.FilterConfiguration;
-import org.apache.cactus.internal.client.WebClientTestCaseDelegator;
+import org.apache.cactus.internal.client.WebClientTestCaseDelegate;
+import org.apache.cactus.internal.server.ServerTestCaseDelegate;
import org.apache.cactus.server.FilterConfigWrapper;
/**
@@ -71,6 +72,8 @@
* <code>FilterConfig</code> and <code>FilterChain</code> objects) must
* subclass this class.
*
+ * @todo Find a way to factorize FilterTestCase and ServletTestCase
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
* @version $Id$
@@ -114,11 +117,18 @@
public FilterChain filterChain;
/**
- * Delegator that provides all Cactus related test case logic. We are using
- * a delegator in order to hide non public API to the users and thus to be
- * able to easily change the implementation.
+ * Delegate that provides all client side Cactus related test case logic.
+ * We are using a delegate in order to hide non public API to the users
+ * and thus to be able to easily change the implementation.
*/
- private WebClientTestCaseDelegator delegator;
+ private WebClientTestCaseDelegate clientDelegate;
+
+ /**
+ * Delegate that provides all server side Cactus related test case logic.
+ * We are using a delegate in order to hide non public API to the users
+ * and thus to be able to easily change the implementation.
+ */
+ private ServerTestCaseDelegate serverDelegate;
/**
* Default constructor defined in order to allow creating Test Case
@@ -163,38 +173,78 @@
*/
void init(Test theTest)
{
- setDelegator(new WebClientTestCaseDelegator(
+ setClientDelegate(new WebClientTestCaseDelegate(
this, theTest, new FilterConfiguration()));
+ setServerDelegate(new ServerTestCaseDelegate(this, theTest));
+ }
+
+ /**
+ * @param theDelegate the client test case delegate
+ */
+ void setClientDelegate(WebClientTestCaseDelegate theDelegate)
+ {
+ this.clientDelegate = theDelegate;
}
/**
- * @param theDelegator the client test case delegator
+ * @param theDelegate the client test case delegate
*/
- void setDelegator(WebClientTestCaseDelegator theDelegator)
+ void setServerDelegate(ServerTestCaseDelegate theDelegate)
{
- this.delegator = theDelegator;
+ this.serverDelegate = theDelegate;
}
/**
- * @return the client test case delegator
+ * @return the client test case delegate
*/
- WebClientTestCaseDelegator getDelegator()
+ WebClientTestCaseDelegate getClientDelegate()
{
- return this.delegator;
+ return this.clientDelegate;
}
/**
- * Runs the bare test. This method is overridden from the JUnit
- * [EMAIL PROTECTED] TestCase} class in order to prevent the latter to call the
- * <code>setUp()</code> and <code>tearDown()</code> methods which, in
- * our case, need to be executed on the server side.
+ * @return the server test case delegate
+ */
+ private ServerTestCaseDelegate getServerDelegate()
+ {
+ return this.serverDelegate;
+ }
+
+ /**
+ * @return true if this test class has been instanciated on the server
+ * side or false otherwise
+ */
+ private boolean isServerSide()
+ {
+ boolean result = false;
+
+ if (this.request != null)
+ {
+ result = true;
+ }
+ return result;
+ }
+
+ /**
+ * Runs the bare test (either on the client side or on the server side).
+ * This method is overridden from the JUnit
+ * [EMAIL PROTECTED] TestCase} class in order to prevent the latter to
immediatly
+ * call the <code>setUp()</code> and <code>tearDown()</code> methods
+ * which, in our case, need to be executed on the server side.
*
* @exception Throwable if any exception is thrown during the test. Any
* exception will be displayed by the JUnit Test Runner
*/
public void runBare() throws Throwable
{
- getDelegator().runBareInit();
+ if (isServerSide())
+ {
+ getServerDelegate().runBareInit();
+ }
+ else
+ {
+ getClientDelegate().runBareInit();
+ }
// Catch the exception just to have a chance to log it
try
@@ -204,7 +254,10 @@
}
catch (Throwable t)
{
- getDelegator().getLogger().debug("Exception in test", t);
+ if (!isServerSide())
+ {
+ getClientDelegate().getLogger().debug("Exception in test", t);
+ }
throw t;
}
}
@@ -212,13 +265,30 @@
/**
* Runs a test case. This method is overriden from the JUnit
* [EMAIL PROTECTED] TestCase} class in order to seamlessly call the
- * Cactus redirector.
+ * Cactus redirector on the client side and the test on the server
+ * side.
*
* @exception Throwable if any error happens during the execution of
* the test
*/
protected void runTest() throws Throwable
{
- getDelegator().runTest();
+ if (isServerSide())
+ {
+ setUp();
+
+ try
+ {
+ getServerDelegate().runServerTest();
+ }
+ finally
+ {
+ tearDown();
+ }
+ }
+ else
+ {
+ getClientDelegate().runTest();
+ }
}
}
1.23 +15 -12
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.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- AbstractWebTestCaller.java 12 Jul 2003 17:19:28 -0000 1.22
+++ AbstractWebTestCaller.java 12 Jul 2003 19:31:41 -0000 1.23
@@ -63,13 +63,13 @@
import javax.servlet.ServletException;
+import junit.framework.Test;
import junit.framework.TestCase;
import org.apache.cactus.HttpServiceDefinition;
import org.apache.cactus.ServiceEnumeration;
import org.apache.cactus.WebTestResult;
import org.apache.cactus.configuration.Version;
-import org.apache.cactus.internal.server.ServerTestCaseWrapper;
import org.apache.cactus.util.ClassLoaderUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -152,13 +152,8 @@
// Set its fields (implicit objects)
setTestCaseFields(testInstance);
- // Wrap it in a server test case that will call all setUp(),
- // testXXX() and tearDown() methods.
- ServerTestCaseWrapper wrappedTestInstance =
- new ServerTestCaseWrapper(testInstance);
-
// Call it's method corresponding to the current test case
- wrappedTestInstance.runBareServerTest();
+ testInstance.runBare();
// Return an instance of <code>WebTestResult</code> with a
// positive result.
@@ -432,19 +427,27 @@
Constructor wrappedConstructor =
getTestClassConstructor(wrappedTestClass);
+ TestCase wrappedTestInstance;
if (wrappedConstructor.getParameterTypes().length == 0)
{
- testInstance =
+ wrappedTestInstance =
(TestCase) wrappedConstructor.newInstance(
new Object[0]);
- testInstance.setName(theTestCaseName);
+ wrappedTestInstance.setName(theTestCaseName);
}
else
{
- testInstance =
+ wrappedTestInstance =
(TestCase) wrappedConstructor.newInstance(
new Object[] {theTestCaseName});
- }
+ }
+
+ constructor = testClass.getConstructor(
+ new Class[] {String.class, Test.class});
+
+ testInstance =
+ (TestCase) constructor.newInstance(
+ new Object[] {theTestCaseName, wrappedTestInstance});
}
}
catch (Exception e)
1.9 +10 -10
jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestGlobalBeginEnd.java
Index: TestGlobalBeginEnd.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/samples/servlet/src/test-cactus/share/org/apache/cactus/sample/unit/TestGlobalBeginEnd.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- TestGlobalBeginEnd.java 12 Jul 2003 16:08:11 -0000 1.8
+++ TestGlobalBeginEnd.java 12 Jul 2003 19:31:41 -0000 1.9
@@ -59,8 +59,6 @@
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;
-import org.apache.cactus.configuration.ServletConfiguration;
-import org.apache.cactus.internal.client.WebClientTestCaseDelegator;
/**
* Test global client side <code>begin()</code> and <code>end()</code>
@@ -84,15 +82,17 @@
*/
protected void runTest() throws Throwable
{
- WebClientTestCaseDelegator delegator =
- new WebClientTestCaseDelegator(this, this,
- new ServletConfiguration());
+ super.runTest();
- delegator.runTest();
-
- if (!this.isClientGlobalEndCalled)
+ // Make sure we verify if end() has been called only on
+ // the client side. Reason is that the runTest() method is
+ // called both on the client side and on the server side.
+ if (this.request == null)
{
- fail("end() has not been called");
+ if (!this.isClientGlobalEndCalled)
+ {
+ fail("end() has not been called");
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]