1 7 package ch.ethz.prose; 8 9 import java.util.*; 11 12 import junit.framework.*; 13 import ch.ethz.jvmai.MethodEntryJoinPoint; 14 import ch.ethz.prose.crosscut.*; 15 import ch.ethz.prose.engine.JoinPointRequest; 16 import ch.ethz.prose.filter.PointCutter; 17 import ch.ethz.prose.filter.Within; 18 19 25 public 26 class ExtensionInsertionTest extends TestCase { 27 28 static class TestClass { 30 public void hello(String stuff) 31 { 32 int a =1; 33 } 34 35 public void byebye(String stuff, String stuf1) 36 { 37 int b =1; 38 } 39 } 40 41 42 static class TestClassWithInt { 43 int receivedI = 0; 44 public void hello(int i) 45 { 46 receivedI = i; 47 } 48 } 49 50 static class ModParExtension extends DefaultAspect 51 { 52 Crosscut myCC1 = new MethodCut() 53 { 54 public void METHOD_ARGS(TestClassWithInt receiver, int i) 55 { 56 if (thisJoinPoint() instanceof MethodEntryJoinPoint) 57 ((MethodEntryJoinPoint)thisJoinPoint()).setArg(0,new Integer (20)); 58 } 59 60 protected PointCutter pointCutter() 61 { 62 return null; 63 } 64 }; 65 } 66 67 static class NonPubExtension extends DefaultAspect 68 { 69 public boolean called = false; 70 Crosscut pc1 = new MethodCut() 71 { 72 public void METHOD_ARGS(TestClass cls, String stuff, REST params) 73 { 74 called = true; 75 } 76 protected PointCutter pointCutter() { return null;} 77 }; 78 } 79 80 81 82 public static class SimpleExtension extends DefaultAspect 83 { 84 public static List lastArgumentCalls = new Vector(); 85 86 public static class Crosscut1 extends MethodCut 87 { 88 public void METHOD_ARGS(TestClass cls, String stuff, REST params) 89 { 90 lastArgumentCalls.add("C1 received " + stuff); 91 } 92 93 protected PointCutter pointCutter() { return null;} 94 } 95 96 public static class Crosscut2 extends MethodCut 97 { 98 public void METHOD_ARGS(ANY thisObj,String stuff, String stuff1) 99 { 100 lastArgumentCalls.add("C2 received " + stuff); 101 } 102 103 protected PointCutter pointCutter() { return Within.method("byebye");} 104 } 105 106 Crosscut1 c1 = new Crosscut1(); 107 Crosscut2 c2 = new Crosscut2(); 108 } 109 110 111 static abstract class AbstractMethodCut extends MethodCut 112 { 113 public abstract void METHOD_ARGS(TestClass t, REST r); 114 115 protected PointCutter pointCutter() 116 { 117 return Within.method("hello").OR(Within.method("byebye")); 118 } 119 }; 120 121 static boolean concVisited = false; 122 static Vector visitedBy = new Vector(); 123 124 static class ConcreteMethodCut1 extends AbstractMethodCut 125 { 126 public void METHOD_ARGS(TestClass t, REST r) 127 { 128 concVisited = true; 129 visitedBy.add(getOwner()); 130 } 131 }; 132 133 static class ConcreteMethodCut2 extends ConcreteMethodCut1 134 { 135 protected PointCutter pointCutter() 136 { 137 return Within.method("byebye"). AND(super.pointCutter()); 138 } 139 }; 140 141 static class ConcreteExtension extends DefaultAspect 142 { 143 Crosscut c = new ConcreteMethodCut1(); 144 }; 145 146 static class ConcreteExtension1 extends DefaultAspect 147 { 148 Crosscut c = new ConcreteMethodCut2(); 149 }; 150 151 TestClass testObject = null; 152 SimpleExtension testExtension = null; 153 NonPubExtension npExtension = null; 154 ConcreteExtension concExtension = null; 155 ConcreteExtension1 concExtension1 = null; 156 157 161 public ExtensionInsertionTest(String name) 162 { 163 super(name); 164 } 165 166 167 170 protected void setUp() { 171 try { 173 ProseSystem.startup(); 174 } 175 catch ( Exception e ) { 176 e.printStackTrace(); 177 Assert.fail("ProseSystem.startup() failed."); 178 } 179 testExtension = new SimpleExtension(); 180 npExtension = new NonPubExtension(); 181 testObject = new TestClass(); 182 concExtension = new ConcreteExtension(); 183 concExtension1 = new ConcreteExtension1(); 184 } 185 186 189 protected void tearDown() { 190 try { 191 ProseSystem.teardown(); 192 } 193 catch ( Exception e ) { 194 e.printStackTrace(); 195 } 196 } 197 198 199 200 public void _testRegisterListener() throws Exception 201 { 202 try { 203 Iterator i1 = testExtension.c1.createRequest().iterator(); 204 while (i1.hasNext()) 205 { 206 JoinPointRequest crtReq = (JoinPointRequest)(i1.next()); 207 208 ProseSystem.getAspectManager().getJoinPointManager().registerListener(testExtension.c1,crtReq); 209 } 210 211 i1 = testExtension.c2.createRequest().iterator(); 212 while (i1.hasNext()) 213 { 214 JoinPointRequest crtReq = (JoinPointRequest)(i1.next()); 215 ProseSystem.getAspectManager().getJoinPointManager().registerListener(testExtension.c1,crtReq); 216 } 217 } 218 catch (Exception e) { 219 e.printStackTrace(); 220 } 221 } 222 223 231 public void testExtensionInsertion() throws Exception 232 { 233 try { 234 ProseSystem.getAspectManager().insert(testExtension); 235 236 for(int i=0; i < 5; i++) 237 testObject.hello("hello invocation [ " + i + " ]"); 238 239 for(int i=5; i < 10; i++) 240 testObject.byebye("byebye invocation [" + i + "]","foo"); 241 242 assertTrue("thirty notifications", SimpleExtension.lastArgumentCalls.size()==30); 243 ProseSystem.getAspectManager().withdraw(testExtension); 244 } 245 catch (Throwable e) { 246 e.printStackTrace(); 247 throw (Exception )e; 248 } 249 } 250 251 255 public void testInnerCrosscutInsertion() throws Exception 256 { 257 ProseSystem.getAspectManager().insert(npExtension); 258 testObject.byebye("byebye", "invocation"); 259 assertTrue("Inner class method called", npExtension.called); 260 ProseSystem.getAspectManager().withdraw(npExtension); 261 } 262 263 266 public void testTeardownQuality() 267 { 268 ProseSystem.getAspectManager().insert(npExtension); 269 try 270 { ProseSystem.teardown();} 271 catch (SystemTeardownException e) 272 { Assert.fail("ProseSystem.teardown() failed");} 273 testObject.byebye("byebye","invocation"); 274 assertTrue("Inner class method not called",!npExtension.called); 275 } 276 277 278 public void testConcreteExtension() 279 { 280 ProseSystem.getAspectManager().insert(concExtension); 281 concVisited = false; 282 testObject.hello(""); 283 assertTrue("conc was visted by hello",concVisited); 284 concVisited = false; 285 testObject.byebye("",""); 286 assertTrue("conc was visted by bybye",concVisited); 287 } 288 289 290 public void testConcreteExtension1() 291 { 292 ProseSystem.getAspectManager().insert(concExtension1); 293 concVisited = false; 294 testObject.hello(""); 295 assertTrue("conc was NOT visted by hello",!concVisited); 296 concVisited = false; 297 testObject.byebye("",""); 298 assertTrue("conc was visted by bybye",concVisited); 299 } 300 301 302 public void testExtensionUniqueness() 303 { 304 ConcreteExtension ce1 = new ConcreteExtension(); 305 ce1.associateTo("first"); 306 ConcreteExtension ce2 = new ConcreteExtension(); 307 ce2.associateTo("first"); 308 309 ProseSystem.getAspectManager().insert(ce1); 310 311 try { 314 ProseSystem.getAspectManager().insert(ce2); Assert.fail("was able to insert second aspect with the same id"); 316 } 317 catch (Throwable e) 318 { 319 } 321 } 322 323 public void testParamModification() 324 { 325 ModParExtension mpe = new ModParExtension(); 326 ProseSystem.getAspectManager().insert(mpe); 327 TestClassWithInt objWithInt = new TestClassWithInt(); 328 objWithInt.hello(10); 329 assertEquals("ModParExtension has doubled the value",20,objWithInt.receivedI); 330 } 331 332 public void testNotificationOrder() 333 { 334 visitedBy = new Vector(); 335 ConcreteExtension ce1 = new ConcreteExtension(); 336 ce1.setPriority(1); 337 ce1.associateTo("first"); 338 ConcreteExtension ce2 = new ConcreteExtension(); 339 ce2.associateTo("second"); 340 ce2.setPriority(2); 341 342 ProseSystem.getAspectManager().insert(ce1); 343 ProseSystem.getAspectManager().insert(ce2); 344 testObject.byebye("",""); 345 346 Vector firstOrderVisitedBy = visitedBy; 347 visitedBy = new Vector(); 348 ProseSystem.getAspectManager().withdraw(ce1); 349 ProseSystem.getAspectManager().withdraw(ce2); 350 351 ce1.setPriority(2); 352 ce2.setPriority(1); 353 ProseSystem.getAspectManager().insert(ce1); 354 ProseSystem.getAspectManager().insert(ce2); 355 testObject.byebye("",""); 356 357 358 assertTrue("vectors differ in ordering",!firstOrderVisitedBy.equals(visitedBy)); 359 } 360 361 365 public static 366 Test suite() 367 { 368 return new TestSuite(ExtensionInsertionTest.class); 369 } 370 371 } 372 373 374 | Popular Tags |