KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > crosscut > ExceptionCutTest


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

7 package ch.ethz.prose.crosscut;
8
9 // used packages
10
import java.io.IOException JavaDoc;
11 import java.util.*;
12
13 import junit.framework.*;
14 import ch.ethz.inf.iks.jvmai.jvmdi.ItemManipulationException;
15 import ch.ethz.jvmai.ExceptionJoinPoint;
16 import ch.ethz.jvmai.JVMAspectInterface;
17 import ch.ethz.prose.*;
18 import ch.ethz.prose.engine.*;
19 import ch.ethz.prose.filter.PointCutter;
20
21 /**
22  * JUnit testcase for class ThrowCut.
23  *
24  * @version $Revision: 1.2 $
25  * @author Philippe Schoch
26  */

27 public
28 class ExceptionCutTest extends TestCase {
29
30   // fixture
31

32   // Define your own AspectManager for setting your own JoinPointManager!
33
public static class MyExtensionManager extends LocalAspectManager {
34     public MyExtensionManager(boolean ic, JVMAspectInterface ai)
35       { super(ic, ai); }
36     protected void createJoinPointManager(boolean ic, JVMAspectInterface ai)
37       { jpm = new MyJoinPointManager(ic, ai, false); if (ic) jpm.connectToJVMAI(); }
38
39   }
40
41   static class MyJoinPointManager extends JoinPointManager {
42     public MyJoinPointManager(boolean ic, JVMAspectInterface ai, boolean rev) { super(ic,ai,rev); }
43     public Map getReq2listener() { return req2listener; }
44     public Set getClassLoadListeners() { return classLoadListeners; }
45   }
46
47   static class TestException extends Throwable JavaDoc
48   {
49     public TestException() {super();}
50     public TestException(String JavaDoc s) {super(s);}
51   }
52
53   static class ExampleClass
54   {
55     public ExampleClass() {}
56     public static void method1() throws Exception JavaDoc { throw new Exception JavaDoc(); }
57     public static void method2() throws TestException { throw new TestException(); }
58     public void method3() throws UnknownError JavaDoc { throw new UnknownError JavaDoc(); }
59     public void method4() throws IllegalAspectException, ItemManipulationException { throw new IllegalAspectException(); }
60     public void method5() throws IOException JavaDoc { int i=0; if (i==0) throw new IOException JavaDoc(); throw new IllegalAspectException(); }
61     public void method6() { throw new RuntimeException JavaDoc(); }
62   }
63
64   public static class TestExtension extends DefaultAspect
65   {
66     public Crosscut c1 = new ThrowCut()
67     {
68       public void THROW_ARGS()
69         { counter1++; }
70     protected PointCutter pointCutter()
71     {
72         return null;
73     }
74
75       public Class JavaDoc[] potentialCrosscutClasses()
76       {
77         Class JavaDoc[] result = { ExampleClass.class,
78                 Object JavaDoc.class,
79                 ThrowCut.class,
80                 Exception JavaDoc.class,
81                 TestException.class,
82                 UnknownError JavaDoc.class,
83                 IllegalAspectException.class,
84                 ItemManipulationException.class
85             };
86     return result;
87       }
88     };
89   }
90
91   public static class TestExtension2 extends DefaultAspect
92   {
93     public Crosscut c2 = new ThrowCut()
94     {
95         public void THROW_ARGS()
96         {
97             counter2++;
98             eventClass = ((ExceptionJoinPoint)thisJoinPoint()).getException().getClass();
99         }
100         protected PointCutter pointCutter()
101         {
102             return null;
103         }
104    };
105       public Class JavaDoc[] getAllClasses() { return ((AbstractCrosscut)c2).potentialCrosscutClasses(); }
106   }
107
108     static int counter2=0;
109     ExampleClass exCls;
110     ThrowCut testCrosscut = null;
111     Map myReq2listener = null;
112     static int counter1 = 0;
113     static Class JavaDoc eventClass = null;
114
115     /**
116      * Construct test with given name.
117      * @param name test name
118      */

119     public ExceptionCutTest(String JavaDoc name)
120     {
121         super(name);
122     }
123
124     String JavaDoc oldExMgr;
125     /**
126      * Set up fixture.
127      */

128     protected
129     void setUp()
130     {
131         try
132         {
133             oldExMgr=System.getProperty("ch.ethz.prose.EXManager");
134             System.setProperty("ch.ethz.prose.EXManager", "ch.ethz.prose.crosscut.ExceptionCutTest$MyExtensionManager");
135             ProseSystem.startup();
136         }
137         catch ( Exception JavaDoc e )
138         {
139             e.printStackTrace();
140             tearDown();
141             Assert.fail("ProseSystem.startup() failed.");
142         }
143         exCls = new ExampleClass();
144     }
145
146     protected void tearDown()
147     {
148         try
149         {
150             System.err.println("TearingDown" + this);
151             ProseSystem.teardown();
152             System.setProperty("ch.ethz.prose.EXManager", oldExMgr);
153         }
154         catch (Exception JavaDoc e)
155         {
156             Assert.fail("ProseSystem.teardown() failed");
157         }
158     }
159
160
161     public void test0020_createRequestsAndEvents()
162     {
163         ProseSystem.getAspectManager().insert(new TestExtension());
164         myReq2listener = ((MyJoinPointManager)ProseSystem.getAspectManager().getJoinPointManager()).getReq2listener();
165         assertTrue("Exactly 5 JoinPoints created", myReq2listener.size() == 5);
166     
167             counter1 = 0;
168         try { ExampleClass.method1(); } catch (Throwable JavaDoc e) { }
169         try { ExampleClass.method2(); } catch (Throwable JavaDoc e) { }
170         try { exCls.method3(); } catch (Throwable JavaDoc e) { }
171         try { exCls.method4(); } catch (Throwable JavaDoc e) { }
172         assertEquals("Exactly 4 ExceptionEvents reported", 4,counter1);
173     
174         counter1 = 0;
175         try { exCls.method5(); } catch (Throwable JavaDoc e) { }
176         try { exCls.method6(); } catch (Throwable JavaDoc e) { }
177         assertTrue("0 ExceptionEvents reported", counter1 == 0);
178     }
179
180
181     public void test0030_allLoadedClasses()
182     {
183         Iterator it, il, ir;
184         int removeme=0;
185         TestExtension2 t2 = new TestExtension2();
186     
187         // 1. we 'test' insert this extension, and get its crosscut request.
188
ProseSystem.getAspectManager().insert(t2);
189         CrosscutRequest joinPointsWeHaveAskedFor = ((Crosscut)(t2.getCrosscuts().get(0))).createRequest();
190         ProseSystem.getAspectManager().withdraw(t2);
191     
192         // 2. we extract from this crosscut request the 'requested Exception classes'
193
Vector requestedExceptionClasses = new Vector();
194         it = joinPointsWeHaveAskedFor.iterator();
195         while (it.hasNext())
196         {
197             JoinPointRequest jpr = (JoinPointRequest)(it.next());
198             if (jpr instanceof ExceptionThrowRequest)
199               requestedExceptionClasses.add(((ExceptionThrowRequest)jpr).getExceptionClass());
200         }
201     
202         //2. we add in 'loaded Exception Classes' the exceptions currently loaded --
203
// should be the same as 'joinPoints we have asked for'
204
Class JavaDoc[] allClasses = t2.getAllClasses();
205         Vector loadedExceptionClasses = new Vector();
206         for (int j=0; j<allClasses.length;j++)
207           if (Throwable JavaDoc.class.isAssignableFrom((allClasses[j])))
208                 loadedExceptionClasses.add(allClasses[j]);
209     
210         // 3. now we do the test: do we get exaclty what we have asked for>
211
il = loadedExceptionClasses.iterator();
212         while (il.hasNext())
213           assertTrue("Crosscut request contains loaded class", requestedExceptionClasses.contains(il.next()));
214         ir = requestedExceptionClasses.iterator();
215         while (ir.hasNext())
216     
217           assertTrue("loaded classes contains requested class", loadedExceptionClasses.contains(ir.next()));
218     
219         // Now for every loaded throwable class, an instance of it is thrown
220
// and caught which has the effect of incrementing
221
// the counter in the advice method since all throwable classes are registered for crosscutting.
222

223         // 4. We create a list of throwable objects;
224
List throwableObjects = new Vector();
225         il = loadedExceptionClasses.iterator();
226     
227         while (il.hasNext())
228         {
229             Class JavaDoc actualClass = (Class JavaDoc)il.next();
230             try
231               {
232             throwableObjects.add(actualClass.newInstance());
233               }
234             catch (InstantiationException JavaDoc ie)
235               { /* well, this exception is will *not* be thrown */ }
236             catch (IllegalAccessException JavaDoc iae)
237               { /* neither will this one */ }
238         }
239     
240         // 5. isert the extension, measure the number of thrown exception and then
241
// withdraw the extension. Finally we check that the number
242
// of notifications is the same thing as we have thrown.
243
ProseSystem.getAspectManager().insert(t2);
244     
245         il = throwableObjects.iterator();
246         counter2=0;
247         while (il.hasNext())
248         {
249             Throwable JavaDoc exc= (Throwable JavaDoc)il.next();
250             try {throw exc;} catch(Throwable JavaDoc e) {} // this is what we count
251
}
252         assertEquals("For every Exception Class an event", counter2, throwableObjects.size());
253         ProseSystem.getAspectManager().withdraw(t2);
254   }
255
256   /**
257    * Test suite.
258    * @return test instance
259    */

260   public static
261   Test suite()
262   {
263     return new TestSuite(ExceptionCutTest.class);
264   }
265
266 }
267
268
269 //======================================================================
270
//
271
// $Log: ExceptionCutTest.java,v $
272
// Revision 1.2 2004/05/12 17:26:51 anicoara
273
// Adapt Junit tests to 3.8.1 version and the new package structure
274
//
275
// Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
276
// Imported from ETH Zurich
277
//
278
// Revision 1.1 2003/05/05 14:03:27 popovici
279
// renaming from runes to prose
280
//
281
// Revision 1.14 2003/04/27 13:08:55 popovici
282
// Specializers renamed to PointCutter
283
//
284
// Revision 1.13 2003/04/17 15:43:47 popovici
285
// crosscuts renamed to 'getCrosscuts'
286
// createCrosscuts renamed to 'crosscuts'
287
//
288
// Revision 1.12 2003/04/17 15:15:16 popovici
289
// Extension->Aspect renaming
290
//
291
// Revision 1.11 2003/04/17 12:49:42 popovici
292
// Refactoring of the crosscut package
293
// ExceptionCut renamed to ThrowCut
294
// McutSignature is now SignaturePattern
295
//
296
// Revision 1.10 2003/04/17 08:46:47 popovici
297
// Important functionality additions
298
// - Cflow specializers
299
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
300
// - Transactional capabilities
301
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
302
// between static and dynamic specializers.
303
// - Functionality pulled up in abstract classes
304
// - Uniformization of advice methods patterns and names
305
//
306
// Revision 1.9 2003/03/04 18:36:41 popovici
307
// Organization of imprts
308
//
309
// Revision 1.8 2003/03/04 11:25:59 popovici
310
// Important refactorization step (march):
311
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
312
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
313
// structures
314
//
315
// Revision 1.7 2003/01/27 12:52:22 pschoch
316
// Advice-Method now without parameter
317
//
318
// Revision 1.6 2002/11/27 15:10:54 popovici
319
// Refactorisation of the 'test0030_allLoadedClasses' test. Used to
320
// be very unstable (depending on the number of loaded exceptions/and on timing)
321
//
322
// Revision 1.5 2002/11/27 15:04:25 pschoch
323
// test0030_allLoadedClasses changed: Counter removed. Now, assert that event exception class is same as thrown exception class
324
//
325
// Revision 1.4 2002/11/27 11:45:25 pschoch
326
// Bug Fix in test0030_allLoadedClasses counter into two separate counters and catch clause inserted for special case test0030_allLoadedClasses
327
//
328
// Revision 1.3 2002/11/26 17:15:36 pschoch
329
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
330
// ProseSystem now owns and starts the Aspect interface.
331
// ProseSystem now containes a 'test' AspectManager
332
// AspectManager now owns the JoinPointManager.
333
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
334
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
335
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
336
// Documentation updated accordingly.
337
//
338
// Revision 1.2 2002/10/31 18:26:44 pschoch
339
// Capability of crosscutting Exceptions added to prose.
340
//
341
//
342
Popular Tags