1 22 package org.jboss.test.aop.scope; 23 24 import java.lang.reflect.Method ; 25 import java.util.ArrayList ; 26 27 import org.jboss.aop.Advised; 28 import org.jboss.aop.joinpoint.MethodInvocation; 29 import org.jboss.aop.util.MethodHashing; 30 import org.jboss.test.aop.AOPTestWithSetup; 31 32 37 public class ScopeTestCase extends AOPTestWithSetup 38 { 39 40 public static void main(String [] args) 41 { 42 junit.textui.TestRunner.run(ScopeTestCase.class); 43 } 44 45 public ScopeTestCase(String arg0) 46 { 47 super(arg0); 48 } 49 50 public void testSimplePerVmAspect() 51 { 52 Interceptions.clear(); 53 POJOWithAspect pojo = new POJOWithAspect(); 54 assertEquals(5, pojo.perVmA(5)); 55 pojo.perVmB(); 56 57 POJO2WithAspect pojo2 = new POJO2WithAspect(); 58 pojo2.perVmA(100); 59 pojo2.perVmB(); 60 61 assertEquals(4, Interceptions.size()); 62 assertEquals(Interceptions.get(0), Interceptions.get(1)); 63 assertEquals(Interceptions.get(1), Interceptions.get(2)); 64 assertEquals(Interceptions.get(2), Interceptions.get(3)); 65 } 66 67 public void testSimplePerVmInterceptor() 68 { 69 Interceptions.clear(); 70 POJOWithInterceptor pojo = new POJOWithInterceptor(); 71 assertEquals(5, pojo.perVmA(5)); 72 pojo.perVmB(); 73 74 POJO2WithInterceptor pojo2 = new POJO2WithInterceptor(); 75 pojo2.perVmA(50); 76 pojo2.perVmB(); 77 78 assertEquals(4, Interceptions.size()); 79 assertEquals(Interceptions.get(0), Interceptions.get(1)); 80 assertEquals(Interceptions.get(1), Interceptions.get(2)); 81 assertEquals(Interceptions.get(2), Interceptions.get(3)); 82 } 83 84 public void testSimplePerVmAspectFactory() 85 { 86 Interceptions.clear(); 87 POJOWithAspectFactory pojo = new POJOWithAspectFactory(); 88 assertEquals(5, pojo.perVmA(5)); 89 pojo.perVmB(); 90 91 POJO2WithAspectFactory pojo2 = new POJO2WithAspectFactory(); 92 pojo2.perVmA(15); 93 pojo2.perVmB(); 94 95 assertEquals(4, Interceptions.size()); 96 assertEquals(Interceptions.get(0), Interceptions.get(1)); 97 assertEquals(Interceptions.get(1), Interceptions.get(2)); 98 assertEquals(Interceptions.get(2), Interceptions.get(3)); 99 } 100 101 public void testSimplePerVmInterceptorFactory() 102 { 103 Interceptions.clear(); 104 POJOWithInterceptorFactory pojo = new POJOWithInterceptorFactory(); 105 assertEquals(5, pojo.perVmA(5)); 106 pojo.perVmB(); 107 108 POJO2WithInterceptorFactory pojo2 = new POJO2WithInterceptorFactory(); 109 pojo2.perVmA(15); 110 pojo2.perVmB(); 111 112 assertEquals(4, Interceptions.size()); 113 assertEquals(Interceptions.get(0), Interceptions.get(1)); 114 assertEquals(Interceptions.get(1), Interceptions.get(2)); 115 assertEquals(Interceptions.get(2), Interceptions.get(3)); 116 } 117 118 public void testSimplePerClassAspect() 119 { 120 Interceptions.clear(); 121 POJOWithAspect pojo = new POJOWithAspect(); 122 assertEquals(5, pojo.perClazzA(5)); 123 pojo.perClazzB(); 124 125 POJO2WithAspect pojo2 = new POJO2WithAspect(); 126 pojo2.perClazzA(5); 127 pojo2.perClazzB(); 128 129 assertEquals(4, Interceptions.size()); 130 assertEquals(Interceptions.get(0), Interceptions.get(1)); 131 assertNotSame(Interceptions.get(1), Interceptions.get(2)); 132 assertEquals(Interceptions.get(2), Interceptions.get(3)); 133 } 134 135 public void testSimplePerClassInterceptor() 136 { 137 Interceptions.clear(); 138 POJOWithInterceptor pojo = new POJOWithInterceptor(); 139 assertEquals(5, pojo.perClazzA(5)); 140 pojo.perClazzB(); 141 142 POJO2WithInterceptor pojo2 = new POJO2WithInterceptor(); 143 pojo2.perClazzA(50); 144 pojo2.perClazzB(); 145 146 for (int i = 0 ; i < Interceptions.size() ; i++) 147 { 148 System.out.println("intercepted by " + Interceptions.get(i)); 149 } 150 151 assertEquals(4, Interceptions.size()); 152 assertEquals(Interceptions.get(0), Interceptions.get(1)); 153 assertNotSame(Interceptions.get(1), Interceptions.get(2)); 154 assertEquals(Interceptions.get(2), Interceptions.get(3)); 155 } 156 157 public void testSimplePerClassAspectFactory() 158 { 159 Interceptions.clear(); 160 POJOWithAspectFactory pojo = new POJOWithAspectFactory(); 161 assertEquals(5, pojo.perClazzA(5)); 162 pojo.perClazzB(); 163 164 POJO2WithAspectFactory pojo2 = new POJO2WithAspectFactory(); 165 pojo2.perClazzA(5); 166 pojo2.perClazzB(); 167 168 169 assertEquals(4, Interceptions.size()); 170 assertEquals(Interceptions.get(0), Interceptions.get(1)); 171 assertNotSame(Interceptions.get(1), Interceptions.get(2)); 172 assertEquals(Interceptions.get(2), Interceptions.get(3)); 173 } 174 175 public void testSimplePerClassInterceptorFactory() 176 { 177 Interceptions.clear(); 178 POJOWithInterceptorFactory pojo = new POJOWithInterceptorFactory(); 179 assertEquals(5, pojo.perClazzA(5)); 180 pojo.perClazzB(); 181 182 POJO2WithInterceptorFactory pojo2 = new POJO2WithInterceptorFactory(); 183 pojo2.perClazzA(5); 184 pojo2.perClazzB(); 185 186 for (int i = 0 ; i < Interceptions.size() ; i++) 187 { 188 System.out.println("intercepted by " + Interceptions.get(i)); 189 } 190 assertEquals(4, Interceptions.size()); 191 assertEquals(Interceptions.get(0), Interceptions.get(1)); 192 assertNotSame(Interceptions.get(1), Interceptions.get(2)); 193 assertEquals(Interceptions.get(2), Interceptions.get(3)); 194 } 195 196 197 public void testSimplePerInstanceAspect() 198 { 199 Interceptions.clear(); 200 POJOWithAspect pojo = new POJOWithAspect(); 201 POJO2WithAspect pojo2 = new POJO2WithAspect(); 202 POJOWithAspect pojo3 = new POJOWithAspect(); 203 POJO2WithAspect pojo4 = new POJO2WithAspect(); 204 205 assertEquals(5, pojo.perInstanceA(5)); 206 pojo2.perInstanceA(5); 207 pojo3.perInstanceA(5); 208 pojo4.perInstanceA(5); 209 210 pojo.perInstanceB(); 211 pojo2.perInstanceB(); 212 pojo3.perInstanceB(); 213 pojo4.perInstanceB(); 214 215 216 assertEquals(8, Interceptions.size()); 217 218 assertEquals(Interceptions.get(0), Interceptions.get(4)); 219 assertEquals(Interceptions.get(1), Interceptions.get(5)); 220 assertEquals(Interceptions.get(2), Interceptions.get(6)); 221 assertEquals(Interceptions.get(3), Interceptions.get(7)); 222 223 assertNotSame(Interceptions.get(0), Interceptions.get(1)); 224 assertNotSame(Interceptions.get(0), Interceptions.get(2)); 225 assertNotSame(Interceptions.get(0), Interceptions.get(3)); 226 assertNotSame(Interceptions.get(1), Interceptions.get(2)); 227 assertNotSame(Interceptions.get(1), Interceptions.get(3)); 228 assertNotSame(Interceptions.get(2), Interceptions.get(3)); 229 230 assertNotSame(Interceptions.get(4), Interceptions.get(5)); 231 assertNotSame(Interceptions.get(4), Interceptions.get(6)); 232 assertNotSame(Interceptions.get(4), Interceptions.get(7)); 233 assertNotSame(Interceptions.get(5), Interceptions.get(6)); 234 assertNotSame(Interceptions.get(5), Interceptions.get(7)); 235 assertNotSame(Interceptions.get(6), Interceptions.get(7)); 236 } 237 238 public void testSimplePerInstanceInterceptor() 239 { 240 Interceptions.clear(); 241 POJOWithInterceptor pojo = new POJOWithInterceptor(); 242 POJOWithInterceptor pojo2 = new POJOWithInterceptor(); 243 POJOWithInterceptor pojo3 = new POJOWithInterceptor(); 244 POJOWithInterceptor pojo4 = new POJOWithInterceptor(); 245 246 assertEquals(5, pojo.perInstanceA(5)); 247 pojo2.perInstanceA(5); 248 pojo3.perInstanceA(5); 249 pojo4.perInstanceA(5); 250 251 pojo.perInstanceB(); 252 pojo2.perInstanceB(); 253 pojo3.perInstanceB(); 254 pojo4.perInstanceB(); 255 256 257 assertEquals(8, Interceptions.size()); 258 assertEquals(Interceptions.get(0), Interceptions.get(4)); 259 assertEquals(Interceptions.get(1), Interceptions.get(5)); 260 assertEquals(Interceptions.get(2), Interceptions.get(6)); 261 assertEquals(Interceptions.get(3), Interceptions.get(7)); 262 263 assertNotSame(Interceptions.get(0), Interceptions.get(1)); 264 assertNotSame(Interceptions.get(0), Interceptions.get(2)); 265 assertNotSame(Interceptions.get(0), Interceptions.get(3)); 266 assertNotSame(Interceptions.get(1), Interceptions.get(2)); 267 assertNotSame(Interceptions.get(1), Interceptions.get(3)); 268 assertNotSame(Interceptions.get(2), Interceptions.get(3)); 269 270 assertNotSame(Interceptions.get(4), Interceptions.get(5)); 271 assertNotSame(Interceptions.get(4), Interceptions.get(6)); 272 assertNotSame(Interceptions.get(4), Interceptions.get(7)); 273 assertNotSame(Interceptions.get(5), Interceptions.get(6)); 274 assertNotSame(Interceptions.get(5), Interceptions.get(7)); 275 assertNotSame(Interceptions.get(6), Interceptions.get(7)); 276 } 277 278 public void testSimplePerInstanceAspectFactory() 279 { 280 Interceptions.clear(); 281 POJOWithAspectFactory pojo = new POJOWithAspectFactory(); 282 POJOWithAspectFactory pojo2 = new POJOWithAspectFactory(); 283 POJOWithAspectFactory pojo3 = new POJOWithAspectFactory(); 284 POJOWithAspectFactory pojo4 = new POJOWithAspectFactory(); 285 286 assertEquals(5, pojo.perInstanceA(5)); 287 pojo2.perInstanceA(5); 288 pojo3.perInstanceA(5); 289 pojo4.perInstanceA(5); 290 291 pojo.perInstanceB(); 292 pojo2.perInstanceB(); 293 pojo3.perInstanceB(); 294 pojo4.perInstanceB(); 295 296 297 assertEquals(8, Interceptions.size()); 298 assertEquals(Interceptions.get(0), Interceptions.get(4)); 299 assertEquals(Interceptions.get(1), Interceptions.get(5)); 300 assertEquals(Interceptions.get(2), Interceptions.get(6)); 301 assertEquals(Interceptions.get(3), Interceptions.get(7)); 302 303 assertNotSame(Interceptions.get(0), Interceptions.get(1)); 304 assertNotSame(Interceptions.get(0), Interceptions.get(2)); 305 assertNotSame(Interceptions.get(0), Interceptions.get(3)); 306 assertNotSame(Interceptions.get(1), Interceptions.get(2)); 307 assertNotSame(Interceptions.get(1), Interceptions.get(3)); 308 assertNotSame(Interceptions.get(2), Interceptions.get(3)); 309 310 assertNotSame(Interceptions.get(4), Interceptions.get(5)); 311 assertNotSame(Interceptions.get(4), Interceptions.get(6)); 312 assertNotSame(Interceptions.get(4), Interceptions.get(7)); 313 assertNotSame(Interceptions.get(5), Interceptions.get(6)); 314 assertNotSame(Interceptions.get(5), Interceptions.get(7)); 315 assertNotSame(Interceptions.get(6), Interceptions.get(7)); 316 } 317 318 public void testSimplePerInstanceInterceptorFactory() 319 { 320 Interceptions.clear(); 321 POJOWithInterceptorFactory pojo = new POJOWithInterceptorFactory(); 322 POJOWithInterceptorFactory pojo2 = new POJOWithInterceptorFactory(); 323 POJOWithInterceptorFactory pojo3 = new POJOWithInterceptorFactory(); 324 POJOWithInterceptorFactory pojo4 = new POJOWithInterceptorFactory(); 325 326 assertEquals(5, pojo.perInstanceA(5)); 327 pojo2.perInstanceA(5); 328 pojo3.perInstanceA(5); 329 pojo4.perInstanceA(5); 330 331 pojo.perInstanceB(); 332 pojo2.perInstanceB(); 333 pojo3.perInstanceB(); 334 pojo4.perInstanceB(); 335 336 337 338 assertEquals(8, Interceptions.size()); 339 assertEquals(Interceptions.get(0), Interceptions.get(4)); 340 assertEquals(Interceptions.get(1), Interceptions.get(5)); 341 assertEquals(Interceptions.get(2), Interceptions.get(6)); 342 assertEquals(Interceptions.get(3), Interceptions.get(7)); 343 344 assertNotSame(Interceptions.get(0), Interceptions.get(1)); 345 assertNotSame(Interceptions.get(0), Interceptions.get(2)); 346 assertNotSame(Interceptions.get(0), Interceptions.get(3)); 347 assertNotSame(Interceptions.get(1), Interceptions.get(2)); 348 assertNotSame(Interceptions.get(1), Interceptions.get(3)); 349 assertNotSame(Interceptions.get(2), Interceptions.get(3)); 350 351 assertNotSame(Interceptions.get(4), Interceptions.get(5)); 352 assertNotSame(Interceptions.get(4), Interceptions.get(6)); 353 assertNotSame(Interceptions.get(4), Interceptions.get(7)); 354 assertNotSame(Interceptions.get(5), Interceptions.get(6)); 355 assertNotSame(Interceptions.get(5), Interceptions.get(7)); 356 assertNotSame(Interceptions.get(6), Interceptions.get(7)); 357 } 358 359 public void testSimplePerJoinpointAspect() 360 { 361 Interceptions.clear(); 362 POJOWithAspect pojo = new POJOWithAspect(); 363 POJO2WithAspect pojo2 = new POJO2WithAspect(); 364 POJOWithAspect pojo3 = new POJOWithAspect(); 365 POJO2WithAspect pojo4 = new POJO2WithAspect(); 366 367 assertEquals(5, pojo.perJoinpointA(5)); 368 pojo2.perJoinpointA(5); 369 pojo3.perJoinpointA(5); 370 pojo4.perJoinpointA(5); 371 372 pojo.perJoinpointB(); 373 pojo2.perJoinpointB(); 374 pojo3.perJoinpointB(); 375 pojo4.perJoinpointB(); 376 377 pojo.perJoinpointA(5); 378 pojo2.perJoinpointA(5); 379 pojo3.perJoinpointA(5); 380 pojo4.perJoinpointA(5); 381 382 pojo.perJoinpointB(); 383 pojo2.perJoinpointB(); 384 pojo3.perJoinpointB(); 385 pojo4.perJoinpointB(); 386 387 assertEquals(16, Interceptions.size()); 388 389 int[] results = new int[]{8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7}; 390 391 for (int i = 0 ; i < results.length ; i++) 392 { 393 for (int j = 0 ; j < results.length ; j++) 394 { 395 if (i == j || Math.abs(i - j) == results.length/2 ) 396 { 397 assertEquals("interceptions " + i + " and " + j + " should be the same", Interceptions.get(i), Interceptions.get(j)); 398 } 399 else 400 { 401 assertNotSame("interceptions " + i + " and " + j + " should not be the same", Interceptions.get(i), Interceptions.get(j)); 402 } 403 } 404 } 405 } 406 407 public void testSimplePerJoinpointInterceptor() 408 { 409 Interceptions.clear(); 410 POJOWithInterceptor pojo = new POJOWithInterceptor(); 411 POJOWithInterceptor pojo2 = new POJOWithInterceptor(); 412 POJOWithInterceptor pojo3 = new POJOWithInterceptor(); 413 POJOWithInterceptor pojo4 = new POJOWithInterceptor(); 414 415 assertEquals(5, pojo.perJoinpointA(5)); 416 pojo2.perJoinpointA(5); 417 pojo3.perJoinpointA(5); 418 pojo4.perJoinpointA(5); 419 420 pojo.perJoinpointB(); 421 pojo2.perJoinpointB(); 422 pojo3.perJoinpointB(); 423 pojo4.perJoinpointB(); 424 425 pojo.perJoinpointA(5); 426 pojo2.perJoinpointA(5); 427 pojo3.perJoinpointA(5); 428 pojo4.perJoinpointA(5); 429 430 pojo.perJoinpointB(); 431 pojo2.perJoinpointB(); 432 pojo3.perJoinpointB(); 433 pojo4.perJoinpointB(); 434 435 assertEquals(16, Interceptions.size()); 436 437 int[] results = new int[]{8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7}; 438 439 for (int i = 0 ; i < results.length ; i++) 440 { 441 for (int j = 0 ; j < results.length ; j++) 442 { 443 if (i == j || Math.abs(i - j) == results.length/2 ) 444 { 445 assertEquals("interceptions " + i + " and " + j + " should be the same", Interceptions.get(i), Interceptions.get(j)); 446 } 447 else 448 { 449 assertNotSame("interceptions " + i + " and " + j + " should not be the same", Interceptions.get(i), Interceptions.get(j)); 450 } 451 } 452 } 453 } 454 455 public void testSimplePerJoinpointAspectFactory() 456 { 457 Interceptions.clear(); 458 POJOWithAspectFactory pojo = new POJOWithAspectFactory(); 459 POJOWithAspectFactory pojo2 = new POJOWithAspectFactory(); 460 POJOWithAspectFactory pojo3 = new POJOWithAspectFactory(); 461 POJOWithAspectFactory pojo4 = new POJOWithAspectFactory(); 462 463 assertEquals(5, pojo.perJoinpointA(5)); 464 pojo2.perJoinpointA(5); 465 pojo3.perJoinpointA(5); 466 pojo4.perJoinpointA(5); 467 468 pojo.perJoinpointB(); 469 pojo2.perJoinpointB(); 470 pojo3.perJoinpointB(); 471 pojo4.perJoinpointB(); 472 473 pojo.perJoinpointA(5); 474 pojo2.perJoinpointA(5); 475 pojo3.perJoinpointA(5); 476 pojo4.perJoinpointA(5); 477 478 pojo.perJoinpointB(); 479 pojo2.perJoinpointB(); 480 pojo3.perJoinpointB(); 481 pojo4.perJoinpointB(); 482 483 assertEquals(16, Interceptions.size()); 484 485 int[] results = new int[]{8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7}; 486 487 for (int i = 0 ; i < results.length ; i++) 488 { 489 for (int j = 0 ; j < results.length ; j++) 490 { 491 if (i == j || Math.abs(i - j) == results.length/2 ) 492 { 493 assertEquals("interceptions " + i + " and " + j + " should be the same", Interceptions.get(i), Interceptions.get(j)); 494 } 495 else 496 { 497 assertNotSame("interceptions " + i + " and " + j + " should not be the same", Interceptions.get(i), Interceptions.get(j)); 498 } 499 } 500 } 501 } 502 503 public void testSimplePerJoinpointInterceptorFactory() 504 { 505 Interceptions.clear(); 506 POJOWithInterceptorFactory pojo = new POJOWithInterceptorFactory(); 507 POJOWithInterceptorFactory pojo2 = new POJOWithInterceptorFactory(); 508 POJOWithInterceptorFactory pojo3 = new POJOWithInterceptorFactory(); 509 POJOWithInterceptorFactory pojo4 = new POJOWithInterceptorFactory(); 510 511 assertEquals(5, pojo.perJoinpointA(5)); 512 pojo2.perJoinpointA(5); 513 pojo3.perJoinpointA(5); 514 pojo4.perJoinpointA(5); 515 516 pojo.perJoinpointB(); 517 pojo2.perJoinpointB(); 518 pojo3.perJoinpointB(); 519 pojo4.perJoinpointB(); 520 521 pojo.perJoinpointA(5); 522 pojo2.perJoinpointA(5); 523 pojo3.perJoinpointA(5); 524 pojo4.perJoinpointA(5); 525 526 pojo.perJoinpointB(); 527 pojo2.perJoinpointB(); 528 pojo3.perJoinpointB(); 529 pojo4.perJoinpointB(); 530 531 532 533 assertEquals(16, Interceptions.size()); 534 535 int[] results = new int[]{8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7}; 536 537 for (int i = 0 ; i < results.length ; i++) 538 { 539 for (int j = 0 ; j < results.length ; j++) 540 { 541 if (i == j || Math.abs(i - j) == results.length/2 ) 542 { 543 assertEquals("interceptions " + i + " and " + j + " should be the same", Interceptions.get(i), Interceptions.get(j)); 544 } 545 else 546 { 547 assertNotSame("interceptions " + i + " and " + j + " should not be the same", Interceptions.get(i), Interceptions.get(j)); 548 } 549 } 550 } 551 } 552 553 554 final static String [] staticNames = { 555 PerJoinpointInterceptor.class.getName(), 556 PerVmInterceptor.class.getName(), 557 PerClassAspect.class.getName(), 558 PerClassJoinpointInterceptor.class.getName()}; 559 560 final static String [] nonStaticNames = { 561 PerJoinpointInterceptor.class.getName(), 562 PerVmInterceptor.class.getName(), 563 PerClassAspect.class.getName(), 564 PerClassJoinpointInterceptor.class.getName(), 565 PerInstanceAspect.class.getName()}; 566 567 final static String [] staticAndConstructionNames = { 568 PerJoinpointInterceptor.class.getName(), 570 PerVmInterceptor.class.getName(), 571 PerClassAspect.class.getName(), 572 PerClassJoinpointInterceptor.class.getName(), 573 PerJoinpointInterceptor.class.getName(), 575 PerVmInterceptor.class.getName(), 576 PerClassAspect.class.getName(), 577 PerClassJoinpointInterceptor.class.getName(), 578 PerInstanceAspect.class.getName()}; 579 580 public void testPOJOStaticWithMixedChains() throws Exception 581 { 582 System.out.println("TEST POJO WITH MIXED CHAINS"); 583 584 Interceptions.clear(); 585 assertEquals(5, POJOWithMixedChains.staticMethod(5)); 586 checkExpectedNames(staticNames); 587 ArrayList staticMethod = (ArrayList )Interceptions.interceptions.clone(); 588 589 Interceptions.clear(); 590 POJOWithMixedChains.voidStaticMethod(); 591 checkExpectedNames(staticNames); 592 ArrayList staticVoidMethod = (ArrayList )Interceptions.interceptions.clone(); 593 594 Interceptions.clear(); 595 POJOWithMixedChains.staticField = 100; 596 checkExpectedNames(staticNames); 597 ArrayList staticFieldWrite = (ArrayList )Interceptions.interceptions.clone(); 598 599 Interceptions.clear(); 600 assertEquals(100, POJOWithMixedChains.staticField); 601 checkExpectedNames(staticNames); 602 ArrayList staticFieldRead = (ArrayList )Interceptions.interceptions.clone(); 603 604 checkSimilarities(staticMethod, staticVoidMethod, new Boolean [] {Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE}); 605 checkSimilarities(staticMethod, staticFieldWrite, new Boolean [] {Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE}); 606 checkSimilarities(staticFieldWrite, staticFieldRead, new Boolean [] {Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE}); 607 } 608 609 public void testPOJOConstructionWithMixedChains() throws Exception 610 { 611 Interceptions.clear(); 612 POJOWithMixedChains pojo = new POJOWithMixedChains(); 613 checkExpectedNames(staticAndConstructionNames); 614 ArrayList ctorA = (ArrayList )Interceptions.interceptions.clone(); 615 616 Interceptions.clear(); 617 POJOWithMixedChains pojo2 = new POJOWithMixedChains(100); 618 checkExpectedNames(staticAndConstructionNames); 619 ArrayList ctorB = (ArrayList )Interceptions.interceptions.clone(); 620 checkSimilarities(ctorA, ctorB, new Boolean [] {Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE}); 621 } 622 623 public void testPOJOMethodsWithMixedChains() throws Exception 624 { 625 POJOWithMixedChains pojo = new POJOWithMixedChains(); 626 POJOWithMixedChains pojo2 = new POJOWithMixedChains(); 627 Interceptions.clear(); 628 assertEquals(100, pojo.method(100)); 629 checkExpectedNames(nonStaticNames); 630 ArrayList methodInterceptionsA = (ArrayList )Interceptions.interceptions.clone(); 631 632 Interceptions.clear(); 633 assertEquals(100, pojo2.method(100)); 634 checkExpectedNames(nonStaticNames); 635 ArrayList methodInterceptionsB = (ArrayList )Interceptions.interceptions.clone(); 636 637 System.out.println("Checking methodA and methodB"); 638 checkSimilarities(methodInterceptionsA, methodInterceptionsB, new Boolean [] {Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE}); 639 } 640 641 public void testPOJOFieldsWithMixedChains() throws Exception 642 { 643 POJOWithMixedChains pojo = new POJOWithMixedChains(); 644 POJOWithMixedChains pojo2 = new POJOWithMixedChains(); 645 646 Interceptions.clear(); 647 pojo.field = 100; 648 checkExpectedNames(nonStaticNames); 649 ArrayList fieldWriteA = (ArrayList )Interceptions.interceptions.clone(); 650 651 Interceptions.clear(); 652 pojo2.field = 50; 653 checkExpectedNames(nonStaticNames); 654 ArrayList fieldWriteB = (ArrayList )Interceptions.interceptions.clone(); 655 656 Interceptions.clear(); 657 assertEquals(100, pojo.field); 658 checkExpectedNames(nonStaticNames); 659 ArrayList fieldReadA = (ArrayList )Interceptions.interceptions.clone(); 660 661 Interceptions.clear(); 662 assertEquals(50, pojo2.field); 663 checkExpectedNames(nonStaticNames); 664 ArrayList fieldReadB = (ArrayList )Interceptions.interceptions.clone(); 665 666 checkSimilarities(fieldWriteA, fieldWriteB, new Boolean [] {Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE}); 667 checkSimilarities(fieldReadA, fieldReadB, new Boolean [] {Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE}); 668 checkSimilarities(fieldReadA, fieldWriteA, new Boolean [] {Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE}); 669 checkSimilarities(fieldReadB, fieldWriteB, new Boolean [] {Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE}); 670 } 671 672 public void testCallerFromCtorWithMixedChains() throws Exception 673 { 674 Interceptions.clear(); 675 CallingPOJO pojo = new CallingPOJO(); 676 CallingPOJO pojo2 = new CallingPOJO(); 677 678 String [] expectedNames = new String [16]; 679 680 for (int i = 0 ; i < 16 ; i += 4) 681 { 682 System.arraycopy(staticNames, 0, expectedNames, i, staticNames.length); 683 } 684 checkExpectedNames(expectedNames); 685 assertNotSame(Interceptions.get(0), Interceptions.get(4)); 686 assertSame(Interceptions.get(1), Interceptions.get(5)); 687 assertSame(Interceptions.get(2), Interceptions.get(6)); 688 assertNotSame(Interceptions.get(3), Interceptions.get(7)); 689 690 691 692 for (int i = 0 ; i < Interceptions.size() ; i ++) 693 { 694 System.out.println(Interceptions.get(i)); 695 } 696 697 Interceptions.clear(); 698 pojo = new CallingPOJO(); 699 ArrayList ctorA = (ArrayList )Interceptions.interceptions.clone(); 700 701 Interceptions.clear(); 702 pojo2 = new CallingPOJO(); 703 ArrayList ctorB = (ArrayList )Interceptions.interceptions.clone(); 704 705 assertNotSame(Interceptions.get(0), Interceptions.get(4)); 706 assertSame(Interceptions.get(1), Interceptions.get(5)); 707 assertSame(Interceptions.get(2), Interceptions.get(6)); 708 assertNotSame(Interceptions.get(3), Interceptions.get(7)); 709 710 checkSimilarities(ctorA, ctorB, new Boolean [] {Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE}); 711 } 712 713 public void testCallerFromStaticMethodWithMixedChains() throws Exception 714 { 715 Interceptions.clear(); 716 CallingPOJO.callFromStatic(); 717 CallingPOJO.callFromStatic(); 718 719 String [] expectedNames = new String [16]; 720 721 for (int i = 0 ; i < 16 ; i += 4) 722 { 723 System.arraycopy(staticNames, 0, expectedNames, i, staticNames.length); 724 } 725 checkExpectedNames(expectedNames); 726 assertNotSame(Interceptions.get(0), Interceptions.get(4)); 727 assertSame(Interceptions.get(1), Interceptions.get(5)); 728 assertSame(Interceptions.get(2), Interceptions.get(6)); 729 assertNotSame(Interceptions.get(3), Interceptions.get(7)); 730 731 732 733 for (int i = 0 ; i < Interceptions.size() ; i ++) 734 { 735 System.out.println(Interceptions.get(i)); 736 } 737 738 Interceptions.clear(); 739 CallingPOJO.callFromStatic(); 740 ArrayList staticA = (ArrayList )Interceptions.interceptions.clone(); 741 742 Interceptions.clear(); 743 CallingPOJO.callFromStatic(); 744 ArrayList staticB = (ArrayList )Interceptions.interceptions.clone(); 745 746 assertNotSame(Interceptions.get(0), Interceptions.get(4)); 747 assertSame(Interceptions.get(1), Interceptions.get(5)); 748 assertSame(Interceptions.get(2), Interceptions.get(6)); 749 assertNotSame(Interceptions.get(3), Interceptions.get(7)); 750 751 checkSimilarities(staticA, staticB, new Boolean [] {Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE}); 752 753 Interceptions.clear(); 754 CallingPOJO pojo = new CallingPOJO(); 755 ArrayList ctorA = (ArrayList )Interceptions.interceptions.clone(); 756 757 checkSimilarities(staticA, ctorA, new Boolean [] {Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE}); 758 } 759 760 public void testCallerFromMethodWithMixedChains() throws Exception 761 { 762 CallingPOJO pojo = new CallingPOJO(); 763 CallingPOJO pojo2 = new CallingPOJO(); 764 Interceptions.clear(); 765 766 pojo.call(); 767 pojo2.call(); 768 769 String [] expectedNames = new String [20]; 770 771 for (int i = 0 ; i < 20 ; i += 5) 772 { 773 System.arraycopy(nonStaticNames, 0, expectedNames, i, nonStaticNames.length); 774 } 775 checkExpectedNames(expectedNames); 776 777 assertNotSame(Interceptions.get(0), Interceptions.get(5)); 779 assertNotSame(Interceptions.get(0), Interceptions.get(10)); 780 assertNotSame(Interceptions.get(0), Interceptions.get(15)); 781 assertNotSame(Interceptions.get(5), Interceptions.get(10)); 782 assertNotSame(Interceptions.get(5), Interceptions.get(15)); 783 assertNotSame(Interceptions.get(10), Interceptions.get(15)); 784 785 if (Interceptions.get(1) != Interceptions.get(6) || Interceptions.get(1) != Interceptions.get(11) || Interceptions.get(1)!= Interceptions.get(16)) 787 throw new Exception ("PER_CLASS scoped should have been the same"); 788 if (Interceptions.get(2) != Interceptions.get(7) || Interceptions.get(2) != Interceptions.get(12) || Interceptions.get(2)!= Interceptions.get(17)) 789 throw new Exception ("PER_CLASS scoped should have been the same"); 790 791 assertSame(Interceptions.get(3), Interceptions.get(13)); 793 assertSame(Interceptions.get(8), Interceptions.get(18)); 794 assertNotSame(Interceptions.get(3), Interceptions.get(8)); 795 796 assertSame(Interceptions.get(4), Interceptions.get(9)); 798 assertSame(Interceptions.get(14), Interceptions.get(19)); 799 assertNotSame(Interceptions.get(4), Interceptions.get(14)); 800 } 801 802 public void testDynamicInvoke() throws Throwable 803 { 804 POJOWithMixedChains pojo = new POJOWithMixedChains(); 805 806 Interceptions.clear(); 807 pojo.dynamicMethod(); 808 ArrayList methodA = (ArrayList )Interceptions.interceptions.clone(); 809 810 Interceptions.clear(); 811 Method method = pojo.getClass().getMethod("dynamicMethod", new Class [0]); 814 long hash = MethodHashing.calculateHash(method); 815 MethodInvocation invocation = new MethodInvocation(null, hash, null, null, null); 816 invocation.setArguments(new Object [0]); 817 818 ((Advised)pojo)._getAdvisor().dynamicInvoke(pojo, invocation); 819 820 ArrayList methodB = (ArrayList )Interceptions.interceptions.clone(); 821 822 checkSimilarities(methodA, methodB); 823 } 824 825 public void testCopy() throws Exception 826 { 827 System.out.println("TEST COPY"); 828 Interceptions.clear(); 829 assertEquals(5, CopyPOJOWithMixedChains.staticMethod(5)); 830 assertEquals(8, Interceptions.size()); 831 checkSimilaritiesInTowHalves(Interceptions.interceptions); 832 833 Interceptions.clear(); 834 CopyPOJOWithMixedChains.staticField = 17; 835 assertEquals(8, Interceptions.size()); 836 checkSimilaritiesInTowHalves(Interceptions.interceptions); 837 838 Interceptions.clear(); 839 assertEquals(17, CopyPOJOWithMixedChains.staticField); 840 assertEquals(8, Interceptions.size()); 841 checkSimilaritiesInTowHalves(Interceptions.interceptions); 842 843 Interceptions.clear(); 844 CopyPOJOWithMixedChains pojo = new CopyPOJOWithMixedChains(5); 845 assertEquals(8, Interceptions.size()); 846 checkSimilaritiesInTowHalves(Interceptions.interceptions); 847 848 Interceptions.clear(); 849 pojo.field = 99; 850 assertEquals(10, Interceptions.size()); 851 checkSimilaritiesInTowHalves(Interceptions.interceptions); 852 853 Interceptions.clear(); 854 assertEquals(99, pojo.field); 855 assertEquals(10, Interceptions.size()); 856 checkSimilaritiesInTowHalves(Interceptions.interceptions); 857 } 858 859 private void checkSimilaritiesInTowHalves(ArrayList all) 860 { 861 ArrayList listA = new ArrayList (); 862 ArrayList listB = new ArrayList (); 863 864 for (int i = 0 ; i < all.size(); i++) 865 { 866 if (i < all.size()/2) 867 { 868 listA.add(all.get(i)); 869 } 870 else 871 { 872 listB.add(all.get(i)); 873 } 874 } 875 876 checkSimilarities(listA, listB); 877 } 878 879 private void checkSimilarities(ArrayList listA, ArrayList listB) 880 { 881 Boolean [] same = new Boolean [listA.size()]; 882 for (int i = 0 ; i < listA.size() ; i++) 883 { 884 same[i] = Boolean.TRUE; 885 } 886 checkSimilarities(listA, listB, same); 887 } 888 889 private void checkSimilarities(ArrayList listA, ArrayList listB, Boolean [] same) 890 { 891 assertEquals(listA.size(), listB.size()); 892 assertEquals(listA.size(), same.length); 893 for (int i = 0 ; i < listA.size() ; i++) 894 { 895 if (same[i].booleanValue()) 896 { 897 assertEquals(Integer.toString(i), listA.get(i), listB.get(i)); 898 } 899 else 900 { 901 assertNotSame(Integer.toString(i), listA.get(i), listB.get(i)); 902 } 903 } 904 905 } 906 907 private void checkExpectedNames(String [] expected) 908 { 909 assertEquals(expected.length, Interceptions.size()); 910 for (int i = 0 ; i < expected.length ; i++) 911 { 912 assertEquals(Integer.toString(i), expected[i], Interceptions.get(i).getClass().getName()); 913 } 914 } 915 } 916 | Popular Tags |