1 package ch.ethz.prose.crosscut; 2 3 import junit.framework.*; 4 import ch.ethz.prose.DefaultAspect; 5 import ch.ethz.prose.ProseSystem; 6 import ch.ethz.prose.filter.Exceptions; 7 import ch.ethz.prose.filter.PointCutter; 8 9 10 16 public class ExceptionCatchCutTest extends TestCase { 17 18 ExceptionCatchCutTest excCatchCutTest = null; 19 ExampleCatch1 excCatch1; 20 ExampleCatch2 excCatch2; 21 ExampleCatch3 excCatch3; 22 int catchCounter = 0; 23 24 25 public class TestException extends Exception { 26 public TestException() {super(); } 27 public TestException(String s) {super(s);} 28 } 29 30 public class TestException2 extends Exception { 31 public TestException2() {super(); } 32 public TestException2(String s) {super(s);} 33 } 34 35 public class TestException3 extends Exception { 36 public TestException3() {super(); } 37 public TestException3(String s) {super(s);} 38 } 39 40 public class TestException4 extends RuntimeException { } 41 public class TestException5 extends RuntimeException { } 42 public class TestException6 extends RuntimeException { } 43 44 public class ExampleCatch1 45 { 46 public void throwMethod1() { try{ throw new TestException(); } catch(TestException e) { } } 47 public void throwMethod2() { try{ throw new TestException2(); } catch(TestException2 e) { } } 48 } 49 50 public class ExampleCatch2 51 { 52 public void throwMethod3(){ try{ throw new TestException3(); } catch(TestException3 e) { } } 53 public void throwMethod4(){ try{ throw new TestException4(); } catch(TestException4 e) { } } 54 public void throwMethod42(){ try{ throw new TestException(); } catch(TestException e) { } } 55 } 56 57 public class ExampleCatch3 58 { 59 public void throwMethod5(){ try{ throw new TestException2("test31 test32"); } catch(TestException2 e) { } } 60 public void throwMethod6(){ try{ throw new TestException("test41 test42"); } catch(TestException e) { } } 61 public void throwMethod7() { } 62 } 63 64 public class TestExtension1 extends DefaultAspect 65 { 66 public Crosscut c1 = new CatchCut() 67 { 68 public void CATCH_ARGS() 69 { 70 catchCounter++; 71 } 73 74 protected PointCutter pointCutter() 75 { 76 return ( Exceptions.type(".*TestException") ); } 88 }; 89 90 public Crosscut c11 = new CatchCut() 92 { 93 public void CATCH_ARGS() 94 { 95 catchCounter++; 96 } 98 99 protected PointCutter pointCutter() 100 { 101 return ( Exceptions.subtypeOf(java.lang.RuntimeException .class ) ); } 107 }; 108 } 109 110 public class TestExtension2 extends DefaultAspect 111 { 112 public Crosscut c2 = new CatchCut() 113 { 114 public void CATCH_ARGS() 115 { 116 catchCounter++; 117 System.out.println("CATCH_ARGS2 -> catchCounter = " + catchCounter); 118 } 119 120 protected PointCutter pointCutter() 121 { 122 return ( Exceptions.subtypeOf(java.lang.RuntimeException .class ) ); } 128 }; 129 } 130 131 132 public class TestExtension3 extends DefaultAspect 133 { 134 public Crosscut c3 = new ThrowCut() 135 { 136 public void THROW_ARGS() 137 { 138 catchCounter++; 139 } 141 142 protected PointCutter pointCutter() 143 { 144 return ( Exceptions.type("Exception") ); } 149 }; 150 } 151 152 153 157 public ExceptionCatchCutTest (String name) 158 { 159 super(name); 160 } 161 162 165 protected void setUp() 166 { 167 try{ 168 ProseSystem.startup(); 169 } 170 catch (Exception e) 171 { 172 e.printStackTrace(); 173 tearDown(); 174 Assert.fail("ProseSystem.startup() failed"); 175 } 176 177 excCatch1 = new ExampleCatch1(); 178 excCatch2 = new ExampleCatch2(); 179 excCatch3 = new ExampleCatch3(); 180 } 181 182 protected void tearDown() 183 { 184 try { 185 ProseSystem.teardown(); 187 } 188 catch (Exception e) { 189 Assert.fail("ProseSystem.teardown() failed"); 190 } 191 } 192 193 194 public void test0001_createRequestsAndEvents() 195 { 196 Class toload; 198 toload = TestException.class; 199 toload = TestException2.class; 200 toload = TestException3.class; 201 toload = TestException4.class; 202 toload = TestException5.class; 203 toload = TestException6.class; 204 205 TestExtension1 myExtension1 = new TestExtension1(); 206 ProseSystem.getAspectManager().insert(myExtension1); 207 209 211 215 217 excCatch1.throwMethod1(); 218 excCatch1.throwMethod2(); 219 excCatch2.throwMethod3(); 220 excCatch2.throwMethod4(); 221 excCatch2.throwMethod42(); 222 excCatch3.throwMethod5(); 223 excCatch3.throwMethod6(); 224 excCatch3.throwMethod7(); 225 226 228 assertEquals("6 ExceptionCatchEvents reported ", 8,catchCounter); 229 } 230 231 235 public static Test suite() 236 { 237 return new TestSuite(ExceptionCatchCutTest.class); 238 } 239 } 240 | Popular Tags |