KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: ThiSTest.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 java.lang.reflect.Field JavaDoc;
11
12 import junit.framework.*;
13 import ch.ethz.jvmai.*;
14 import ch.ethz.prose.*;
15 import ch.ethz.prose.engine.*;
16
17
18 /**
19  * JUnit testcase for class CFlow.
20  *
21  * @version $Revision: 1.3 $
22  * @author Gerard Roos
23  */

24 public class ThiSTest extends TestCase {
25
26       // fixture
27

28       // used to catch and store the created events.
29
public static class CreateEvents extends JoinPointListener {
30
31         public CreateEvents() {}
32
33         public void joinPointReached(MethodEntryJoinPoint jp)
34         {
35           thisTest._testEqual(jp);
36           thisTest._testSame(jp);
37           thisTest._testInClass(jp);
38           thisTest._testInstanceOf(jp);
39         }
40
41         public void joinPointReached(MethodExitJoinPoint jp)
42         {
43           thisTest._testEqual(jp);
44           thisTest._testSame(jp);
45           thisTest._testInClass(jp);
46           thisTest._testInstanceOf(jp);
47         }
48
49         public void joinPointReached(FieldAccessJoinPoint jp)
50         {
51           thisTest._testEqual(jp);
52           thisTest._testSame(jp);
53           thisTest._testInClass(jp);
54           thisTest._testInstanceOf(jp);
55         }
56
57         public void joinPointReached(FieldModificationJoinPoint jp)
58         {
59           thisTest._testEqual(jp);
60           thisTest._testSame(jp);
61           thisTest._testInClass(jp);
62           thisTest._testInstanceOf(jp);
63         }
64
65         public void joinPointReached(ExceptionJoinPoint jp)
66         {
67           thisTest._testEqual(jp);
68           thisTest._testSame(jp);
69           thisTest._testInClass(jp);
70           thisTest._testInstanceOf(jp);
71         }
72
73         public void joinPointReached(ExceptionCatchJoinPoint jp)
74         {
75           thisTest._testEqual(jp);
76           thisTest._testSame(jp);
77           thisTest._testInClass(jp);
78           thisTest._testInstanceOf(jp);
79         }
80
81       }
82
83
84       public static class CheckSubclass extends JoinPointListener {
85
86         boolean isSubClass = false;
87         boolean isSuperClass = false;
88
89         public void joinPointReached(MethodEntryJoinPoint jp)
90         {
91
92         }
93
94         public void joinPointReached(MethodExitJoinPoint jp)
95         {
96
97         }
98
99 // field access will be on SubTest
100
public void joinPointReached(FieldAccessJoinPoint jp)
101         {
102              // access will be on a subclass
103
isSubClass = Within.subType(TestClass.class).isSpecialEvent(jp);
104
105         }
106
107 // field modification will be on test
108
public void joinPointReached(FieldModificationJoinPoint jp)
109         {
110             isSuperClass = Within.superType(TestSubClass.class).isSpecialEvent(jp);
111         }
112
113         public void joinPointReached(ExceptionJoinPoint jp)
114         {
115
116         }
117
118         public void joinPointReached(ExceptionCatchJoinPoint jp)
119         {
120
121         }
122
123       }
124
125       // tester
126
static class TestClass
127       {
128         public int i;
129
130         public TestClass()
131         {
132         }
133
134         public void dummyAccess()
135         {
136             int b = i;
137         }
138
139         public void dummySet()
140         {
141             i = 1;
142         }
143       }
144
145       static class TestSubClass extends TestClass implements java.io.Serializable JavaDoc
146       {
147       }
148
149
150       static TestClass testobj;
151       PointFilter spec;
152       JoinPointListener jpl;
153       FieldAccessRequest accessReq;
154       FieldModificationRequest modifyReq;
155
156       // to find this ThiSTest instance from within the JoinPointListener:
157
static ThiSTest thisTest;
158
159       static JoinPointManager jpm ;
160
161       /**
162        * Construct test with given name.
163        * @param name test name
164        */

165       public ThiSTest(String JavaDoc name) {
166         super(name);
167         thisTest = this;
168       }
169
170
171       /**
172        * Set up fixture.
173        */

174       protected void setUp() {
175
176         Field JavaDoc f = null;
177         jpl = new CreateEvents();
178
179         testobj = new TestSubClass();
180         // first we want to get real FieldAccess and FieldModification
181
// Events.
182
try
183         {
184             ProseSystem.startup();
185         }
186         catch ( SystemStartupException e )
187         {
188             Assert.fail("ExtensionSystemSetupException");
189         }
190
191         jpm = ProseSystem.getAspectManager().getJoinPointManager();
192         try
193         {
194             f = TestClass.class.getDeclaredField("i");
195         }
196         catch ( Exception JavaDoc e )
197         {
198             Assert.fail("Error getting field.");
199         }
200         accessReq = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f);
201         modifyReq = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f);
202       }
203
204
205       protected void tearDown()
206       {
207         try
208           { ProseSystem.teardown(); }
209         catch (SystemTeardownException tde)
210           { Assert.fail("Aspect.teardown() failed "); }
211       }
212
213       /**
214        * Run the tests. Actually, because we can not save events, we need
215        * the tests to be run from within the JoinPointListener.
216        */

