1 20 package org.apache.cactus.internal.server; 21 22 import java.io.IOException ; 23 import java.io.Writer ; 24 25 import java.lang.reflect.Constructor ; 26 27 import javax.servlet.ServletException ; 28 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 32 import org.apache.cactus.internal.CactusTestCase; 33 import org.apache.cactus.internal.HttpServiceDefinition; 34 import org.apache.cactus.internal.ServiceEnumeration; 35 import org.apache.cactus.internal.WebTestResult; 36 import org.apache.cactus.internal.configuration.Version; 37 import org.apache.cactus.internal.util.ClassLoaderUtils; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 48 public abstract class AbstractWebTestCaller 49 { 50 54 protected static final String TEST_RESULTS = 55 "ServletTestRedirector_TestResults"; 56 57 60 private static final Log LOGGER = 61 LogFactory.getLog(AbstractWebTestCaller.class); 62 63 67 protected WebImplicitObjects webImplicitObjects; 68 69 72 public AbstractWebTestCaller(WebImplicitObjects theObjects) 73 { 74 this.webImplicitObjects = theObjects; 75 } 76 77 85 protected abstract void setTestCaseFields(TestCase theTestCase) 86 throws Exception ; 87 88 93 protected abstract Writer getResponseWriter() throws IOException ; 94 95 102 public void doTest() throws ServletException 103 { 104 WebTestResult result = null; 105 106 try 107 { 108 TestCase testInstance = getTestClassInstance( 110 getTestClassName(), getWrappedTestClassName(), 111 getTestMethodName()); 112 113 setTestCaseFields(testInstance); 115 116 if (testInstance instanceof CactusTestCase) 118 { 119 ((CactusTestCase) testInstance).runBareServer(); 120 121 } 122 else 123 { 124 testInstance.runBare(); 125 } 126 127 result = new WebTestResult(); 130 } 131 catch (Throwable e) 132 { 133 result = new WebTestResult(e); 136 } 137 138 LOGGER.debug("Test result : [" + result + "]"); 139 140 141 this.webImplicitObjects.getServletContext() 143 .setAttribute(TEST_RESULTS, result); 144 145 LOGGER.debug("Result saved in context scope"); 146 } 147 148 153 public void doGetResults() throws ServletException 154 { 155 WebTestResult result = (WebTestResult) (this.webImplicitObjects 163 .getServletContext().getAttribute(TEST_RESULTS)); 164 165 if (result == null) 170 { 171 String message = "Error getting test result. This could happen " 172 + "for example if you're using a load-balancer. Please disable " 173 + "it before running Cactus tests."; 174 175 LOGGER.error(message); 176 throw new ServletException (message); 177 } 178 179 LOGGER.debug("Test Result = [" + result + "]"); 180 181 183 webImplicitObjects.getHttpServletResponse().setContentType( 185 "text/xml; charset=UTF-8"); 186 187 try 188 { 189 Writer writer = getResponseWriter(); 190 191 writer.write(result.toXml()); 192 writer.close(); 193 } 194 catch (IOException e) 195 { 196 String message = "Error writing WebTestResult instance to output " 197 + "stream"; 198 199 LOGGER.error(message, e); 200 throw new ServletException (message, e); 201 } 202 } 203 204 210 public void doRunTest() throws ServletException 211 { 212 } 216 217 223 public void doGetVersion() throws ServletException 224 { 225 try 226 { 227 Writer writer = getResponseWriter(); 228 writer.write(Version.VERSION); 229 writer.close(); 230 } 231 catch (IOException e) 232 { 233 String message = "Error writing HTTP response back to client " 234 + "for service [" + ServiceEnumeration.GET_VERSION_SERVICE 235 + "]"; 236 237 LOGGER.error(message, e); 238 throw new ServletException (message, e); 239 } 240 } 241 242 249 public void doCreateSession() throws ServletException 250 { 251 this.webImplicitObjects.getHttpServletRequest().getSession(true); 253 254 try 255 { 256 Writer writer = getResponseWriter(); 257 writer.close(); 258 } 259 catch (IOException e) 260 { 261 String message = "Error writing HTTP response back to client " 262 + "for service [" + ServiceEnumeration.CREATE_SESSION_SERVICE 263 + "]"; 264 265 LOGGER.error(message, e); 266 throw new ServletException (message, e); 267 } 268 } 269 270 275 protected String getTestClassName() throws ServletException 276 { 277 String queryString = this.webImplicitObjects.getHttpServletRequest() 278 .getQueryString(); 279 String className = ServletUtil.getQueryStringParameter(queryString, 280 HttpServiceDefinition.CLASS_NAME_PARAM); 281 282 if (className == null) 283 { 284 String message = "Missing class name parameter [" 285 + HttpServiceDefinition.CLASS_NAME_PARAM 286 + "] in HTTP request."; 287 288 LOGGER.error(message); 289 throw new ServletException (message); 290 } 291 292 LOGGER.debug("Class to call = [" + className + "]"); 293 294 return className; 295 } 296 297 303 protected String getWrappedTestClassName() throws ServletException 304 { 305 String queryString = this.webImplicitObjects.getHttpServletRequest() 306 .getQueryString(); 307 String className = ServletUtil.getQueryStringParameter(queryString, 308 HttpServiceDefinition.WRAPPED_CLASS_NAME_PARAM); 309 310 if (className == null) 311 { 312 LOGGER.debug("No wrapped test class"); 313 } 314 else 315 { 316 LOGGER.debug("Wrapped test class = [" + className + "]"); 317 } 318 319 return className; 320 } 321 322 328 protected String getTestMethodName() throws ServletException 329 { 330 String queryString = this.webImplicitObjects.getHttpServletRequest() 331 .getQueryString(); 332 String methodName = ServletUtil.getQueryStringParameter(queryString, 333 HttpServiceDefinition.METHOD_NAME_PARAM); 334 335 if (methodName == null) 336 { 337 String message = "Missing method name parameter [" 338 + HttpServiceDefinition.METHOD_NAME_PARAM 339 + "] in HTTP request."; 340 341 LOGGER.error(message); 342 throw new ServletException (message); 343 } 344 345 LOGGER.debug("Method to call = " + methodName); 346 347 return methodName; 348 } 349 350 354 protected boolean isAutoSession() 355 { 356 String queryString = this.webImplicitObjects.getHttpServletRequest() 357 .getQueryString(); 358 String autoSession = ServletUtil.getQueryStringParameter(queryString, 359 HttpServiceDefinition.AUTOSESSION_NAME_PARAM); 360 361 boolean isAutomaticSession = 362 Boolean.valueOf(autoSession).booleanValue(); 363 364 LOGGER.debug("Auto session is " + isAutomaticSession); 365 366 return isAutomaticSession; 367 } 368 369 379 protected TestCase getTestClassInstance( 380 String theClassName, String theWrappedClassName, 381 String theTestCaseName) throws ServletException 382 { 383 Class testClass = getTestClassClass(theClassName); 385 TestCase testInstance = null; 386 Constructor constructor; 387 388 try 389 { 390 if (theWrappedClassName == null) 391 { 392 constructor = getTestClassConstructor(testClass); 393 394 if (constructor.getParameterTypes().length == 0) 395 { 396 testInstance = (TestCase) constructor.newInstance( 397 new Object [0]); 398 ((TestCase) testInstance).setName(theTestCaseName); 399 } 400 else 401 { 402 testInstance = (TestCase) constructor.newInstance( 403 new Object [] {theTestCaseName}); 404 } 405 } 406 else 407 { 408 Class wrappedTestClass = 409 getTestClassClass(theWrappedClassName); 410 Constructor wrappedConstructor = 411 getTestClassConstructor(wrappedTestClass); 412 413 TestCase wrappedTestInstance; 414 if (wrappedConstructor.getParameterTypes().length == 0) 415 { 416 wrappedTestInstance = 417 (TestCase) wrappedConstructor.newInstance( 418 new Object [0]); 419 wrappedTestInstance.setName(theTestCaseName); 420 } 421 else 422 { 423 wrappedTestInstance = 424 (TestCase) wrappedConstructor.newInstance( 425 new Object [] {theTestCaseName}); 426 } 427 428 constructor = testClass.getConstructor( 429 new Class [] {String .class, Test.class}); 430 431 testInstance = 432 (TestCase) constructor.newInstance( 433 new Object [] {theTestCaseName, wrappedTestInstance}); 434 } 435 } 436 catch (Exception e) 437 { 438 String message = "Error instantiating class [" + theClassName + "([" 439 + theTestCaseName + "], [" + theWrappedClassName + "])]"; 440 441 LOGGER.error(message, e); 442 throw new ServletException (message, e); 443 } 444 445 return testInstance; 446 } 447 448 454 private Constructor getTestClassConstructor(Class theTestClass) 455 throws NoSuchMethodException 456 { 457 Constructor constructor; 458 try 459 { 460 constructor = theTestClass.getConstructor( 461 new Class [] {String .class}); 462 } 463 catch (NoSuchMethodException e) 464 { 465 constructor = theTestClass.getConstructor(new Class [0]); 466 } 467 return constructor; 468 } 469 470 477 protected Class getTestClassClass(String theClassName) 478 throws ServletException 479 { 480 Class testClass = null; 482 483 try 484 { 485 testClass = ClassLoaderUtils.loadClass(theClassName, 486 this.getClass()); 487 } 488 catch (Exception e) 489 { 490 String message = "Error finding class [" + theClassName 491 + "] using both the Context classloader and the webapp " 492 + "classloader. Possible causes include:\r\n"; 493 494 message += ("\t- Your webapp does not include your test " 495 + "classes,\r\n"); 496 message += ("\t- The cactus.jar is not located in your " 497 + "WEB-INF/lib directory and your Container has not set the " 498 + "Context classloader to point to the webapp one"); 499 500 LOGGER.error(message, e); 501 throw new ServletException (message, e); 502 } 503 504 return testClass; 505 } 506 507 } 508 | Popular Tags |