1 7 package ch.ethz.prose.filter; 8 9 import java.lang.reflect.Field ; 11 12 import junit.framework.*; 13 import ch.ethz.jvmai.*; 14 import ch.ethz.prose.*; 15 import ch.ethz.prose.engine.*; 16 17 18 24 public class ThiSTest extends TestCase { 25 26 28 public static class CreateEvents extends JoinPointListener { 30 31 public CreateEvents() {} 32 33 public void joinPointReached(MethodEntryJoinPoint jp) 34 { 35 thisTest._testEqual(jp); 36 thisTest._testSame(jp); 37 thisTest._testInClass(jp); 38 thisTest._testInstanceOf(jp); 39 } 40 41 public void joinPointReached(MethodExitJoinPoint jp) 42 { 43 thisTest._testEqual(jp); 44 thisTest._testSame(jp); 45 thisTest._testInClass(jp); 46 thisTest._testInstanceOf(jp); 47 } 48 49 public void joinPointReached(FieldAccessJoinPoint jp) 50 { 51 thisTest._testEqual(jp); 52 thisTest._testSame(jp); 53 thisTest._testInClass(jp); 54 thisTest._testInstanceOf(jp); 55 } 56 57 public void joinPointReached(FieldModificationJoinPoint jp) 58 { 59 thisTest._testEqual(jp); 60 thisTest._testSame(jp); 61 thisTest._testInClass(jp); 62 thisTest._testInstanceOf(jp); 63 } 64 65 public void joinPointReached(ExceptionJoinPoint jp) 66 { 67 thisTest._testEqual(jp); 68 thisTest._testSame(jp); 69 thisTest._testInClass(jp); 70 thisTest._testInstanceOf(jp); 71 } 72 73 public void joinPointReached(ExceptionCatchJoinPoint jp) 74 { 75 thisTest._testEqual(jp); 76 thisTest._testSame(jp); 77 thisTest._testInClass(jp); 78 thisTest._testInstanceOf(jp); 79 } 80 81 } 82 83 84 public static class CheckSubclass extends JoinPointListener { 85 86 boolean isSubClass = false; 87 boolean isSuperClass = false; 88 89 public void joinPointReached(MethodEntryJoinPoint jp) 90 { 91 92 } 93 94 public void joinPointReached(MethodExitJoinPoint jp) 95 { 96 97 } 98 99 public void joinPointReached(FieldAccessJoinPoint jp) 101 { 102 isSubClass = Within.subType(TestClass.class).isSpecialEvent(jp); 104 105 } 106 107 public void joinPointReached(FieldModificationJoinPoint jp) 109 { 110 isSuperClass = Within.superType(TestSubClass.class).isSpecialEvent(jp); 111 } 112 113 public void joinPointReached(ExceptionJoinPoint jp) 114 { 115 116 } 117 118 public void joinPointReached(ExceptionCatchJoinPoint jp) 119 { 120 121 } 122 123 } 124 125 static class TestClass 127 { 128 public int i; 129 130 public TestClass() 131 { 132 } 133 134 public void dummyAccess() 135 { 136 int b = i; 137 } 138 139 public void dummySet() 140 { 141 i = 1; 142 } 143 } 144 145 static class TestSubClass extends TestClass implements java.io.Serializable 146 { 147 } 148 149 150 static TestClass testobj; 151 PointFilter spec; 152 JoinPointListener jpl; 153 FieldAccessRequest accessReq; 154 FieldModificationRequest modifyReq; 155 156 static ThiSTest thisTest; 158 159 static JoinPointManager jpm ; 160 161 165 public ThiSTest(String name) { 166 super(name); 167 thisTest = this; 168 } 169 170 171 174 protected void setUp() { 175 176 Field f = null; 177 jpl = new CreateEvents(); 178 179 testobj = new TestSubClass(); 180 try 183 { 184 ProseSystem.startup(); 185 } 186 catch ( SystemStartupException e ) 187 { 188 Assert.fail("ExtensionSystemSetupException"); 189 } 190 191 jpm = ProseSystem.getAspectManager().getJoinPointManager(); 192 try 193 { 194 f = TestClass.class.getDeclaredField("i"); 195 } 196 catch ( Exception e ) 197 { 198 Assert.fail("Error getting field."); 199 } 200 accessReq = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f); 201 modifyReq = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f); 202 } 203 204 205 protected void tearDown() 206 { 207 try 208 { ProseSystem.teardown(); } 209 catch (SystemTeardownException tde) 210 { Assert.fail("Aspect.teardown() failed "); } 211 } 212 213 217 public void test_02_ObjectSpecializers() 218 { 219 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, accessReq); 221 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, modifyReq); 222 ProseSystem.getAspectManager().getJoinPointManager().resumeListenerNotification(Thread.currentThread()); 223 224 testobj.dummySet(); 226 227 testobj.dummyAccess(); 229 230 ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl); 231 } 232 233 public void test_01_SubtypeSpecializers() throws Exception 234 { 235 CheckSubclass csJpl = new CheckSubclass(); 236 238 239 ProseSystem.getAspectManager().getJoinPointManager().registerListener(csJpl, accessReq); 240 ProseSystem.getAspectManager().getJoinPointManager().registerListener(csJpl, modifyReq); 241 ProseSystem.getAspectManager().getJoinPointManager().resumeListenerNotification(Thread.currentThread()); 242 243 testobj.dummySet(); 245 testobj = new TestSubClass(); 246 testobj.dummyAccess(); 248 249 ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl); 250 251 assertTrue(csJpl.isSubClass); 252 assertTrue(csJpl.isSuperClass); 253 } 254 255 256 static class MyException extends RuntimeException {}; 257 258 public void test_03_ThrowInAdvice() throws Exception 259 { 260 CheckSubclass csJpl = new CheckSubclass() 261 { 262 public void joinPointReached(FieldAccessJoinPoint jp) 264 { 265 try 266 { 267 Assert.fail(); 268 } 269 catch (Throwable e) 270 {} 271 } 272 }; 273 274 275 ProseSystem.getAspectManager().getJoinPointManager().registerListener(csJpl, accessReq); 276 ProseSystem.getAspectManager().getJoinPointManager().resumeListenerNotification(Thread.currentThread()); 277 278 testobj.dummyAccess(); 279 280 ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl); 281 282 } 283 284 private void doNull() 285 { 286 String x = null; 287 x.length(); 288 } 289 290 293 void _testEqual(JoinPoint ev) 294 { 295 Exception e = null; 296 spec = This.equalsTo(testobj); 297 assertTrue("1", spec.isSpecialEvent(ev)); 298 299 spec = This.equalsTo(new TestClass()); 300 assertTrue("2", ! spec.isSpecialEvent(ev)); 301 } 302 303 306 void _testSame(JoinPoint ev) { 307 Exception e = null; 308 spec = This.isSameObject(testobj); 309 assertTrue("1", spec.isSpecialEvent(ev)); 310 spec = This.isSameObject(new TestClass()); 311 assertTrue("2", ! spec.isSpecialEvent(ev)); 312 313 } 314 315 318 void _testInstanceOf(JoinPoint ev) 319 { 320 spec = This.subtypeOf(java.io.Serializable .class); 321 assertTrue("3", spec.isSpecialEvent(ev)); 322 323 spec = This.subtypeOf(java.lang.String .class); 324 assertTrue("4", ! spec.isSpecialEvent(ev)); 325 } 326 327 void _testInClass(JoinPoint ev) 328 { 329 spec = This.type(".*Sub.*"); 330 assertTrue("5", spec.isSpecialEvent(ev)); 331 332 spec = This.type("TestClass"); 333 assertTrue("6", !spec.isSpecialEvent(ev)); 334 } 335 336 337 341 public static Test suite() { 342 return new TestSuite(ThiSTest.class); 343 } 344 345 } 346 347 348 | Popular Tags |