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 23 public class TargetSTest extends TestCase { 24 25 27 public static class CreateEvents extends JoinPointListener { 29 30 public CreateEvents() {} 31 32 public void joinPointReached(MethodEntryJoinPoint jp) 33 { 34 thisTest._testEqual(jp); 35 thisTest._testSame(jp); 36 thisTest._testInClass(jp); 37 thisTest._testInstanceOf(jp); 38 } 39 40 public void joinPointReached(MethodExitJoinPoint jp) 41 { 42 thisTest._testEqual(jp); 43 thisTest._testSame(jp); 44 thisTest._testInClass(jp); 45 thisTest._testInstanceOf(jp); 46 } 47 48 public void joinPointReached(FieldAccessJoinPoint jp) 49 { 50 thisTest._testEqual(jp); 51 thisTest._testSame(jp); 52 thisTest._testInClass(jp); 53 thisTest._testInstanceOf(jp); 54 } 55 56 public void joinPointReached(FieldModificationJoinPoint jp) 57 { 58 thisTest._testEqual(jp); 59 thisTest._testSame(jp); 60 thisTest._testInClass(jp); 61 thisTest._testInstanceOf(jp); 62 } 63 64 public void joinPointReached(ExceptionJoinPoint jp) 65 { 66 thisTest._testEqual(jp); 67 thisTest._testSame(jp); 68 thisTest._testInClass(jp); 69 thisTest._testInstanceOf(jp); 70 } 71 72 public void joinPointReached(ExceptionCatchJoinPoint jp) 73 { 74 thisTest._testEqual(jp); 75 thisTest._testSame(jp); 76 thisTest._testInClass(jp); 77 thisTest._testInstanceOf(jp); 78 } 79 80 } 81 82 static class TestClass 84 { 85 public int i; 86 87 public TestClass() 88 { 89 } 90 91 public void dummyAccess() 92 { 93 int b = i; 94 } 95 96 public void dummySet() 97 { 98 i = 1; 99 } 100 } 101 102 static class TestSubClass extends TestClass implements java.io.Serializable 103 { 104 } 105 106 107 TestClass testobj; 108 PointFilter spec; 109 JoinPointListener jpl; 110 FieldAccessRequest accessReq; 111 FieldModificationRequest modifyReq; 112 113 static TargetSTest thisTest; 115 static JoinPointManager jpm ; 116 117 118 122 public TargetSTest(String name) { 123 super(name); 124 thisTest = this; 125 } 126 127 128 131 protected void setUp() { 132 Field f = null; 133 jpl = new CreateEvents(); 134 135 testobj = new TestSubClass(); 136 try 139 { 140 ProseSystem.startup(); 141 } 142 catch ( SystemStartupException e ) 143 { 144 Assert.fail("ExtensionSystemSetupException"); 145 } 146 147 jpm = ProseSystem.getAspectManager().getJoinPointManager(); 148 149 150 try 151 { 152 f = TestClass.class.getDeclaredField("i"); 153 } 154 catch ( Exception e ) 155 { 156 Assert.fail("Error getting field."); 157 } 158 159 accessReq = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f); 160 modifyReq = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f); 161 } 162 163 164 protected void tearDown() 165 { 166 try 167 { ProseSystem.teardown(); } 168 catch (SystemTeardownException tde) 169 { Assert.fail("Aspect.teardown() failed "); } 170 } 171 172 176 public void testObjectSpecializers() 177 { 178 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, accessReq); 180 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, modifyReq); 181 ProseSystem.getAspectManager().getJoinPointManager().resumeListenerNotification(Thread.currentThread()); 182 183 testobj.dummySet(); 185 186 testobj.dummyAccess(); 188 189 ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl); 190 } 191 192 193 196 void _testEqual(JoinPoint ev) { 197 Exception e = null; 198 spec = Target.equalsTo(testobj); 199 assertTrue("1", spec.isSpecialEvent(ev)); 200 201 spec = Target.equalsTo(new TestClass()); 202 assertTrue("2", ! spec.isSpecialEvent(ev)); 203 204 try 205 { 206 spec = Target.equalsTo(null); 207 Assert.fail("Setting the CFlow Receiver to null should throw a " + 208 "NullPointerException!"); 209 } 210 catch (IllegalArgumentException npe ) 211 { 212 e = npe; 213 } 214 assertNotNull("Setting the CFlow Receiver to null should have thrown a " + 215 "NullPointerException!", e); 216 } 217 218 221 void _testSame(JoinPoint ev) { 222 Exception e = null; 223 spec = Target.isSameObject(testobj); 224 assertTrue("1", spec.isSpecialEvent(ev)); 225 226 spec = Target.isSameObject(new TestClass()); 227 assertTrue("2", ! spec.isSpecialEvent(ev)); 228 229 try 230 { 231 spec = Target.isSameObject(null); 232 Assert.fail("Setting the CFlow Receiver to null should throw a " + 233 "NullPointerException!"); 234 } 235 catch ( IllegalArgumentException npe ) 236 { 237 e = npe; 238 } 239 assertNotNull("Setting the CFlow Receiver to null should have thrown a " + 240 "NullPointerException!", e); 241 } 242 243 244 247 void _testInstanceOf(JoinPoint ev) 248 { 249 spec = Target.subtypeOf(java.io.Serializable .class); 250 assertTrue("3", spec.isSpecialEvent(ev)); 251 252 spec = Target.subtypeOf(java.lang.String .class); 253 assertTrue("4", ! spec.isSpecialEvent(ev)); 254 } 255 256 void _testInClass(JoinPoint ev) 257 { 258 spec = Target.type(".*Sub.*"); 259 assertTrue("5", spec.isSpecialEvent(ev)); 260 261 spec = Target.type("TestClass"); 262 assertTrue("6", !spec.isSpecialEvent(ev)); 263 } 264 265 266 270 public static Test suite() { 271 return new TestSuite(TargetSTest.class); 272 } 273 } 274 275 276 | Popular Tags |