KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: ExceptionSTest.java,v 1.3 2004/05/12 17:26:52 anicoara Exp $
2
// =====================================================================
3
//
4
// (history at end)
5
//
6

7 package ch.ethz.prose.filter;
8
9 // used packages
10
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 /**
18  * JUnit testcase for class Exceptions.
19  *
20  * @version $Revision: 1.3 $
21  * @author Philippe Schoch
22  */

23 public
24 class ExceptionSTest extends TestCase {
25
26   // fixture
27
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            // have to be complete
52
}
53   }
54
55   static class Example1
56   {
57       public void method1() throws Exception JavaDoc { throw new Exception JavaDoc(); }
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 JavaDoc();}
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 JavaDoc("def ghi"); }
74       public void function3() { throw new NumberFormatException JavaDoc("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   /**
97    * Construct test with given name.
98    * @param name test name
99    */

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

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     // for the static testing part
126
Request1 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,Exception JavaDoc.class);
127     Request2 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,RuntimeException JavaDoc.class);
128     Request3 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ThreadDeath JavaDoc.class);
129     Request4 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ItemManipulationException.class);
130     Request5 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,NoClassDefFoundError JavaDoc.class);
131     Request6 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,ClassCastException JavaDoc.class);
132     Request7 = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,NumberFormatException JavaDoc.class);
133
134
135     // for the dynamic testing part
136
ex1 = new Example1();
137     bsp2 = new Beispiel2();
138     exCls3 = new ExampleClass3();
139
140     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,Exception JavaDoc.class));
141     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,RuntimeException JavaDoc.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 JavaDoc.class));
145     ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,NumberFormatException JavaDoc.class));
146   }
147
148
149   protected void tearDown() throws Exception JavaDoc
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 JavaDoc.class) );
161 // PointFilter MySpec2 =
162
// Exceptions.inPackage("jvmai") .OR( Exceptions.subclassOf(java.lang.IllegalArgumentException.class) );
163
// PointFilter MySpec3 =
164
// Exceptions.qualifiedNamed("java.lang.Exception") .OR( Exceptions.qualifiedNamed(".lang.*Class") );
165

