1 7 package ch.ethz.prose; 8 9 import java.lang.reflect.Field ; 11 import java.lang.reflect.Method ; 12 import java.util.Date ; 13 import java.util.List ; 14 15 import junit.framework.*; 16 import ch.ethz.jvmai.*; 17 24 public 25 class JVMInfoInterfaceTest extends TestCase { 26 27 JVMAspectInterface aspectInterface = null; 28 29 Method method = null; 30 Field field = null; 31 32 36 public JVMInfoInterfaceTest(String name) 37 { 38 super(name); 39 } 40 41 static class TestClass1 { 42 public int field; 43 public String parameterInput = ""; 44 public int method(boolean a, byte b, short c, int d, long e, float f, double g, char h, Object i, String j) 45 { 46 parameterInput = "a=" + a + "/b=" + b + "/c=" + c + "/d=" + d + "/e=" + e + "/f=" + f + "/g=" + g + "/h=" + h; 47 a=true; 48 b = (byte)-b; 49 c = (short)-c; 50 d = -d; 51 e = -e; 52 f = -f; 53 g = -g; 54 h = 'z'; 55 i = new Date (); 56 j = "moon"; 57 return d*2; 58 } 59 } 60 61 static class TestException extends RuntimeException 62 {} 63 64 static class TestClass2 { 65 } 66 67 68 protected void setUp() throws Exception 69 { 70 String providerClassName = System.getProperty("ch.ethz.prose.JVMAIProvider","ch.ethz.inf.iks.jvmai.jvmdi.DebuggerProvider"); 71 Class providerClass = Class.forName(providerClassName); 72 Provider provider = (Provider)providerClass.newInstance(); 73 aspectInterface = provider.getAspectInterface(); 74 75 aspectInterface.startup(new String [0],true); 77 method = TestClass1.class.getDeclaredMethods()[0]; 78 field = TestClass1.class.getDeclaredFields()[0]; 79 } 80 81 82 protected void tearDown() 83 { 84 try 85 { 86 try { aspectInterface.clearMethodEntryWatch (method); } catch (RuntimeException e) { } 87 try { aspectInterface.clearMethodExitWatch (method); } catch (RuntimeException e) { } 88 try { aspectInterface.clearFieldAccessWatch (field); } catch (RuntimeException e) { } 89 try { aspectInterface.clearFieldModificationWatch(field ); } catch (RuntimeException e) { } 90 try { aspectInterface.clearExceptionThrowWatch (TestException.class); } catch (RuntimeException e) { } 91 try { aspectInterface.clearExceptionCatchWatch (TestException.class); } catch (RuntimeException e) { } 92 try { aspectInterface.setJoinPointHook(null); } catch (RuntimeException e) { } 93 aspectInterface.teardown(); 94 } 95 catch (RuntimeException e) 96 { 97 } 98 } 99 100 101 public void test_0010_GetLoadedClasses() 102 { 103 104 List classes = aspectInterface.getLoadedClasses(); 105 assertTrue("class TestClass1 is loaded",classes.contains(TestClass1.class)); 106 } 107 108 class TestKindHook extends JoinPointHook 109 { 110 String ofaKind = null; 111 String ofmKind = null; 112 String omenKind = null; 113 String omexKind = null; 114 String oexKind = null; 115 String oexcKind = null; 116 public void onFieldAccess(FieldAccessJoinPoint joinPoint) { ofaKind = joinPoint.getKind(); } 117 public void onFieldModification(FieldModificationJoinPoint joinPoint) { ofmKind = joinPoint.getKind(); } 118 public void onMethodEntry(MethodEntryJoinPoint joinPoint) { omenKind = joinPoint.getKind(); } 119 public void onMethodExit(MethodExitJoinPoint joinPoint) { omexKind = joinPoint.getKind(); } 120 public void onExceptionThrow(ExceptionJoinPoint joinPoint) { oexKind = joinPoint.getKind(); } 121 public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) { oexcKind = joinPoint.getKind(); } 122 public void onClassLoad(Class cls) { } 123 }; 124 125 126 public void test_0030_GetJoinPointKinds() 127 { 128 TestKindHook hook=new TestKindHook(); 129 TestClass1 test = new TestClass1(); 130 131 aspectInterface.setFieldAccessWatch (field ,"hi"); 132 aspectInterface.setFieldModificationWatch(field ,"there"); 133 aspectInterface.setMethodEntryWatch (method,"how are"); 134 aspectInterface.setMethodExitWatch (method,"you"); 135 aspectInterface.setExceptionThrowWatch (TestException.class,"you"); 136 aspectInterface.setExceptionCatchWatch (TestException.class,"you"); 137 aspectInterface.resumeNotification(Thread.currentThread()); 138 139 aspectInterface.setJoinPointHook(hook); 140 enclosingLevel1(test); 141 aspectInterface.setJoinPointHook(null); 142 143 assertEquals("Field modif",FieldModificationJoinPoint.KIND,hook.ofmKind); 144 assertEquals("Method entry",MethodEntryJoinPoint.KIND,hook.omenKind); 145 assertEquals("Method exit",MethodExitJoinPoint.KIND,hook.omexKind); 146 assertEquals("Exception throw",ExceptionJoinPoint.KIND,hook.oexKind); 147 assertEquals("Exception catch",ExceptionCatchJoinPoint.KIND,hook.oexcKind); 148 assertEquals("Field access",FieldAccessJoinPoint.KIND,hook.ofaKind); 149 aspectInterface.clearFieldAccessWatch (field ); 150 aspectInterface.clearFieldModificationWatch(field ); 151 aspectInterface.clearMethodEntryWatch (method); 152 aspectInterface.clearMethodExitWatch (method); 153 aspectInterface.clearExceptionThrowWatch (TestException.class); 154 aspectInterface.clearExceptionCatchWatch (TestException.class); 155 156 } 157 158 private void enclosingLevel1(TestClass1 test) 159 { 160 int t = test.method(false,(byte)1,(short)2,3,4l,5.0f,6.0,'a',"hello","world"); 161 test.field = test.field + 1; 162 try { throw new TestException(); } catch (Exception e) {} 163 } 164 165 private void enclosingLevel2(int i) 166 { 167 TestClass1 test = new TestClass1(); 168 enclosingLevel1(test); 169 } 170 171 private void enclosingLevel3() 172 { 173 enclosingLevel2(1); 174 } 175 176 class TestEnclosingHook extends JoinPointHook 177 { 178 Method [] caller = new Method [6]; 179 Method [] callerCaller = new Method [6]; 180 Object [] thisCaller = new Object [6]; 181 Object [] thisCallerCaller = new Object [6]; 182 public void onFieldAccess(FieldAccessJoinPoint joinPoint) 183 { 184 CodeJoinPoint callerJp = joinPoint.getEnclosingJoinPoint(); 185 caller[0] = callerJp.getMethod(); 186 thisCaller[0] = callerJp.getThis(); 187 CodeJoinPoint callerCallerJp = callerJp.getEnclosingJoinPoint(); 188 callerCaller[0] = callerCallerJp.getMethod(); 189 thisCallerCaller[0] = callerCallerJp.getThis(); 190 } 191 public void onFieldModification(FieldModificationJoinPoint joinPoint) 192 { 193 CodeJoinPoint callerJp = joinPoint.getEnclosingJoinPoint(); 194 caller[1] = callerJp.getMethod(); 195 thisCaller[1] = callerJp.getThis(); 196 CodeJoinPoint callerCallerJp = callerJp.getEnclosingJoinPoint(); 197 callerCaller[1] = callerCallerJp.getMethod(); 198 thisCallerCaller[1] = callerCallerJp.getThis(); 199 } 200 public void onMethodEntry(MethodEntryJoinPoint joinPoint) 201 { 202 203 CodeJoinPoint callerJp = joinPoint.getEnclosingJoinPoint(); 204 caller[2] = callerJp.getMethod(); 205 thisCaller[2] = callerJp.getThis(); 206 CodeJoinPoint callerCallerJp = callerJp.getEnclosingJoinPoint(); 207 callerCaller[2] = callerCallerJp.getMethod(); 208 thisCallerCaller[2] = callerCallerJp.getThis(); 209 } 210 public void onMethodExit(MethodExitJoinPoint joinPoint) 211 { 212 CodeJoinPoint callerJp = joinPoint.getEnclosingJoinPoint(); 213 caller[3] = callerJp.getMethod(); 214 thisCaller[3] = callerJp.getThis(); 215 CodeJoinPoint callerCallerJp = callerJp.getEnclosingJoinPoint(); 216 callerCaller[3] = callerCallerJp.getMethod(); 217 thisCallerCaller[3] = callerCallerJp.getThis(); 218 } 219 public void onExceptionThrow(ExceptionJoinPoint joinPoint) 220 { 221 CodeJoinPoint callerJp = joinPoint.getEnclosingJoinPoint(); 222 caller[4] = callerJp.getMethod(); 223 thisCaller[4] = callerJp.getThis(); 224 CodeJoinPoint callerCallerJp = callerJp.getEnclosingJoinPoint(); 225 callerCaller[4] = callerCallerJp.getMethod(); 226 thisCallerCaller[4] = callerCallerJp.getThis(); 227 } 228 229 public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) 230 { 231 CodeJoinPoint callerJp = joinPoint.getEnclosingJoinPoint(); 232 caller[5] = callerJp.getMethod(); 233 thisCaller[5] = callerJp.getThis(); 234 CodeJoinPoint callerCallerJp = callerJp.getEnclosingJoinPoint(); 235 callerCaller[5] = callerCallerJp.getMethod(); 236 thisCallerCaller[5] = callerCallerJp.getThis(); 237 } 238 239 public void onClassLoad(Class cls) {} 240 }; 241 242 243 public void test_0025_GetEnclosingJoinPoint() 244 { 245 aspectInterface.setFieldAccessWatch (field ,"hi"); 246 aspectInterface.setFieldModificationWatch(field ,"there"); 247 aspectInterface.setMethodEntryWatch (method,"how are"); 248 aspectInterface.setMethodExitWatch (method,"you"); 249 aspectInterface.setExceptionThrowWatch (TestException.class,"you"); 250 aspectInterface.setExceptionCatchWatch (TestException.class,"you"); 251 TestEnclosingHook hook = new TestEnclosingHook(); 252 aspectInterface.setJoinPointHook(hook); 253 enclosingLevel3(); 254 aspectInterface.setJoinPointHook(null); 255 256 assertEquals("Caller " + 0 + " is enclosingLevel1","enclosingLevel2",hook.caller[0].getName()); 257 assertEquals("Caller " + 1 + " is enclosingLevel1","enclosingLevel2",hook.caller[1].getName()); 258 assertEquals("Caller " + 2 + " is enclosingLevel1","enclosingLevel1",hook.caller[2].getName()); 259 assertEquals("Caller " + 3 + " is enclosingLevel1","enclosingLevel1",hook.caller[3].getName()); 260 assertEquals("Caller " + 4 + " is enclosingLevel1","enclosingLevel2",hook.caller[4].getName()); 261 assertEquals("Caller " + 5 + " is enclosingLevel1","enclosingLevel2",hook.caller[5].getName()); 262 263 assertEquals("This " + 0 + " is enclosingLevel1",this,hook.thisCaller[0]); 264 assertEquals("This " + 1 + " is enclosingLevel1",this,hook.thisCaller[1]); 265 assertEquals("This " + 2 + " is enclosingLevel1",this,hook.thisCaller[2]); 266 assertEquals("This " + 3 + " is enclosingLevel1",this,hook.thisCaller[3]); 267 assertEquals("This " + 4 + " is enclosingLevel1",this,hook.thisCaller[4]); 268 assertEquals("This " + 5 + " is enclosingLevel1",this,hook.thisCaller[5]); 269 270 assertEquals("CCaller " + 0 + " is enclosingLevel1","enclosingLevel3",hook.callerCaller[0].getName()); 271 assertEquals("CCaller " + 1 + " is enclosingLevel1","enclosingLevel3",hook.callerCaller[1].getName()); 272 assertEquals("CCaller " + 2 + " is enclosingLevel1","enclosingLevel2",hook.callerCaller[2].getName()); 273 assertEquals("CCaller " + 3 + " is enclosingLevel1","enclosingLevel2",hook.callerCaller[3].getName()); 274 assertEquals("CCaller " + 4 + " is enclosingLevel1","enclosingLevel3",hook.callerCaller[4].getName()); 275 assertEquals("CCaller " + 5 + " is enclosingLevel1","enclosingLevel3",hook.callerCaller[5].getName()); 276 } 277 278 public void test_0030_GetLocalVariableInfo() 279 { 280 284 } 285 286 287 288 public void test_0040_GetFieldJoinPointInfo() 289 { 290 291 Object aopTag = new Object (); 292 JoinPointHook hook = null; 293 TestClass1 test = new TestClass1(); 294 295 hook = new JoinPointHook() { 296 public void onFieldAccess(FieldAccessJoinPoint joinPoint) { 297 298 assertEquals("fieldClass and fieldId denote field",field,joinPoint.getField()); 299 assertTrue("getFieldOwner returns an instance of TestClass1",joinPoint.getTarget() instanceof TestClass1); 300 Integer value = (Integer )joinPoint.getValue(); 301 } 303 public void onFieldModification(FieldModificationJoinPoint joinPoint) { 304 assertEquals("fieldClass and fieldId denote field",field,joinPoint.getField()); 305 assertTrue("getFieldOwner returns an instance of TestClass1",joinPoint.getTarget() instanceof TestClass1); 306 Integer value1 = (Integer )joinPoint.getValue(); 307 Integer value2 = (Integer )joinPoint.getNewValue(); 308 } 310 public void onMethodEntry(MethodEntryJoinPoint joinPoint) { } 311 public void onMethodExit(MethodExitJoinPoint joinPoint) { } 312 public void onExceptionThrow(ExceptionJoinPoint joinPoint) { } 313 public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) { } 314 public void onClassLoad(Class cls) { } 315 }; 316 317 aspectInterface.setJoinPointHook(hook); 318 319 test.field = 1; 320 321 aspectInterface.setFieldAccessWatch (field ,aopTag); 322 aspectInterface.setFieldModificationWatch(field ,aopTag); 323 324 325 int r = test.field; aspectInterface.suspendNotification(Thread.currentThread()); 328 assertTrue("onFieldAccess was able to change the field value",test.field==2); 329 aspectInterface.resumeNotification(Thread.currentThread()); 330 331 test.field = 3; aspectInterface.suspendNotification(Thread.currentThread()); 334 assertTrue("onFieldModification was able to change the new field value",test.field==5); 335 aspectInterface.resumeNotification(Thread.currentThread()); 336 337 } 338 339 340 341 public void test_0050_GetMethodJoinPointInfo() 342 { 343 344 Object aopTag = new Object (); 345 JoinPointHook hook = null; 346 TestClass1 test = new TestClass1(); 347 348 hook = new JoinPointHook() { 349 public void onFieldAccess(FieldAccessJoinPoint joinPoint) { } 350 public void onFieldModification(FieldModificationJoinPoint joinPoint) { } 351 public void onMethodEntry(MethodEntryJoinPoint joinPoint) { 352 353 354 assertEquals("methodClass and methodId denotes method",method,joinPoint.getMethod()); 355 assertTrue("getThisValue returns an instance of TestClass1",joinPoint.getThis() instanceof TestClass1); 356 357 Object [] params = joinPoint.getArgs(); 358 359 assertTrue("type of param[1] is ok",params[0] instanceof Boolean ); 360 assertTrue("type of param[2] is ok",params[1] instanceof Byte ); 361 assertTrue("type of param[3] is ok",params[2] instanceof Short ); 362 assertTrue("type of param[4] is ok",params[3] instanceof Integer ); 363 assertTrue("type of param[5] is ok",params[4] instanceof Long ); 364 assertTrue("type of param[6] is ok",params[5] instanceof Float ); 365 assertTrue("type of param[7] is ok",params[6] instanceof Double ); 366 assertTrue("type of param[8] is ok",params[7] instanceof Character ); 367 assertTrue("type of param[9] is ok",params[8] instanceof Object ); 368 assertTrue("type of param[10] is ok",params[9] instanceof String ); 369 370 assertTrue("value of param[1] is ok",params[0].equals(new Boolean (false))); 371 assertTrue("value of param[2] is ok",params[1].equals(new Byte ((byte)1))); 372 assertTrue("value of param[3] is ok",params[2].equals(new Short ((short)2))); 373 assertTrue("value of param[4] is ok",params[3].equals(new Integer (3))); 374 assertTrue("value of param[5] is ok",params[4].equals(new Long (4l))); 375 assertTrue("value of param[6] is ok",params[5].equals(new Float (5.0f))); 376 assertTrue("value of param[7] is ok",params[6].equals(new Double (6.0))); 377 assertTrue("value of param[8] is ok",params[7].equals(new Character ('a'))); 378 assertTrue("value of param[9] is ok",params[8].equals("hello")); 379 assertTrue("value of param[10] is ok",params[9].equals("world")); 380 381 joinPoint.setArg(0,new Boolean (true)); 382 joinPoint.setArg(1,new Byte ((byte)10)); 383 joinPoint.setArg(2,new Short ((short)20)); 384 joinPoint.setArg(3,new Integer (30)); 385 joinPoint.setArg(4,new Long (40l)); 386 joinPoint.setArg(5,new Float (50.0f)); 387 joinPoint.setArg(6,new Double (60.0)); 388 joinPoint.setArg(7,new Character ('k')); 389 joinPoint.setArg(8,"olleh"); 390 joinPoint.setArg(9,"dlrow"); 391 } 392 393 public void onMethodExit(MethodExitJoinPoint joinPoint) { 394 assertEquals("methodClass and methodId denotes method",method,joinPoint.getMethod()); 395 assertTrue("getThisValue returns an instance of TestClass1",joinPoint.getThis() instanceof TestClass1); 396 397 Object [] params = joinPoint.getArgs(); 398 399 400 assertTrue("type of param[1] is ok",params[0] instanceof Boolean ); 401 assertTrue("type of param[2] is ok",params[1] instanceof Byte ); 402 assertTrue("type of param[3] is ok",params[2] instanceof Short ); 403 assertTrue("type of param[4] is ok",params[3] instanceof Integer ); 404 assertTrue("type of param[5] is ok",params[4] instanceof Long ); 405 assertTrue("type of param[6] is ok",params[5] instanceof Float ); 406 assertTrue("type of param[7] is ok",params[6] instanceof Double ); 407 assertTrue("type of param[8] is ok",params[7] instanceof Character ); 408 assertTrue("type of param[9] is ok",params[8] instanceof Object ); 409 assertTrue("type of param[10] is ok",params[9] instanceof String ); 410 411 assertTrue("value of param[1] is ok",params[0].equals(new Boolean (true))); 412 assertTrue("value of param[2] is ok",params[1].equals(new Byte ((byte)-10))); 413 assertTrue("value of param[3] is ok",params[2].equals(new Short ((short)-20))); 414 assertTrue("value of param[4] is ok",params[3].equals(new Integer (-30))); 415 assertTrue("value of param[5] is ok",params[4].equals(new Long (-40l))); 416 assertTrue("value of param[6] is ok",params[5].equals(new Float (-50.0f))); 417 assertTrue("value of param[7] is ok",params[6].equals(new Double (-60.0))); 418 assertTrue("value of param[8] is ok",params[7].equals(new Character ('z'))); 419 assertTrue("value of param[9] is ok",params[8] instanceof Date ); 420 assertTrue("value of param[10] is ok",params[9].equals("moon")); 421 422 452 460 461 try 462 { 463 Integer value = (Integer )joinPoint.getResult(); 464 joinPoint.setResult(new Integer (value.intValue()+1)); 465 } 466 catch (RuntimeException x) 467 { 468 469 } 470 471 } 472 public void onExceptionThrow(ExceptionJoinPoint joinPoint) { } 473 public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) { } 474 public void onClassLoad(Class cls) { } 475 }; 476 477 478 aspectInterface.setJoinPointHook(hook); 479 480 aspectInterface.setMethodEntryWatch (method,aopTag); 481 aspectInterface.setMethodExitWatch (method,aopTag); 482 483 int t = test.method(false,(byte)1,(short)2,3,4l,5.0f,6.0,'a',"hello","world"); 484 485 486 aspectInterface.clearMethodEntryWatch (method); 487 aspectInterface.clearMethodExitWatch (method); 488 489 assertTrue("onMethodExit was able to change the return value",t==-59); 490 491 492 } 493 494 495 499 public static 500 Test suite() 501 { 502 return new TestSuite(JVMInfoInterfaceTest.class); 503 } 504 505 } 506 507 508
| Popular Tags
|