001: // $Id: FieldEventIntegrationTest.java,v 1.3 2004/05/12 17:26:51 anicoara Exp $
002: // =====================================================================
003: //
004: // (history at end)
005: //
006:
007: package ch.ethz.prose;
008:
009: // used packages
010: import java.lang.reflect.Field;
011:
012: import junit.framework.*;
013: import ch.ethz.jvmai.*;
014: import ch.ethz.prose.engine.*;
015:
016: /**
017: * JUnit testcase. This is an integration test (black box) and tests
018: * the functionality of the classes
019: * <ul>
020: * <li> FieldAccessRequestImpl
021: * <li> fieldModificationRequestImpl
022: * <li> FieldSignatureImpl
023: * <li> FieldAccessEventImpl
024: * <li> FieldModificationEventImpl
025: * <li> (FieldEventImpl)
026: * </ul>
027: * by accessing them through the following interfaces only:
028: * <ul>
029: * <li> FieldAccessRequest
030: * <li> FieldModificationRequest
031: * <li> FieldAccessEvent
032: * <li> FieldModificationEvent
033: * </ul>
034: *
035: * @version $Revision: 1.3 $
036: * @author Gerard Roos
037: */
038: public class FieldEventIntegrationTest extends TestCase {
039:
040: static class TestClass {
041: public int instanceField;
042: static String classField = "classField";
043:
044: Integer obj;
045:
046: public TestClass() {
047: }
048: }
049:
050: static class FieldListener extends JoinPointListener {
051: public int read;
052: public int write;
053:
054: public Field field;
055: public Object owner, oldValue, newValue;
056:
057: public FieldListener() {
058: }
059:
060: public void joinPointReached(FieldAccessJoinPoint e) {
061: field = null;
062: owner = null;
063: newValue = null;
064:
065: read++;
066: field = e.getField();
067: owner = e.getTarget();
068: try {
069: oldValue = field.get(owner);
070: } catch (Exception exc) {
071: Assert
072: .fail("Could not get oldValue on FieldAccessEvent");
073: }
074: }
075:
076: public void joinPointReached(FieldModificationJoinPoint e) {
077: write++;
078: field = e.getField();
079: owner = e.getTarget();
080: try {
081: oldValue = field.get(owner);
082: } catch (Exception exc) {
083: Assert
084: .fail("Could not get oldValue on FieldModificationEvent");
085: }
086: newValue = e.getNewValue();
087: }
088:
089: public void joinPointReached(MethodEntryJoinPoint jp) {
090: Assert
091: .fail("The event received in FieldListener should be a "
092: + "FieldAccessJoinPoint or FieldModificationJoinPoint!!");
093: }
094:
095: public void joinPointReached(MethodExitJoinPoint jp) {
096: Assert
097: .fail("The event received in FieldListener should be a "
098: + "FieldAccessJoinPoint or FieldModificationJoinPoint!!");
099: }
100:
101: public void joinPointReached(ExceptionJoinPoint jp) {
102: Assert
103: .fail("The event received in FieldListener should be a "
104: + "FieldAccessJoinPoint or FieldModificationJoinPoint!!");
105: }
106:
107: public void joinPointReached(ExceptionCatchJoinPoint jp) {
108: Assert
109: .fail("The event received in FieldListener should be a "
110: + "FieldAccessJoinPoint or FieldModificationJoinPoint!!");
111: }
112:
113: public void joinPointReached(ConstructorJoinPoint jp) {
114: Assert
115: .fail("The event received in FieldListener should be a "
116: + "FieldAccessJoinPoint or FieldModificationJoinPoint!!");
117: }
118: }
119:
120: JoinPointManager jpm;
121: TestClass testClass;
122: TestClass anotherTestClass;
123: Class c;
124: Field classF, instanceF, objF;
125: FieldAccessRequest accessInstReq, accessClsReq, accessObjReq;
126: FieldModificationRequest modInstReq, modClsReq, modObjReq;
127: FieldListener jpl;
128:
129: /**
130: * Construct test with given name.
131: * @param name test name
132: */
133: public FieldEventIntegrationTest(String name) {
134: super (name);
135: }
136:
137: /**
138: * Set up fixture.
139: */
140: protected void setUp() {
141: testClass = new TestClass();
142: anotherTestClass = new TestClass();
143: jpl = new FieldListener();
144:
145: try {
146: ProseSystem.startup();
147: } catch (SystemStartupException e) {
148: Assert.fail("SystemStartupException");
149: }
150:
151: c = testClass.getClass();
152: try {
153: instanceF = c.getDeclaredField("instanceField");
154: classF = c.getDeclaredField("classField");
155: objF = c.getDeclaredField("obj");
156: } catch (Exception e) {
157: Assert
158: .fail("Field \"instanceField\" or \"classField\" or \"obj\" "
159: + "not found in Class \"TestClass\".");
160: }
161:
162: jpm = ProseSystem.getAspectManager().getJoinPointManager();
163:
164: JVMAspectInterface aspectInterface = jpm.getAspectInterface();
165:
166: accessInstReq = (FieldAccessRequest) jpm
167: .createJoinPointRequest(FieldAccessJoinPoint.KIND,
168: instanceF);
169: modInstReq = (FieldModificationRequest) jpm
170: .createJoinPointRequest(
171: FieldModificationJoinPoint.KIND, instanceF);
172: accessClsReq = (FieldAccessRequest) jpm.createJoinPointRequest(
173: FieldAccessJoinPoint.KIND, classF);
174: modClsReq = (FieldModificationRequest) jpm
175: .createJoinPointRequest(
176: FieldModificationJoinPoint.KIND, classF);
177: accessObjReq = (FieldAccessRequest) jpm.createJoinPointRequest(
178: FieldAccessJoinPoint.KIND, objF);
179: modObjReq = (FieldModificationRequest) jpm
180: .createJoinPointRequest(
181: FieldModificationJoinPoint.KIND, objF);
182: }
183:
184: protected void tearDown() throws SystemTeardownException {
185: try {
186: ProseSystem.teardown();
187: } catch (Exception e) {
188: Assert.fail("ProseSystem.teardown() failed.");
189: }
190: }
191:
192: /**
193: * Accesses and modifies an instance field.
194: */
195: public void testInstanceField() throws Exception {
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) 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) jpl.oldValue).intValue() == 0);
226: assertTrue(((Integer) 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) 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: * Accesses and modifies a class (static) field.
250: */
251: public void testClassField() throws Exception {
252: // init
253: jpl.read = 0;
254: jpl.write = 0;
255: TestClass.classField = "initialized";
256: assertTrue(TestClass.classField.equals("initialized"));
257: assertTrue(jpl.read == 0);
258: assertTrue(jpl.write == 0);
259:
260: // register and enable the joinpoints:
261: jpm.registerListener(jpl, accessClsReq);
262: jpm.registerListener(jpl, modClsReq);
263:
264: // access classField
265: assertTrue(TestClass.classField.equals("initialized"));
266: assertTrue(jpl.read == 1);
267: assertTrue(jpl.write == 0);
268: assertTrue(jpl.field.equals(classF));
269: assertTrue(((String) jpl.oldValue).equals("initialized"));
270:
271: // write to classField
272: TestClass.classField = "modified";
273: assertTrue(jpl.read == 1);
274: assertTrue(jpl.write == 1);
275: assertTrue(jpl.field.equals(classF));
276: assertTrue(((String) jpl.oldValue).equals("initialized"));
277: assertEquals("modified", ((String) jpl.newValue));
278:
279: // access classField again
280: assertTrue(TestClass.classField.equals("modified"));
281: assertTrue(jpl.read == 2);
282: assertTrue(jpl.write == 1);
283: assertTrue(jpl.field.equals(classF));
284: assertTrue(((String) jpl.oldValue).equals("modified"));
285:
286: // unregister the listener and disable the joinpoints:
287: jpm.unregisterListener(jpl);
288:
289: // write and read should not trigger events if
290: // unregistering successful
291: TestClass.classField = "reset";
292: assertTrue(TestClass.classField.equals("reset"));
293: assertTrue(jpl.read == 2); // should not have changed
294: assertTrue(jpl.write == 1); // should not have changed
295: }
296:
297: public void testObjectField() throws Exception {
298: // init
299: jpl.read = 0;
300: jpl.write = 0;
301: testClass.obj = new Integer(1);
302: assertTrue("o1", testClass.obj.intValue() == 1);
303: assertTrue("o2", jpl.read == 0);
304: assertTrue("o3", jpl.write == 0);
305:
306: // register and enable the joinpoints:
307: jpm.registerListener(jpl, accessObjReq);
308: jpm.registerListener(jpl, modObjReq);
309:
310: // access obj
311: assertTrue("o4", testClass.obj.intValue() == 1);
312: assertTrue("o5", jpl.read == 1);
313: assertTrue("o6", jpl.write == 0);
314: assertTrue("o7", jpl.field.equals(objF));
315: assertTrue("o8", ((Integer) jpl.oldValue).intValue() == 1);
316:
317: // write to obj
318: testClass.obj = new Integer(2);
319: assertTrue("o9", jpl.read == 1);
320: assertTrue("o10", jpl.write == 1);
321: assertTrue("o11", jpl.field.equals(objF));
322: assertTrue("o12", ((Integer) jpl.field.get(testClass))
323: .intValue() == 2);
324: assertTrue("o13", ((Integer) jpl.oldValue).intValue() == 1);
325: assertEquals("o14", new Integer(2), ((Integer) jpl.newValue));
326:
327: // access obj again
328: assertTrue("o15", testClass.obj.intValue() == 2);
329: assertTrue("o16", jpl.read == 2);
330: assertTrue("o17", jpl.write == 1);
331: assertTrue("o18", jpl.field.equals(objF));
332: assertTrue("o19", ((Integer) jpl.oldValue).intValue() == 2);
333:
334: // unregister the listener and disable the joinpoints:
335: jpm.unregisterListener(jpl);
336:
337: // write and read should not trigger events if
338: // unregistering successful
339: testClass.obj = new Integer(0);
340: assertTrue("o20", testClass.obj.intValue() == 0);
341: assertTrue("o21", jpl.read == 2); // should not have changed
342: assertTrue("o22", jpl.write == 1); // should not have changed
343: }
344:
345: /**
346: * Test suite.
347: * @return test instance
348: */
349: public static Test suite() {
350: return new TestSuite(FieldEventIntegrationTest.class);
351: }
352:
353: }
354:
355: //======================================================================
356: //
357: // $Log: FieldEventIntegrationTest.java,v $
358: // Revision 1.3 2004/05/12 17:26:51 anicoara
359: // Adapt Junit tests to 3.8.1 version and the new package structure
360: //
361: // Revision 1.1.1.1 2003/07/02 15:30:42 apopovic
362: // Imported from ETH Zurich
363: //
364: // Revision 1.2 2003/07/02 12:42:33 anicoara
365: // Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
366: //
367: // Revision 1.1 2003/05/05 14:02:32 popovici
368: // renaming from runes to prose
369: //
370: // Revision 1.12 2003/04/26 18:51:41 popovici
371: // 1 Bug fix which lead to a refactoring step:
372: // 1. the bug: 'JoinPointRequests' used to write to a static list, which survived a startup/teardown;
373: // now this list belongs to the JoinPointManager;
374: // 2. the refactoring: the JoinPointManager now creates (and shares state) with join-points.
375: //
376: // Revision 1.11 2003/04/17 15:15:01 popovici
377: // Extension->Aspect renaming
378: //
379: // Revision 1.10 2003/03/04 18:36:08 popovici
380: // Organization of imprts
381: //
382: // Revision 1.9 2003/03/04 11:25:56 popovici
383: // Important refactorization step (march):
384: // - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
385: // - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
386: // structures
387: //
388: // Revision 1.8 2002/11/26 17:15:30 pschoch
389: // RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
390: // ProseSystem now owns and starts the Aspect interface.
391: // ProseSystem now containes a 'test' AspectManager
392: // AspectManager now owns the JoinPointManager.
393: // ExtensionManger can be 'connected' to the JVM, or disconnected. The
394: // JoinPointManager of a connected Ext.Mgr enables joinpoints; the
395: // JoinPointManger of a disconnected Ext.Mgr never enables join-points
396: // Documentation updated accordingly.
397: //
398: // Revision 1.7 2002/10/31 18:26:44 pschoch
399: // Capability of crosscutting Exceptions added to prose.
400: //
401: // Revision 1.6 2002/10/25 07:42:27 popovici
402: // Undo Chnages Phillippe
403: //
404: // Revision 1.4 2002/03/12 09:49:33 popovici
405: // Join Point listener now abstract class (performance reasons)
406: //
407: // Revision 1.3 2002/02/21 13:03:05 popovici
408: // Updated to new performance-optimized design: Crosscuts receive joinpoints, no Event notification, etc
409: //
410: // Revision 1.2 2002/02/05 11:20:26 smarkwal
411: // modifications to test JVMAI-based implementation
412: //
413: // Revision 1.1.1.1 2001/11/29 18:13:30 popovici
414: // Sources from runes
415: //
416: // Revision 1.1.2.6 2001/03/26 14:12:43 popovici
417: // 'asert' replaced in 2 places with 'assertEquals'
418: //
419: // Revision 1.1.2.5 2001/02/22 16:53:21 popovici
420: // ProseSystem.setup replaced with startup; teardown introduced
421: //
422: // Revision 1.1.2.4 2000/12/08 14:17:54 groos
423: // added test for observing access and modification of object fields.
424: //
425: // Revision 1.1.2.3 2000/11/23 13:11:39 groos
426: // Added test for access and modification of static fields.
427: //
428: // Revision 1.1.2.2 2000/11/22 16:42:19 groos
429: // Object initialisation for tests moved from 'testEventImpl()' to 'setUp()'.
430: //
431: // Revision 1.1.2.1 2000/11/21 14:41:07 groos
432: // initial revision.
433: //
|