KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > FieldEventIntegrationTest


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

7 package ch.ethz.prose;
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.engine.*;
15
16 /**
17  * JUnit testcase. This is an integration test (black box) and tests
18  * the functionality of the classes
19  * <ul>
20  * <li> FieldAccessRequestImpl
21  * <li> fieldModificationRequestImpl
22  * <li> FieldSignatureImpl
23  * <li> FieldAccessEventImpl
24  * <li> FieldModificationEventImpl
25  * <li> (FieldEventImpl)
26  * </ul>
27  * by accessing them through the following interfaces only:
28  * <ul>
29  * <li> FieldAccessRequest
30  * <li> FieldModificationRequest
31  * <li> FieldAccessEvent
32  * <li> FieldModificationEvent
33  * </ul>
34  *
35  * @version $Revision: 1.3 $
36  * @author Gerard Roos
37  */

38 public class FieldEventIntegrationTest extends TestCase {
39
40       // fixture
41
static class TestClass {
42         public int instanceField;
43         static String JavaDoc classField = "classField";
44
45         Integer JavaDoc obj;
46
47         public TestClass() { }
48       }
49
50       static class FieldListener extends JoinPointListener {
51         public int read;
52         public int write;
53
54         public Field JavaDoc field;
55         public Object JavaDoc owner, oldValue, newValue;
56
57         public FieldListener() { }
58
59         public void joinPointReached(FieldAccessJoinPoint e)
60         {
61           field = null;
62           owner = null;
63           newValue = null;
64
65           read++;
66           field = e.getField();
67           owner = e.getTarget();
68           try
69           {
70               oldValue = field.get(owner);
71           }
72           catch ( Exception JavaDoc exc )
73           {
74               Assert.fail("Could not get oldValue on FieldAccessEvent");
75           }
76         }
77
78         public void joinPointReached(FieldModificationJoinPoint e)
79         {
80               write++;
81               field = e.getField();
82               owner = e.getTarget();
83               try
84                   {
85                   oldValue = field.get(owner);
86                   }
87               catch ( Exception JavaDoc exc )
88                   {
89                   Assert.fail("Could not get oldValue on FieldModificationEvent");
90                   }
91               newValue = e.getNewValue();
92         }
93
94         public void joinPointReached(MethodEntryJoinPoint jp)
95         {
96               Assert.fail("The event received in FieldListener should be a " +
97                       "FieldAccessJoinPoint or FieldModificationJoinPoint!!");
98         }
99
100         public void joinPointReached(MethodExitJoinPoint jp)
101         {
102               Assert.fail("The event received in FieldListener should be a " +
103                       "FieldAccessJoinPoint or FieldModificationJoinPoint!!");
104         }
105
106         public void joinPointReached(ExceptionJoinPoint jp)
107         {
108               Assert.fail("The event received in FieldListener should be a " +
109                       "FieldAccessJoinPoint or FieldModificationJoinPoint!!");
110         }
111
112         public void joinPointReached(ExceptionCatchJoinPoint jp)
113         {
114             Assert.fail("The event received in FieldListener should be a " +
115                     "FieldExceptionCatchJoinPoint!!");
116         }
117       }
118
119
120       JoinPointManager jpm;
121       TestClass testClass;
122       TestClass anotherTestClass;
123       Class JavaDoc c;
124       Field JavaDoc classF, instanceF, objF;
125       FieldAccessRequest accessInstReq, accessClsReq, accessObjReq;
126       FieldModificationRequest modInstReq, modClsReq, modObjReq;
127       FieldListener jpl;
128
129
130       /**
131        * Construct test with given name.
132        * @param name test name
133        */

134       public FieldEventIntegrationTest(String JavaDoc name) {
135         super(name);
136       }
137
138
139       /**
140        * Set up fixture.
141        */

142       protected void setUp() {
143         testClass = new TestClass();
144         anotherTestClass = new TestClass();
145         jpl = new FieldListener();
146
147         try {
148           ProseSystem.startup();
149         }
150         catch ( SystemStartupException e ) {
151           Assert.fail("SystemStartupException");
152         }
153
154         c = testClass.getClass();
155         try {
156           instanceF = c.getDeclaredField("instanceField");
157           classF = c.getDeclaredField("classField");
158           objF = c.getDeclaredField("obj");
159         }
160         catch ( Exception JavaDoc e ) {
161           Assert.fail("Field \"instanceField\" or \"classField\" or \"obj\" " +
162               "not found in Class \"TestClass\".");
163         }
164
165         jpm = ProseSystem.getAspectManager().getJoinPointManager();
166
167         JVMAspectInterface aspectInterface = jpm.getAspectInterface();
168
169         accessInstReq = (FieldAccessRequest)jpm.createJoinPointRequest( FieldAccessJoinPoint.KIND ,instanceF);
170         modInstReq = (FieldModificationRequest)jpm.createJoinPointRequest( FieldModificationJoinPoint.KIND,instanceF);
171         accessClsReq = (FieldAccessRequest)jpm.createJoinPointRequest( FieldAccessJoinPoint.KIND ,classF );
172         modClsReq = (FieldModificationRequest)jpm.createJoinPointRequest( FieldModificationJoinPoint.KIND,classF );
173         accessObjReq = (FieldAccessRequest)jpm.createJoinPointRequest( FieldAccessJoinPoint.KIND ,objF );
174         modObjReq = (FieldModificationRequest)jpm.createJoinPointRequest( FieldModificationJoinPoint.KIND,objF );
175       }
176
177
178       /**
179        *
180        */

181       protected void tearDown() throws SystemTeardownException {
182         try {
183           ProseSystem.teardown();
184         }
185         catch ( Exception JavaDoc e ) {
186           Assert.fail("ProseSystem.teardown() failed.");
187         }
188       }
189
190
191
192       /**
193        * Accesses and modifies an instance field.
194        */

195       public void testInstanceField() throws Exception JavaDoc {
196         // init
197
jpl.read = 0;
198         jpl.write = 0;
199         testClass.instanceField = 0;
200         assertTrue(testClass.instanceField == 0);
201         assertTrue(jpl.read == 0);
202         assertTrue(jpl.write == 0);
203
204         // register and enable the joinpoints:
205
jpm.registerListener(jpl, accessInstReq);
206         jpm.registerListener(jpl, modInstReq);
207
208         // access instanceField
209
assertTrue(testClass.instanceField == 0);
210         assertTrue(jpl.read == 1);
211         assertTrue(jpl.write == 0);
212         assertTrue(jpl.field.equals(instanceF));
213         assertTrue(jpl.owner.equals(testClass));
214         assertTrue(! jpl.owner.equals(anotherTestClass));
215         assertTrue(((Integer JavaDoc)jpl.oldValue).intValue() == 0);
216
217         // write to instanceField
218
testClass.instanceField = 999;
219         assertTrue(jpl.read == 1);
220         assertTrue(jpl.write == 1);
221         assertTrue(jpl.field.equals(instanceF));
222         assertTrue(jpl.owner.equals(testClass));
223         assertTrue(! jpl.owner.equals(anotherTestClass));
224         assertTrue(jpl.field.getInt(testClass) == 999);
225         assertTrue(((Integer JavaDoc)jpl.oldValue).intValue() == 0);
226         assertTrue(((Integer JavaDoc)jpl.newValue).intValue() == 999);
227
228         // access instanceField again
229
assertTrue(testClass.instanceField == 999);
230         assertTrue(jpl.read == 2);
231         assertTrue(jpl.write == 1);
232         assertTrue(jpl.field.equals(instanceF));
233         assertTrue(jpl.owner.equals(testClass));
234         assertTrue(! jpl.owner.equals(anotherTestClass));
235         assertTrue(((Integer JavaDoc)jpl.oldValue).intValue() == 999);
236
237         // unregister the listener and disable the joinpoints:
238
jpm.unregisterListener(jpl);
239
240         // write and read should not trigger events if
241
// unregistering successful
242
testClass.instanceField = 10;
243         assertTrue(testClass.instanceField == 10);
244         assertTrue(jpl.read == 2); // should not have changed
245
assertTrue(jpl.write == 1); // should not have changed
246
}
247
248
249       /**
250        * Accesses and modifies a class (static) field.
251        */

252       public void testClassField() throws Exception JavaDoc {
253         // init
254
jpl.read = 0;
255         jpl.write = 0;
256         TestClass.classField = "initialized";
257         assertTrue(TestClass.classField.equals("initialized"));
258         assertTrue(jpl.read == 0);
259         assertTrue(jpl.write == 0);
260
261         // register and enable the joinpoints:
262
jpm.registerListener(jpl, accessClsReq);
263         jpm.registerListener(jpl, modClsReq);
264
265         // access classField
266
assertTrue(TestClass.classField.equals("initialized"));
267         assertTrue(jpl.read == 1);
268         assertTrue(jpl.write == 0);
269         assertTrue(jpl.field.equals(classF));
270         assertTrue(((String JavaDoc)jpl.oldValue).equals("initialized"));
271
272         // write to classField
273
TestClass.classField = "modified";
274         assertTrue(jpl.read == 1);
275         assertTrue(jpl.write == 1);
276         assertTrue(jpl.field.equals(classF));
277         assertTrue(((String JavaDoc)jpl.oldValue).equals("initialized"));
278         assertEquals("modified",((String JavaDoc)jpl.newValue));
279
280         // access classField again
281
assertTrue(TestClass.classField.equals("modified"));
282         assertTrue(jpl.read == 2);
283         assertTrue(jpl.write == 1);
284         assertTrue(jpl.field.equals(classF));
285         assertTrue(((String JavaDoc)jpl.oldValue).equals("modified"));
286
287         // unregister the listener and disable the joinpoints:
288
jpm.unregisterListener(jpl);
289
290         // write and read should not trigger events if
291
// unregistering successful
292
TestClass.classField = "reset";
293         assertTrue(TestClass.classField.equals("reset"));
294         assertTrue(jpl.read == 2); // should not have changed
295
assertTrue(jpl.write == 1); // should not have changed
296
}
297
298
299       public void testObjectField() throws Exception JavaDoc {
300         // init
301
jpl.read = 0;
302         jpl.write = 0;
303         testClass.obj = new Integer JavaDoc(1);
304         assertTrue("o1", testClass.obj.intValue() == 1);
305         assertTrue("o2", jpl.read == 0);
306         assertTrue("o3", jpl.write == 0);
307
308         // register and enable the joinpoints:
309
jpm.registerListener(jpl, accessObjReq);
310         jpm.registerListener(jpl, modObjReq);
311
312         // access obj
313
assertTrue("o4", testClass.obj.intValue() == 1);
314         assertTrue("o5", jpl.read == 1);
315         assertTrue("o6", jpl.write == 0);
316         assertTrue("o7", jpl.field.equals(objF));
317         assertTrue("o8", ((Integer JavaDoc)jpl.oldValue).intValue() == 1);
318
319         // write to obj
320
testClass.obj = new Integer JavaDoc(2);
321         assertTrue("o9", jpl.read == 1);
322         assertTrue("o10", jpl.write == 1);
323         assertTrue("o11", jpl.field.equals(objF));
324         assertTrue("o12", ((Integer JavaDoc)jpl.field.get(testClass)).intValue() == 2);
325         assertTrue("o13", ((Integer JavaDoc)jpl.oldValue).intValue() == 1);
326         assertEquals("o14", new Integer JavaDoc(2),((Integer JavaDoc)jpl.newValue));
327
328         // access obj again
329
assertTrue("o15", testClass.obj.intValue() == 2);
330         assertTrue("o16", jpl.read == 2);
331         assertTrue("o17", jpl.write == 1);
332         assertTrue("o18", jpl.field.equals(objF));
333         assertTrue("o19", ((Integer JavaDoc)jpl.oldValue).intValue() == 2);
334
335         // unregister the listener and disable the joinpoints:
336
jpm.unregisterListener(jpl);
337
338         // write and read should not trigger events if
339
// unregistering successful
340
testClass.obj = new Integer JavaDoc(0);
341         assertTrue("o20", testClass.obj.intValue() == 0);
342         assertTrue("o21", jpl.read == 2); // should not have changed
343
assertTrue("o22", jpl.write == 1); // should not have changed
344
}
345
346       /**
347        * Test suite.
348        * @return test instance
349        */

350       public static
351       Test suite() {
352         return new TestSuite(FieldEventIntegrationTest.class);
353       }
354
355     }
356
357
358 //======================================================================
359
//
360
// $Log: FieldEventIntegrationTest.java,v $
361
// Revision 1.3 2004/05/12 17:26:51 anicoara
362
// Adapt Junit tests to 3.8.1 version and the new package structure
363
//
364
// Revision 1.1.1.1 2003/07/02 15:30:42 apopovic
365
// Imported from ETH Zurich
366
//
367
// Revision 1.2 2003/07/02 12:42:33 anicoara
368
// Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
369
//
370
// Revision 1.1 2003/05/05 14:02:32 popovici
371
// renaming from runes to prose
372
//
373
// Revision 1.12 2003/04/26 18:51:41 popovici
374
// 1 Bug fix which lead to a refactoring step:
375
// 1. the bug: 'JoinPointRequests' used to write to a static list, which survived a startup/teardown;
376
// now this list belongs to the JoinPointManager;
377
// 2. the refactoring: the JoinPointManager now creates (and shares state) with join-points.
378
//
379
// Revision 1.11 2003/04/17 15:15:01 popovici
380
// Extension->Aspect renaming
381
//
382
// Revision 1.10 2003/03/04 18:36:08 popovici
383
// Organization of imprts
384
//
385
// Revision 1.9 2003/03/04 11:25:56 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.8 2002/11/26 17:15:30 pschoch
392
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
393
// ProseSystem now owns and starts the Aspect interface.
394
// ProseSystem now containes a 'test' AspectManager
395
// AspectManager now owns the JoinPointManager.
396
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
397
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
398
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
399
// Documentation updated accordingly.
400
//
401
// Revision 1.7 2002/10/31 18:26:44 pschoch
402
// Capability of crosscutting Exceptions added to prose.
403
//
404
// Revision 1.6 2002/10/25 07:42:27 popovici
405
// Undo Chnages Phillippe
406
//
407
// Revision 1.4 2002/03/12 09:49:33 popovici
408
// Join Point listener now abstract class (performance reasons)
409
//
410
// Revision 1.3 2002/02/21 13:03:05 popovici
411
// Updated to new performance-optimized design: Crosscuts receive joinpoints, no Event notification, etc
412
//
413
// Revision 1.2 2002/02/05 11:20:26 smarkwal
414
// modifications to test JVMAI-based implementation
415
//
416
// Revision 1.1.1.1 2001/11/29 18:13:30 popovici
417
// Sources from runes
418
//
419
// Revision 1.1.2.6 2001/03/26 14:12:43 popovici
420
// 'asert' replaced in 2 places with 'assertEquals'
421
//
422
// Revision 1.1.2.5 2001/02/22 16:53:21 popovici
423
// ProseSystem.setup replaced with startup; teardown introduced
424
//
425
// Revision 1.1.2.4 2000/12/08 14:17:54 groos
426
// added test for observing access and modification of object fields.
427
//
428
// Revision 1.1.2.3 2000/11/23 13:11:39 groos
429
// Added test for access and modification of static fields.
430
//
431
// Revision 1.1.2.2 2000/11/22 16:42:19 groos
432
// Object initialisation for tests moved from 'testEventImpl()' to 'setUp()'.
433
//
434
// Revision 1.1.2.1 2000/11/21 14:41:07 groos
435
// initial revision.
436
//
437
Popular Tags