001: // $Id: JoinPointManagerTest.java,v 1.3 2004/05/12 17:26:52 anicoara Exp $
002: // =====================================================================
003: //
004: // (history at end)
005: //
006:
007: package ch.ethz.prose.engine;
008:
009: // used packages
010: import java.util.*;
011:
012: import junit.framework.*;
013: import ch.ethz.inf.iks.jvmai.jvmdi.MethodExecutionJoinPointImpl;
014: import ch.ethz.jvmai.*;
015:
016: /**
017: * JUnit testcase for class JoinPointManager (WhiteBox).
018: *
019: * @version $Revision: 1.3 $
020: * @author Andrei Popovici
021: */
022: public class JoinPointManagerTest extends TestCase {
023:
024: static class TestListener extends JoinPointListener {
025: public List receivedEvents = new Vector();
026:
027: public void joinPointReached(FieldAccessJoinPoint e) {
028: receivedEvents.add(e);
029: }
030:
031: public void joinPointReached(FieldModificationJoinPoint e) {
032: receivedEvents.add(e);
033: }
034:
035: public void joinPointReached(MethodEntryJoinPoint e) {
036: receivedEvents.add(e);
037: }
038:
039: public void joinPointReached(MethodExitJoinPoint e) {
040: receivedEvents.add(e);
041: }
042:
043: public void joinPointReached(ExceptionJoinPoint e) {
044: receivedEvents.add(e);
045: }
046:
047: public void joinPointReached(ExceptionCatchJoinPoint e) {
048: receivedEvents.add(e);
049: }
050:
051: public void joinPointReached(ConstructorJoinPoint e) {
052: receivedEvents.add(e);
053: }
054: }
055:
056: static class TestRequest extends JoinPointRequest {
057: public String getKind() {
058: return "";
059: }
060:
061: public int getMask() {
062: return 0;
063: }
064:
065: long mySignature;
066:
067: public TestRequest(long signature) {
068: super (null);
069: mySignature = signature;
070: }
071:
072: protected void setWatch(Object listeners) {
073: }
074:
075: protected void clearWatch() {
076: }
077:
078: public boolean equals(Object other) {
079: return ((other instanceof TestRequest) && (((TestRequest) other).mySignature == mySignature));
080: }
081:
082: public int hashCode() {
083: return (int) mySignature;
084: }
085: }
086:
087: /**
088: * Construct test with given name.
089: * @param name test name
090: */
091: public JoinPointManagerTest(String name) {
092: super (name);
093: }
094:
095: class VisibleJoinPointManager extends JoinPointManager {
096: public VisibleJoinPointManager(boolean ic,
097: JVMAspectInterface ai, boolean rev) {
098: super (ic, ai, rev);
099: }
100:
101: public Map getReq2listener() {
102: return req2listener;
103: }
104:
105: public Map getListener2Req() {
106: return listener2req;
107: }
108: }
109:
110: VisibleJoinPointManager jpMgr = null;
111:
112: JoinPointRequest jpr1 = null;
113: JoinPointRequest jpr2 = null;
114: JoinPointRequest jpr3 = null;
115: JoinPointListener jpList1 = null;
116: JoinPointListener jpList2 = null;
117: MethodEntryJoinPoint jp1 = null;
118: MethodEntryJoinPoint jp2 = null;
119:
120: static class TestMethodEntryJoinPoint extends
121: MethodExecutionJoinPointImpl implements
122: MethodEntryJoinPoint {
123: ListenerList llist;
124:
125: protected TestMethodEntryJoinPoint(ListenerList l) {
126: super (null, null);
127: llist = l;
128: }
129:
130: public Object getAopTag() {
131: return llist;
132: }
133:
134: public boolean equals(Object other) {
135: if (other instanceof TestMethodEntryJoinPoint)
136: return true;
137: else
138: return false;
139: }
140:
141: public int hashCode() {
142: return 0xDEADBEEF;
143: }
144: };
145:
146: /**
147: * Set up fixture.
148: */
149: protected void setUp() {
150: jpMgr = new VisibleJoinPointManager(false, null, true); // isConnected == false because not connectedToJVMAI!
151: jpr1 = new TestRequest(1000);
152: jpr2 = new TestRequest(2000);
153: jpr3 = new TestRequest(1000);
154: jpList1 = new TestListener();
155: jpList2 = new TestListener();
156:
157: // we register the following tupels:
158: // (jpr1,jpList1)
159: // (jpr2,jpList2)
160: // (jpr3,jpList2)
161: // the result shoud be
162: // jpr1,jpr3 -> listener 1,2
163: // jpr2 -> listener 2
164: // we expect to have a map of length 2 (sig2listeners)
165: jpMgr.registerListener(jpList1, jpr1);
166: jpMgr.registerListener(jpList2, jpr2);
167: jpMgr.registerListener(jpList2, jpr3);
168:
169: jp1 = new TestMethodEntryJoinPoint(
170: (ListenerList) ((VisibleJoinPointManager) jpMgr)
171: .getReq2listener().get(jpr1));
172: jp2 = new TestMethodEntryJoinPoint(
173: (ListenerList) ((VisibleJoinPointManager) jpMgr)
174: .getReq2listener().get(jpr2));
175: }
176:
177: public void testEvents() {
178: // check whether the test-classes created for this test are ok..
179: assertTrue(jpr1.equals(jpr3));
180: assertTrue(!jpr1.equals(jpr2));
181: }
182:
183: public void testRegisterListener() {
184: assertTrue(((VisibleJoinPointManager) jpMgr).getReq2listener()
185: .size() == 2);
186: assertTrue(((VisibleJoinPointManager) jpMgr).getListener2Req()
187: .size() == 2);
188: assertTrue(((Set) ((VisibleJoinPointManager) jpMgr)
189: .getListener2Req().get(jpList2)).size() == 2);
190: }
191:
192: public void testNotifyListeners() {
193: // we notify classes with jpe1; we expect the listeners 1 & 2 to be notified
194: // (to contain at least one event this event should be the same);
195: jpMgr.onMethodEntry(jp1);
196: assertEquals(((TestListener) jpList1).receivedEvents,
197: ((TestListener) jpList2).receivedEvents);
198: assertTrue(((TestListener) jpList1).receivedEvents.size() > 0);
199:
200: // clean the listeners's log
201: ((TestListener) jpList1).receivedEvents = new Vector();
202: ((TestListener) jpList2).receivedEvents = new Vector();
203:
204: // we notify with jpe2; only listener 2 should be notified
205: jpMgr.onMethodEntry(jp2);
206: assertTrue(((TestListener) jpList1).receivedEvents.isEmpty());
207: assertTrue(!((TestListener) jpList2).receivedEvents.isEmpty());
208: assertTrue(((TestListener) jpList2).receivedEvents.get(0)
209: .equals(jp2));
210: }
211:
212: public void testUnregisterListeners() {
213: // we deregister listener 1;
214: jpMgr.unregisterListener(jpList2);
215:
216: // we expect the length of the sig2listener mapping to be 1;
217: assertTrue(((VisibleJoinPointManager) jpMgr).getReq2listener()
218: .size() == 1);
219: assertTrue(((VisibleJoinPointManager) jpMgr).getListener2Req()
220: .size() == 1);
221: assertTrue(((Set) ((VisibleJoinPointManager) jpMgr)
222: .getListener2Req().get(jpList2)) == null);
223:
224: // we expect that the only listener remaining is listener 1 listening on
225: // events with signature 1000
226: // notifyEvents
227: jpMgr.onMethodEntry(jp1);
228: assertTrue(!((TestListener) jpList1).receivedEvents.isEmpty());
229: assertTrue(((TestListener) jpList2).receivedEvents.isEmpty());
230: }
231:
232: /**
233: * Test suite.
234: * @return test instance
235: */
236: public static Test suite() {
237: return new TestSuite(JoinPointManagerTest.class);
238: }
239: }
240:
241: //======================================================================
242: //
243: // $Log: JoinPointManagerTest.java,v $
244: // Revision 1.3 2004/05/12 17:26:52 anicoara
245: // Adapt Junit tests to 3.8.1 version and the new package structure
246: //
247: // Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
248: // Imported from ETH Zurich
249: //
250: // Revision 1.4 2003/07/02 12:42:35 anicoara
251: // Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
252: //
253: // Revision 1.3 2003/05/20 16:04:55 popovici
254: //
255: // New QueryManager replaces functionality in AspectManager (better Soc)
256: // New 'Surrogate' classes for usage in the QueryManager
257: // The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
258: //
259: // Revision 1.2 2003/05/05 17:46:16 popovici
260: // Refactorization step (runes->prose) cleanup
261: //
262: // Revision 1.1 2003/05/05 14:03:30 popovici
263: // renaming from runes to prose
264: //
265: // Revision 1.12 2003/04/17 15:15:18 popovici
266: // Extension->Aspect renaming
267: //
268: // Revision 1.11 2003/04/17 12:49:44 popovici
269: // Refactoring of the crosscut package
270: // ExceptionCut renamed to ThrowCut
271: // McutSignature is now SignaturePattern
272: //
273: // Revision 1.10 2003/04/17 08:46:54 popovici
274: // Important functionality additions
275: // - Cflow specializers
276: // - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
277: // - Transactional capabilities
278: // - Total refactoring of Specializer evaluation, which permits fine-grained distinction
279: // between static and dynamic specializers.
280: // - Functionality pulled up in abstract classes
281: // - Uniformization of advice methods patterns and names
282: //
283: // Revision 1.9 2003/03/04 18:36:42 popovici
284: // Organization of imprts
285: //
286: // Revision 1.8 2003/03/04 11:26:05 popovici
287: // Important refactorization step (march):
288: // - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
289: // - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
290: // structures
291: //
292: // Revision 1.7 2002/11/26 17:15:46 pschoch
293: // RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
294: // ProseSystem now owns and starts the Aspect interface.
295: // ProseSystem now containes a 'test' AspectManager
296: // AspectManager now owns the JoinPointManager.
297: // ExtensionManger can be 'connected' to the JVM, or disconnected. The
298: // JoinPointManager of a connected Ext.Mgr enables joinpoints; the
299: // JoinPointManger of a disconnected Ext.Mgr never enables join-points
300: // Documentation updated accordingly.
301: //
302: // Revision 1.6 2002/10/31 18:26:47 pschoch
303: // Capability of crosscutting Exceptions added to prose.
304: //
305: // Revision 1.5 2002/10/25 07:42:29 popovici
306: // Undo Chnages Phillippe
307: //
308: // Revision 1.3 2002/03/12 09:49:33 popovici
309: // Join Point listener now abstract class (performance reasons)
310: //
311: // Revision 1.2 2002/02/21 13:03:07 popovici
312: // Updated to new performance-optimized design: Crosscuts receive joinpoints, no Event notification, etc
313: //
314: // Revision 1.1 2002/02/05 11:16:16 smarkwal
315: // AbstractJoinPointManagerTest renamed to JoinPointManagerTest and changed to support new JVMAI-based JoinPointManager
316: //
317: // Revision 1.1.1.1 2001/11/29 18:13:33 popovici
318: // Sources from runes
319: //
320: // Revision 1.1.2.2 2001/11/22 09:42:25 popovici
321: // Indentation, minor non-semantic changes, message improvements,
322: // method name harmonization
323: //
324: // Revision 1.1.2.1 2000/10/30 15:58:15 popovici
325: // Initial Revision; Renaming from 'JoinPointMangerImpl'
326: //
327: // Revision 1.1.2.2 2000/10/24 18:10:13 popovici
328: // Minor documentation fix.
329: //
330: // Revision 1.1.2.1 2000/10/18 19:33:48 popovici
331: // Useless System.err.println removed.
332: //
333: // Revision 1.1 2000/10/16 11:42:53 popovici
334: // Initial Revision
335: //
|