|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ClientTestCaseDelegate.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus.internal.client;
|
|
| 58 |
|
|
| 59 |
import java.lang.reflect.InvocationTargetException;
|
|
| 60 |
import java.lang.reflect.Method;
|
|
| 61 |
import java.lang.reflect.Modifier;
|
|
| 62 |
|
|
| 63 |
import junit.framework.Assert;
|
|
| 64 |
import junit.framework.Test;
|
|
| 65 |
|
|
| 66 |
import org.apache.cactus.Request;
|
|
| 67 |
import org.apache.cactus.WebRequest;
|
|
| 68 |
import org.apache.cactus.configuration.Configuration;
|
|
| 69 |
import org.apache.cactus.util.JUnitVersionHelper;
|
|
| 70 |
import org.apache.commons.logging.Log;
|
|
| 71 |
import org.apache.commons.logging.LogFactory;
|
|
| 72 |
|
|
| 73 |
/**
|
|
| 74 |
* Delegate class that provides useful methods for the Cactus
|
|
| 75 |
* <code>XXXTestCase</code> classes. All the methods provided are independent
|
|
| 76 |
* of any communication protocol between client side and server side (HTTP,
|
|
| 77 |
* JMS, etc). Subclasses will define additional behaviour that depends on the
|
|
| 78 |
* protocol.
|
|
| 79 |
*
|
|
| 80 |
* It provides the ability to run common code before each test on the client
|
|
| 81 |
* side (note that calling common tear down code is delegated to child classes
|
|
| 82 |
* as the method signature depends on the protocol used).
|
|
| 83 |
*
|
|
| 84 |
* In addition it provides the ability to execute some one time (per-JVM)
|
|
| 85 |
* initialisation code (a pity this is not provided in JUnit). It can be
|
|
| 86 |
* useful to start an embedded server for example. Note: In the future this
|
|
| 87 |
* should be refatored and provided using a custom JUnit TestSuite.
|
|
| 88 |
*
|
|
| 89 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 90 |
*
|
|
| 91 |
* @version $Id: ClientTestCaseDelegate.java,v 1.2.2.1 2003/08/31 14:38:18 vmassol Exp $
|
|
| 92 |
*/
|
|
| 93 |
public class ClientTestCaseDelegate extends Assert |
|
| 94 |
{
|
|
| 95 |
/**
|
|
| 96 |
* The prefix of a test method.
|
|
| 97 |
*/
|
|
| 98 |
protected static final String TEST_METHOD_PREFIX = "test"; |
|
| 99 |
|
|
| 100 |
/**
|
|
| 101 |
* The prefix of a begin test method.
|
|
| 102 |
*/
|
|
| 103 |
protected static final String BEGIN_METHOD_PREFIX = "begin"; |
|
| 104 |
|
|
| 105 |
/**
|
|
| 106 |
* The prefix of an end test method.
|
|
| 107 |
*/
|
|
| 108 |
protected static final String END_METHOD_PREFIX = "end"; |
|
| 109 |
|
|
| 110 |
/**
|
|
| 111 |
* The name of the method that is called before each test on the client
|
|
| 112 |
* side (if it exists).
|
|
| 113 |
*/
|
|
| 114 |
protected static final String CLIENT_GLOBAL_BEGIN_METHOD = "begin"; |
|
| 115 |
|
|
| 116 |
/**
|
|
| 117 |
* The name of the method that is called after each test on the client
|
|
| 118 |
* side (if it exists).
|
|
| 119 |
*/
|
|
| 120 |
protected static final String CLIENT_GLOBAL_END_METHOD = "end"; |
|
| 121 |
|
|
| 122 |
/**
|
|
| 123 |
* The logger (only used on the client side).
|
|
| 124 |
*/
|
|
| 125 |
private Log logger;
|
|
| 126 |
|
|
| 127 |
/**
|
|
| 128 |
* The Cactus configuration.
|
|
| 129 |
*/
|
|
| 130 |
private Configuration configuration;
|
|
| 131 |
|
|
| 132 |
/**
|
|
| 133 |
* Pure JUnit Test Case that we are wrapping (if any)
|
|
| 134 |
*/
|
|
| 135 |
private Test wrappedTest;
|
|
| 136 |
|
|
| 137 |
/**
|
|
| 138 |
* The test we are delegating for.
|
|
| 139 |
*/
|
|
| 140 |
private Test delegatedTest;
|
|
| 141 |
|
|
| 142 |
/**
|
|
| 143 |
* @param theDelegatedTest the test we are delegating for
|
|
| 144 |
* @param theWrappedTest the test being wrapped by this delegate (or null
|
|
| 145 |
* if none)
|
|
| 146 |
* @param theConfiguration the configuration to use
|
|
| 147 |
*/
|
|
| 148 | 0 |
public ClientTestCaseDelegate(Test theDelegatedTest,
|
| 149 |
Test theWrappedTest, Configuration theConfiguration) |
|
| 150 |
{
|
|
| 151 | 0 |
if (theDelegatedTest == null) |
| 152 |
{
|
|
| 153 | 0 |
throw new IllegalStateException( |
| 154 |
"The test object passed must not be null");
|
|
| 155 |
} |
|
| 156 |
|
|
| 157 | 0 |
setDelegatedTest(theDelegatedTest); |
| 158 | 0 |
setWrappedTest(theWrappedTest); |
| 159 | 0 |
setConfiguration(theConfiguration); |
| 160 |
} |
|
| 161 |
|
|
| 162 |
/**
|
|
| 163 |
* @param theWrappedTest the pure JUnit test that we need to wrap
|
|
| 164 |
*/
|
|
| 165 | 0 |
public void setWrappedTest(Test theWrappedTest) |
| 166 |
{
|
|
| 167 | 0 |
this.wrappedTest = theWrappedTest;
|
| 168 |
} |
|
| 169 |
|
|
| 170 |
/**
|
|
| 171 |
* @param theDelegatedTest the test we are delegating for
|
|
| 172 |
*/
|
|
| 173 | 0 |
public void setDelegatedTest(Test theDelegatedTest) |
| 174 |
{
|
|
| 175 | 0 |
this.delegatedTest = theDelegatedTest;
|
| 176 |
} |
|
| 177 |
|
|
| 178 |
/**
|
|
| 179 |
* @return the wrapped JUnit test
|
|
| 180 |
*/
|
|
| 181 | 0 |
public Test getWrappedTest()
|
| 182 |
{
|
|
| 183 | 0 |
return this.wrappedTest; |
| 184 |
} |
|
| 185 |
|
|
| 186 |
/**
|
|
| 187 |
* @return the test we are delegating for
|
|
| 188 |
*/
|
|
| 189 | 0 |
public Test getDelegatedTest()
|
| 190 |
{
|
|
| 191 | 0 |
return this.delegatedTest; |
| 192 |
} |
|
| 193 |
|
|
| 194 |
/**
|
|
| 195 |
* @return the test on which we will operate. If there is a wrapped
|
|
| 196 |
* test then the returned test is the wrapped test. Otherwise we
|
|
| 197 |
* return the delegated test.
|
|
| 198 |
*/
|
|
| 199 | 0 |
public Test getTest()
|
| 200 |
{
|
|
| 201 | 0 |
Test activeTest; |
| 202 | 0 |
if (getWrappedTest() != null) |
| 203 |
{
|
|
| 204 | 0 |
activeTest = getWrappedTest(); |
| 205 |
} |
|
| 206 |
else
|
|
| 207 |
{
|
|
| 208 | 0 |
activeTest = getDelegatedTest(); |
| 209 |
} |
|
| 210 | 0 |
return activeTest;
|
| 211 |
} |
|
| 212 |
|
|
| 213 |
|
|
| 214 |
/**
|
|
| 215 |
* @return The logger used by the <code>TestCase</code> class and
|
|
| 216 |
* subclasses to perform logging.
|
|
| 217 |
*/
|
|
| 218 | 0 |
public final Log getLogger()
|
| 219 |
{
|
|
| 220 | 0 |
return this.logger; |
| 221 |
} |
|
| 222 |
|
|
| 223 |
/**
|
|
| 224 |
* @param theLogger the logger to use
|
|
| 225 |
*/
|
|
| 226 | 0 |
protected void setLogger(Log theLogger) |
| 227 |
{
|
|
| 228 | 0 |
this.logger = theLogger;
|
| 229 |
} |
|
| 230 |
|
|
| 231 |
/**
|
|
| 232 |
* @return the Cactus configuration
|
|
| 233 |
*/
|
|
| 234 | 0 |
public Configuration getConfiguration()
|
| 235 |
{
|
|
| 236 | 0 |
return this.configuration; |
| 237 |
} |
|
| 238 |
|
|
| 239 |
/**
|
|
| 240 |
* Sets the Cactus configuration
|
|
| 241 |
*
|
|
| 242 |
* @param theConfiguration the Cactus configuration
|
|
| 243 |
*/
|
|
| 244 | 0 |
public void setConfiguration(Configuration theConfiguration) |
| 245 |
{
|
|
| 246 | 0 |
this.configuration = theConfiguration;
|
| 247 |
} |
|
| 248 |
|
|
| 249 |
/**
|
|
| 250 |
* @return the name of the test method to call without the
|
|
| 251 |
* TEST_METHOD_PREFIX prefix
|
|
| 252 |
*/
|
|
| 253 | 0 |
private String getBaseMethodName()
|
| 254 |
{
|
|
| 255 |
// Sanity check
|
|
| 256 | 0 |
if (!getCurrentTestName().startsWith(TEST_METHOD_PREFIX))
|
| 257 |
{
|
|
| 258 | 0 |
throw new RuntimeException("bad name [" |
| 259 |
+ getCurrentTestName() |
|
| 260 |
+ "]. It should start with ["
|
|
| 261 |
+ TEST_METHOD_PREFIX + "].");
|
|
| 262 |
} |
|
| 263 |
|
|
| 264 | 0 |
return getCurrentTestName().substring(
|
| 265 |
TEST_METHOD_PREFIX.length()); |
|
| 266 |
} |
|
| 267 |
|
|
| 268 |
/**
|
|
| 269 |
* @return the name of the test begin method to call that initialize the
|
|
| 270 |
* test by initializing the <code>WebRequest</code> object
|
|
| 271 |
* for the test case.
|
|
| 272 |
*/
|
|
| 273 | 0 |
protected String getBeginMethodName()
|
| 274 |
{
|
|
| 275 | 0 |
return BEGIN_METHOD_PREFIX + getBaseMethodName();
|
| 276 |
} |
|
| 277 |
|
|
| 278 |
/**
|
|
| 279 |
* @return the name of the test end method to call when the test has been
|
|
| 280 |
* run on the server. It can be used to verify returned headers,
|
|
| 281 |
* cookies, ...
|
|
| 282 |
*/
|
|
| 283 | 0 |
protected String getEndMethodName()
|
| 284 |
{
|
|
| 285 | 0 |
return END_METHOD_PREFIX + getBaseMethodName();
|
| 286 |
} |
|
| 287 |
|
|
| 288 |
/**
|
|
| 289 |
* Perform client side initializations before each test, such as
|
|
| 290 |
* re-initializating the logger and printing some logging information.
|
|
| 291 |
*/
|
|
| 292 | 0 |
public void runBareInit() |
| 293 |
{
|
|
| 294 |
// We make sure we reinitialize The logger with the name of the
|
|
| 295 |
// current extending class so that log statements will contain the
|
|
| 296 |
// actual class name (that's why the logged instance is not static).
|
|
| 297 | 0 |
this.logger = LogFactory.getLog(this.getClass()); |
| 298 |
|
|
| 299 |
// Mark beginning of test on client side
|
|
| 300 | 0 |
getLogger().debug("------------- Test: "
|
| 301 |
+ this.getCurrentTestName());
|
|
| 302 |
} |
|
| 303 |
|
|
| 304 |
/**
|
|
| 305 |
* Call a begin method which takes Cactus WebRequest as parameter
|
|
| 306 |
*
|
|
| 307 |
* @param theRequest the request object which will contain data that will
|
|
| 308 |
* be used to connect to the Cactus server side redirectors.
|
|
| 309 |
* @param theMethodName the name of the begin method to call
|
|
| 310 |
* @exception Throwable any error that occurred when calling the method
|
|
| 311 |
*/
|
|
| 312 | 0 |
private void callGenericBeginMethod(Request theRequest, |
| 313 |
String theMethodName) throws Throwable
|
|
| 314 |
{
|
|
| 315 |
// First, verify if a begin method exist. If one is found, verify if
|
|
| 316 |
// it has the correct signature. If not, send a warning.
|
|
| 317 | 0 |
Method[] methods = getTest().getClass().getMethods(); |
| 318 |
|
|
| 319 | 0 |
for (int i = 0; i < methods.length; i++) |
| 320 |
{
|
|
| 321 | 0 |
if (methods[i].getName().equals(theMethodName))
|
| 322 |
{
|
|
| 323 |
// Check return type
|
|
| 324 | 0 |
if (!methods[i].getReturnType().getName().equals("void")) |
| 325 |
{
|
|
| 326 | 0 |
fail("The method [" + methods[i].getName()
|
| 327 |
+ "] should return void and not ["
|
|
| 328 |
+ methods[i].getReturnType().getName() + "]");
|
|
| 329 |
} |
|
| 330 |
|
|
| 331 |
// Check if method is public
|
|
| 332 | 0 |
if (!Modifier.isPublic(methods[i].getModifiers()))
|
| 333 |
{
|
|
| 334 | 0 |
fail("Method [" + methods[i].getName()
|
| 335 |
+ "] should be declared public");
|
|
| 336 |
} |
|
| 337 |
|
|
| 338 |
// Check parameters
|
|
| 339 | 0 |
Class[] parameters = methods[i].getParameterTypes(); |
| 340 |
|
|
| 341 | 0 |
if (parameters.length != 1)
|
| 342 |
{
|
|
| 343 | 0 |
fail("The method [" + methods[i].getName()
|
| 344 |
+ "] must accept a single parameter derived from "
|
|
| 345 |
+ "class [" + WebRequest.class.getName() + "], " |
|
| 346 |
+ "but " + parameters.length
|
|
| 347 |
+ " parameters were found");
|
|
| 348 |
} |
|
| 349 | 0 |
else if (!theRequest.getClass().isAssignableFrom(parameters[0])) |
| 350 |
{
|
|
| 351 | 0 |
fail("The method [" + methods[i].getName()
|
| 352 |
+ "] must accept a single parameter derived from "
|
|
| 353 |
+ "class [" + theRequest.getClass().getName() + "], " |
|
| 354 |
+ "but found a [" + parameters[0].getName() + "] " |
|
| 355 |
+ "parameter instead");
|
|
| 356 |
} |
|
| 357 |
|
|
| 358 | 0 |
try
|
| 359 |
{
|
|
| 360 | 0 |
methods[i].invoke(getTest(), new Object[] {theRequest});
|
| 361 |
|
|
| 362 | 0 |
break;
|
| 363 |
} |
|
| 364 |
catch (InvocationTargetException e)
|
|
| 365 |
{
|
|
| 366 | 0 |
e.fillInStackTrace(); |
| 367 | 0 |
throw e.getTargetException();
|
| 368 |
} |
|
| 369 |
catch (IllegalAccessException e)
|
|
| 370 |
{
|
|
| 371 | 0 |
e.fillInStackTrace(); |
| 372 | 0 |
throw e;
|
| 373 |
} |
|
| 374 |
} |
|
| 375 |
} |
|
| 376 |
} |
|
| 377 |
|
|
| 378 |
/**
|
|
| 379 |
* Call the global begin method. This is the method that is called before
|
|
| 380 |
* each test if it exists. It is called on the client side only.
|
|
| 381 |
*
|
|
| 382 |
* @param theRequest the request object which will contain data that will
|
|
| 383 |
* be used to connect to the Cactus server side redirectors.
|
|
| 384 |
* @exception Throwable any error that occurred when calling the method
|
|
| 385 |
*/
|
|
| 386 | 0 |
protected void callClientGlobalBegin(Request theRequest) throws Throwable |
| 387 |
{
|
|
| 388 | 0 |
callGenericBeginMethod(theRequest, CLIENT_GLOBAL_BEGIN_METHOD); |
| 389 |
} |
|
| 390 |
|
|
| 391 |
/**
|
|
| 392 |
* Call the test case begin method.
|
|
| 393 |
*
|
|
| 394 |
* @param theRequest the request object to pass to the begin method.
|
|
| 395 |
* @exception Throwable any error that occurred when calling the begin
|
|
| 396 |
* method for the current test case.
|
|
| 397 |
*/
|
|
| 398 | 0 |
public void callBeginMethod(Request theRequest) throws Throwable |
| 399 |
{
|
|
| 400 | 0 |
callGenericBeginMethod(theRequest, getBeginMethodName()); |
| 401 |
} |
|
| 402 |
|
|
| 403 |
/**
|
|
| 404 |
* @see #getCurrentTestName()
|
|
| 405 |
* @deprecated Use {@link #getCurrentTestName()} instead
|
|
| 406 |
*/
|
|
| 407 | 0 |
protected String getCurrentTestMethod()
|
| 408 |
{
|
|
| 409 | 0 |
return getCurrentTestName();
|
| 410 |
} |
|
| 411 |
|
|
| 412 |
/**
|
|
| 413 |
* @return the name of the current test case being executed (it corresponds
|
|
| 414 |
* to the name of the test method with the "test" prefix removed.
|
|
| 415 |
* For example, for "testSomeTestOk" would return "someTestOk".
|
|
| 416 |
*/
|
|
| 417 | 0 |
protected String getCurrentTestName()
|
| 418 |
{
|
|
| 419 | 0 |
return JUnitVersionHelper.getTestCaseName(getDelegatedTest());
|
| 420 |
} |
|
| 421 |
|
|
| 422 |
/**
|
|
| 423 |
* @return The wrapped test name, if any (null otherwise).
|
|
| 424 |
*/
|
|
| 425 | 0 |
public String getWrappedTestName()
|
| 426 |
{
|
|
| 427 | 0 |
if (isWrappingATest())
|
| 428 |
{
|
|
| 429 | 0 |
return getWrappedTest().getClass().getName();
|
| 430 |
} |
|
| 431 | 0 |
return null; |
| 432 |
} |
|
| 433 |
|
|
| 434 |
/**
|
|
| 435 |
* @return whether this test case wraps another
|
|
| 436 |
*/
|
|
| 437 | 0 |
public boolean isWrappingATest() |
| 438 |
{
|
|
| 439 | 0 |
return (getWrappedTest() != null); |
| 440 |
} |
|
| 441 |
} |
|
| 442 |
|
|
||||||||||