|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractTestSuite.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;
|
|
| 58 |
|
|
| 59 |
import java.io.PrintWriter;
|
|
| 60 |
import java.io.StringWriter;
|
|
| 61 |
import java.lang.reflect.Constructor;
|
|
| 62 |
import java.lang.reflect.InvocationTargetException;
|
|
| 63 |
import java.lang.reflect.Method;
|
|
| 64 |
import java.lang.reflect.Modifier;
|
|
| 65 |
import java.util.Enumeration;
|
|
| 66 |
import java.util.Vector;
|
|
| 67 |
|
|
| 68 |
import junit.framework.Test;
|
|
| 69 |
import junit.framework.TestCase;
|
|
| 70 |
import junit.framework.TestResult;
|
|
| 71 |
|
|
| 72 |
/**
|
|
| 73 |
* Test Suite that wraps all the tests of the suite in Cactus Test Case
|
|
| 74 |
* objects so that pure JUnit tests can be run on the server side.
|
|
| 75 |
*
|
|
| 76 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 77 |
*
|
|
| 78 |
* @version $Id: AbstractTestSuite.java,v 1.6 2003/07/12 16:08:12 vmassol Exp $
|
|
| 79 |
* @since 1.5
|
|
| 80 |
*/
|
|
| 81 |
public abstract class AbstractTestSuite implements Test |
|
| 82 |
{
|
|
| 83 |
/**
|
|
| 84 |
* Lists of tests to execute (Test objects)
|
|
| 85 |
*/
|
|
| 86 |
private Vector tests = new Vector(10); |
|
| 87 |
|
|
| 88 |
/**
|
|
| 89 |
* Name of the current test suite
|
|
| 90 |
*/
|
|
| 91 |
private String name;
|
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* @see junit.framework.TestSuite#TestSuite()
|
|
| 95 |
*/
|
|
| 96 | 0 |
public AbstractTestSuite()
|
| 97 |
{
|
|
| 98 |
} |
|
| 99 |
|
|
| 100 |
/**
|
|
| 101 |
* @see junit.framework.TestSuite#TestSuite(Class)
|
|
| 102 |
*/
|
|
| 103 | 0 |
public AbstractTestSuite(final Class theClass)
|
| 104 |
{
|
|
| 105 | 0 |
setName(theClass.getName()); |
| 106 | 0 |
Constructor constructor; |
| 107 | 0 |
try
|
| 108 |
{
|
|
| 109 |
// Avoid generating multiple error messages
|
|
| 110 | 0 |
constructor = getTestConstructor(theClass); |
| 111 |
} |
|
| 112 |
catch (NoSuchMethodException e)
|
|
| 113 |
{
|
|
| 114 | 0 |
addTest(warning("Class " + theClass.getName()
|
| 115 |
+ " has no public constructor TestCase(String name)"));
|
|
| 116 | 0 |
return;
|
| 117 |
} |
|
| 118 |
|
|
| 119 | 0 |
if (!Modifier.isPublic(theClass.getModifiers()))
|
| 120 |
{
|
|
| 121 | 0 |
addTest(warning("Class " + theClass.getName() + " is not public")); |
| 122 | 0 |
return;
|
| 123 |
} |
|
| 124 |
|
|
| 125 | 0 |
Class superClass = theClass; |
| 126 | 0 |
Vector names = new Vector();
|
| 127 | 0 |
while (Test.class.isAssignableFrom(superClass)) |
| 128 |
{
|
|
| 129 | 0 |
Method[] methods = superClass.getDeclaredMethods(); |
| 130 | 0 |
for (int i = 0; i < methods.length; i++) |
| 131 |
{
|
|
| 132 | 0 |
addTestMethod(methods[i], names, constructor); |
| 133 |
} |
|
| 134 | 0 |
superClass = superClass.getSuperclass(); |
| 135 |
} |
|
| 136 | 0 |
if (this.tests.size() == 0) |
| 137 |
{
|
|
| 138 | 0 |
addTest(warning("No tests found in " + theClass.getName()));
|
| 139 |
} |
|
| 140 |
} |
|
| 141 |
|
|
| 142 |
/**
|
|
| 143 |
* @see junit.framework.TestSuite#TestSuite(String)
|
|
| 144 |
*/
|
|
| 145 | 0 |
public AbstractTestSuite(String theName)
|
| 146 |
{
|
|
| 147 | 0 |
setName(theName); |
| 148 |
} |
|
| 149 |
|
|
| 150 |
/**
|
|
| 151 |
* @see junit.framework.TestSuite#addTest(Test)
|
|
| 152 |
*/
|
|
| 153 | 0 |
public void addTest(Test theTest) |
| 154 |
{
|
|
| 155 | 0 |
this.tests.addElement(theTest);
|
| 156 |
} |
|
| 157 |
|
|
| 158 |
/**
|
|
| 159 |
* @see junit.framework.TestSuite#addTestSuite(Class)
|
|
| 160 |
*/
|
|
| 161 | 0 |
public void addTestSuite(Class theTestClass) |
| 162 |
{
|
|
| 163 | 0 |
addTest(createTestSuite(theTestClass)); |
| 164 |
} |
|
| 165 |
|
|
| 166 |
/**
|
|
| 167 |
* @see junit.framework.TestSuite#addTestMethod(Method, Vector, Constructor)
|
|
| 168 |
*/
|
|
| 169 | 0 |
private void addTestMethod(Method theMethod, Vector theNames, |
| 170 |
Constructor theConstructor) |
|
| 171 |
{
|
|
| 172 | 0 |
String name = theMethod.getName(); |
| 173 | 0 |
if (theNames.contains(name))
|
| 174 |
{
|
|
| 175 | 0 |
return;
|
| 176 |
} |
|
| 177 | 0 |
if (isPublicTestMethod(theMethod))
|
| 178 |
{
|
|
| 179 | 0 |
theNames.addElement(name); |
| 180 |
|
|
| 181 | 0 |
try
|
| 182 |
{
|
|
| 183 |
// Note: We wrap the Test in a Cactus Test Case
|
|
| 184 | 0 |
Object constructorInstance; |
| 185 | 0 |
if (theConstructor.getParameterTypes().length == 0)
|
| 186 |
{
|
|
| 187 | 0 |
constructorInstance = theConstructor.newInstance( |
| 188 |
new Object[0]);
|
|
| 189 | 0 |
if (constructorInstance instanceof TestCase) |
| 190 |
{
|
|
| 191 | 0 |
((TestCase) constructorInstance).setName(name); |
| 192 |
} |
|
| 193 |
} |
|
| 194 |
else
|
|
| 195 |
{
|
|
| 196 | 0 |
constructorInstance = theConstructor.newInstance( |
| 197 |
new Object[] {name});
|
|
| 198 |
} |
|
| 199 | 0 |
addTest(new ServletTestCase(name, (Test) constructorInstance));
|
| 200 |
} |
|
| 201 |
catch (InstantiationException e)
|
|
| 202 |
{
|
|
| 203 | 0 |
addTest(warning("Cannot instantiate test case: " + name
|
| 204 |
+ " (" + exceptionToString(e) + ")")); |
|
| 205 |
} |
|
| 206 |
catch (InvocationTargetException e)
|
|
| 207 |
{
|
|
| 208 | 0 |
addTest(warning("Exception in constructor: " + name + " (" |
| 209 |
+ exceptionToString(e.getTargetException()) + ")"));
|
|
| 210 |
} |
|
| 211 |
catch (IllegalAccessException e)
|
|
| 212 |
{
|
|
| 213 | 0 |
addTest(warning("Cannot access test case: " + name + " (" |
| 214 |
+ exceptionToString(e) + ")"));
|
|
| 215 |
} |
|
| 216 |
} |
|
| 217 |
else
|
|
| 218 |
{
|
|
| 219 |
// Almost a test method
|
|
| 220 | 0 |
if (isTestMethod(theMethod))
|
| 221 |
{
|
|
| 222 | 0 |
addTest(warning("Test method isn't public: "
|
| 223 |
+ theMethod.getName())); |
|
| 224 |
} |
|
| 225 |
} |
|
| 226 |
} |
|
| 227 |
|
|
| 228 |
/**
|
|
| 229 |
* @see junit.framework.TestSuite#exceptionToString(Throwable)
|
|
| 230 |
*/
|
|
| 231 | 0 |
private String exceptionToString(Throwable theThrowable)
|
| 232 |
{
|
|
| 233 | 0 |
StringWriter stringWriter = new StringWriter();
|
| 234 | 0 |
PrintWriter writer = new PrintWriter(stringWriter);
|
| 235 | 0 |
theThrowable.printStackTrace(writer); |
| 236 | 0 |
return stringWriter.toString();
|
| 237 |
} |
|
| 238 |
|
|
| 239 |
/**
|
|
| 240 |
* @see junit.framework.TestSuite#countTestCases()
|
|
| 241 |
*/
|
|
| 242 | 0 |
public int countTestCases() |
| 243 |
{
|
|
| 244 | 0 |
int count = 0;
|
| 245 | 0 |
for (Enumeration e = tests(); e.hasMoreElements();)
|
| 246 |
{
|
|
| 247 | 0 |
Test test = (Test) e.nextElement(); |
| 248 | 0 |
count = count + test.countTestCases(); |
| 249 |
} |
|
| 250 | 0 |
return count;
|
| 251 |
} |
|
| 252 |
|
|
| 253 |
/**
|
|
| 254 |
* @see junit.framework.TestSuite#isPublicTestMethod(Method)
|
|
| 255 |
*/
|
|
| 256 | 0 |
private boolean isPublicTestMethod(Method theMethod) |
| 257 |
{
|
|
| 258 | 0 |
return isTestMethod(theMethod)
|
| 259 |
&& Modifier.isPublic(theMethod.getModifiers()); |
|
| 260 |
} |
|
| 261 |
|
|
| 262 |
/**
|
|
| 263 |
* @see junit.framework.TestSuite#isTestMethod(Method)
|
|
| 264 |
*/
|
|
| 265 | 0 |
private boolean isTestMethod(Method theMethod) |
| 266 |
{
|
|
| 267 | 0 |
String name = theMethod.getName(); |
| 268 | 0 |
Class[] parameters = theMethod.getParameterTypes(); |
| 269 | 0 |
Class returnType = theMethod.getReturnType(); |
| 270 | 0 |
return parameters.length == 0
|
| 271 |
&& name.startsWith("test")
|
|
| 272 |
&& returnType.equals(Void.TYPE); |
|
| 273 |
} |
|
| 274 |
|
|
| 275 |
/**
|
|
| 276 |
* @see junit.framework.TestSuite#run(TestResult)
|
|
| 277 |
*/
|
|
| 278 | 0 |
public void run(TestResult theResult) |
| 279 |
{
|
|
| 280 | 0 |
for (Enumeration e = tests(); e.hasMoreElements();)
|
| 281 |
{
|
|
| 282 | 0 |
if (theResult.shouldStop())
|
| 283 |
{
|
|
| 284 | 0 |
break;
|
| 285 |
} |
|
| 286 | 0 |
Test test = (Test) e.nextElement(); |
| 287 | 0 |
runTest(test, theResult); |
| 288 |
} |
|
| 289 |
} |
|
| 290 |
|
|
| 291 |
/**
|
|
| 292 |
* @see junit.framework.TestSuite#runTest(Test, TestResult)
|
|
| 293 |
*/
|
|
| 294 | 0 |
public void runTest(Test theTest, TestResult theResult) |
| 295 |
{
|
|
| 296 | 0 |
theTest.run(theResult); |
| 297 |
} |
|
| 298 |
|
|
| 299 |
/**
|
|
| 300 |
* @see junit.framework.TestSuite#testAt(int)
|
|
| 301 |
*/
|
|
| 302 | 0 |
public Test testAt(int theIndex) |
| 303 |
{
|
|
| 304 | 0 |
return (Test) this.tests.elementAt(theIndex); |
| 305 |
} |
|
| 306 |
|
|
| 307 |
/**
|
|
| 308 |
* Gets a constructor which takes a single String as
|
|
| 309 |
* its argument or a no arg constructor.
|
|
| 310 |
*
|
|
| 311 |
* @param theClass the class for which to find the constructor
|
|
| 312 |
* @return the valid constructor found
|
|
| 313 |
* @exception NoSuchMethodException if no valid constructor is
|
|
| 314 |
* found
|
|
| 315 |
*/
|
|
| 316 | 0 |
public static Constructor getTestConstructor(Class theClass) |
| 317 |
throws NoSuchMethodException
|
|
| 318 |
{
|
|
| 319 | 0 |
Class[] args = {String.class};
|
| 320 | 0 |
try
|
| 321 |
{
|
|
| 322 | 0 |
return theClass.getConstructor(args);
|
| 323 |
} |
|
| 324 |
catch (NoSuchMethodException e)
|
|
| 325 |
{
|
|
| 326 |
// fall through
|
|
| 327 |
} |
|
| 328 | 0 |
return theClass.getConstructor(new Class[0]); |
| 329 |
} |
|
| 330 |
|
|
| 331 |
/**
|
|
| 332 |
* @see junit.framework.TestSuite#testCount()
|
|
| 333 |
*/
|
|
| 334 | 0 |
public int testCount() |
| 335 |
{
|
|
| 336 | 0 |
return this.tests.size(); |
| 337 |
} |
|
| 338 |
|
|
| 339 |
/**
|
|
| 340 |
* @see junit.framework.TestSuite#tests()
|
|
| 341 |
*/
|
|
| 342 | 0 |
public Enumeration tests()
|
| 343 |
{
|
|
| 344 | 0 |
return this.tests.elements(); |
| 345 |
} |
|
| 346 |
|
|
| 347 |
/**
|
|
| 348 |
* @see junit.framework.TestSuite#toString()
|
|
| 349 |
*/
|
|
| 350 | 0 |
public String toString()
|
| 351 |
{
|
|
| 352 | 0 |
if (getName() != null) |
| 353 |
{
|
|
| 354 | 0 |
return getName();
|
| 355 |
} |
|
| 356 | 0 |
return super.toString(); |
| 357 |
} |
|
| 358 |
|
|
| 359 |
/**
|
|
| 360 |
* @see junit.framework.TestSuite#setName(String)
|
|
| 361 |
*/
|
|
| 362 | 0 |
public void setName(String theName) |
| 363 |
{
|
|
| 364 | 0 |
this.name = theName;
|
| 365 |
} |
|
| 366 |
|
|
| 367 |
/**
|
|
| 368 |
* @see junit.framework.TestSuite#getName()
|
|
| 369 |
*/
|
|
| 370 | 0 |
public String getName()
|
| 371 |
{
|
|
| 372 | 0 |
return this.name; |
| 373 |
} |
|
| 374 |
|
|
| 375 |
/**
|
|
| 376 |
* @see junit.framework.TestSuite#warning(String)
|
|
| 377 |
*/
|
|
| 378 | 0 |
private static Test warning(final String theMessage) |
| 379 |
{
|
|
| 380 | 0 |
return new TestCase("warning") |
| 381 |
{
|
|
| 382 | 0 |
protected void runTest() |
| 383 |
{
|
|
| 384 | 0 |
fail(theMessage); |
| 385 |
} |
|
| 386 |
}; |
|
| 387 |
} |
|
| 388 |
|
|
| 389 |
/**
|
|
| 390 |
* @param theTestClass the test class containing the tests to be included
|
|
| 391 |
* in the Cactus Test Suite
|
|
| 392 |
* @return a Cactus Test Suite (ex: ServletTestSuite) initialized with a
|
|
| 393 |
* test class
|
|
| 394 |
*/
|
|
| 395 |
protected abstract Test createTestSuite(Class theTestClass);
|
|
| 396 |
|
|
| 397 |
/**
|
|
| 398 |
* @param theName the name of the Cactus Test Case
|
|
| 399 |
* @param theTest the wrapped test
|
|
| 400 |
* @return a Cactus Test Case object initialized with the give name and
|
|
| 401 |
* wrapped test
|
|
| 402 |
*/
|
|
| 403 |
protected abstract Test createCactusTestCase(String theName, Test theTest);
|
|
| 404 |
} |
|
| 405 |
|
|
||||||||||