217       public void test_02_ObjectSpecializers()
218         {
219           // now register the requests and get the events.
220
ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, accessReq);
221           ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, modifyReq);
222           ProseSystem.getAspectManager().getJoinPointManager().resumeListenerNotification(Thread.currentThread());
223
224           // test the access specializers
225
testobj.dummySet();
226
227           // test the modification specializers
228
testobj.dummyAccess();
229
230           ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl);
231       }
232
233       public void test_01_SubtypeSpecializers() throws Exception JavaDoc
234       {
235               CheckSubclass csJpl = new CheckSubclass();
236               // now register the requests and get the events.
237

238
239             ProseSystem.getAspectManager().getJoinPointManager().registerListener(csJpl, accessReq);
240             ProseSystem.getAspectManager().getJoinPointManager().registerListener(csJpl, modifyReq);
241                     ProseSystem.getAspectManager().getJoinPointManager().resumeListenerNotification(Thread.currentThread());
242
243             // test the access specializers
244
testobj.dummySet();
245             testobj = new TestSubClass();
246             // test the modification specializers
247
testobj.dummyAccess();
248
249             ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl);
250
251             assertTrue(csJpl.isSubClass);
252             assertTrue(csJpl.isSuperClass);
253       }
254
255
256       static class MyException extends RuntimeException JavaDoc{};
257
258       public void test_03_ThrowInAdvice() throws Exception JavaDoc
259           {
260               CheckSubclass csJpl = new CheckSubclass()
261             {
262               // now register the requests and get the events.
263
public void joinPointReached(FieldAccessJoinPoint jp)
264                 {
265                   try
266                 {
267                   Assert.fail();
268                 }
269                   catch (Throwable JavaDoc e)
270                 {}
271                 }
272             };
273
274
275             ProseSystem.getAspectManager().getJoinPointManager().registerListener(csJpl, accessReq);
276                     ProseSystem.getAspectManager().getJoinPointManager().resumeListenerNotification(Thread.currentThread());
277
278             testobj.dummyAccess();
279
280             ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl);
281
282         }
283
284       private void doNull()
285       {
286           String JavaDoc x = null;
287                   x.length();
288       }
289
290       /**
291        * Test the This.equal functionality
292        */

293       void _testEqual(JoinPoint ev)
294       {
295         Exception JavaDoc e = null;
296         spec = This.equalsTo(testobj);
297         assertTrue("1", spec.isSpecialEvent(ev));
298
299         spec = This.equalsTo(new TestClass());
300         assertTrue("2", ! spec.isSpecialEvent(ev));
301       }
302
303       /**
304        * Test the This.same functionality
305        */

306       void _testSame(JoinPoint ev) {
307         Exception JavaDoc e = null;
308         spec = This.isSameObject(testobj);
309         assertTrue("1", spec.isSpecialEvent(ev));
310         spec = This.isSameObject(new TestClass());
311         assertTrue("2", ! spec.isSpecialEvent(ev));
312
313       }
314
315       /**
316        * Test the CFlow.Fields functionality
317        */

