1 7 package ch.ethz.prose; 8 9 import java.lang.reflect.Method ; 11 import java.util.*; 12 13 import junit.framework.*; 14 import ch.ethz.jvmai.ExceptionJoinPoint; 15 import ch.ethz.prose.crosscut.*; 16 import ch.ethz.prose.engine.ExceptionThrowRequest; 17 import ch.ethz.prose.engine.JoinPointManager; 18 import ch.ethz.prose.filter.*; 19 import ch.ethz.prose.query.*; 20 21 28 public 29 class LocalExtensionManagerTest extends TestCase { 30 31 public boolean visited = false; 33 public boolean visited1 = false; 34 public boolean visited2=false; 35 public void myTestMethod() { } 36 37 class TestExtension1 extends DefaultAspect 38 { 39 public void insertionAction(boolean flag) 40 { 41 LocalExtensionManagerTest.this.myTestMethod(); 42 } 43 44 public Crosscut c1 = new MethodCut() 45 { 46 public void METHOD_ARGS(LocalExtensionManagerTest x) 47 { 48 visited1=true; 49 visited = true; 50 } 51 52 protected PointCutter pointCutter() { return Within.method("myTestMethod");} 53 }; 54 }; 55 56 class TestExtension2 extends DefaultAspect 57 { 58 public void insertionAction(boolean flag) 59 { 60 LocalExtensionManagerTest.this.myTestMethod(); 61 } 62 63 public Crosscut c1 = new MethodCut() 64 { 65 public void METHOD_ARGS(LocalExtensionManagerTest x) 66 { 67 visited2 = true; 68 } 69 protected PointCutter pointCutter() { return Within.method("myTestMethod");} 70 }; 71 } 72 73 74 Aspect tstExtension = null; 75 Aspect tstExtension2 = null; 76 77 78 int ASP = QueryManager.SELECT_ASPECT; int CRO = QueryManager.SELECT_CROSSCUT; int JOI = QueryManager.SELECT_JOINPOINT; 80 int ASP_CRO = ASP+CRO; int ASP_JOI = ASP+JOI; int CRO_JOI = CRO+JOI; 81 int ASP_CRO_JOI = ASP+CRO+JOI; 82 int GROUP_ASP = QueryManager.GROUP_BY_ASPECT; 83 int GROUP_CRO = QueryManager.GROUP_BY_CROSSCUT; 84 int GROUP_JOI = QueryManager.GROUP_BY_JOINPOINT; 85 86 JoinPointManager jpm = null; 87 AspectManager exMngr = null; 88 89 public static class Extension1 extends DefaultAspect 90 { 91 public Crosscut c1a = new ThrowCut() 92 { 93 public void THROW_ARGS() { } 94 public Class [] potentialCrosscutClasses() 95 { 96 Class [] result = { LinkageError .class, 97 IllegalStateException .class, 98 InstantiationError .class, 99 NullPointerException .class, 100 RuntimeException .class, 101 ClassFormatError .class, 102 UnknownError .class}; 103 return result; 104 } 105 protected PointCutter pointCutter() 106 { return (Exceptions.type("LinkageError")) . OR (Exceptions.type("IllegalStateException")) ;} 107 }; 108 }; 109 110 public static class Extension2 extends DefaultAspect 111 { 112 public Crosscut c2a = new ThrowCut() 114 { 115 public void THROW_ARGS() { } 116 public Class [] potentialCrosscutClasses() 117 { Class [] result = { LinkageError .class, 118 IllegalStateException .class, 119 InstantiationError .class, 120 NullPointerException .class, 121 RuntimeException .class, 122 ClassFormatError .class, 123 UnknownError .class}; return result; } 124 protected PointCutter pointCutter() 125 { 126 return (Exceptions.type("InstantiationError")) . OR (Exceptions.type("UnknownError")); 127 } 128 }; 129 130 public Crosscut c2b = new ThrowCut() 131 { 132 public void THROW_ARGS() { } 134 public Class [] potentialCrosscutClasses() 135 { Class [] result = { LinkageError .class, 136 IllegalStateException .class, 137 InstantiationError .class, 138 NullPointerException .class, 139 RuntimeException .class, 140 ClassFormatError .class, 141 UnknownError .class}; return result; } 142 protected PointCutter pointCutter() 143 { return (Exceptions.type("NullPointerException")) . OR (Exceptions.type("RuntimeException"));} 144 }; 145 146 public Crosscut c2c = new ThrowCut() 148 { 149 public void THROW_ARGS() { } 150 public Class [] potentialCrosscutClasses() 151 { 152 Class [] result = { LinkageError .class, 153 IllegalStateException .class, 154 InstantiationError .class, 155 NullPointerException .class, 156 RuntimeException .class, 157 ClassFormatError .class, 158 UnknownError .class}; return result; 159 } 160 protected PointCutter pointCutter() 161 { return (Exceptions.type("ClassFormatError")) . OR (Exceptions.type("UnknownError")); } 162 }; 163 }; 164 165 public static class Extension3 extends DefaultAspect 167 { 168 public Crosscut c3a = new ThrowCut() 169 { 170 public void THROW_ARGS() { } 171 protected PointCutter pointCutter() 172 { return (Exceptions.type("thisIsSurelyNotAnException")) ;} 173 }; 174 }; 175 176 177 181 public LocalExtensionManagerTest(String name) 182 { 183 super(name); 184 } 185 186 187 190 public 191 void setUp() 192 { 193 194 tstExtension = new TestExtension1(); 195 tstExtension2 = new TestExtension2(); 196 try { 197 ProseSystem.startup(); 198 exMngr = ProseSystem.getAspectManager(); 199 jpm = ProseSystem.getAspectManager().getJoinPointManager(); 200 } 201 catch (SystemStartupException e) 202 { 203 Assert.fail("could not start ProseSystem"); 204 } 205 } 206 207 public 208 void tearDown() 209 { 210 try 211 { 212 ProseSystem.teardown(); 213 } 214 catch (SystemTeardownException e) 215 { 216 Assert.fail("could not teardown ProseSystem"); 217 } 218 } 219 220 225 public void test0010_SilenceDuringInsertion() 226 { 227 visited = false; 228 ProseSystem.getAspectManager().insert(tstExtension); 229 assertTrue("not visited during insertion",!visited); 230 visited = false; 231 myTestMethod(); 232 assertTrue("visited after insertion", visited); 233 } 234 235 236 237 public void test0020_queryAspect() 238 { 239 List result; 240 List aspectList = new Vector(); 241 AspectManager exMngr = ProseSystem.getAspectManager(); 242 243 Aspect aspect1 = new Extension1(); 244 Aspect aspect2 = new Extension2(); 245 Aspect aspect3 = new Extension3(); 246 ProseSystem.getAspectManager().insert(aspect1); 247 ProseSystem.getAspectManager().insert(aspect2); 248 ProseSystem.getAspectManager().insert(aspect3); 249 aspectList.add(new AspectSurrogate(aspect1)); 250 aspectList.add(new AspectSurrogate(aspect2)); 251 aspectList.add(new AspectSurrogate(aspect3)); 252 QueryManager qMgr = new QueryManager(ProseSystem.getAspectManager()); 253 254 assertTrue("wrong select range", qMgr.queryAspects(aspectList, 0x00, GROUP_ASP).isEmpty()); 256 257 result = qMgr.queryAspects(aspectList, ASP_CRO_JOI, GROUP_ASP); 259 assertEquals("select Aspect + Crosscut + Joinpoint", 9, result.size()); 260 261 result = qMgr.queryAspects(aspectList, ASP_CRO, GROUP_ASP); 262 assertEquals("select Aspect + Crosscut", 5, result.size()); 263 264 267 result = qMgr.queryAspects(aspectList, CRO_JOI, GROUP_ASP); 268 assertEquals("select Crosscut + Joinpoint", 9, result.size()); 269 270 result = qMgr.queryAspects(aspectList, ASP, GROUP_ASP); 271 assertEquals("select Aspect", 3, result.size()); 272 273 result = qMgr.queryAspects(aspectList, CRO, GROUP_ASP); 274 assertEquals("select Crosscut", 5, result.size()); 275 276 result = qMgr.queryAspects(aspectList, JOI, GROUP_ASP); 277 assertEquals("select Joinpoint", 8, result.size()); 278 } 279 280 281 public void test0030_queryCrosscut() 282 { 283 List result; 284 List crosscutList = new Vector(); 285 286 LocalExtensionManagerTest.Extension1 aspect1 = new Extension1(); 287 LocalExtensionManagerTest.Extension2 aspect2 = new Extension2(); 288 LocalExtensionManagerTest.Extension3 aspect3 = new Extension3(); 289 ProseSystem.getAspectManager().insert(aspect1); 290 ProseSystem.getAspectManager().insert(aspect2); 291 ProseSystem.getAspectManager().insert(aspect3); 292 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect1),aspect1.c1a)); 293 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect2),aspect2.c2a)); 294 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect2),aspect2.c2b)); 295 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect2),aspect2.c2c)); 296 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect3),aspect3.c3a)); 297 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect1),aspect1.c1a)); 298 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect1),aspect1.c1a)); 299 QueryManager qMgr = new QueryManager(ProseSystem.getAspectManager()); 300 301 assertTrue("wrong select range", qMgr.queryCrosscuts(crosscutList, 0x00, GROUP_ASP).isEmpty()); 303 304 result = qMgr.queryCrosscuts(crosscutList, ASP_CRO_JOI, GROUP_ASP); 305 assertEquals("select Aspect + Crosscut + Joinpoint", 9, result.size()); 306 307 result = qMgr.queryCrosscuts(crosscutList, ASP_CRO, GROUP_ASP); 308 assertEquals("select Aspect + Crosscut", 5, result.size()); 309 310 result = qMgr.queryCrosscuts(crosscutList, ASP_JOI, GROUP_ASP); 311 assertEquals("select Aspect + Joinpoint", 8, result.size()); 312 313 result = qMgr.queryCrosscuts(crosscutList, CRO_JOI, GROUP_ASP); 314 assertEquals("select Crosscut + Joinpoint", 9, result.size()); 315 316 result = qMgr.queryCrosscuts(crosscutList, ASP, GROUP_ASP); 317 assertEquals("select Aspect", 3, result.size()); 318 319 result = qMgr.queryCrosscuts(crosscutList, CRO, GROUP_ASP); 320 assertEquals("select Crosscut", 5, result.size()); 321 322 result = qMgr.queryCrosscuts(crosscutList, JOI, GROUP_ASP); 323 assertEquals("select Joinpoint", 8, result.size()); 324 } 325 326 327 public void test0040_queryJoinpoint() 328 { 329 List result; 330 List joinpointList = new Vector(); 331 try{ 332 Aspect aspect1 = new Extension1(); 333 Aspect aspect2 = new Extension2(); 334 Aspect aspect3 = new Extension3(); 335 QueryManager qMgr = new QueryManager(ProseSystem.getAspectManager()); 336 337 ProseSystem.getAspectManager().insert(aspect1); 338 ProseSystem.getAspectManager().insert(aspect2); 339 ProseSystem.getAspectManager().insert(aspect3); 340 341 342 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,LinkageError .class))); 343 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,IllegalStateException .class))); 344 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,InstantiationError .class))); 345 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,UnknownError .class))); 346 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,NullPointerException .class))); 347 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,RuntimeException .class))); 348 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ClassFormatError .class))); 349 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,SystemStartupException.class))); 351 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,RuntimeException .class))); 353 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ClassFormatError .class))); 354 355 assertTrue("wrong select range", qMgr.queryJoinpoints(joinpointList, 0x00, GROUP_ASP).isEmpty()); 357 358 result = qMgr.queryJoinpoints(joinpointList, ASP_CRO_JOI, GROUP_ASP); 359 assertEquals("select Aspect + Crosscut + Joinpoint", 8, result.size()); 360 361 result = qMgr.queryJoinpoints(joinpointList, ASP_CRO, GROUP_ASP); 362 assertEquals("select Aspect + Crosscut", 4, result.size()); 363 364 result = qMgr.queryJoinpoints(joinpointList, ASP_JOI, GROUP_ASP); 365 assertEquals("select Aspect + Joinpoint", 7, result.size()); 366 367 result = qMgr.queryJoinpoints(joinpointList, CRO_JOI, GROUP_ASP); 368 assertEquals("select Crosscut + Joinpoint", 8, result.size()); 369 370 result = qMgr.queryJoinpoints(joinpointList, ASP, GROUP_ASP); 371 assertEquals("select Aspect", 2, result.size()); 372 373 result = qMgr.queryJoinpoints(joinpointList, CRO, GROUP_ASP); 374 assertEquals("select Crosscut", 4, result.size()); 375 376 result = qMgr.queryJoinpoints(joinpointList, JOI, GROUP_ASP); 377 assertEquals("select Joinpoint", 7, result.size()); 378 } catch (Exception e) {e.printStackTrace();} 379 } 380 381 382 public void test0050_queryGroupBy() 384 { 385 try{ 386 List result; 387 List aspectList = new Vector(); 388 List crosscutList = new Vector(); 389 List joinpointList = new Vector(); 390 391 LocalExtensionManagerTest.Extension1 aspect1 = new Extension1(); 392 LocalExtensionManagerTest.Extension2 aspect2 = new Extension2(); 393 LocalExtensionManagerTest.Extension3 aspect3 = new Extension3(); 394 ProseSystem.getAspectManager().insert(aspect1); 395 ProseSystem.getAspectManager().insert(aspect2); 396 ProseSystem.getAspectManager().insert(aspect3); 397 aspectList.add(new AspectSurrogate(aspect1)); 398 aspectList.add(new AspectSurrogate(aspect2)); 399 aspectList.add(new AspectSurrogate(aspect3)); 400 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect1),aspect1.c1a)); 401 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect2),aspect2.c2a)); 402 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect2),aspect2.c2b)); 403 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect2),aspect2.c2c)); 404 crosscutList.add(new CrosscutSurrogate(new AspectSurrogate(aspect3),aspect3.c3a)); 405 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,LinkageError .class))); 406 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,IllegalStateException .class))); 407 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,InstantiationError .class))); 408 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,UnknownError .class))); 409 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,NullPointerException .class))); 410 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,RuntimeException .class))); 411 joinpointList.add(new JoinPointRequestSurrogate((ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ClassFormatError .class))); 412 413 QueryManager qMngr = new QueryManager(ProseSystem.getAspectManager()); 414 415 result = qMngr.queryAspects(aspectList, ASP_CRO_JOI, GROUP_ASP); 416 assertEquals("queryAspects with groupBy Aspects", 2, computeTransitions(result, "getAspectSurrogate")); 417 result = qMngr.queryAspects(aspectList, ASP_CRO_JOI, GROUP_CRO); 418 419 assertEquals("queryAspects with groupBy Crosscuts", 4, computeTransitions(result, "getCrosscutSurrogate")); 420 result = qMngr.queryAspects(aspectList, ASP_CRO_JOI, GROUP_JOI); 421 assertEquals("queryAspects with groupBy Joinpoints", 7, computeTransitions(result, "getRequestSurrogate")); 422 423 result = qMngr.queryCrosscuts(crosscutList, ASP_CRO_JOI, GROUP_ASP); 424 assertEquals("queryCrosscuts with groupBy Aspects", 2, computeTransitions(result, "getAspectSurrogate")); 425 result = qMngr.queryCrosscuts(crosscutList, ASP_CRO_JOI, GROUP_CRO); 426 assertEquals("queryCrosscuts with groupBy Crosscuts", 4, computeTransitions(result, "getCrosscutSurrogate")); 427 result = qMngr.queryCrosscuts(crosscutList, ASP_CRO_JOI, GROUP_JOI); 428 assertEquals("queryCrosscuts with groupBy Joinpoints", 7, computeTransitions(result, "getRequestSurrogate")); 429 430 result = qMngr.queryJoinpoints(joinpointList, ASP_CRO_JOI, GROUP_ASP); 431 assertEquals("queryJoinpoints with groupBy Aspects", 1, computeTransitions(result, "getAspectSurrogate")); 432 result = qMngr.queryJoinpoints(joinpointList, ASP_CRO_JOI, GROUP_CRO); 433 assertEquals("queryJoinpoints with groupBy Crosscuts", 3, computeTransitions(result, "getCrosscutSurrogate")); 434 result = qMngr.queryJoinpoints(joinpointList, ASP_CRO_JOI, GROUP_JOI); 435 assertEquals("queryJoinpoints with groupBy Joinpoints", 6, computeTransitions(result, "getRequestSurrogate")); 436 } 437 catch (Exception e) 438 { 439 e.printStackTrace(); 440 throw new RuntimeException (e.toString()); 441 } 442 } 443 444 445 private int computeTransitions(List resultList, String queryMethod) 448 { 449 try 450 { 451 Object o=null; int code=0, codeOld=0; boolean start=true; 452 int transition = 0; 453 Method m = Tuple.class.getDeclaredMethod(queryMethod, new Class [] {}); 454 Object [] emptyArray = new Object [] {}; 455 456 Iterator i = resultList.listIterator(); 457 while (i.hasNext()) 458 { 459 Tuple crtTuple = (Tuple)i.next(); 460 461 o = m.invoke(crtTuple, emptyArray); 462 463 if (o == null) 464 code = 0; 465 else { 466 code = o.hashCode(); 467 } 468 469 if (start) 470 { 471 start = false; 472 codeOld = code; 473 continue; 474 } 475 if (code != codeOld) 476 transition++; 477 478 codeOld = code; 479 } 480 return transition; 481 } 482 catch (Exception e) { 483 throw new RuntimeException (e.getMessage()); 484 } 485 } 486 487 public void test0060_testCommit() 488 { 489 Object txId = new Object (); 490 491 visited1 = false; visited2=false; 492 ProseSystem.getAspectManager().insert(tstExtension,txId); 493 myTestMethod(); 494 assertTrue("[1] not visited before commit",!visited1); 495 assertTrue("[2] not visited before commit",!visited2); 496 ProseSystem.getAspectManager().insert(tstExtension2,txId); 497 myTestMethod(); 498 assertTrue("[3] not visited before commit",!visited1); 499 assertTrue("[4] not visited before commit",!visited2); 500 ProseSystem.getAspectManager().commit(txId); 501 myTestMethod(); 502 assertTrue("[5] visited after commit",visited1); 503 assertTrue("[6] visited after commit",visited2); 504 } 505 506 public void test0070_testAbort() 507 { 508 Object txId = new Object (); 509 510 visited1 = false; visited2=false; 511 ProseSystem.getAspectManager().insert(tstExtension,txId); 512 myTestMethod(); 513 assertTrue("[1] not visited before commit",!visited1); 514 assertTrue("[2] not visited before commit",!visited2); 515 ProseSystem.getAspectManager().insert(tstExtension2,txId); 516 myTestMethod(); 517 assertTrue("[3] not visited before commit",!visited1); 518 assertTrue("[4] not visited before commit",!visited2); 519 ProseSystem.getAspectManager().abort(txId); 520 myTestMethod(); 521 assertTrue("[5] visited after commit",!visited1); 522 assertTrue("[6] visited after commit",!visited2); 523 assertEquals("no extension inserted", ProseSystem.getAspectManager().getAllAspects().size(),0); 524 } 525 526 527 528 532 public static 533 Test suite() 534 { 535 return new TestSuite(LocalExtensionManagerTest.class); 536 } 537 538 } 539 540 541
| Popular Tags
|