KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: TargetSTest.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  * JUnit testcase for class CFlow.
19  *
20  * @version $Revision: 1.3 $
21  * @author Gerard Roos
22  */

23 public class TargetSTest extends TestCase {
24
25       // fixture
26

27       // used to catch and store the created events.
28
public static class CreateEvents extends JoinPointListener {
29
30         public CreateEvents() {}
31
32         public void joinPointReached(MethodEntryJoinPoint jp)
33           {
34           thisTest._testEqual(jp);
35           thisTest._testSame(jp);
36           thisTest._testInClass(jp);
37           thisTest._testInstanceOf(jp);
38           }
39
40         public void joinPointReached(MethodExitJoinPoint jp)
41           {
42           thisTest._testEqual(jp);
43           thisTest._testSame(jp);
44           thisTest._testInClass(jp);
45           thisTest._testInstanceOf(jp);
46           }
47
48         public void joinPointReached(FieldAccessJoinPoint jp)
49           {
50           thisTest._testEqual(jp);
51           thisTest._testSame(jp);
52           thisTest._testInClass(jp);
53           thisTest._testInstanceOf(jp);
54           }
55
56         public void joinPointReached(FieldModificationJoinPoint jp)
57           {
58           thisTest._testEqual(jp);
59           thisTest._testSame(jp);
60           thisTest._testInClass(jp);
61           thisTest._testInstanceOf(jp);
62           }
63
64         public void joinPointReached(ExceptionJoinPoint jp)
65           {
66           thisTest._testEqual(jp);
67           thisTest._testSame(jp);
68           thisTest._testInClass(jp);
69           thisTest._testInstanceOf(jp);
70           }
71
72         public void joinPointReached(ExceptionCatchJoinPoint jp)
73           {
74           thisTest._testEqual(jp);
75           thisTest._testSame(jp);
76           thisTest._testInClass(jp);
77           thisTest._testInstanceOf(jp);
78           }
79
80       }
81
82       // tester
83
static class TestClass
84       {
85         public int i;
86
87         public TestClass()
88           {
89           }
90
91         public void dummyAccess()
92           {
93         int b = i;
94           }
95
96         public void dummySet()
97           {
98         i = 1;
99           }
100       }
101
102       static class TestSubClass extends TestClass implements java.io.Serializable JavaDoc
103       {
104       }
105
106
107       TestClass testobj;
108       PointFilter spec;
109       JoinPointListener jpl;
110       FieldAccessRequest accessReq;
111       FieldModificationRequest modifyReq;
112
113       // to find this TargetSTest instance from within the JoinPointListener:
114
static TargetSTest thisTest;
115       static JoinPointManager jpm ;
116
117
118       /**
119        * Construct test with given name.
120        * @param name test name
121        */

122       public TargetSTest(String JavaDoc name) {
123         super(name);
124         thisTest = this;
125       }
126
127
128       /**
129        * Set up fixture.
130        */

131       protected void setUp() {
132         Field JavaDoc f = null;
133         jpl = new CreateEvents();
134
135         testobj = new TestSubClass();
136         // first we want to get real FieldAccess and FieldModification
137
// Events.
138
try
139         {
140             ProseSystem.startup();
141         }
142         catch ( SystemStartupException e )
143         {
144             Assert.fail("ExtensionSystemSetupException");
145         }
146
147         jpm = ProseSystem.getAspectManager().getJoinPointManager();
148
149
150         try
151         {
152             f = TestClass.class.getDeclaredField("i");
153         }
154         catch ( Exception JavaDoc e )
155         {
156             Assert.fail("Error getting field.");
157         }
158
159         accessReq = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f);
160         modifyReq = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f);
161       }
162
163
164       protected void tearDown()
165       {
166         try
167           { ProseSystem.teardown(); }
168         catch (SystemTeardownException tde)
169           { Assert.fail("Aspect.teardown() failed "); }
170       }
171
172       /**
173        * Run the tests. Actually, because we can not save events, we need
174        * the tests to be run from within the JoinPointListener.
175        */

176       public void testObjectSpecializers()
177         {
178           // now register the requests and get the events.
179
ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, accessReq);
180           ProseSystem.getAspectManager().getJoinPointManager().registerListener(jpl, modifyReq);
181           ProseSystem.getAspectManager().getJoinPointManager().resumeListenerNotification(Thread.currentThread());
182
183           // test the access specializers
184
testobj.dummySet();
185
186           // test the modification specializers
187
testobj.dummyAccess();
188
189           ProseSystem.getAspectManager().getJoinPointManager().unregisterListener(jpl);
190       }
191
192
193       /**
194        * Test the Target.equal functionality
195        */

196       void _testEqual(JoinPoint ev) {
197         Exception JavaDoc e = null;
198         spec = Target.equalsTo(testobj);
199         assertTrue("1", spec.isSpecialEvent(ev));
200
201         spec = Target.equalsTo(new TestClass());
202         assertTrue("2", ! spec.isSpecialEvent(ev));
203
204         try
205           {
206             spec = Target.equalsTo(null);
207             Assert.fail("Setting the CFlow Receiver to null should throw a " +
208                     "NullPointerException!");
209           }
210         catch (IllegalArgumentException JavaDoc npe )
211           {
212             e = npe;
213           }
214         assertNotNull("Setting the CFlow Receiver to null should have thrown a " +
215               "NullPointerException!", e);
216       }
217
218       /**
219        * Test the Target.same functionality
220        */

