KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > filter > ExceptionCatchSTest


1 package ch.ethz.prose.filter;
2
3 // used packages
4
import junit.framework.*;
5 import ch.ethz.inf.iks.jvmai.jvmdi.ItemManipulationException;
6 import ch.ethz.jvmai.*;
7 import ch.ethz.prose.ProseSystem;
8 import ch.ethz.prose.SystemStartupException;
9 import ch.ethz.prose.engine.*;
10
11 /**
12  * JUnit testcase for class ExceptionCatchSTest.
13  *
14  * @version $Revision: 1.3 $
15  * @author Angela Nicoara
16  */

17 public
18 class ExceptionCatchSTest extends TestCase {
19
20   static class MyJoinPointListener extends JoinPointListener
21   {
22     public MyJoinPointListener() { }
23
24     public void joinPointReached(MethodEntryJoinPoint jp) { }
25     public void joinPointReached(MethodExitJoinPoint jp) { }
26     public void joinPointReached(FieldAccessJoinPoint jp) { }
27     public void joinPointReached(FieldModificationJoinPoint jp) { }
28     public void joinPointReached(ExceptionJoinPoint jp) { }
29     public void joinPointReached(ExceptionCatchJoinPoint jp)
30     {
31       excCatchSTest.testCatchInClass(jp);
32       excCatchSTest.testCatchInMethod(jp);
33       excCatchSTest.testCatchMessageNamed(jp);
34     }
35   }
36
37   static class TestException extends Exception JavaDoc {
38     public TestException() {super(); }
39     public TestException(String JavaDoc s) {super(s);}
40   }
41
42   static class TestException2 extends Exception JavaDoc {
43     public TestException2() {super(); }
44     public TestException2(String JavaDoc s) {super(s);}
45   }
46
47   static class TestException3 extends Exception JavaDoc {
48     public TestException3() {super(); }
49     public TestException3(String JavaDoc s) {super(s);}
50   }
51
52   static class TestException4 extends RuntimeException JavaDoc { }
53   static class TestException5 extends RuntimeException JavaDoc { }
54   static class TestException6 extends RuntimeException JavaDoc { }
55
56   static class ExampleCatch1
57   {
58     public void throwMethod1() { try{ throw new TestException(); } catch(TestException e) { /**System.out.println("ExceptionCatchSTest1 \n"); */ } }
59     public void throwMethod2() { try{ throw new TestException2(); } catch(TestException2 e) { /**System.out.println("ExceptionCatchSTest2 \n"); */} }
60   }
61
62   static class ExampleCatch2
63   {
64     public void throwMethod3(){ try{ throw new TestException3(); } catch(TestException3 e) { /**System.out.println("ExceptionCatchSTest3 \n"); */} }
65     public void throwMethod4(){ try{ throw new TestException4(); } catch(TestException4 e) { /**System.out.println("ExceptionCatchSTest4 \n"); */} }
66   }
67
68   static class ExampleCatch3
69   {
70     public void throwMethod5(){ try{ throw new TestException2("test31 test32"); } catch(TestException2 e) { /**System.out.println("ExceptionCatchSTest5 \n"); */} }
71     public void throwMethod6(){ try{ throw new TestException3("test41 test42"); } catch(TestException3 e) { /**System.out.println("ExceptionCatchSTest6 \n"); */} }
72     public void throwMethod7() { }
73   }
74
75   ExceptionCatchRequest reqCatch1;
76   ExceptionCatchRequest reqCatch2;
77   ExceptionCatchRequest reqCatch3;
78   ExceptionCatchRequest reqCatch4;
79   ExceptionCatchRequest reqCatch5;
80   ExceptionCatchRequest reqCatch6;
81   ExceptionCatchRequest reqCatch7;
82
83   JoinPointManager jpmCatch;
84   JoinPointListener jplCatch;
85   PointCutter specCatch;
86   static ExceptionCatchSTest excCatchSTest;
87   static int[] counterCatch = {0,0,0,0,0,0,0,0,0,0,0,0};
88   static int excCounterCatch = 0;
89
90   ExampleCatch1 excCatch1;
91   ExampleCatch2 excCatch2;
92   ExampleCatch3 excCatch3;
93
94
95   /**
96    * Construct test with given name.
97    * @param name test name
98    */

99   public ExceptionCatchSTest (String JavaDoc name)
100   {
101     super(name);
102     excCatchSTest = this;
103   }
104
105
106   /**
107    * Set up fixture.
108    */

109   protected void setUp()
110   {
111     try { ProseSystem.startup(); } catch ( SystemStartupException e ) { Assert.fail("ExtensionSystemSetupException"); }
112
113     jpmCatch = ProseSystem.getAspectManager().getJoinPointManager();
114     jplCatch = new MyJoinPointListener();
115
116     // for the static testing part
117
reqCatch1 = (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,Exception JavaDoc.class);
118     reqCatch2 = (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,RuntimeException JavaDoc.class);
119     reqCatch3 = (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,ThreadDeath JavaDoc.class);
120     reqCatch4 = (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,ItemManipulationException.class);
121     reqCatch5 = (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,NoClassDefFoundError JavaDoc.class);
122     reqCatch6 = (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,ClassCastException JavaDoc.class);
123     reqCatch7 = (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,NumberFormatException JavaDoc.class);
124
125     // for the dynamic testing part
126
excCatch1 = new ExampleCatch1();
127     excCatch2 = new ExampleCatch2();
128     excCatch3 = new ExampleCatch3();
129 /**
130     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,Exception.class));
131     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,RuntimeException.class));
132     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,JVMAIRuntimeException.class));
133     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,WatchNotSetException.class));
134     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,IllegalThreadStateException.class));
135     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,NumberFormatException.class));
136 */

137     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,TestException.class));
138     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,TestException2.class));
139     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,TestException3.class));
140     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,TestException4.class));
141     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,TestException5.class));
142     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jplCatch, (ExceptionCatchRequest)jpmCatch.createJoinPointRequest(ExceptionCatchJoinPoint.KIND,TestException6.class));
143   }
144
145   protected void tearDown() throws Exception JavaDoc
146   {
147     ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jplCatch);
148     ProseSystem.teardown();
149   }
150
151   public void test001_FilterRequests()
152   {
153       PointFilter MySpec0 = Exceptions.type(".*Exception") .AND ( Exceptions.subtypeOf(java.lang.RuntimeException JavaDoc.class) );
154       PointFilter MySpec1 = Exceptions.type(".*Exception") .OR ( Exceptions.subtypeOf(java.lang.RuntimeException JavaDoc.class) );
155       PointFilter MySpec2 = Exceptions.type(".*Exception") .OR ( Exceptions.supertypeOf(java.lang.IllegalArgumentException JavaDoc.class) );
156       PointFilter MySpec3 = Exceptions.type(".*Exception") .AND ( Exceptions.supertypeOf(java.lang.IllegalArgumentException JavaDoc.class) );
157
158       assertTrue("0.1", ! MySpec0.isSpecialRequest(reqCatch1));
159       assertTrue("1.1", MySpec1.isSpecialRequest(reqCatch1));
160       assertTrue("2.1", MySpec2.isSpecialRequest(reqCatch1));
161       assertTrue("3.1", MySpec3.isSpecialRequest(reqCatch1));
162       
163       assertTrue("0.2", MySpec0.isSpecialRequest(reqCatch2));
164       assertTrue("1.2", MySpec1.isSpecialRequest(reqCatch2));
165       assertTrue("2.2", MySpec2.isSpecialRequest(reqCatch2));
166       assertTrue("3.2", MySpec3.isSpecialRequest(reqCatch2));
167
168       assertTrue("0.3", ! MySpec0.isSpecialRequest(reqCatch3));
169       assertTrue("1.3", ! MySpec1.isSpecialRequest(reqCatch3));
170       assertTrue("2.3", ! MySpec2.isSpecialRequest(reqCatch3));
171       assertTrue("3.3", ! MySpec3.isSpecialRequest(reqCatch3));
172
173       assertTrue("0.4", MySpec0.isSpecialRequest(reqCatch4));
174       assertTrue("1.4", MySpec1.isSpecialRequest(reqCatch4));
175       assertTrue("2.4", MySpec2.isSpecialRequest(reqCatch4));
176       assertTrue("3.4", ! MySpec3.isSpecialRequest(reqCatch4));
177
178       assertTrue("0.5", ! MySpec0.isSpecialRequest(reqCatch5));
179       assertTrue("1.5", ! MySpec1.isSpecialRequest(reqCatch5));
180       assertTrue("2.5", ! MySpec2.isSpecialRequest(reqCatch5));
181       assertTrue("3.5", ! MySpec3.isSpecialRequest(reqCatch5));
182
183       assertTrue("0.6", MySpec0.isSpecialRequest(reqCatch6));
184       assertTrue("1.6", MySpec1.isSpecialRequest(reqCatch6));
185       assertTrue("2.6", MySpec2.isSpecialRequest(reqCatch6));
186       assertTrue("3.6", ! MySpec3.isSpecialRequest(reqCatch6));
187
188       assertTrue("0.7", MySpec0.isSpecialRequest(reqCatch7));
189       assertTrue("1.7", MySpec1.isSpecialRequest(reqCatch7));
190       assertTrue("2.7", MySpec2.isSpecialRequest(reqCatch7));
191       assertTrue("3.7", ! MySpec3.isSpecialRequest(reqCatch7));
192   }
193
194   public void test002_FilterEvents()
195   {
196       // seven different methods which each throw an exception are executed and afterwards if is
197
// looked up if the catch specializer passes the event
198

199       excCatch1.throwMethod1();
200       excCatch1.throwMethod2();
201       excCatch2.throwMethod3();
202       excCatch2.throwMethod4();
203       excCatch3.throwMethod5();
204       excCatch3.throwMethod6();
205
206
207       assertEquals("0:Catch in class Example ", 6,counterCatch[0]);
208       assertEquals("1:Catch in class JoinPoint ", 0,counterCatch[1]);
209       assertEquals("2:Catch in (2|3)", 4,counterCatch[2]);
210       assertEquals("3:Catch in class NULL", 0,counterCatch[3]);
211
212       assertEquals("4:Catch in (Method1|Method3) ", 2,counterCatch[4]);
213       assertEquals("5:Catch in 1", 1,counterCatch[5]);
214       assertEquals("6:Catch in empty", 6,counterCatch[6]);
215       assertTrue("7", counterCatch[7]==0);
216
217       assertTrue("Example", counterCatch[0]==6);
218       assertTrue("JoinPoint", counterCatch[1]==0);
219       assertTrue("2|3", counterCatch[2]==4);
220       assertTrue("[null(1)]", counterCatch[3]==0);
221
222       assertEquals("function|method", 2,counterCatch[4]);
223       assertTrue("1", counterCatch[5]==1);
224       assertTrue("[empty(1)]", counterCatch[6]==6);
225       assertTrue("[null(2)]", counterCatch[7]==0);
226
227       assertTrue("31", counterCatch[8]==1);
228       assertTrue("2", counterCatch[9]==2);
229       assertTrue("[empty(2)]", counterCatch[10]==2);
230       assertTrue("[null(3)]", counterCatch[11]==4);
231
232       assertTrue("NullPointerExceptions", excCounterCatch==12); // how many NullPointerException are thrown (== 2*6)
233
}
234
235
236   public void testCatchInClass(JoinPoint jpeCatch)
237   {
238     try
239     {
240       specCatch = Within.type("Example");
241       if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[0]++;
242
243       specCatch = Within.type("JoinPoint");
244       if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[1]++;
245
246       specCatch = Within.type("2|3");
247       if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[2]++;
248
249       specCatch = Within.type((String JavaDoc)null);
250       if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[3]++;
251     }
252     catch (NullPointerException JavaDoc e) { excCounterCatch++; }
253   }
254
255
256   public void testCatchInMethod(JoinPoint jpeCatch)
257   {
258     try
259     {
260         specCatch = Within.method("Method1|Method3");
261         if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[4]++;
262     
263         specCatch = Within.method("1");
264         if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[5]++;
265     
266         specCatch = Within.method("");
267         if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[6]++;
268     
269         //FIXME: here we have an assertion failure & and
270
// a PROSE error. Withing this method, a null pointer
271
// exception is thrown and cought. Since we are in the jvmaiHook,
272
// this leads to strage behavior: for all method exits, the
273
// same exception is reported.
274
specCatch = Within.method(null);
275         if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[7]++;
276     }
277     catch (NullPointerException JavaDoc e) { excCounterCatch++; }
278   }
279
280
281   public void testCatchMessageNamed(JoinPoint jpeCatch)
282   {
283        specCatch = Exceptions.withMessage("31");
284        if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[8]++;
285
286        specCatch = Exceptions.withMessage("2");
287        if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[9]++;
288
289        specCatch = Exceptions.withMessage("");
290        if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[10]++;
291
292        specCatch = Exceptions.withMessage(null);
293        if (specCatch.isSpecialEvent(jpeCatch)) counterCatch[11]++;
294   }
295
296
297   /**
298    * Test suite.
299    * @return test instance
300    */

301   public static
302   Test suite()
303   {
304     return new TestSuite(ExceptionCatchSTest.class);
305   }
306
307 }
Popular Tags