1 20 package org.apache.cactus.internal.util; 21 22 import java.lang.reflect.Method ; 23 import junit.framework.AssertionFailedError; 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import org.apache.cactus.Request; 27 import org.apache.cactus.WebResponse; 28 29 34 public class TestTestCaseImplementChecker extends TestCase 35 { 36 39 class NormalTest extends TestCase 40 { 41 44 public NormalTest(String theName) 45 { 46 super(theName); 47 } 48 49 52 public void testDummy() 53 { 54 } 55 } 56 57 60 class NoNameTest extends TestCase 61 { 62 65 public NoNameTest(String theName) 66 { 67 } 68 69 72 public void testDummy() 73 { 74 } 75 } 76 77 81 class MethodHolder 82 { 83 85 public MethodHolder() 86 { 87 } 88 89 91 94 public void beginNormal(Request theRequest) 95 { 96 } 97 98 102 public String beginReturnsString(Request theRequest) 103 { 104 return "a string"; 105 } 106 107 110 protected void beginProtected(Request theRequest) 111 { 112 } 113 114 117 private void beginPrivate(Request theRequest) 118 { 119 } 120 121 123 public void beginNoParam() 124 { 125 } 126 127 131 public void beginWithTwoParams(Request theRequest, Object theObject) 132 { 133 } 134 135 138 public void beginWithStringParam(String theString) 139 { 140 } 141 142 144 147 public void endNormal(WebResponse theResponse) 148 { 149 } 150 151 155 public String endReturnsString(WebResponse theResponse) 156 { 157 return "a string"; 158 } 159 160 163 protected void endProtected(WebResponse theResponse) 164 { 165 } 166 167 170 private void endPrivate(WebResponse theResponse) 171 { 172 } 173 174 176 public void endNoParam() 177 { 178 } 179 180 184 public void endWithTwoParams(WebResponse theResponse, Object theObject) 185 { 186 } 187 188 191 public void endWithStringParam(String theString) 192 { 193 } 194 } 195 196 199 private String shouldNotHere() 200 { 201 return "shold not be here"; 202 } 203 204 208 private String shouldNotHere(Throwable theThrowable) 209 { 210 return shouldNotHere() + ": " + theThrowable.getClass().getName() 211 + "[" + theThrowable.getMessage() + "]"; 212 } 213 214 217 public void testCheckTestName() 218 { 219 Test test; 220 try 221 { 222 test = new NormalTest("testDummy"); 223 TestCaseImplementChecker.checkTestName(test); 224 } 225 catch (Throwable t) 226 { 227 fail(shouldNotHere(t)); 228 } 229 230 try 231 { 232 test = new NoNameTest("testDummy"); 233 TestCaseImplementChecker.checkTestName(test); 234 fail(shouldNotHere()); 235 } 236 catch (TestCaseImplementError e) 237 { 238 assertEquals("No test name found. The test [" 239 + TestTestCaseImplementChecker.NoNameTest.class.getName() 240 + "] is not properly implemented.", e.getMessage()); 241 } 242 catch (Throwable t) 243 { 244 fail(shouldNotHere(t)); 245 } 246 } 247 248 251 public void testCheckAsBeginMethod() 252 { 253 255 try 256 { 257 Method method = MethodHolder.class.getMethod( 258 "beginNormal", new Class []{Request.class}); 259 TestCaseImplementChecker.checkAsBeginMethod(method); 260 } 261 catch (Throwable t) 262 { 263 fail(shouldNotHere(t)); 264 } 265 266 268 try 269 { 270 Method method = MethodHolder.class.getMethod( 271 "beginReturnsString", new Class []{Request.class}); 272 TestCaseImplementChecker.checkAsBeginMethod(method); 273 fail(shouldNotHere()); 274 } 275 catch (AssertionFailedError e) 276 { 277 assertEquals("The method [beginReturnsString] " 278 + "should return void and not [java.lang.String]", 279 e.getMessage()); 280 } 281 catch (Throwable t) 282 { 283 fail(shouldNotHere(t)); 284 } 285 286 288 try 289 { 290 Method method = MethodHolder.class.getDeclaredMethod( 291 "beginProtected", new Class []{Request.class}); 292 TestCaseImplementChecker.checkAsBeginMethod(method); 293 fail(shouldNotHere()); 294 } 295 catch (AssertionFailedError e) 296 { 297 assertEquals("The method [beginProtected] " 298 + "should be declared public", e.getMessage()); 299 } 300 catch (Throwable t) 301 { 302 fail(shouldNotHere(t)); 303 } 304 305 307 try 308 { 309 Method method = MethodHolder.class.getDeclaredMethod( 310 "beginPrivate", new Class []{Request.class}); 311 TestCaseImplementChecker.checkAsBeginMethod(method); 312 fail(shouldNotHere()); 313 } 314 catch (AssertionFailedError e) 315 { 316 assertEquals("The method [beginPrivate] " 317 + "should be declared public", e.getMessage()); 318 } 319 catch (Throwable t) 320 { 321 fail(shouldNotHere(t)); 322 } 323 324 326 try 327 { 328 Method method = MethodHolder.class.getMethod( 329 "beginNoParam", new Class []{}); 330 TestCaseImplementChecker.checkAsBeginMethod(method); 331 fail(shouldNotHere()); 332 } 333 catch (AssertionFailedError e) 334 { 335 assertEquals("The method [beginNoParam] must have 1 parameter(s), " 336 + "but 0 parameter(s) were found", e.getMessage()); 337 } 338 catch (Throwable t) 339 { 340 fail(shouldNotHere(t)); 341 } 342 343 345 try 346 { 347 Method method = MethodHolder.class.getMethod( 348 "beginWithTwoParams", 349 new Class []{Request.class, Object .class}); 350 TestCaseImplementChecker.checkAsBeginMethod(method); 351 fail(shouldNotHere()); 352 } 353 catch (AssertionFailedError e) 354 { 355 assertEquals("The method [beginWithTwoParams] " 356 + "must have 1 parameter(s), " 357 + "but 2 parameter(s) were found", e.getMessage()); 358 } 359 catch (Throwable t) 360 { 361 fail(shouldNotHere(t)); 362 } 363 364 try 366 { 367 Method method = MethodHolder.class.getMethod( 368 "beginWithStringParam", new Class []{String .class}); 369 TestCaseImplementChecker.checkAsBeginMethod(method); 370 fail(shouldNotHere()); 371 } 372 catch (AssertionFailedError e) 373 { 374 assertEquals("The method [beginWithStringParam] " 375 + "must accept [org.apache.cactus.Request] " 376 + "as 1st parameter, but found a " 377 + "[java.lang.String] parameter instead", 378 e.getMessage()); 379 } 380 catch (Throwable t) 381 { 382 fail(shouldNotHere(t)); 383 } 384 } 385 386 387 391 public void testCheckAsEndMethod() 392 { 393 395 try 396 { 397 Method method = MethodHolder.class.getMethod( 398 "endNormal", new Class []{WebResponse.class}); 399 TestCaseImplementChecker.checkAsEndMethod(method); 400 } 401 catch (Throwable t) 402 { 403 fail(shouldNotHere(t)); 404 } 405 406 408 try 409 { 410 Method method = MethodHolder.class.getMethod( 411 "endReturnsString", new Class []{WebResponse.class}); 412 TestCaseImplementChecker.checkAsEndMethod(method); 413 fail(shouldNotHere()); 414 } 415 catch (AssertionFailedError e) 416 { 417 assertEquals("The method [endReturnsString] " 418 + "should return void and not [java.lang.String]", 419 e.getMessage()); 420 } 421 catch (Throwable t) 422 { 423 fail(shouldNotHere(t)); 424 } 425 426 428 try 429 { 430 Method method = MethodHolder.class.getDeclaredMethod( 431 "endProtected", new Class []{WebResponse.class}); 432 TestCaseImplementChecker.checkAsEndMethod(method); 433 fail(shouldNotHere()); 434 } 435 catch (AssertionFailedError e) 436 { 437 assertEquals("The method [endProtected] " 438 + "should be declared public", e.getMessage()); 439 } 440 catch (Throwable t) 441 { 442 fail(shouldNotHere(t)); 443 } 444 445 447 try 448 { 449 Method method = MethodHolder.class.getDeclaredMethod( 450 "endPrivate", new Class []{WebResponse.class}); 451 TestCaseImplementChecker.checkAsEndMethod(method); 452 fail(shouldNotHere()); 453 } 454 catch (AssertionFailedError e) 455 { 456 assertEquals("The method [endPrivate] " 457 + "should be declared public", e.getMessage()); 458 } 459 catch (Throwable t) 460 { 461 fail(shouldNotHere(t)); 462 } 463 464 466 try 467 { 468 Method method = MethodHolder.class.getMethod( 469 "endNoParam", new Class []{}); 470 TestCaseImplementChecker.checkAsEndMethod(method); 471 fail(shouldNotHere()); 472 } 473 catch (AssertionFailedError e) 474 { 475 assertEquals("The method [endNoParam] must have 1 parameter(s), " 476 + "but 0 parameter(s) were found", e.getMessage()); 477 } 478 catch (Throwable t) 479 { 480 fail(shouldNotHere(t)); 481 } 482 483 485 try 486 { 487 Method method = MethodHolder.class.getMethod( 488 "endWithTwoParams", 489 new Class []{WebResponse.class, Object .class}); 490 TestCaseImplementChecker.checkAsEndMethod(method); 491 fail(shouldNotHere()); 492 } 493 catch (AssertionFailedError e) 494 { 495 assertEquals("The method [endWithTwoParams] " 496 + "must have 1 parameter(s), " 497 + "but 2 parameter(s) were found", e.getMessage()); 498 } 499 catch (Throwable t) 500 { 501 fail(shouldNotHere(t)); 502 } 503 } 504 } 505 | Popular Tags |