221       void _testSame(JoinPoint ev) {
222         Exception JavaDoc e = null;
223         spec = Target.isSameObject(testobj);
224         assertTrue("1", spec.isSpecialEvent(ev));
225
226         spec = Target.isSameObject(new TestClass());
227         assertTrue("2", ! spec.isSpecialEvent(ev));
228
229         try
230           {
231             spec = Target.isSameObject(null);
232             Assert.fail("Setting the CFlow Receiver to null should throw a " +
233                     "NullPointerException!");
234           }
235         catch ( IllegalArgumentException JavaDoc npe )
236           {
237             e = npe;
238           }
239         assertNotNull("Setting the CFlow Receiver to null should have thrown a " +
240               "NullPointerException!", e);
241       }
242
243
244       /**
245        * Test the CFlow.Fields functionality
246        */

247       void _testInstanceOf(JoinPoint ev)
248         {
249           spec = Target.subtypeOf(java.io.Serializable JavaDoc.class);
250           assertTrue("3", spec.isSpecialEvent(ev));
251
252           spec = Target.subtypeOf(java.lang.String JavaDoc.class);
253           assertTrue("4", ! spec.isSpecialEvent(ev));
254         }
255
256       void _testInClass(JoinPoint ev)
257         {
258           spec = Target.type(".*Sub.*");
259           assertTrue("5", spec.isSpecialEvent(ev));
260
261           spec = Target.type("TestClass");
262           assertTrue("6", !spec.isSpecialEvent(ev));
263         }
264
265
266       /**
267        * Test suite.
268        * @return test instance
269        */

270       public static Test suite() {
271         return new TestSuite(TargetSTest.class);
272       }
273 }
274
275
276 //======================================================================
277
//
278
// $Log: TargetSTest.java,v $
279
// Revision 1.3 2004/05/12 17:26:52 anicoara
280
// Adapt Junit tests to 3.8.1 version and the new package structure
281
//
282
// Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
283
// Imported from ETH Zurich
284
//
285
// Revision 1.2 2003/07/02 12:42:37 anicoara
286
// Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
287
//
288
// Revision 1.1 2003/05/05 14:02:58 popovici
289
// renaming from runes to prose
290
//
291
// Revision 1.15 2003/04/27 13:09:01 popovici
292
// Specializers renamed to PointCutter
293
//
294
// Revision 1.14 2003/04/26 18:51:44 popovici
295
// 1 Bug fix which lead to a refactoring step:
296
// 1. the bug: 'JoinPointRequests' used to write to a static list, which survived a startup/teardown;
297
// now this list belongs to the JoinPointManager;
298
// 2. the refactoring: the JoinPointManager now creates (and shares state) with join-points.
299
//
300
// Revision 1.13 2003/04/25 15:15:12 popovici
301
// FieldS renamed to 'Fields'
302
//
303
// Revision 1.12 2003/04/17 15:14:59 popovici
304
// Extension->Aspect renaming
305
//
306
// Revision 1.11 2003/04/17 14:46:05 popovici
307
// ThiS renamed to This; additional method renamings
308
//
309
// Revision 1.10 2003/04/17 13:59:38 popovici
310
// This class and methods renamed
311
//
312
// Revision 1.9 2003/04/17 12:49:29 popovici
313
// Refactoring of the crosscut package
314
// ExceptionCut renamed to ThrowCut
315
// McutSignature is now SignaturePattern
316
//
317
// Revision 1.8 2003/04/17 08:46:52 popovici
318
// Important functionality additions
319
// - Cflow specializers
320
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
321
// - Transactional capabilities
322
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
323
// between static and dynamic specializers.
324
// - Functionality pulled up in abstract classes
325
// - Uniformization of advice methods patterns and names
326
//
327
// Revision 1.7 2003/03/04 18:36:12 popovici
328
// Organization of imprts
329
//
330
// Revision 1.6 2003/03/04 11:26:03 popovici
331
// Important refactorization step (march):
332
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
333
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
334
// structures
335
//
336
// Revision 1.5 2002/11/26 17:15:42 pschoch
337
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
338
// ProseSystem now owns and starts the Aspect interface.
339
// ProseSystem now containes a 'test' AspectManager
340
// AspectManager now owns the JoinPointManager.
341
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
342
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
343
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
344
// Documentation updated accordingly.
345
//
346
// Revision 1.4 2002/10/31 18:26:46 pschoch
347
// Capability of crosscutting Exceptions added to prose.
348
//
349
// Revision 1.3 2002/10/25 07:42:28 popovici
350
// Undo Chnages Phillippe
351
//
352
// Revision 1.1 2002/05/16 09:18:24 popovici
353
// ClasseS and CFlow replaced with Target, This; the crosscut spec package is refactorized. Now all
354
// crosscuts are grouped (abstract definitions + concrete definitions). Crosscuts explicitely are dynamic and static, and
355
// or/Not/And combinations can be created.
356
//
357
// Revision 1.4 2002/03/12 09:49:33 popovici
358
// Join Point listener now abstract class (performance reasons)
359
//
360
// Revision 1.3 2002/02/21 13:03:07 popovici
361
// Updated to new performance-optimized design: Crosscuts receive joinpoints, no Event notification, etc
362
//
363
// Revision 1.2 2002/02/05 11:18:24 smarkwal
364
// modifications to test JVMAI-based implementation
365
//
366
// Revision 1.1.1.1 2001/11/29 18:13:31 popovici
367
// Sources from runes
368
//
369
// Revision 1.1.2.2 2001/02/22 16:50:21 popovici
370
// ProseSystem.setup replaced with startup; teardown introduced
371
//
372
// Revision 1.1.2.1 2000/12/12 08:31:10 groos
373
// initial revision.
374
//
375
Popular Tags