1 7 package ch.ethz.prose; 8 9 import java.lang.reflect.Field ; 11 import java.lang.reflect.Method ; 12 13 import junit.framework.*; 14 import ch.ethz.jvmai.*; 15 16 23 public 24 class JVMAspectInterfaceTest extends TestCase { 25 26 JVMAspectInterface aspectInterface = null; 27 Method method = null; 28 Field field = null; 29 Class exceptionClass = null; 30 31 35 public JVMAspectInterfaceTest(String name) 36 { 37 super(name); 38 } 39 40 static class TestHook extends JoinPointHook { 41 public int oFA,oFM,oMEn,oMEx,oExt, oExc, oCL; 42 public void reset() { oFA = 0; oFM = 0; oMEn = 0; oMEx = 0; oExt = 0; oExc = 0; oCL = 0; } 43 public void onFieldAccess (FieldAccessJoinPoint joinPoint) { oFA++; } 44 public void onFieldModification(FieldModificationJoinPoint joinPoint) { oFM++; } 45 public void onMethodEntry (MethodEntryJoinPoint joinPoint) { oMEn++; } 46 public void onMethodExit (MethodExitJoinPoint joinPoint) { oMEx++; } 47 public void onExceptionThrow (ExceptionJoinPoint joinPoint) { oExt++; } 48 public void onExceptionCatch (ExceptionCatchJoinPoint joinPoint) { oExc++; } 49 public void onClassLoad (Class cls) { oCL++; } 50 } 51 52 static class TestException extends Exception { 53 public TestException() {super(); } 54 public TestException(String s) {super(s);} 55 } 56 57 static class TestException2 extends Exception { 58 public TestException2() {super(); } 59 public TestException2(String s) {super(s);} 60 } 61 62 static class TestException3 extends Exception { 63 public TestException3() {super(); } 64 public TestException3(String s) {super(s);} 65 } 66 67 static class TestException4 extends RuntimeException { } 68 69 static class TestCatch { 70 public int field1; 71 public int field2; 72 public void method1() { } 73 public void throwMethod2(){ 74 try{ if (field1 == 1) throw new TestException(); }catch(TestException e){ } 75 } 76 public void throwMethod3(){ 77 try{ throw new TestException2(); } catch(TestException2 e) {} 78 } 79 public void throwMethod4(){ 80 try{ if (field2 == 2) throw new TestException2(); } catch (TestException2 e){ } 81 } 82 public void throwMethod5(){ 83 try{ if (field2 == 2) throw new TestException3(); } catch (TestException3 e){ } 84 } 85 public void throwMethod6(){ 86 try{ throw new TestException4(); } catch(Exception e) { } 87 } 88 } 89 90 static class TestClass1 { 91 public void method() { } 92 public int field; 93 public void throwMethod() {try {throw new TestException();} catch (TestException e) {} } 94 public void run() { 95 method(); 96 field = field + 1; 97 throwMethod(); 98 } 99 } 100 101 static abstract class TestClass2 { 102 } 103 104 static abstract class TestClass3 { 105 public abstract void method(); 106 } 107 108 static abstract class TestAbstractClass { 109 110 abstract void method08(); 112 abstract public void method09(); 113 abstract protected void method10(); 114 115 void method11() {} 117 public void method12() {} 118 protected void method13() {} 119 private void method14() {} 120 121 void method17() {} 123 public void method18() {} 124 protected void method19() {} 125 126 static void method20() {} 128 static public void method21() {} 129 static protected void method22() {} 130 131 132 int field00; 134 public int field01; 135 protected int field02; 136 private int field03; 137 138 int field04; 140 public int field05; 141 protected int field06; 142 143 static int field07; 145 static public int field08; 146 static protected int field09; 147 148 } 149 150 static interface TestInterface { 151 void method15(); 153 public void method16(); 154 155 } 156 157 static class TestClass4 extends TestAbstractClass implements TestInterface { 158 159 int field00; 161 public int field01; 162 protected int field02; 163 private int field03; 164 165 int field10; 167 public int field11; 168 protected int field12; 169 private int field13; 170 171 static int field14; 173 static public int field15; 174 static protected int field16; 175 static private int field17; 176 177 void method00() {} 179 public void method01() {} 180 protected void method02() {} 181 private void method03() {} 182 183 static void method04() {} 185 static public void method05() {} 186 static protected void method06() {} 187 static private void method07() {} 188 189 void method08() {} 191 public void method09() {} 192 protected void method10() {} 193 void method11() {} 195 public void method12() {} 196 protected void method13() {} 197 private void method14() {} 198 public void method15() {} 200 public void method16() {} 201 202 203 public void callMethods() { 204 method00(); 205 method01(); 206 method02(); 207 method03(); 208 method04(); 209 method05(); 210 method06(); 211 method07(); 212 method08(); 213 method09(); 214 method10(); 215 method11(); 216 method12(); 217 method13(); 218 method14(); 219 method15(); 220 method16(); 221 method17(); 222 method18(); 223 method19(); 224 method20(); 225 method21(); 226 method22(); 227 } 228 229 public void accessFields() { 230 field00 = field00+1; 231 field01 = field01+1; 232 field02 = field02+1; 233 field03 = field03+1; 234 field04 = field04+1; 235 field05 = field05+1; 236 field06 = field06+1; 237 field07 = field07+1; 238 field08 = field08+1; 239 field09 = field09+1; 240 field10 = field10+1; 241 field11 = field11+1; 242 field12 = field12+1; 243 field13 = field13+1; 244 field14 = field14+1; 245 field15 = field15+1; 246 field16 = field16+1; 247 field17 = field17+1; 248 } 249 250 } 251 252 static class TestClass5 253 { 254 int exit; 255 public void method0() 256 { 257 if (exit == 0) 258 return; 259 else 260 { 261 switch(exit) 262 { 263 case 2: return; 264 case 3: return; 265 } 266 return; 267 } 268 } 269 270 public int method1() 271 { 272 if (exit == 0) 273 return 0; 274 else 275 { 276 switch(exit) 277 { 278 case 2: return 2; 279 case 3: return 3; 280 } 281 return 4; 282 } 283 } 284 285 public Object method2() 286 { 287 if (exit == 0) 288 return "hi0"; 289 else 290 { 291 switch(exit) 292 { 293 case 2: return "hi2"; 294 case 3: return "hi3"; 295 } 296 return "hi4"; 297 } 298 } 299 300 public double method3() 301 { 302 if (exit == 0) 303 return 1.0; 304 else 305 { 306 switch(exit) 307 { 308 case 2: return 2.0; 309 case 3: return 3.0; 310 } 311 return 4.0; 312 } 313 } 314 } 315 316 static class TestThread extends Thread { 317 private TestClass1 test; 318 public TestThread(TestClass1 t) { test = t; } 319 public void run() 320 { 321 if(test==null) return; 322 test.run(); 323 } 324 } 325 326 protected void setUp() throws Exception 327 { 328 String providerClassName = System.getProperty("ch.ethz.prose.JVMAIProvider","ch.ethz.inf.iks.jvmai.jvmdi.DebuggerProvider"); 329 Class providerClass = Class.forName(providerClassName); 330 Provider provider = (Provider)providerClass.newInstance(); 331 aspectInterface = provider.getAspectInterface(); 332 method = TestClass1.class.getDeclaredMethods()[0]; 333 field = TestClass1.class.getDeclaredFields()[0]; 334 } 335 336 protected void tearDown() 337 { 338 try 339 { 340 341 try { aspectInterface.clearMethodEntryWatch (method); } catch (RuntimeException e) { } 342 try { aspectInterface.clearMethodExitWatch (method); } catch (RuntimeException e) { } 343 try { aspectInterface.clearFieldAccessWatch (field); } catch (RuntimeException e) { } 344 try { aspectInterface.clearFieldModificationWatch(field); } catch (RuntimeException e) { } 345 try { aspectInterface.clearExceptionThrowWatch (TestException.class); } catch (RuntimeException e) { } 346 try { aspectInterface.clearExceptionCatchWatch (TestException.class); } catch (RuntimeException e) { } 347 try { aspectInterface.setJoinPointHook(null); } catch (RuntimeException e) { } 348 } 349 catch (RuntimeException e) 350 { 351 } 352 } 353 354 public void test_0010_NotInitializedExceptions() 355 { 356 357 int exceptions = 0; 359 360 361 try { aspectInterface.setJoinPointHook(null); } 362 catch (RuntimeException e) { if(e instanceof NotInitializedException) {exceptions++; }} 363 364 try { aspectInterface.setFieldAccessWatch(null,null); } 365 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 366 367 try { aspectInterface.setFieldModificationWatch(null,null); } 368 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 369 370 try { aspectInterface.setMethodEntryWatch(null,null); } 371 catch (RuntimeException e) { if(e instanceof NotInitializedException) {exceptions++; }} 372 373 try { aspectInterface.setMethodExitWatch(null,null); } 374 catch (RuntimeException e) { if(e instanceof NotInitializedException) {exceptions++; }} 375 376 try { aspectInterface.setExceptionThrowWatch(null,null); } 377 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 378 379 try { aspectInterface.setExceptionCatchWatch(null,null); } 380 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 381 382 383 try { aspectInterface.clearFieldAccessWatch(null); } 384 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 385 386 try { aspectInterface.clearFieldModificationWatch(null); } 387 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 388 389 try { aspectInterface.clearMethodEntryWatch(null); } 390 catch (RuntimeException e) { if(e instanceof NotInitializedException) {exceptions++; }} 391 392 try { aspectInterface.clearMethodExitWatch(null); } 393 catch (RuntimeException e) { if(e instanceof NotInitializedException) {exceptions++; }} 394 395 try { aspectInterface.clearExceptionThrowWatch(null); } 396 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 397 398 try { aspectInterface.clearExceptionCatchWatch(null); } 399 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 400 401 try { aspectInterface.suspendNotification(null); } 402 catch (RuntimeException e) { if(e instanceof NotInitializedException) {exceptions++; }} 403 404 try { aspectInterface.resumeNotification(null); } 405 catch (RuntimeException e) { if(e instanceof NotInitializedException){ exceptions++; }} 406 407 assertEquals("13 NotInitializedException",exceptions,15); 408 409 } 410 411 public void test_0020_Startup() 412 { 413 try 414 { 415 try 416 { 417 aspectInterface.startup(new String [0],true); 418 aspectInterface.startup(new String [0],false); 419 } 420 catch (RuntimeException e) 421 { 422 assertTrue("calling startup a second time is ignored",false); 423 } 424 } 425 catch (Throwable e) 426 { 427 e.printStackTrace(); 428 throw new RuntimeException ("startup failed" + e); 429 } 430 431 } 432 433 public void test_0030_SetClearWatch() 434 { 435 436 Object aopTag = new Object (); 437 int exceptions = 0; 438 439 440 try { aspectInterface.setMethodEntryWatch (method,aopTag); } 442 catch (RuntimeException e) { exceptions++; System.err.println("A1");} 443 try { aspectInterface.setMethodExitWatch (method,aopTag); } 444 catch (RuntimeException e) { exceptions++; System.err.println("A2"); } 445 try { aspectInterface.setFieldAccessWatch (field ,aopTag); } 446 catch (RuntimeException e) { exceptions++; System.err.println("A3"); } 447 try { aspectInterface.setFieldModificationWatch(field ,aopTag); } 448 catch (RuntimeException e) { exceptions++; System.err.println("A4"); } 449 try { aspectInterface.setExceptionThrowWatch (TestException.class,aopTag); } 450 catch (RuntimeException e) { exceptions++; System.err.println("A5");} 451 try { aspectInterface.setExceptionCatchWatch (TestException.class,aopTag); } 452 catch (RuntimeException e) { exceptions++; System.err.println("A6");} 453 454 assertEquals("no exceptions when setting watches",0,exceptions); 455 456 457 try { aspectInterface.clearMethodEntryWatch (method); } catch (RuntimeException e) 459 { exceptions++; System.err.println("B1");} 460 try { aspectInterface.clearMethodExitWatch (method); } catch (RuntimeException e) 461 { exceptions++;System.err.println("B2"); } 462 try { aspectInterface.clearFieldAccessWatch (field ); } catch (RuntimeException e) 463 { exceptions++; System.err.println("B3");} 464 try { aspectInterface.clearFieldModificationWatch(field ); } catch (RuntimeException e) 465 { exceptions++; System.err.println("B4");} 466 try { aspectInterface.clearExceptionThrowWatch (TestException.class ); } catch (RuntimeException e) 467 { exceptions++;System.err.println("B5"); } 468 try { aspectInterface.clearExceptionCatchWatch (TestException.class ); } catch (RuntimeException e) 469 { exceptions++;System.err.println("B6"); } 470 assertEquals("no exceptions when clearing watches",0,exceptions); 471 } 472 473 public void test_0040_SetClearWatch_Exceptions() 474 { 475 476 Object aopTag = new Object (); 477 int exceptions = 0; 478 long id = 0; 479 480 exceptions = 0; 482 483 aspectInterface.setMethodEntryWatch (method,aopTag); 484 try { aspectInterface.setMethodEntryWatch (method,aopTag); } 485 catch (WatchAlreadySetException e) { exceptions++; } 486 487 aspectInterface.setMethodExitWatch (method,aopTag); 488 try { aspectInterface.setMethodExitWatch (method,aopTag); } 489 catch (WatchAlreadySetException e) { exceptions++; } 490 491 aspectInterface.setFieldAccessWatch (field ,aopTag); 492 try { aspectInterface.setFieldAccessWatch (field ,aopTag); } 493 catch (WatchAlreadySetException e) { exceptions++; } 494 495 aspectInterface.setFieldModificationWatch(field ,aopTag); 496 try { aspectInterface.setFieldModificationWatch(field ,aopTag); } 497 catch (WatchAlreadySetException e) { exceptions++; } 498 499 aspectInterface.setExceptionThrowWatch (TestException.class, aopTag); 500 try { aspectInterface.setExceptionThrowWatch (TestException.class, aopTag); } 501 catch (WatchAlreadySetException e) { exceptions++; } 502 503 aspectInterface.setExceptionCatchWatch (TestException.class, aopTag); 504 try { aspectInterface.setExceptionCatchWatch (TestException.class, aopTag); } 505 catch (WatchAlreadySetException e) { exceptions++; } 506 507 assertEquals("6 WatchAlreadySetExceptions",6,exceptions); 508 509 exceptions = 0; 511 aspectInterface.clearMethodEntryWatch (method); 512 513 try { aspectInterface.clearMethodEntryWatch (method); } 514 catch (WatchNotSetException e) { exceptions++; } 515 516 aspectInterface.clearMethodExitWatch (method); 517 try { aspectInterface.clearMethodExitWatch (method); } 518 catch (WatchNotSetException e) { exceptions++; } 519 520 aspectInterface.clearFieldAccessWatch (field); 521 try { aspectInterface.clearFieldAccessWatch (field); } 522 catch (WatchNotSetException e) { exceptions++; } 523 524 aspectInterface.clearFieldModificationWatch(field); 525 try { aspectInterface.clearFieldModificationWatch(field); } 526 catch (WatchNotSetException e) { exceptions++; } 527 528 aspectInterface.clearExceptionThrowWatch (TestException.class ); 529 try { aspectInterface.clearExceptionThrowWatch (TestException.class ); } 530 catch (WatchNotSetException e) { exceptions++; } 531 532 aspectInterface.clearExceptionCatchWatch (TestException.class ); 533 try { aspectInterface.clearExceptionCatchWatch (TestException.class ); } 534 catch (WatchNotSetException e) { exceptions++; } 535 536 assertEquals("6 WatchNotSetExceptions",6,exceptions); 537 538 539 exceptions = 0; 540 541 method = TestInterface.class.getDeclaredMethods()[0]; 543 exceptions = 0; 544 try { aspectInterface.setMethodEntryWatch(method,aopTag); } catch (CannotSetWatchException e) { exceptions++; } 545 try { aspectInterface.setMethodExitWatch (method,aopTag); } catch (CannotSetWatchException e) { exceptions++; } 546 assertTrue("2 CannotSetWatchExceptions (interface method)",exceptions==2); 547 548 method = TestClass3.class.getDeclaredMethods()[0]; 550 exceptions = 0; 551 try { aspectInterface.setMethodEntryWatch(method,aopTag); } catch (CannotSetWatchException e) { exceptions++; } 552 try { aspectInterface.setMethodExitWatch (method,aopTag); } catch (CannotSetWatchException e) { exceptions++; } 553 assertTrue("2 CannotSetWatchExceptions (abstract method)",exceptions==2); 554 555 exceptions = 0; 557 try { aspectInterface.setMethodEntryWatch (null,aopTag); } catch (NullPointerException e) { exceptions++; } 558 try { aspectInterface.setMethodExitWatch (null,aopTag); } catch (NullPointerException e) { exceptions++; } 559 try { aspectInterface.setFieldAccessWatch (null,aopTag); } catch (NullPointerException e) { exceptions++; } 560 try { aspectInterface.setFieldModificationWatch (null,aopTag); } catch (NullPointerException e) { exceptions++; } 561 try { aspectInterface.setExceptionThrowWatch (null, aopTag); } catch (NullPointerException e) { exceptions++; } 562 try { aspectInterface.setExceptionCatchWatch (null, aopTag); } catch (NullPointerException e) { exceptions++; } 563 try { aspectInterface.clearMethodEntryWatch (null); } catch (NullPointerException e) { exceptions++; } 564 try { aspectInterface.clearMethodExitWatch (null); } catch (NullPointerException e) { exceptions++; } 565 try { aspectInterface.clearFieldAccessWatch (null); } catch (NullPointerException e) { exceptions++; } 566 try { aspectInterface.clearFieldModificationWatch(null); } catch (NullPointerException e) { exceptions++; } 567 try { aspectInterface.clearExceptionThrowWatch (null); } catch (NullPointerException e) { exceptions++; } 568 try { aspectInterface.clearExceptionCatchWatch (null); } catch (NullPointerException e) { exceptions++; } 569 assertTrue("12 NullPointerExceptions",exceptions==12); 570 571 } 572 573 public void test_0050_SetJoinPointHook() 574 { 575 int exceptions = 0; 576 TestHook hook = new TestHook(); 577 578 try { aspectInterface.setJoinPointHook(hook); } catch (RuntimeException e) { exceptions++; } 579 assertTrue("no exceptions while setting hook",exceptions==0); 580 581 try { aspectInterface.setJoinPointHook(hook); } catch (RuntimeException e) { exceptions++; } 583 assertTrue("no exceptions while re-setting hook",exceptions==0); 584 585 try { aspectInterface.setJoinPointHook(null); } catch (RuntimeException e) { exceptions++; } 587 assertTrue("no exceptions while setting hook to 'null'",exceptions==0); 588 589 try { aspectInterface.setJoinPointHook(hook); } catch (RuntimeException e) { exceptions++; } 590 assertTrue("no exceptions while setting hook",exceptions==0); 591 592 } 593 594 public void test_0060_HookNotification() 595 { 596 597 Object aopTag = new Object (); 598 TestHook hook = new TestHook(); 599 TestClass1 test = new TestClass1(); 600 601 aspectInterface.setMethodEntryWatch (method,aopTag); 602 aspectInterface.setMethodExitWatch (method,aopTag); 603 aspectInterface.setFieldAccessWatch (field ,aopTag); 604 aspectInterface.setFieldModificationWatch(field ,aopTag); 605 aspectInterface.setExceptionThrowWatch (TestException.class, aopTag); 606 aspectInterface.setExceptionCatchWatch (TestException.class, aopTag); 607 aspectInterface.resumeNotification(Thread.currentThread()); 608 609 aspectInterface.setJoinPointHook(hook); 610 hook.reset(); 611 test.run(); 612 613 assertEquals("1 field access event" ,1,hook.oFA); 614 assertEquals("1 field modification event",1,hook.oFM); 615 assertEquals("1 method entry event" ,1,hook.oMEn); 616 assertEquals("1 method exit event" ,1,hook.oMEx); 617 assertEquals("1 exception throw event(1)",1,hook.oExt); 618 assertEquals("1 exception catch event(1)",1,hook.oExc); 619 620 aspectInterface.setJoinPointHook(hook); 622 hook.reset(); 623 test.run(); 624 625 assertEquals("1 field access event" ,1,hook.oFA ); 626 assertEquals("1 field modification event",1,hook.oFM ); 627 assertEquals("1 method entry event" ,1,hook.oMEn); 628 assertEquals("1 method exit event" ,1,hook.oMEx); 629 assertEquals("1 exception throw event(2)",1,hook.oExt); 630 assertEquals("1 exception catch event(2)",1,hook.oExc); 631 632 aspectInterface.setJoinPointHook(null); 634 hook.reset(); 635 test.run(); 636 637 assertEquals("no field access events" ,0,hook.oFA ); 638 assertEquals("no field modification events",0,hook.oFM ); 639 assertEquals("no method entry events" ,0,hook.oMEn); 640 assertEquals("no method exit events" ,0,hook.oMEx); 641 assertEquals("no exception throw events" ,0,hook.oExt); 642 assertEquals("no exception catch events" ,0,hook.oExc); 643 644 aspectInterface.setJoinPointHook(hook); 645 hook.reset(); 646 test.run(); 647 648 assertEquals("1 field access event" ,1,hook.oFA ); 649 assertEquals("1 field modification event",1,hook.oFM ); 650 assertEquals("1 method entry event" ,1,hook.oMEn); 651 assertEquals("1 method exit event" ,1,hook.oMEx); 652 assertEquals("1 exception throw event" ,1,hook.oExt); 653 assertEquals("1 exception catch event" ,1,hook.oExc); 654 655 aspectInterface.clearMethodEntryWatch (method); 656 aspectInterface.clearMethodExitWatch (method); 657 aspectInterface.clearFieldAccessWatch (field); 658 aspectInterface.clearFieldModificationWatch(field); 659 aspectInterface.clearExceptionThrowWatch (TestException.class ); 660 aspectInterface.clearExceptionCatchWatch (TestException.class ); 661 aspectInterface.resumeNotification(Thread.currentThread()); 662 } 663 664 public void test_0070_AopTagHandling() 665 { 666 667 Class exception = TestException.class; 668 int exceptions = 0; 669 JoinPointHook hook = null; 670 TestClass1 test = new TestClass1(); 671 672 exceptions = 0; 674 try { aspectInterface.setMethodEntryWatch (method,null); } catch (RuntimeException e) { exceptions++; } 675 try { aspectInterface.setMethodExitWatch (method,null); } catch (RuntimeException e) { exceptions++; } 676 try { aspectInterface.setFieldAccessWatch (field ,null); } catch (RuntimeException e) { exceptions++; } 677 try { aspectInterface.setFieldModificationWatch (field ,null); } catch (RuntimeException e) { exceptions++; } 678 try { aspectInterface.setExceptionThrowWatch (TestException.class, null); } catch (RuntimeException e) { exceptions++; } 679 try { aspectInterface.setExceptionCatchWatch (TestException.class, null); } catch (RuntimeException e) { exceptions++; } 680 assertEquals("exceptions while setting watches with aopTag equals to 'null'",exceptions,6); 681 682 683 exceptions = 0; 685 final Object stringTag = "hi, I'm a tag"; 686 try { aspectInterface.setMethodEntryWatch (method,stringTag); } 687 catch (WatchAlreadySetException e) { exceptions++; } 688 689 try { aspectInterface.setMethodExitWatch (method,stringTag); } 690 catch (WatchAlreadySetException e) { exceptions++; } 691 692 try { aspectInterface.setFieldAccessWatch (field ,stringTag); } 693 catch (WatchAlreadySetException e) { exceptions++; } 694 695 try { aspectInterface.setFieldModificationWatch (field ,stringTag); } 696 catch (WatchAlreadySetException e) { exceptions++; } 697 698 try { aspectInterface.setExceptionThrowWatch (TestException.class, stringTag); } 699 catch (WatchAlreadySetException e) { exceptions++; } 700 701 try { aspectInterface.setExceptionCatchWatch (TestException.class, stringTag); } 702 catch (WatchAlreadySetException e) { exceptions++; } 703 704 assertEquals("NO exceptions while re-setting watches with non-null tags",exceptions,0); 705 706 exceptions = 0; 708 try { aspectInterface.setMethodEntryWatch (method,stringTag); } 709 catch (WatchAlreadySetException e) { exceptions++; } 710 711 try { aspectInterface.setMethodExitWatch (method,stringTag); } 712 catch (WatchAlreadySetException e) { exceptions++; } 713 714 try { aspectInterface.setFieldAccessWatch (field ,stringTag); } 715 catch (WatchAlreadySetException e) { exceptions++; } 716 717 try { aspectInterface.setFieldModificationWatch (field ,stringTag); } 718 catch (WatchAlreadySetException e) { exceptions++; } 719 720 try { aspectInterface.setExceptionThrowWatch (TestException.class,stringTag); } 721 catch (WatchAlreadySetException e) { exceptions++; } 722 723 try { aspectInterface.setExceptionCatchWatch (TestException.class,stringTag); } 724 catch (WatchAlreadySetException e) { exceptions++; } 725 726 assertEquals("6 exceptions while re-setting watches with non null tags",exceptions,6); 727 728 class TagTestHook extends JoinPointHook 730 { 731 Object omenTag; 732 Object omexTag; 733 Object ofmTag; 734 Object ofaTag; 735 Object oexTag; 736 Object oexcTag; 737 738 public void onFieldAccess (FieldAccessJoinPoint joinPoint) { ofaTag=joinPoint.getAopTag(); } 739 public void onMethodEntry (MethodEntryJoinPoint joinPoint) { omenTag=joinPoint.getAopTag(); } 740 public void onMethodExit (MethodExitJoinPoint joinPoint) {omexTag=joinPoint.getAopTag(); } 741 public void onExceptionThrow (ExceptionJoinPoint joinPoint) { oexTag=joinPoint.getAopTag(); } 742 public void onExceptionCatch (ExceptionCatchJoinPoint joinPoint) { oexcTag=joinPoint.getAopTag(); } 743 public void onFieldModification(FieldModificationJoinPoint joinPoint) {ofmTag=joinPoint.getAopTag(); } 744 745 public void onClassLoad(Class cls) { } 746 }; 747 748 TagTestHook tthook = new TagTestHook(); 749 750 aspectInterface.resumeNotification(Thread.currentThread()); 751 aspectInterface.setJoinPointHook(tthook); 752 test.run(); 753 754 assertEquals("FA:aopTag:",stringTag,tthook.ofaTag); 755 assertEquals("MEN:aopTag:",stringTag,tthook.omenTag); 756 assertEquals("MEX:aopTag:",stringTag,tthook.omexTag); 757 assertEquals("ET:aopTag:",stringTag,tthook.oexTag); 758 assertEquals("EC:aopTag:",stringTag,tthook.oexcTag); 759 assertEquals("FM:aopTag:",stringTag,tthook.ofmTag); 760 761 762 exceptions = 0; 764 try { aspectInterface.clearMethodEntryWatch (method); } catch (RuntimeException e) { exceptions++; } 765 try { aspectInterface.clearMethodExitWatch (method); } catch (RuntimeException e) { exceptions++; } 766 try { aspectInterface.clearFieldAccessWatch (field ); } catch (RuntimeException e) { exceptions++; } 767 try { aspectInterface.clearFieldModificationWatch(field ); } catch (RuntimeException e) { exceptions++; } 768 try { aspectInterface.clearExceptionThrowWatch (TestException.class ); } catch (RuntimeException e) { exceptions++; } 769 try { aspectInterface.clearExceptionCatchWatch (TestException.class ); } catch (RuntimeException e) { exceptions++; } 770 assertTrue("no exceptions while clearing watches with aopTag equals to 'null'",exceptions==0); 771 772 exceptions = 0;try { aspectInterface.clearFieldAccessWatch (field ); } catch (WatchNotSetException e) { exceptions++; } 774 775 try { aspectInterface.clearFieldModificationWatch(field ); } catch (WatchNotSetException e) { exceptions++; } 776 777 try { aspectInterface.clearMethodExitWatch (method); } catch (WatchNotSetException e) { exceptions++; } 778 779 try { aspectInterface.clearMethodEntryWatch (method); } catch (WatchNotSetException e) { exceptions++; } 780 781 try { aspectInterface.clearExceptionThrowWatch (TestException.class ); } catch (WatchNotSetException e) { exceptions++; } 782 783 try { aspectInterface.clearExceptionCatchWatch (TestException.class ); } catch (WatchNotSetException e) { exceptions++; } 784 785 assertEquals("exceptions/re-clearing watches",6,exceptions); 786 787 aspectInterface.setMethodEntryWatch (method,method); 789 aspectInterface.setMethodExitWatch (method,method); 790 aspectInterface.setFieldAccessWatch (field ,field ); 791 aspectInterface.setFieldModificationWatch(field ,field ); 792 aspectInterface.setExceptionThrowWatch (TestException.class, exception); 793 aspectInterface.setExceptionCatchWatch (TestException.class, exception); 794 795 hook = new JoinPointHook() { 796 public void onFieldAccess(FieldAccessJoinPoint joinPoint) { 797 Field field = joinPoint.getField(); 798 assertTrue("FieldAccessJoinPoint.getAopTag() contains correct field",field.equals(joinPoint.getAopTag())); 799 } 800 public void onFieldModification(FieldModificationJoinPoint joinPoint) { 801 Field field = joinPoint.getField(); 802 assertTrue("FieldModificationJoinPoint.getAopTag() contains correct field",field.equals(joinPoint.getAopTag())); 803 } 804 public void onMethodEntry(MethodEntryJoinPoint joinPoint) { 805 Method method = joinPoint.getMethod(); 806 assertTrue("MethodEntryJoinPoint.getAopTag() contains correct method",method.equals(joinPoint.getAopTag())); 807 } 808 public void onMethodExit(MethodExitJoinPoint joinPoint) { 809 Method method = joinPoint.getMethod(); 810 assertTrue("MethodExitJoinPoint.getAopTag() contains correct method",method.equals(joinPoint.getAopTag())); 811 } 812 public void onExceptionThrow(ExceptionJoinPoint joinPoint) { 813 Class exceptionClass = joinPoint.getException().getClass(); 814 assertTrue("ExceptionThrowJoinPoint.getAopTag() contains correct exception",exceptionClass.equals(joinPoint.getAopTag())); 815 } 816 public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) { 817 Class exceptionCatchClass = joinPoint.getException().getClass(); 818 assertTrue("ExceptionCatchJoinPoint.getAopTag() contains correct exception",exceptionCatchClass.equals(joinPoint.getAopTag())); 819 } 820 public void onClassLoad(Class cls) { } 821 }; 822 823 aspectInterface.resumeNotification(Thread.currentThread()); 824 aspectInterface.setJoinPointHook(hook); 825 test.method(); 826 test.field = test.field + 1; 827 test.throwMethod(); 828 829 aspectInterface.clearMethodEntryWatch (method); 830 aspectInterface.clearMethodExitWatch (method); 831 aspectInterface.clearFieldAccessWatch (field); 832 aspectInterface.clearFieldModificationWatch(field ); 833 aspectInterface.clearExceptionThrowWatch (TestException.class ); 834 aspectInterface.clearExceptionCatchWatch (TestException.class ); 835 836 aspectInterface.setJoinPointHook(null); 837 aspectInterface.resumeNotification(Thread.currentThread()); 838 839 } 840 841 842 public void test_0080_SuspendResumeNotification() 843 { 844 845 Object aopTag = new Object (); 846 int exceptions = 0; 847 TestHook hook = new TestHook(); 848 TestClass1 test = new TestClass1(); 849 TestThread thread = null; 850 851 aspectInterface.setMethodEntryWatch (method,aopTag); 852 aspectInterface.setMethodExitWatch (method,aopTag); 853 aspectInterface.setFieldAccessWatch (field ,aopTag); 854 aspectInterface.setFieldModificationWatch(field ,aopTag); 855 aspectInterface.setExceptionThrowWatch (TestException.class, aopTag); 856 aspectInterface.setExceptionCatchWatch (TestException.class, aopTag); 857 858 aspectInterface.resumeNotification(Thread.currentThread()); 859 aspectInterface.setJoinPointHook(hook); 860 861 hook.reset(); 863 test.run(); 864 assertEquals("no suspend test, notifications",6,hook.oMEx+hook.oMEn+hook.oFA+hook.oFM+hook.oExt+hook.oExc); 865 866 hook.reset(); 868 aspectInterface.suspendNotification(Thread.currentThread()); 869 test.run(); 870 aspectInterface.resumeNotification(Thread.currentThread()); 871 assertTrue("no notifications",hook.oMEx+hook.oMEn+hook.oFA+hook.oFM+hook.oExt+hook.oExc==0); 872 873 thread = new TestThread(test); 875 hook.reset(); 876 aspectInterface.suspendNotification(thread); 877 thread.start(); 878 test.run(); 879 try { thread.join(); } catch (InterruptedException e) { } 880 aspectInterface.resumeNotification(thread); 881 assertEquals("notifications from this thread",6,hook.oMEx+hook.oMEn+hook.oFA+hook.oFM+hook.oExt+hook.oExc); 882 883 thread = new TestThread(test); 885 hook.reset(); 886 thread.start(); 887 try { thread.join(); } catch (InterruptedException e) { } 888 assertEquals("notifications from other thread",6,hook.oMEx+hook.oMEn+hook.oFA+hook.oFM+hook.oExt+hook.oExc); 889 890 thread = new TestThread(test); 892 hook.reset(); 893 894 aspectInterface.suspendNotification(thread); 895 thread.start(); 900 try { thread.join(); } catch (InterruptedException e) { } 901 aspectInterface.resumeNotification(thread); 902 assertEquals("notifications after suspend",0,hook.oMEx+hook.oMEn+hook.oFA+hook.oFM+hook.oExt+hook.oExc); 903 904 aspectInterface.clearMethodEntryWatch (method); 905 aspectInterface.clearMethodExitWatch (method); 906 aspectInterface.clearFieldAccessWatch (field ); 907 aspectInterface.clearFieldModificationWatch(field ); 908 aspectInterface.clearExceptionThrowWatch (TestException.class ); 909 aspectInterface.clearExceptionCatchWatch (TestException.class ); 910 aspectInterface.resumeNotification(Thread.currentThread()); 911 } 912 913 public void test_0090_ClassLoadNotification() throws Exception 914 { 915 TestHook hook = new TestHook(); 916 aspectInterface.setJoinPointHook(hook); 917 Class cls = Class.forName("ch.ethz.prose.UnreferencedTestClass"); 918 aspectInterface.setJoinPointHook(null); 919 assertEquals("Number of 'onClassLoad' calls was increased",1,hook.oCL); 920 } 921 922 public void test_0100_ModifiersAndInheritance() 923 { 924 925 TestHook hook = null; 926 TestClass4 test = null; 927 Method [] methods = null; 928 Field [] fields = null; 929 930 methods = TestClass4.class.getDeclaredMethods(); 931 fields = TestClass4.class.getDeclaredFields(); 932 for(int m=0; m<methods.length; m++) { 933 aspectInterface.setMethodEntryWatch(methods[m],"entag"); 934 aspectInterface.setMethodExitWatch(methods[m],"extag"); 935 } 936 937 for(int f=0; f<fields.length; f++) { 938 aspectInterface.setFieldAccessWatch(fields[f],new Object ()); 939 aspectInterface.setFieldModificationWatch(fields[f],new Object ()); 940 } 941 942 test = new TestClass4(); 943 hook = new TestHook(); 944 aspectInterface.setJoinPointHook(hook); 945 aspectInterface.resumeNotification(Thread.currentThread()); 946 947 test.callMethods(); 948 test.accessFields(); 949 aspectInterface.setJoinPointHook(null); 950 assertTrue(hook.oMEn + " of "+methods.length+" method entry events received" ,hook.oMEn==methods.length); 951 assertTrue(hook.oMEx + " of "+methods.length+" method entry events received" ,hook.oMEx==methods.length); 952 assertTrue(hook.oFA + " of "+fields.length +" field access events received" ,hook.oFA ==fields.length ); 953 assertTrue(hook.oFM + " of "+fields.length +" field modification events received",hook.oFM ==fields.length ); 954 955 for(int m=0; m<methods.length; m++) { 956 aspectInterface.clearMethodEntryWatch(methods[m]); 957 aspectInterface.clearMethodExitWatch(methods[m]); 958 } 959 for(int f=0; f<fields.length; f++) { 960 aspectInterface.clearFieldAccessWatch(fields[f]); 961 aspectInterface.clearFieldModificationWatch(fields[f]); 962 } 963 aspectInterface.resumeNotification(Thread.currentThread()); 964 } 965 966 public void test_0110_MethodExitJoinPoints() 967 { 968 969 TestHook hook = null; 970 TestClass5 test = null; 971 Method [] methods = null; 972 973 974 methods = TestClass5.class.getDeclaredMethods(); 975 976 for(int m=0; m<methods.length; m++) 977 aspectInterface.setMethodExitWatch(methods[m],"extag"); 978 979 aspectInterface.resumeNotification(Thread.currentThread()); 980 981 test = new TestClass5(); 982 hook = new TestHook(); 983 aspectInterface.setJoinPointHook(hook); 984 for(int exitPoint=0; exitPoint<5;exitPoint++) 985 { 986 test.exit=exitPoint; 987 test.method0(); 988 test.method1(); 989 test.method2(); 990 test.method3(); 991 } 992 993 aspectInterface.setJoinPointHook(null); 994 995 assertEquals(hook.oMEx + " of "+methods.length+" method entry events received" ,methods.length*5,hook.oMEx); 996 997 for(int m=0; m<methods.length; m++) 998 aspectInterface.clearMethodExitWatch(methods[m]); 999 aspectInterface.resumeNotification(Thread.currentThread()); 1000 } 1001 1002 1003 public void test_0120_CatchThrow() 1004 { 1005 Object aopTag = new Object (); 1006 Object aopTag2 = new Object (); 1007 TestHook hook = new TestHook(); 1008 TestCatch testCatch = new TestCatch(); 1009 int exceptions = 0; 1010 1011 aspectInterface.setExceptionThrowWatch (TestException.class, aopTag); 1012 aspectInterface.setExceptionCatchWatch (TestException.class, aopTag); 1013 aspectInterface.setJoinPointHook(hook); 1014 1015 hook.reset(); 1016 testCatch.field1 = 0; 1017 testCatch.field1 = testCatch.field1 + 1; 1018 testCatch.throwMethod2(); 1019 1020 assertEquals("1 exception throw event(1)",1,hook.oExt); 1021 assertEquals("1 exception catch event(1)",1,hook.oExc); 1022 1023 1025 aspectInterface.setJoinPointHook(hook); 1027 hook.reset(); 1028 testCatch.field1 = 0; 1029 testCatch.field1 = testCatch.field1 + 1; 1030 testCatch.throwMethod2(); 1031 1032 assertEquals("1 exception throw event(2)",1,hook.oExt); 1033 assertEquals("1 exception catch event(2)",1,hook.oExc); 1034 1035 1037 aspectInterface.setJoinPointHook(null); 1039 1040 hook.reset(); 1041 testCatch.field1 = 0; 1042 testCatch.field1 = testCatch.field1 + 1; 1043 testCatch.throwMethod2(); 1044 1045 assertEquals("no exception throw events",0,hook.oExt); 1046 assertEquals("no exception catch events",0,hook.oExc); 1047 1048 1050 aspectInterface.setJoinPointHook(hook); 1052 hook.reset(); 1053 testCatch.field1 = 0; 1054 testCatch.field1 = testCatch.field1 + 1; 1055 testCatch.throwMethod2(); 1056 1057 assertEquals("1 exception throw event",1,hook.oExt); 1058 assertEquals("1 exception catch event",1,hook.oExc); 1059 1060 aspectInterface.clearExceptionThrowWatch (TestException.class); 1061 aspectInterface.clearExceptionCatchWatch (TestException.class); 1062 1063 1065 exceptions = 0; 1067 1068 aspectInterface.setExceptionThrowWatch (TestException.class, aopTag); 1069 try { aspectInterface.setExceptionThrowWatch (TestException.class, aopTag); } 1070 catch (WatchAlreadySetException e) { exceptions++; } 1071 1072 aspectInterface.setExceptionCatchWatch (TestException.class, aopTag); 1073 try { aspectInterface.setExceptionCatchWatch (TestException.class, aopTag); } 1074 catch (WatchAlreadySetException e) { exceptions++; } 1075 1076 assertEquals("2 WatchAlreadySetExceptions",2,exceptions); 1077 1078 exceptions = 0; 1080 1081 aspectInterface.clearExceptionThrowWatch (TestException.class); 1082 try { aspectInterface.clearExceptionThrowWatch (TestException.class ); } 1083 catch (WatchNotSetException e) { exceptions++; } 1084 1085 aspectInterface.clearExceptionCatchWatch (TestException.class); 1086 try { aspectInterface.clearExceptionCatchWatch (TestException.class); } 1087 catch (WatchNotSetException e) { exceptions++; } 1088 1089 assertEquals("2 WatchNotSetExceptions",2,exceptions); 1090 1091 1093 aspectInterface.setExceptionThrowWatch (TestException.class, aopTag); 1095 aspectInterface.setExceptionThrowWatch (TestException2.class, aopTag); 1096 aspectInterface.setExceptionThrowWatch (TestException3.class, aopTag); 1097 1098 aspectInterface.setExceptionCatchWatch (TestException.class, aopTag); 1099 aspectInterface.setExceptionCatchWatch (TestException2.class, aopTag); 1100 aspectInterface.setExceptionCatchWatch (TestException3.class, aopTag); 1101 1102 aspectInterface.setJoinPointHook(hook); 1103 1104 hook.reset(); 1106 testCatch.field1 = 0; testCatch.field2 = 0; 1107 testCatch.field1 = testCatch.field1 + 2; 1108 testCatch.field2 = testCatch.field2 + 2; 1109 testCatch.throwMethod4(); 1110 testCatch.throwMethod5(); 1111 testCatch.throwMethod3(); 1112 1113 assertEquals("3 exceptions throw events for TestException, 2 and 3",3,hook.oExt); 1114 assertEquals("3 exceptions catch events for TestException, 2 and 3",3,hook.oExc); 1115 1116 hook.reset(); 1119 testCatch.field1 = 0; testCatch.field2 = 0; 1120 testCatch.field1 = testCatch.field1 + 3; 1121 testCatch.field2 = testCatch.field2 + 3; 1122 testCatch.throwMethod4(); 1123 testCatch.throwMethod5(); 1124 testCatch.throwMethod3(); 1125 1126 assertEquals("1 exception throw events for TestException, 2 and 3",1,hook.oExt); 1127 assertEquals("1 exception catch events for TestException, 2 and 3",1,hook.oExc); 1128 1129 aspectInterface.clearExceptionThrowWatch (TestException.class); 1130 aspectInterface.clearExceptionThrowWatch (TestException2.class); 1131 aspectInterface.clearExceptionThrowWatch (TestException3.class); 1132 1133 aspectInterface.clearExceptionCatchWatch (TestException.class); 1134 aspectInterface.clearExceptionCatchWatch (TestException2.class); 1135 aspectInterface.clearExceptionCatchWatch (TestException3.class); 1136 1137 1139 aspectInterface.setExceptionThrowWatch (TestException4.class, aopTag); 1141 aspectInterface.setExceptionCatchWatch (TestException4.class, aopTag); 1142 1143 1146 aspectInterface.setJoinPointHook(hook); 1147 1148 hook.reset(); 1150 testCatch.field2 = 0; 1151 testCatch.field2 = testCatch.field2 + 2; 1152 testCatch.throwMethod6(); 1153 testCatch.throwMethod4(); 1154 1155 assertEquals("1 exception throw events for TestException4",1,hook.oExt); 1156 assertEquals("1 exception catch events for TestException4",1,hook.oExc); 1157 1158 aspectInterface.clearExceptionThrowWatch (TestException4.class); 1159 aspectInterface.clearExceptionCatchWatch (TestException4.class); 1160 1161 1164 } 1165 1166 1167 1171 public static 1172 Test suite() 1173 { 1174 return new TestSuite(JVMAspectInterfaceTest.class); 1175 } 1176 1177} 1178 1179 1180 | Popular Tags |