1 7 package ch.ethz.prose.query; 8 9 import java.lang.reflect.Field ; 11 import java.lang.reflect.Method ; 12 import java.util.TreeMap ; 13 14 import junit.framework.*; 15 import ch.ethz.jvmai.*; 16 import ch.ethz.prose.*; 17 import ch.ethz.prose.crosscut.Crosscut; 18 import ch.ethz.prose.crosscut.ThrowCut; 19 import ch.ethz.prose.engine.*; 20 import ch.ethz.prose.filter.Exceptions; 21 import ch.ethz.prose.filter.PointCutter; 22 23 30 public 31 class SurrogateTest extends TestCase { 32 33 35 public static abstract class MyTestClass 36 { 37 public static int fa; 38 public boolean fb; 39 public static String fc; 40 public AspectManager fd; 41 protected int[] fe; 42 private Object ff; 43 44 public static void ma() {} 45 public String mb(byte a, short b, int c, long d, float e, double f, boolean g) { return null; } 46 public static int mc(Object a, String b, AspectManager c, TreeMap d, boolean e, double[] f, Throwable [][][] g) { return 0; } 47 public abstract Class [] md(); 48 protected void me() {} 49 private boolean mf() { return true; } 50 } 51 52 public static class MyExtension extends DefaultAspect 53 { 54 public Crosscut ca = new ThrowCut() 55 { 56 public void THROW_ARGS() { } 57 public Class [] potentialCrosscutClasses() 58 { Class [] result = { LinkageError .class, 59 IllegalStateException .class, 60 InstantiationError .class, 61 NullPointerException .class, 62 RuntimeException .class, 63 ClassFormatError .class, 64 UnknownError .class}; 65 return result; } 66 67 protected PointCutter pointCutter() 68 { return ( (Exceptions.type("InstantiationError")) . OR (Exceptions.type("UnknownError")) );} 69 }; 70 71 public Crosscut cb = new ThrowCut() 72 { 73 public void THROW_ARGS() { } 74 public Class [] potentialCrosscutClasses() 75 { Class [] result = { LinkageError .class, 76 IllegalStateException .class, 77 InstantiationError .class, 78 NullPointerException .class, 79 RuntimeException .class, 80 ClassFormatError .class, 81 UnknownError .class}; 82 return result; } 83 protected PointCutter pointCutter() 84 { return ( (Exceptions.type("NullPointerException")) . OR (Exceptions.type("RuntimeException")) );} 85 }; 86 87 public Crosscut cc = new ThrowCut() 88 { 89 public void THROW_ARGS() { } 90 public Class [] potentialCrosscutClasses() 91 { Class [] result = { LinkageError .class, 92 IllegalStateException .class, 93 InstantiationError .class, 94 NullPointerException .class, 95 RuntimeException .class, 96 ClassFormatError .class, 97 UnknownError .class}; return result; } 98 protected PointCutter pointCutter() 99 { return ( (Exceptions.type("ClassFormatError")) . OR (Exceptions.type("UnknownError")) );} 100 }; 101 } 102 103 104 MyTestClass tstClsObj = null; 105 Class tstCls = null; 106 JVMAspectInterface ai = null; 107 108 112 public SurrogateTest(String name) 113 { 114 super(name); 115 } 116 117 120 protected 121 void setUp() 122 { 123 try 124 { 125 ProseSystem.startup(); 126 } 127 catch (SystemStartupException e) 128 { 129 Assert.fail("could not start ProseSystem"); 130 } 131 tstClsObj = new MyTestClass() { public Class [] md() { return null; } }; 132 tstCls = SurrogateTest.MyTestClass.class; 133 ai = ProseSystem.getAspectManager().getJoinPointManager().getAspectInterface(); 134 135 } 136 137 public 138 void tearDown() 139 { 140 try 141 { 142 ProseSystem.teardown(); 143 } 144 catch (SystemTeardownException e) 145 { 146 Assert.fail("could not teardown ProseSystem"); 147 } 148 } 149 150 public void test0010_MethodSurrogate() 152 { 153 try 154 { 155 Method m; 156 MethodSurrogate sur; 157 Class [] paramTypes; 158 159 Method [] mArr = tstCls.getDeclaredMethods(); 160 for (int j=0; j < mArr.length; j++) 161 { 162 m = mArr[j]; 163 paramTypes = m.getParameterTypes(); 164 sur = new MethodSurrogate(m); 165 assertTrue("["+j+"] name of Surrogate and method are the same", sur.getName().equals(m.getName())); 166 assertTrue("["+j+"] recreated method is still the same", sur.toRealInstance(tstCls.getName()).equals(m)); 167 } 168 } 169 catch (Exception e) 170 { 171 e.printStackTrace(); 172 } 173 } 174 175 public void test0020_FieldSurrogate() 177 { 178 try 179 { 180 Field f; 181 FieldSurrogate sur; 182 Class type; 183 184 Field [] fArr = tstCls.getDeclaredFields(); 185 for (int j=0; j < fArr.length; j++) 186 { 187 f = fArr[j]; 188 type = f.getType(); 189 sur = new FieldSurrogate(f); 190 assertTrue("["+j+"] name of Surrogate and field are the same", sur.getName().equals(f.getName())); 191 assertTrue("["+j+"] recreated field is still the same", sur.toRealInstance(tstCls.getName()).equals(f)); 192 } 193 } 194 catch (Exception e) 195 { 196 e.printStackTrace(); 197 } 198 } 199 200 public void test0030_ClassSurrogate() 202 { 203 try 204 { 205 Class c; 206 ClassSurrogate sur; 207 208 Class [] cArr = new Class [] {MyTestClass.class, MyExtension.class, SurrogateTest.class}; 209 for (int j=0; j < cArr.length; j++) 210 { 211 c = cArr[j]; 212 sur = new ClassSurrogate(c); 213 assertTrue(c.getName()+": class names are equal", sur.getName().equals(c.getName())); 214 assertTrue(c.getName()+" class is still the same", sur.toRealInstance().equals(c)); 215 } 216 } 217 catch (Exception e) 218 { 219 e.printStackTrace(); 220 } 221 } 222 223 public void test0040_JoinPointRequestSurrogate() 225 { 226 try 227 { 228 JoinPointManager jpm; 229 JoinPointRequest jpr; 230 JoinPointRequestSurrogate sur; 231 Method m; Field f; Class c; 232 jpm = ProseSystem.getAspectManager().getJoinPointManager(); 233 QueryManager qm = new QueryManager(ProseSystem.getAspectManager()); 234 235 Method [] mArr = tstCls.getDeclaredMethods(); 236 for (int j=0; j < mArr.length; j++) 237 { 238 m = mArr[j]; 239 240 jpr = (MethodEntryRequest)jpm.createJoinPointRequest(MethodEntryJoinPoint.KIND,m); 241 sur = new JoinPointRequestSurrogate(jpr); 242 assertTrue("["+j+"] recreated MethodEntryRequest is still the same", 243 qm.reconstructRequest(sur).equals(jpr)); 244 245 jpr = (MethodExitRequest)jpm.createJoinPointRequest(MethodExitJoinPoint.KIND,m); 246 sur = new JoinPointRequestSurrogate(jpr); 247 assertTrue("["+j+"] recreated MethodExitRequest is still the same", 248 qm.reconstructRequest(sur).equals(jpr)); 249 } 250 251 Field [] fArr = tstCls.getDeclaredFields(); 252 for (int j=0; j < fArr.length; j++) 253 { 254 f = fArr[j]; 255 256 jpr = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f); 257 sur = new JoinPointRequestSurrogate(jpr); 258 assertTrue("["+j+"] recreated FieldAccessRequest is still the same", 259 qm.reconstructRequest(sur).equals(jpr)); 260 261 jpr = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f); 262 sur = new JoinPointRequestSurrogate(jpr); 263 assertTrue("["+j+"] recreated FieldModificationRequest is still the same", 264 qm.reconstructRequest(sur).equals(jpr)); 265 } 266 267 Class [] cArr = new Class [] {Error .class, SystemStartupException.class, ArrayIndexOutOfBoundsException .class}; 268 for (int j=0; j < cArr.length; j++) 269 { 270 c = cArr[j]; 271 272 jpr = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,c); 273 sur = new JoinPointRequestSurrogate(jpr); 274 assertTrue("["+j+"] recreated ExceptionThrowRequest is still the same", 275 qm.reconstructRequest(sur).equals(jpr)); 276 277 } 278 } 279 catch (Exception e) 280 { 281 e.printStackTrace(); 282 } 283 } 284 285 286 public void test0050_equals() 287 { 288 Method m; Field f; Class c; Aspect ext = new MyExtension(); 289 Crosscut cr = new ThrowCut() 290 { 291 public void THROW_ARGS() { } 292 public Class [] potentialCrosscutClasses() 293 { Class [] result = { LinkageError .class, 294 IllegalStateException .class, 295 InstantiationError .class, 296 NullPointerException .class, 297 RuntimeException .class, 298 ClassFormatError .class, 299 UnknownError .class} 300 ; return result; } 301 protected PointCutter pointCutter() 302 { return ( (Exceptions.type("InstantiationError")) . OR (Exceptions.type("UnknownError")) );} 303 }; 304 305 JoinPointManager jpm = ProseSystem.getAspectManager().getJoinPointManager(); 306 try 307 { 308 Class c1 = MyTestClass.class; 309 Class c2 = MyTestClass.class; 310 ClassSurrogate s1 = new ClassSurrogate(c1); 311 ClassSurrogate s2 = new ClassSurrogate(c2); 312 assertEquals("ClassSurrogate equals", s1, s2); 313 314 MethodSurrogate sur1, sur2; 315 Class [] paramTypes; 316 Method [] mArr = tstCls.getDeclaredMethods(); 317 for (int j=0; j < mArr.length; j++) 318 { 319 m = mArr[j]; 320 paramTypes = m.getParameterTypes(); 321 sur1 = new MethodSurrogate(m); 322 sur2 = new MethodSurrogate(m); 323 assertEquals("MethodSurrogate equals", sur1, sur2); 324 } 325 326 FieldSurrogate fsur1, fsur2; 327 Class type; 328 Field [] fArr = tstCls.getDeclaredFields(); 329 for (int j=0; j < fArr.length; j++) 330 { 331 f = fArr[j]; 332 type = f.getType(); 333 fsur1 = new FieldSurrogate(f); 334 fsur2 = new FieldSurrogate(f); 335 assertEquals("FieldSurrogate equals", fsur1, fsur2); 336 } 337 338 JoinPointRequest jpr; 339 JoinPointRequestSurrogate jsur1, jsur2; 340 341 mArr = tstCls.getDeclaredMethods(); 342 for (int j=0; j < mArr.length; j++) 343 { 344 m = mArr[j]; 345 346 jpr = (MethodEntryRequest)jpm.createJoinPointRequest(MethodEntryJoinPoint.KIND,m); 347 jsur1 = new JoinPointRequestSurrogate(jpr); 348 jsur2 = new JoinPointRequestSurrogate(jpr); 349 assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2); 350 assertEquals("Tupel equals"+jsur1.getKind(), 351 new Tuple(new AspectSurrogate(ext), new CrosscutSurrogate(new AspectSurrogate(ext),cr), jsur1), 352 new Tuple(new AspectSurrogate(ext), new CrosscutSurrogate(new AspectSurrogate(ext),cr), jsur2)); 353 354 jpr = (MethodExitRequest)jpm.createJoinPointRequest(MethodExitJoinPoint.KIND,m); 355 jsur1 = new JoinPointRequestSurrogate(jpr); 356 jsur2 = new JoinPointRequestSurrogate(jpr); 357 assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2); 358 } 359 360 fArr = tstCls.getDeclaredFields(); 361 for (int j=0; j < fArr.length; j++) 362 { 363 f = fArr[j]; 364 365 jpr = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f); 366 jsur1 = new JoinPointRequestSurrogate(jpr); 367 jsur2 = new JoinPointRequestSurrogate(jpr); 368 assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2); 369 370 jpr = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f); 371 jsur1 = new JoinPointRequestSurrogate(jpr); 372 jsur2 = new JoinPointRequestSurrogate(jpr); 373 assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2); 374 } 375 376 Class [] cArr = new Class [] {Error .class, SystemStartupException.class, ArrayIndexOutOfBoundsException .class}; 377 for (int j=0; j < cArr.length; j++) 378 { 379 c = cArr[j]; 380 381 jpr = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,c); 382 jsur1 = new JoinPointRequestSurrogate(jpr); 383 jsur2 = new JoinPointRequestSurrogate(jpr); 384 assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2); 385 } 386 } 387 catch (Exception e) 388 { 389 e.printStackTrace(); 390 } 391 } 392 393 394 398 public static 399 Test suite() 400 { 401 return new TestSuite(SurrogateTest.class); 402 } 403 404 } 405 406 407
| Popular Tags
|