1 7 package ch.ethz.prose; 8 9 import java.lang.reflect.Field ; 11 12 import junit.framework.*; 13 import ch.ethz.jvmai.*; 14 import ch.ethz.prose.engine.*; 15 16 38 public class FieldEventIntegrationTest extends TestCase { 39 40 static class TestClass { 42 public int instanceField; 43 static String classField = "classField"; 44 45 Integer obj; 46 47 public TestClass() { } 48 } 49 50 static class FieldListener extends JoinPointListener { 51 public int read; 52 public int write; 53 54 public Field field; 55 public Object owner, oldValue, newValue; 56 57 public FieldListener() { } 58 59 public void joinPointReached(FieldAccessJoinPoint e) 60 { 61 field = null; 62 owner = null; 63 newValue = null; 64 65 read++; 66 field = e.getField(); 67 owner = e.getTarget(); 68 try 69 { 70 oldValue = field.get(owner); 71 } 72 catch ( Exception exc ) 73 { 74 Assert.fail("Could not get oldValue on FieldAccessEvent"); 75 } 76 } 77 78 public void joinPointReached(FieldModificationJoinPoint e) 79 { 80 write++; 81 field = e.getField(); 82 owner = e.getTarget(); 83 try 84 { 85 oldValue = field.get(owner); 86 } 87 catch ( Exception exc ) 88 { 89 Assert.fail("Could not get oldValue on FieldModificationEvent"); 90 } 91 newValue = e.getNewValue(); 92 } 93 94 public void joinPointReached(MethodEntryJoinPoint jp) 95 { 96 Assert.fail("The event received in FieldListener should be a " + 97 "FieldAccessJoinPoint or FieldModificationJoinPoint!!"); 98 } 99 100 public void joinPointReached(MethodExitJoinPoint jp) 101 { 102 Assert.fail("The event received in FieldListener should be a " + 103 "FieldAccessJoinPoint or FieldModificationJoinPoint!!"); 104 } 105 106 public void joinPointReached(ExceptionJoinPoint jp) 107 { 108 Assert.fail("The event received in FieldListener should be a " + 109 "FieldAccessJoinPoint or FieldModificationJoinPoint!!"); 110 } 111 112 public void joinPointReached(ExceptionCatchJoinPoint jp) 113 { 114 Assert.fail("The event received in FieldListener should be a " + 115 "FieldExceptionCatchJoinPoint!!"); 116 } 117 } 118 119 120 JoinPointManager jpm; 121 TestClass testClass; 122 TestClass anotherTestClass; 123 Class c; 124 Field classF, instanceF, objF; 125 FieldAccessRequest accessInstReq, accessClsReq, accessObjReq; 126 FieldModificationRequest modInstReq, modClsReq, modObjReq; 127 FieldListener jpl; 128 129 130 134 public FieldEventIntegrationTest(String name) { 135 super(name); 136 } 137 138 139 142 protected void setUp() { 143 testClass = new TestClass(); 144 anotherTestClass = new TestClass(); 145 jpl = new FieldListener(); 146 147 try { 148 ProseSystem.startup(); 149 } 150 catch ( SystemStartupException e ) { 151 Assert.fail("SystemStartupException"); 152 } 153 154 c = testClass.getClass(); 155 try { 156 instanceF = c.getDeclaredField("instanceField"); 157 classF = c.getDeclaredField("classField"); 158 objF = c.getDeclaredField("obj"); 159 } 160 catch ( Exception e ) { 161 Assert.fail("Field \"instanceField\" or \"classField\" or \"obj\" " + 162 "not found in Class \"TestClass\"."); 163 } 164 165 jpm = ProseSystem.getAspectManager().getJoinPointManager(); 166 167 JVMAspectInterface aspectInterface = jpm.getAspectInterface(); 168 169 accessInstReq = (FieldAccessRequest)jpm.createJoinPointRequest( FieldAccessJoinPoint.KIND ,instanceF); 170 modInstReq = (FieldModificationRequest)jpm.createJoinPointRequest( FieldModificationJoinPoint.KIND,instanceF); 171 accessClsReq = (FieldAccessRequest)jpm.createJoinPointRequest( FieldAccessJoinPoint.KIND ,classF ); 172 modClsReq = (FieldModificationRequest)jpm.createJoinPointRequest( FieldModificationJoinPoint.KIND,classF ); 173 accessObjReq = (FieldAccessRequest)jpm.createJoinPointRequest( FieldAccessJoinPoint.KIND ,objF ); 174 modObjReq = (FieldModificationRequest)jpm.createJoinPointRequest( FieldModificationJoinPoint.KIND,objF ); 175 } 176 177 178 181 protected void tearDown() throws SystemTeardownException { 182 try { 183 ProseSystem.teardown(); 184 } 185 catch ( Exception e ) { 186 Assert.fail("ProseSystem.teardown() failed."); 187 } 188 } 189 190 191 192 195 public void testInstanceField() throws Exception { 196 jpl.read = 0; 198 jpl.write = 0; 199 testClass.instanceField = 0; 200 assertTrue(testClass.instanceField == 0); 201 assertTrue(jpl.read == 0); 202 assertTrue(jpl.write == 0); 203 204 jpm.registerListener(jpl, accessInstReq); 206 jpm.registerListener(jpl, modInstReq); 207 208 assertTrue(testClass.instanceField == 0); 210 assertTrue(jpl.read == 1); 211 assertTrue(jpl.write == 0); 212 assertTrue(jpl.field.equals(instanceF)); 213 assertTrue(jpl.owner.equals(testClass)); 214 assertTrue(! jpl.owner.equals(anotherTestClass)); 215 assertTrue(((Integer )jpl.oldValue).intValue() == 0); 216 217 testClass.instanceField = 999; 219 assertTrue(jpl.read == 1); 220 assertTrue(jpl.write == 1); 221 assertTrue(jpl.field.equals(instanceF)); 222 assertTrue(jpl.owner.equals(testClass)); 223 assertTrue(! jpl.owner.equals(anotherTestClass)); 224 assertTrue(jpl.field.getInt(testClass) == 999); 225 assertTrue(((Integer )jpl.oldValue).intValue() == 0); 226 assertTrue(((Integer )jpl.newValue).intValue() == 999); 227 228 assertTrue(testClass.instanceField == 999); 230 assertTrue(jpl.read == 2); 231 assertTrue(jpl.write == 1); 232 assertTrue(jpl.field.equals(instanceF)); 233 assertTrue(jpl.owner.equals(testClass)); 234 assertTrue(! jpl.owner.equals(anotherTestClass)); 235 assertTrue(((Integer )jpl.oldValue).intValue() == 999); 236 237 jpm.unregisterListener(jpl); 239 240 testClass.instanceField = 10; 243 assertTrue(testClass.instanceField == 10); 244 assertTrue(jpl.read == 2); assertTrue(jpl.write == 1); } 247 248 249 252 public void testClassField() throws Exception { 253 jpl.read = 0; 255 jpl.write = 0; 256 TestClass.classField = "initialized"; 257 assertTrue(TestClass.classField.equals("initialized")); 258 assertTrue(jpl.read == 0); 259 assertTrue(jpl.write == 0); 260 261 jpm.registerListener(jpl, accessClsReq); 263 jpm.registerListener(jpl, modClsReq); 264 265 assertTrue(TestClass.classField.equals("initialized")); 267 assertTrue(jpl.read == 1); 268 assertTrue(jpl.write == 0); 269 assertTrue(jpl.field.equals(classF)); 270 assertTrue(((String )jpl.oldValue).equals("initialized")); 271 272 TestClass.classField = "modified"; 274 assertTrue(jpl.read == 1); 275 assertTrue(jpl.write == 1); 276 assertTrue(jpl.field.equals(classF)); 277 assertTrue(((String )jpl.oldValue).equals("initialized")); 278 assertEquals("modified",((String )jpl.newValue)); 279 280 assertTrue(TestClass.classField.equals("modified")); 282 assertTrue(jpl.read == 2); 283 assertTrue(jpl.write == 1); 284 assertTrue(jpl.field.equals(classF)); 285 assertTrue(((String )jpl.oldValue).equals("modified")); 286 287 jpm.unregisterListener(jpl); 289 290 TestClass.classField = "reset"; 293 assertTrue(TestClass.classField.equals("reset")); 294 assertTrue(jpl.read == 2); assertTrue(jpl.write == 1); } 297 298 299 public void testObjectField() throws Exception { 300 jpl.read = 0; 302 jpl.write = 0; 303 testClass.obj = new Integer (1); 304 assertTrue("o1", testClass.obj.intValue() == 1); 305 assertTrue("o2", jpl.read == 0); 306 assertTrue("o3", jpl.write == 0); 307 308 jpm.registerListener(jpl, accessObjReq); 310 jpm.registerListener(jpl, modObjReq); 311 312 assertTrue("o4", testClass.obj.intValue() == 1); 314 assertTrue("o5", jpl.read == 1); 315 assertTrue("o6", jpl.write == 0); 316 assertTrue("o7", jpl.field.equals(objF)); 317 assertTrue("o8", ((Integer )jpl.oldValue).intValue() == 1); 318 319 testClass.obj = new Integer (2); 321 assertTrue("o9", jpl.read == 1); 322 assertTrue("o10", jpl.write == 1); 323 assertTrue("o11", jpl.field.equals(objF)); 324 assertTrue("o12", ((Integer )jpl.field.get(testClass)).intValue() == 2); 325 assertTrue("o13", ((Integer )jpl.oldValue).intValue() == 1); 326 assertEquals("o14", new Integer (2),((Integer )jpl.newValue)); 327 328 assertTrue("o15", testClass.obj.intValue() == 2); 330 assertTrue("o16", jpl.read == 2); 331 assertTrue("o17", jpl.write == 1); 332 assertTrue("o18", jpl.field.equals(objF)); 333 assertTrue("o19", ((Integer )jpl.oldValue).intValue() == 2); 334 335 jpm.unregisterListener(jpl); 337 338 testClass.obj = new Integer (0); 341 assertTrue("o20", testClass.obj.intValue() == 0); 342 assertTrue("o21", jpl.read == 2); assertTrue("o22", jpl.write == 1); } 345 346 350 public static 351 Test suite() { 352 return new TestSuite(FieldEventIntegrationTest.class); 353 } 354 355 } 356 357 358
| Popular Tags
|