1 20 package org.apache.cactus.internal.client; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 25 import junit.framework.Assert; 26 import junit.framework.Test; 27 28 import org.apache.cactus.Request; 29 import org.apache.cactus.internal.util.JUnitVersionHelper; 30 import org.apache.cactus.internal.util.TestCaseImplementChecker; 31 import org.apache.cactus.spi.client.ResponseObjectFactory; 32 import org.apache.cactus.spi.client.connector.ProtocolHandler; 33 import org.apache.cactus.spi.client.connector.ProtocolState; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 46 public class ClientTestCaseCaller extends Assert 47 { 48 51 protected static final String TEST_METHOD_PREFIX = "test"; 52 53 56 protected static final String BEGIN_METHOD_PREFIX = "begin"; 57 58 61 protected static final String END_METHOD_PREFIX = "end"; 62 63 67 protected static final String CLIENT_GLOBAL_BEGIN_METHOD = "begin"; 68 69 73 protected static final String CLIENT_GLOBAL_END_METHOD = "end"; 74 75 78 private Log logger; 79 80 83 private Test wrappedTest; 84 85 88 private Test delegatedTest; 89 90 93 private ProtocolHandler protocolHandler; 94 95 97 104 public ClientTestCaseCaller(Test theDelegatedTest, 105 Test theWrappedTest, ProtocolHandler theProtocolHandler) 106 { 107 if (theDelegatedTest == null) 108 { 109 throw new IllegalStateException ( 110 "The test object passed must not be null"); 111 } 112 113 setDelegatedTest(theDelegatedTest); 114 setWrappedTest(theWrappedTest); 115 this.protocolHandler = theProtocolHandler; 116 } 117 118 120 135 public void runTest() throws Throwable 136 { 137 Request request = this.protocolHandler.createRequest(); 138 139 callGlobalBeginMethod(request); 141 callBeginMethod(request); 142 143 ProtocolState state = this.protocolHandler.runTest( 145 getDelegatedTest(), getWrappedTest(), request); 146 147 Object response = callEndMethod(request, 149 this.protocolHandler.createResponseObjectFactory(state)); 150 151 callGlobalEndMethod(request, 153 this.protocolHandler.createResponseObjectFactory(state), 154 response); 155 156 this.protocolHandler.afterTest(state); 157 } 158 159 163 public final Log getLogger() 164 { 165 return this.logger; 166 } 167 168 172 public void runBareInit() 173 { 174 this.logger = LogFactory.getLog(this.getClass()); 178 179 getLogger().debug("------------- Test: " + this.getCurrentTestName()); 181 } 182 183 190 public void callBeginMethod(Request theRequest) throws Throwable 191 { 192 callGenericBeginMethod(theRequest, getBeginMethodName()); 193 } 194 195 205 public Object callEndMethod(Request theRequest, 206 ResponseObjectFactory theResponseFactory) throws Throwable 207 { 208 return callGenericEndMethod(theRequest, theResponseFactory, 209 getEndMethodName(), null); 210 } 211 212 220 public void callGlobalBeginMethod(Request theRequest) throws Throwable 221 { 222 callGenericBeginMethod(theRequest, CLIENT_GLOBAL_BEGIN_METHOD); 223 } 224 225 235 private void callGlobalEndMethod(Request theRequest, 236 ResponseObjectFactory theResponseFactory, Object theResponse) 237 throws Throwable 238 { 239 callGenericEndMethod(theRequest, theResponseFactory, 240 CLIENT_GLOBAL_END_METHOD, theResponse); 241 } 242 243 245 248 private void setWrappedTest(Test theWrappedTest) 249 { 250 this.wrappedTest = theWrappedTest; 251 } 252 253 256 private void setDelegatedTest(Test theDelegatedTest) 257 { 258 this.delegatedTest = theDelegatedTest; 259 } 260 261 264 private Test getWrappedTest() 265 { 266 return this.wrappedTest; 267 } 268 269 272 private Test getDelegatedTest() 273 { 274 return this.delegatedTest; 275 } 276 277 282 private Test getTest() 283 { 284 Test activeTest; 285 if (getWrappedTest() != null) 286 { 287 activeTest = getWrappedTest(); 288 } 289 else 290 { 291 activeTest = getDelegatedTest(); 292 } 293 return activeTest; 294 } 295 296 300 private String getBaseMethodName() 301 { 302 if (!getCurrentTestName().startsWith(TEST_METHOD_PREFIX)) 304 { 305 throw new RuntimeException ("bad name [" 306 + getCurrentTestName() 307 + "]. It should start with [" 308 + TEST_METHOD_PREFIX + "]."); 309 } 310 311 return getCurrentTestName().substring( 312 TEST_METHOD_PREFIX.length()); 313 } 314 315 320 private String getBeginMethodName() 321 { 322 return BEGIN_METHOD_PREFIX + getBaseMethodName(); 323 } 324 325 330 private String getEndMethodName() 331 { 332 return END_METHOD_PREFIX + getBaseMethodName(); 333 } 334 335 343 private void callGenericBeginMethod(Request theRequest, 344 String theMethodName) throws Throwable 345 { 346 Method [] methods = getTest().getClass().getMethods(); 349 350 for (int i = 0; i < methods.length; i++) 351 { 352 if (methods[i].getName().equals(theMethodName)) 353 { 354 TestCaseImplementChecker.checkAsBeginMethod(methods[i]); 355 356 try 357 { 358 methods[i].invoke(getTest(), new Object [] {theRequest}); 359 360 break; 361 } 362 catch (InvocationTargetException e) 363 { 364 e.fillInStackTrace(); 365 throw e.getTargetException(); 366 } 367 catch (IllegalAccessException e) 368 { 369 e.fillInStackTrace(); 370 throw e; 371 } 372 } 373 } 374 } 375 376 390 private Object callGenericEndMethod(Request theRequest, 391 ResponseObjectFactory theResponseFactory, String theMethodName, 392 Object theResponse) throws Throwable 393 { 394 Method methodToCall = null; 395 Object paramObject = null; 396 397 Method [] methods = getTest().getClass().getMethods(); 398 399 for (int i = 0; i < methods.length; i++) 400 { 401 if (methods[i].getName().equals(theMethodName)) 402 { 403 TestCaseImplementChecker.checkAsEndMethod(methods[i]); 404 405 paramObject = theResponse; 406 407 if (paramObject == null) 408 { 409 Class [] parameters = methods[i].getParameterTypes(); 410 try 411 { 412 paramObject = theResponseFactory.getResponseObject( 413 parameters[0].getName(), theRequest); 414 } 415 catch (ClientException e) 416 { 417 throw new ClientException("The method [" 418 + methods[i].getName() 419 + "] has a bad parameter of type [" 420 + parameters[0].getName() + "]", e); 421 } 422 } 423 424 if (methodToCall != null) 426 { 427 fail("There can only be one method [" 428 + methods[i].getName() + "] per test case. " 429 + "Test case [" + this.getCurrentTestName() 430 + "] has two at least !"); 431 } 432 433 methodToCall = methods[i]; 434 } 435 } 436 437 if (methodToCall != null) 438 { 439 try 440 { 441 methodToCall.invoke(getTest(), new Object [] {paramObject}); 442 } 443 catch (InvocationTargetException e) 444 { 445 e.fillInStackTrace(); 446 throw e.getTargetException(); 447 } 448 catch (IllegalAccessException e) 449 { 450 e.fillInStackTrace(); 451 throw e; 452 } 453 } 454 455 return paramObject; 456 } 457 458 463 private String getCurrentTestName() 464 { 465 return JUnitVersionHelper.getTestCaseName(getDelegatedTest()); 466 } 467 } 468 | Popular Tags |