1 7 package ch.ethz.prose.filter; 8 9 import junit.framework.*; 11 import ch.ethz.inf.iks.jvmai.jvmdi.ItemManipulationException; 12 import ch.ethz.jvmai.*; 13 import ch.ethz.prose.ProseSystem; 14 import ch.ethz.prose.SystemStartupException; 15 import ch.ethz.prose.engine.*; 16 17 23 public 24 class ExceptionSTest extends TestCase { 25 26 static class MyJoinPointListener extends JoinPointListener 28 { 29 public MyJoinPointListener() {} 30 31 public void joinPointReached(MethodEntryJoinPoint jp) 32 { } 33 34 public void joinPointReached(MethodExitJoinPoint jp) 35 { } 36 37 public void joinPointReached(FieldAccessJoinPoint jp) 38 { } 39 40 public void joinPointReached(FieldModificationJoinPoint jp) 41 { } 42 43 public void joinPointReached(ExceptionJoinPoint jp) 44 { 45 exceptionSTest._testInClassThrown(jp); 46 exceptionSTest._testInMethodThrown(jp); 47 exceptionSTest._testMessageNamed(jp); 48 } 49 public void joinPointReached(ExceptionCatchJoinPoint jp) 50 { 51 } 53 } 54 55 static class Example1 56 { 57 public void method1() throws Exception { throw new Exception (); } 58 public void method2() { foo(); } 59 private void foo(){ bar();} 60 private void bar(){ carz();} 61 protected void carz(){ int a=0; a++;baz();} 62 private void baz(){ throw new RuntimeException ();} 63 } 64 65 static class Beispiel2 66 { 67 public void function1() { throw new JVMAIRuntimeException(); } 68 public void method2() { throw new WatchNotSetException("abc def"); } 69 } 70 71 static class ExampleClass3 72 { 73 public void function1() { throw new IllegalThreadStateException ("def ghi"); } 74 public void function3() { throw new NumberFormatException ("ghi jkl"); } 75 } 76 77 78 ExceptionThrowRequest Request1; 79 ExceptionThrowRequest Request2; 80 ExceptionThrowRequest Request3; 81 ExceptionThrowRequest Request4; 82 ExceptionThrowRequest Request5; 83 ExceptionThrowRequest Request6; 84 ExceptionThrowRequest Request7; 85 JoinPointManager jpm; 86 87 JoinPointListener jpl; 88 static ExceptionSTest exceptionSTest; 89 PointCutter spec; 90 Example1 ex1; 91 Beispiel2 bsp2; 92 ExampleClass3 exCls3; 93 static int[] counter = {0,0,0,0,0,0,0,0,0,0,0,0}; 94 static int exceptionCounter = 0; 95 96 100 public ExceptionSTest(String name) 101 { 102 super(name); 103 exceptionSTest = this; 104 } 105 106 109 protected 110 void setUp() 111 { 112 try 113 { 114 ProseSystem.startup(); 115 } 116 catch ( SystemStartupException e ) 117 { 118 Assert.fail("ExtensionSystemSetupException"); 119 } 120 121 jpm = ProseSystem.getAspectManager().getJoinPointManager(); 122 123 jpl = new MyJoinPointListener(); 124 125 Request1 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,Exception .class); 127 Request2 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,RuntimeException .class); 128 Request3 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ThreadDeath .class); 129 Request4 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ItemManipulationException.class); 130 Request5 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,NoClassDefFoundError .class); 131 Request6 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ClassCastException .class); 132 Request7 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,NumberFormatException .class); 133 134 135 ex1 = new Example1(); 137 bsp2 = new Beispiel2(); 138 exCls3 = new ExampleClass3(); 139 140 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,Exception .class)); 141 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,RuntimeException .class)); 142 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,JVMAIRuntimeException.class)); 143 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,WatchNotSetException.class)); 144 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,IllegalThreadStateException .class)); 145 ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,NumberFormatException .class)); 146 } 147 148 149 protected void tearDown() throws Exception 150 { 151 152 ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl); 153 ProseSystem.teardown(); 154 } 155 156 157 public void test010_FilterRequests() 158 { 159 PointFilter MySpec1 = 160 Exceptions.type(".*Exception") .AND( Exceptions.subtypeOf(java.lang.RuntimeException .class) ); 161 166 assertTrue("1.1", ! MySpec1.isSpecialRequest(Request1)); 167 assertTrue("1.2", MySpec1.isSpecialRequest(Request2)); 170 assertTrue("1.3", ! MySpec1.isSpecialRequest(Request3)); 173 assertTrue("1.4", MySpec1.isSpecialRequest(Request4)); 176 assertTrue("1.5", ! MySpec1.isSpecialRequest(Request5)); 179 assertTrue("1.6", MySpec1.isSpecialRequest(Request6)); 182 assertTrue("1.7", MySpec1.isSpecialRequest(Request7)); 185 } 188 189 public void test020_FilterEvents() throws Exception 190 { 191 194 try { ex1.method1(); } catch (Throwable e) { } 195 try { ex1.method2(); } catch (Throwable e) { } 196 try { bsp2.function1(); } catch (Throwable e) { } 197 try { bsp2.method2(); } catch (Throwable e) { } 198 try { exCls3.function1(); } catch (Throwable e) { } 199 try 200 { 201 exCls3.function3(); 202 } 203 catch (Throwable e) 204 { 205 206 } 207 208 assertEquals("0:Thrown in class Example ", 4,counter[0]); 209 assertEquals("1:Thrown in class JoinPoint ", 0,counter[1]); 210 assertEquals("2:Thrown in (2|3)", 4,counter[2]); 211 assertEquals("3:Thrown in class NULL", 0,counter[3]); 212 213 assertEquals("4:Thrown in (function|method) ", 5,counter[4]); 214 assertEquals("5", 3,counter[5]); 215 assertEquals("6", 6,counter[6]); 216 assertTrue("7", counter[7]==0); 217 218 assertTrue("Example", counter[0]==4); 219 assertTrue("JoinPoint", counter[1]==0); 220 assertTrue("2|3", counter[2]==4); 221 assertTrue("[null(1)]", counter[3]==0); 222 223 assertEquals("function|method", 5,counter[4]); 224 assertTrue("1", counter[5]==3); 225 assertTrue("[empty(1)]", counter[6]==6); 226 assertTrue("[null(2)]", counter[7]==0); 227 228 assertTrue("abc", counter[8]==1); 229 assertTrue("def", counter[9]==2); 230 assertTrue("[empty(2)]", counter[10]==3); 231 assertTrue("[null(3)]", counter[11]==3); 232 233 assertTrue("NullPointerExceptions", exceptionCounter==12); } 235 236 public void _testInClassThrown(JoinPoint jpe) 237 { 238 try 239 { 240 spec = Within.type("Example"); 241 if (spec.isSpecialEvent(jpe)) 242 counter[0]++; 243 244 spec = Within.type("JoinPoint"); 245 if (spec.isSpecialEvent(jpe)) 246 counter[1]++; 247 248 spec = Within.type("2|3"); 249 if (spec.isSpecialEvent(jpe)) 250 counter[2]++; 251 252 spec = Within.type((String )null); 253 if (spec.isSpecialEvent(jpe)) 254 counter[3]++; 255 } 256 catch (NullPointerException e) 257 { exceptionCounter++; } 258 } 259 260 public void _testInMethodThrown(JoinPoint jpe) 261 { 262 try 263 { 264 spec = Within.method("function|method"); 265 if (spec.isSpecialEvent(jpe)) 266 counter[4]++; 267 268 spec = Within.method("1"); 269 if (spec.isSpecialEvent(jpe)) 270 counter[5]++; 271 272 273 spec = Within.method(""); 274 if (spec.isSpecialEvent(jpe)) 275 { 276 counter[6]++; 277 } 278 279 spec = Within.method(null); 285 if (spec.isSpecialEvent(jpe)) 286 counter[7]++; 287 } 288 catch (NullPointerException e) 289 { exceptionCounter++; } 290 } 291 292 public void _testMessageNamed(JoinPoint jpe) 293 { 294 spec = Exceptions.withMessage("abc"); 295 if (spec.isSpecialEvent(jpe)) 296 counter[8]++; 297 298 spec = Exceptions.withMessage("def"); 299 300 if (spec.isSpecialEvent(jpe)) 301 counter[9]++; 302 303 spec = Exceptions.withMessage(""); 304 305 if (spec.isSpecialEvent(jpe)) 306 counter[10]++; 307 308 spec = Exceptions.withMessage(null); 309 310 if (spec.isSpecialEvent(jpe)) 311 counter[11]++; 312 } 313 314 318 public static 319 Test suite() 320 { 321 return new TestSuite(ExceptionSTest.class); 322 } 323 324 } 325 326 327 | Popular Tags |