318       void _testInstanceOf(JoinPoint ev)
319       {
320           spec = This.subtypeOf(java.io.Serializable JavaDoc.class);
321           assertTrue("3", spec.isSpecialEvent(ev));
322
323           spec = This.subtypeOf(java.lang.String JavaDoc.class);
324           assertTrue("4", ! spec.isSpecialEvent(ev));
325       }
326
327       void _testInClass(JoinPoint ev)
328       {
329           spec = This.type(".*Sub.*");
330           assertTrue("5", spec.isSpecialEvent(ev));
331
332           spec = This.type("TestClass");
333           assertTrue("6", !spec.isSpecialEvent(ev));
334       }
335
336
337       /**
338        * Test suite.
339        * @return test instance
340        */

341       public static Test suite() {
342         return new TestSuite(ThiSTest.class);
343       }
344
345 }
346
347
348 //======================================================================
349
//
350
// $Log: ThiSTest.java,v $
351
// Revision 1.3 2004/05/12 17:26:52 anicoara
352
// Adapt Junit tests to 3.8.1 version and the new package structure
353
//
354
// Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
355
// Imported from ETH Zurich
356
//
357
// Revision 1.2 2003/07/02 12:42:38 anicoara
358
// Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
359
//
360
// Revision 1.1 2003/05/05 14:02:59 popovici
361
// renaming from runes to prose
362
//
363
// Revision 1.14 2003/04/27 13:09:02 popovici
364
// Specializers renamed to PointCutter
365
//
366
// Revision 1.13 2003/04/26 18:51:45 popovici
367
// 1 Bug fix which lead to a refactoring step:
368
// 1. the bug: 'JoinPointRequests' used to write to a static list, which survived a startup/teardown;
369
// now this list belongs to the JoinPointManager;
370
// 2. the refactoring: the JoinPointManager now creates (and shares state) with join-points.
371
//
372
// Revision 1.12 2003/04/26 14:33:49 popovici
373
// subtypeOf, supertypeOf specializers addde to this and target
374
//
375
// Revision 1.11 2003/04/25 15:15:13 popovici
376
// FieldS renamed to 'Fields'
377
//
378
// Revision 1.10 2003/04/17 15:14:58 popovici
379
// Extension->Aspect renaming
380
//
381
// Revision 1.9 2003/04/17 14:46:06 popovici
382
// ThiS renamed to This; additional method renamings
383
//
384
// Revision 1.8 2003/04/17 13:59:39 popovici
385
// This class and methods renamed
386
//
387
// Revision 1.7 2003/03/04 18:36:12 popovici
388
// Organization of imprts
389
//
390
// Revision 1.6 2003/03/04 11:26:03 popovici
391
// Important refactorization step (march):
392
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
393
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
394
// structures
395
//
396
// Revision 1.5 2002/11/26 17:15:43 pschoch
397
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
398
// ProseSystem now owns and starts the Aspect interface.
399
// ProseSystem now containes a 'test' AspectManager
400
// AspectManager now owns the JoinPointManager.
401
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
402
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
403
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
404
// Documentation updated accordingly.
405
//
406
// Revision 1.4 2002/10/31 18:26:46 pschoch
407
// Capability of crosscutting Exceptions added to prose.
408
//
409
// Revision 1.3 2002/10/25 07:42:28 popovici
410
// Undo Chnages Phillippe
411
//
412
// Revision 1.1 2002/05/16 09:18:25 popovici
413
// ClasseS and CFlow replaced with Target, This; the crosscut spec package is refactorized. Now all
414
// crosscuts are grouped (abstract definitions + concrete definitions). Crosscuts explicitely are dynamic and static, and
415
// or/Not/And combinations can be created.
416
//
417
// Revision 1.4 2002/03/12 09:49:33 popovici
418
// Join Point listener now abstract class (performance reasons)
419
//
420
// Revision 1.3 2002/02/21 13:03:07 popovici
421
// Updated to new performance-optimized design: Crosscuts receive joinpoints, no Event notification, etc
422
//
423
// Revision 1.2 2002/02/05 11:18:24 smarkwal
424
// modifications to test JVMAI-based implementation
425
//
426
// Revision 1.1.1.1 2001/11/29 18:13:31 popovici
427
// Sources from runes
428
//
429
// Revision 1.1.2.2 2001/02/22 16:50:21 popovici
430
// ProseSystem.setup replaced with startup; teardown introduced
431
//
432
// Revision 1.1.2.1 2000/12/12 08:31:10 groos
433
// initial revision.
434
//
435
Popular Tags