166       assertTrue("1.1", ! MySpec1.isSpecialRequest(Request1));
167 // assertTrue("2.1", ! MySpec2.isSpecialRequest(Request1));
168
//assertTrue("3.1", MySpec3.isSpecialRequest(Request1));
169
assertTrue("1.2", MySpec1.isSpecialRequest(Request2));
170 // assertTrue("2.2", ! MySpec2.isSpecialRequest(Request2));
171
//assertTrue("3.2", ! MySpec3.isSpecialRequest(Request2));
172
assertTrue("1.3", ! MySpec1.isSpecialRequest(Request3));
173 // assertTrue("2.3", ! MySpec2.isSpecialRequest(Request3));
174
//assertTrue("3.3", ! MySpec3.isSpecialRequest(Request3));
175
assertTrue("1.4", MySpec1.isSpecialRequest(Request4));
176 // assertTrue("2.4", MySpec2.isSpecialRequest(Request4));
177
//assertTrue("3.4", ! MySpec3.isSpecialRequest(Request4));
178
assertTrue("1.5", ! MySpec1.isSpecialRequest(Request5));
179 // assertTrue("2.5", ! MySpec2.isSpecialRequest(Request5));
180
//assertTrue("3.5", MySpec3.isSpecialRequest(Request5));
181
assertTrue("1.6", MySpec1.isSpecialRequest(Request6));
182 // assertTrue("2.6", ! MySpec2.isSpecialRequest(Request6));
183
//assertTrue("3.6", MySpec3.isSpecialRequest(Request6));
184
assertTrue("1.7", MySpec1.isSpecialRequest(Request7));
185 // assertTrue("2.7", MySpec2.isSpecialRequest(Request7));
186
//assertTrue("3.7", ! MySpec3.isSpecialRequest(Request7));
187
}
188
189   public void test020_FilterEvents() throws Exception JavaDoc
190   {
191       // six different methods which each throw an exception are executed and afterwards if is
192
// looked up if the specializer passes the event
193

194       try { ex1.method1(); } catch (Throwable JavaDoc e) { }
195       try { ex1.method2(); } catch (Throwable JavaDoc e) { }
196       try { bsp2.function1(); } catch (Throwable JavaDoc e) { }
197       try { bsp2.method2(); } catch (Throwable JavaDoc e) { }
198       try { exCls3.function1(); } catch (Throwable JavaDoc e) { }
199       try
200         {
201           exCls3.function3();
202         }
203       catch (Throwable JavaDoc 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); // how many NullPointerException are thrown (== 2*6)
234
}
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 JavaDoc)null);
253       if (spec.isSpecialEvent(jpe))
254         counter[3]++;
255     }
256     catch (NullPointerException JavaDoc 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         //FIXME: here we have an assertion failure & and
280
// a PROSE error. Withing this method, a null pointer
281
// exception is thrown and cought. Since we are in the jvmaiHook,
282
// this leads to strage behavior: for all method exits, the
283
// same exception is reported.
284
spec = Within.method(null);
285         if (spec.isSpecialEvent(jpe))
286         counter[7]++;
287     }
288     catch (NullPointerException JavaDoc 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   /**
315    * Test suite.
316    * @return test instance
317    */

318   public static
319   Test suite()
320   {
321     return new TestSuite(ExceptionSTest.class);
322   }
323
324 }
325
326
327 //======================================================================
328
//
329
// $Log: ExceptionSTest.java,v $
330
// Revision 1.3 2004/05/12 17:26:52 anicoara
331
// Adapt Junit tests to 3.8.1 version and the new package structure
332
//
333
// Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
334
// Imported from ETH Zurich
335
//
336
// Revision 1.2 2003/07/02 12:42:37 anicoara
337
// Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
338
//
339
// Revision 1.1 2003/05/05 14:03:00 popovici
340
// renaming from runes to prose
341
//
342
// Revision 1.13 2003/04/27 13:09:01 popovici
343
// Specializers renamed to PointCutter
344
//
345
// Revision 1.12 2003/04/26 18:51:43 popovici
346
// 1 Bug fix which lead to a refactoring step:
347
// 1. the bug: 'JoinPointRequests' used to write to a static list, which survived a startup/teardown;
348
// now this list belongs to the JoinPointManager;
349
// 2. the refactoring: the JoinPointManager now creates (and shares state) with join-points.
350
//
351
// Revision 1.11 2003/04/17 15:15:00 popovici
352
// Extension->Aspect renaming
353
//
354
// Revision 1.10 2003/04/17 14:51:12 popovici
355
// ExceptionS renamed to Exception; method renamings
356
//
357
// Revision 1.9 2003/04/17 13:54:32 popovici
358
// Refactorization of 'ExecutionS' into 'Within' and 'Executions'.
359
// Method names refer now to 'types'
360
//
361
// Revision 1.8 2003/04/17 12:49:30 popovici
362
// Refactoring of the crosscut package
363
// ExceptionCut renamed to ThrowCut
364
// McutSignature is now SignaturePattern
365
//
366
// Revision 1.7 2003/04/17 08:46:51 popovici
367
// Important functionality additions
368
// - Cflow specializers
369
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
370
// - Transactional capabilities
371
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
372
// between static and dynamic specializers.
373
// - Functionality pulled up in abstract classes
374
// - Uniformization of advice methods patterns and names
375
//
376
// Revision 1.6 2003/03/05 12:08:39 popovici
377
// Deobfuscation of exception filters. They are now
378
// top level classes. This is just an intermediate refactoring
379
// step. Many Exceptions. predicate will be eliminated
380
// by more powerful Specializers like 'Within'
381
//
382
// Revision 1.5 2003/03/04 18:36:12 popovici
383
// Organization of imprts
384
//
385
// Revision 1.4 2003/03/04 11:26:02 popovici
386
// Important refactorization step (march):
387
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
388
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
389
// structures
390
//
391
// Revision 1.3 2002/11/27 17:09:34 popovici
392
// asertions more verbose, changes to show exaclty where the
393
// exception bug appears in 1.4.
394
//
395
// Revision 1.2 2002/11/26 17:15:41 pschoch
396
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
397
// ProseSystem now owns and starts the Aspect interface.
398
// ProseSystem now containes a 'test' AspectManager
399
// AspectManager now owns the JoinPointManager.
400
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
401
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
402
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
403
// Documentation updated accordingly.
404
//
405
// Revision 1.1 2002/10/31 18:26:45 pschoch
406
// Capability of crosscutting Exceptions added to prose.
407
//
408
Popular Tags