1 7 package ch.ethz.prose.crosscut; 8 9 import junit.framework.*; 11 import ch.ethz.prose.DefaultAspect; 12 import ch.ethz.prose.ProseSystem; 13 import ch.ethz.prose.engine.JoinPointManager; 14 import ch.ethz.prose.filter.Fields; 15 import ch.ethz.prose.filter.PointCutter; 16 17 23 public class AllFieldsTest extends TestCase { 24 25 27 static boolean accessed; 28 static boolean modified; 29 30 public static class ExampleClass 31 { 32 public int publicF = 1; 33 protected boolean protectedF = true; 34 double packageF = 0.5; 35 volatile private String privateF = "geheim"; 36 37 public String readPrivateF() { return privateF; } 38 }; 39 40 public static class UserDefined1 extends DefaultAspect 41 { 42 public Crosscut c = new SetCut() 43 { 44 public void SET_ARGS(ExampleClass cls, float x) 45 { modified = true;} 47 protected PointCutter pointCutter() 48 { 49 return null; 50 } 51 52 }; 53 public Crosscut c2 = new GetCut() 54 { 55 public void GET_ARGS(ExampleClass cls, float x) 56 { accessed = true; } 58 protected PointCutter pointCutter() 59 { 60 return null; 61 } 62 63 }; 64 } 65 66 public static class UserDefined2 extends DefaultAspect 67 { 68 public Crosscut c1 = new SetCut() 69 { 70 public void SET_ARGS(ExampleClass cls, int x) 71 { modified = true;} 72 73 protected PointCutter pointCutter() 74 { 75 return null; 76 } 77 78 }; 79 public Crosscut c2 = new GetCut() 80 { 81 public void GET_ARGS(ExampleClass cls, int x) 82 { accessed = true;} 83 84 protected PointCutter pointCutter() 85 { 86 return null; 87 } 88 }; 89 } 90 91 public static class UserDefined3 extends DefaultAspect 92 { 93 public Crosscut c1 = new SetCut() 94 { 95 public void SET_ARGS(AllFieldsTest cls, int x) 96 { modified = true;} 98 protected PointCutter pointCutter() 99 { 100 return null; 101 } 102 }; 103 104 public Crosscut c2 = new GetCut() 105 { 106 public void GET_ARGS(AllFieldsTest cls, int x) 107 { accessed = true;} 109 protected PointCutter pointCutter() 110 { 111 return null; 112 } 113 }; 114 } 115 116 public static class UserDefined4 extends DefaultAspect 117 { 118 public Crosscut c1 = new SetCut() 119 { 120 public void SET_ARGS(ANY cls, int x) 121 { modified = true;} 123 protected PointCutter pointCutter() 124 { 125 return (Fields.declaredInClass(ExampleClass.class)); 126 } 127 128 129 }; 130 131 public Crosscut c2 = new GetCut() 132 { 133 public void GET_ARGS(ANY cls, int x) 134 { accessed = true;} 136 protected PointCutter pointCutter() 137 { 138 return (Fields.declaredInClass(ExampleClass.class)); 139 } 140 }; 141 } 142 143 public static class UserDefined5 extends DefaultAspect 144 { 145 public Crosscut c1 = new SetCut() 146 { 147 public void SET_ARGS(ANY cls, ANY x) 148 { modified = true;} 150 protected PointCutter pointCutter() 151 { 152 return (Fields.declaredInClass(ExampleClass.class)); 153 } 154 155 }; 156 157 public Crosscut c2 = new GetCut() 158 { 159 public void GET_ARGS(ANY cls, ANY x) 160 { accessed = true;} 162 protected PointCutter pointCutter() 163 { 164 return (Fields.declaredInClass(ExampleClass.class)); 165 } 166 }; 167 } 168 169 public static class TestExtension extends DefaultAspect { 171 172 public Crosscut c = new SetCut() 173 { 174 public void SET_ARGS() 175 { 176 modified = true; 177 } 178 179 protected PointCutter pointCutter() 180 { 181 return null; 182 } 183 184 protected Class [] potentialCrosscutClasses() { 186 Class [] result = {ExampleClass.class}; 187 return result; 188 } 189 }; 190 191 public Crosscut c2 = new GetCut() 192 { 193 public void GET_ARGS() 194 { 195 accessed = true; 196 } 197 protected PointCutter pointCutter() 198 { 199 return null; 200 } 201 202 protected Class [] potentialCrosscutClasses() 203 { 204 Class [] result = {AllFieldsTest.ExampleClass.class}; 205 return result; 206 } 207 }; 208 } 209 210 213 ExampleClass ex; 214 SetCut testCrosscut = null; 215 JoinPointManager jpm; 216 217 221 public AllFieldsTest(String name) { 222 super(name); 223 } 224 225 228 protected void setUp() 229 { 230 try 231 { 232 ProseSystem.startup(); 233 } 234 catch ( Exception e ) 235 { 236 Assert.fail("ProseSystem.startup() failed."); 237 } 238 ex = new ExampleClass(); 239 } 240 241 protected void tearDown() 242 { 243 try 244 { ProseSystem.teardown(); } 245 catch (Exception e) 246 { Assert.fail("ProseSystem.teardown() failed"); } 247 } 248 249 protected void getSetPublic() { 250 ex.publicF = ex.publicF + 1; 251 } 252 253 protected void getSetPackage() { 254 ex.packageF = ex.packageF + 1.0; 255 } 256 257 258 public void testUserDefined1() throws Exception 259 { 260 ProseSystem.getAspectManager().insert(new UserDefined1()); 261 accessed = false; modified = false; 262 getSetPublic(); 263 assertTrue("UserDefined1: !modified:",!modified); 264 assertTrue("UserDefined1: !accessed:",!accessed); 265 } 266 267 public void testUserDefined2() throws Exception 268 { 269 UserDefined2 aspect = new UserDefined2(); 270 ProseSystem.getAspectManager().insert(aspect); 271 272 accessed = false; modified = false; 273 getSetPublic(); 274 assertTrue("UserDefined2.publicF: modified:",modified); 275 assertTrue("UserDefined2.publicF: accessed:",accessed); 276 277 accessed = false; modified = false; 278 getSetPackage(); 279 assertTrue("UserDefined2.packageF: !modified:",!modified); 280 assertTrue("UserDefined2.packageF: !accessed:",!accessed); 281 } 282 283 public void testUserDefined3() throws Exception 284 { 285 UserDefined3 aspect = new UserDefined3(); 286 ProseSystem.getAspectManager().insert(aspect); 287 288 accessed = false; modified = false; 289 getSetPublic(); 290 assertTrue("UserDefined3.publicF: !modified:",!modified); 291 assertTrue("UserDefined3.publicF: !accessed:",!accessed); 292 } 293 294 public void testUserDefined4() throws Exception 295 { 296 UserDefined4 aspect = new UserDefined4(); 297 ProseSystem.getAspectManager().insert(aspect); 298 299 accessed = false; modified = false; 300 getSetPublic(); 301 assertTrue("UserDefined4.publicF: modified:",modified); 302 assertTrue("UserDefined4.publicF: accessed:",accessed); 303 304 accessed = false; modified = false; 305 getSetPackage(); 306 assertTrue("UserDefined4.packageF: !modified:",!modified); 307 assertTrue("UserDefined4.packageF: !accessed:",!accessed); 308 } 309 310 public void testUserDefined5() throws Exception 311 { 312 UserDefined5 aspect = new UserDefined5(); 313 ProseSystem.getAspectManager().insert(aspect); 314 315 accessed = false; modified = false; 316 getSetPublic(); 317 assertTrue("UserDefined5.publicF: modified:",modified); 318 assertTrue("UserDefined5.publicF: accessed:",accessed); 319 320 accessed = false; modified = false; 321 getSetPackage(); 322 assertTrue("UserDefined5.packageF: modified:",modified); 323 assertTrue("UserDefined5.packageF: accessed:",accessed); 324 } 325 326 327 public void testExtension() throws Exception { 328 329 try 330 { 331 ProseSystem.getAspectManager().insert(new TestExtension()); 332 runTestExtension(); 333 } 334 catch (RuntimeException e) 335 { 336 e.printStackTrace(); 337 throw e; 338 } 339 } 340 341 protected void runTestExtension() { 342 accessed = false; 344 modified = false; 345 assertTrue(ex.publicF == 1); 346 assertTrue("1", accessed); 347 assertTrue("2", !modified); 348 349 accessed = false; 351 modified = false; 352 assertTrue(ex.protectedF == true); 353 assertTrue("3", accessed); 354 assertTrue("4", !modified); 355 356 accessed = false; 358 modified = false; 359 assertTrue(ex.packageF == 0.5); 360 assertTrue("5", accessed); 361 assertTrue("6", !modified); 362 363 accessed = false; 365 modified = false; 366 ex.publicF = 3; 367 assertTrue("9", !accessed); 368 assertTrue("10", modified); 369 assertTrue(ex.publicF == 3); 370 371 accessed = false; 373 modified = false; 374 ex.protectedF = false; 375 assertTrue("11", !accessed); 376 assertTrue("12", modified); 377 assertTrue(ex.protectedF == false); 378 379 accessed = false; 381 modified = false; 382 ex.packageF = 9.9; 383 assertTrue("13", !accessed); 384 assertTrue("14", modified); 385 assertTrue(ex.packageF == 9.9); 386 387 accessed = false; 389 modified = false; 390 ex.privateF = "foo"; 391 assertTrue("15", !accessed); 392 assertTrue("16", modified); 393 assertTrue(ex.privateF == "foo"); 394 395 accessed = false; 397 modified = false; 398 ex.publicF++; 399 assertTrue("17", accessed); 400 assertTrue("18", modified); 401 assertTrue(ex.publicF++ == 4); 402 403 accessed = false; 405 modified = false; 406 Object x = ex.privateF; 407 assertTrue("8", !modified); 408 assertTrue("7", accessed); 409 } 410 411 415 public static Test suite() { 416 return new TestSuite(AllFieldsTest.class); 417 } 418 419 } 420 421 422
| Popular Tags
|