KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > engine > JoinPointManagerTest


1 // $Id: JoinPointManagerTest.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.engine;
8
9 // used packages
10
import java.util.*;
11
12 import junit.framework.*;
13 import ch.ethz.inf.iks.jvmai.jvmdi.MethodExecutionJoinPointImpl;
14 import ch.ethz.jvmai.*;
15
16 /**
17  * JUnit testcase for class JoinPointManager (WhiteBox).
18  *
19  * @version $Revision: 1.3 $
20  * @author Andrei Popovici
21  */

22 public
23 class JoinPointManagerTest extends TestCase {
24
25     // fixture
26
static class TestListener extends JoinPointListener
27     {
28       public List receivedEvents = new Vector();
29       public void joinPointReached(FieldAccessJoinPoint e)
30       {
31         receivedEvents.add(e);
32       }
33
34       public void joinPointReached(FieldModificationJoinPoint e)
35       {
36         receivedEvents.add(e);
37       }
38
39       public void joinPointReached(MethodEntryJoinPoint e)
40       {
41         receivedEvents.add(e);
42       }
43
44       public void joinPointReached(MethodExitJoinPoint e)
45       {
46         receivedEvents.add(e);
47       }
48
49       public void joinPointReached(ExceptionJoinPoint e)
50       {
51         receivedEvents.add(e);
52       }
53
54       public void joinPointReached(ExceptionCatchJoinPoint e)
55       {
56         receivedEvents.add(e);
57       }
58       
59     }
60
61
62     static class TestRequest extends JoinPointRequest
63     {
64         public String JavaDoc getKind()
65         {
66             return "";
67         }
68         public int getMask()
69         {
70           return 0;
71         }
72         long mySignature;
73         public TestRequest(long signature)
74         {
75             super(null);
76             mySignature = signature;
77         }
78         protected void setWatch(Object JavaDoc listeners) { }
79         protected void clearWatch() { }
80         public boolean equals(Object JavaDoc other) { return ((other instanceof TestRequest) && (((TestRequest)other).mySignature==mySignature)); }
81         public int hashCode() { return (int)mySignature; }
82     }
83
84   /**
85    * Construct test with given name.
86    * @param name test name
87    */

88   public JoinPointManagerTest(String JavaDoc name)
89   {
90     super(name);
91   }
92
93
94   class VisibleJoinPointManager extends JoinPointManager
95   {
96     public VisibleJoinPointManager(boolean ic, JVMAspectInterface ai, boolean rev) {super(ic,ai,rev);}
97     public Map getReq2listener()
98       { return req2listener; }
99     public Map getListener2Req()
100       { return listener2req; }
101   }
102
103     VisibleJoinPointManager jpMgr = null;
104
105     JoinPointRequest jpr1 = null;
106     JoinPointRequest jpr2 = null;
107     JoinPointRequest jpr3 = null;
108     JoinPointListener jpList1 = null;
109     JoinPointListener jpList2 = null;
110     MethodEntryJoinPoint jp1 = null;
111     MethodEntryJoinPoint jp2 = null;
112     static class TestMethodEntryJoinPoint extends MethodExecutionJoinPointImpl implements MethodEntryJoinPoint
113     {
114         ListenerList llist;
115         protected TestMethodEntryJoinPoint(ListenerList l)
116         {
117             super(null,null);
118             llist=l;
119         }
120     
121         public Object JavaDoc getAopTag()
122         {
123             return llist;
124         }
125     
126           public boolean equals(Object JavaDoc other)
127         {
128           if (other instanceof TestMethodEntryJoinPoint)
129             return true;
130           else
131             return false;
132         }
133     
134           public int hashCode()
135         {
136           return 0xDEADBEEF;
137         }
138     };
139
140    /**
141    * Set up fixture.
142    */

143   protected
144   void setUp()
145   {
146     jpMgr = new VisibleJoinPointManager(false, null, true); // isConnected == false because not connectedToJVMAI!
147
jpr1 = new TestRequest(1000);
148     jpr2 = new TestRequest(2000);
149     jpr3 = new TestRequest(1000);
150     jpList1 = new TestListener();
151     jpList2 = new TestListener();
152
153     // we register the following tupels:
154
// (jpr1,jpList1)
155
// (jpr2,jpList2)
156
// (jpr3,jpList2)
157
// the result shoud be
158
// jpr1,jpr3 -> listener 1,2
159
// jpr2 -> listener 2
160
// we expect to have a map of length 2 (sig2listeners)
161
jpMgr.registerListener(jpList1,jpr1);
162     jpMgr.registerListener(jpList2,jpr2);
163     jpMgr.registerListener(jpList2,jpr3);
164
165
166     jp1 = new TestMethodEntryJoinPoint((ListenerList)((VisibleJoinPointManager)jpMgr).getReq2listener().get(jpr1));
167     jp2 = new TestMethodEntryJoinPoint((ListenerList)((VisibleJoinPointManager)jpMgr).getReq2listener().get(jpr2));
168   }
169
170     public void testEvents()
171     {
172     // check whether the test-classes created for this test are ok..
173
assertTrue(jpr1.equals(jpr3));
174     assertTrue(!jpr1.equals(jpr2));
175     }
176
177     public void testRegisterListener()
178     {
179       assertTrue(((VisibleJoinPointManager)jpMgr).getReq2listener().size() == 2);
180       assertTrue(((VisibleJoinPointManager)jpMgr).getListener2Req().size() == 2);
181       assertTrue(((Set)((VisibleJoinPointManager)jpMgr).getListener2Req().get(jpList2)).size() == 2);
182     }
183
184
185
186     public void testNotifyListeners()
187     {
188         // we notify classes with jpe1; we expect the listeners 1 & 2 to be notified
189
// (to contain at least one event this event should be the same);
190
jpMgr.onMethodEntry(jp1);
191         assertEquals(((TestListener)jpList1).receivedEvents,((TestListener)jpList2).receivedEvents);
192         assertTrue(((TestListener)jpList1).receivedEvents.size() > 0);
193     
194         // clean the listeners's log
195
((TestListener)jpList1).receivedEvents = new Vector();
196         ((TestListener)jpList2).receivedEvents = new Vector();
197     
198         // we notify with jpe2; only listener 2 should be notified
199
jpMgr.onMethodEntry(jp2);
200         assertTrue(((TestListener)jpList1).receivedEvents.isEmpty());
201         assertTrue(!((TestListener)jpList2).receivedEvents.isEmpty());
202         assertTrue(((TestListener)jpList2).receivedEvents.get(0).equals(jp2));
203     }
204
205     public void testUnregisterListeners()
206     {
207         // we deregister listener 1;
208
jpMgr.unregisterListener(jpList2);
209     
210         // we expect the length of the sig2listener mapping to be 1;
211
assertTrue(((VisibleJoinPointManager)jpMgr).getReq2listener().size() == 1);
212         assertTrue(((VisibleJoinPointManager)jpMgr).getListener2Req().size() == 1);
213             assertTrue(((Set)((VisibleJoinPointManager)jpMgr).getListener2Req().get(jpList2)) == null);
214     
215         // we expect that the only listener remaining is listener 1 listening on
216
// events with signature 1000
217
// notifyEvents
218
jpMgr.onMethodEntry(jp1);
219         assertTrue(!((TestListener)jpList1).receivedEvents.isEmpty());
220         assertTrue(((TestListener)jpList2).receivedEvents.isEmpty());
221     }
222
223   /**
224    * Test suite.
225    * @return test instance
226    */

227   public static
228   Test suite()
229   {
230     return new TestSuite(JoinPointManagerTest.class);
231   }
232 }
233
234 //======================================================================
235
//
236
// $Log: JoinPointManagerTest.java,v $
237
// Revision 1.3 2004/05/12 17:26:52 anicoara
238
// Adapt Junit tests to 3.8.1 version and the new package structure
239
//
240
// Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
241
// Imported from ETH Zurich
242
//
243
// Revision 1.4 2003/07/02 12:42:35 anicoara
244
// Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
245
//
246
// Revision 1.3 2003/05/20 16:04:55 popovici
247
//
248
// New QueryManager replaces functionality in AspectManager (better Soc)
249
// New 'Surrogate' classes for usage in the QueryManager
250
// The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
251
//
252
// Revision 1.2 2003/05/05 17:46:16 popovici
253
// Refactorization step (runes->prose) cleanup
254
//
255
// Revision 1.1 2003/05/05 14:03:30 popovici
256
// renaming from runes to prose
257
//
258
// Revision 1.12 2003/04/17 15:15:18 popovici
259
// Extension->Aspect renaming
260
//
261
// Revision 1.11 2003/04/17 12:49:44 popovici
262
// Refactoring of the crosscut package
263
// ExceptionCut renamed to ThrowCut
264
// McutSignature is now SignaturePattern
265
//
266
// Revision 1.10 2003/04/17 08:46:54 popovici
267
// Important functionality additions
268
// - Cflow specializers
269
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
270
// - Transactional capabilities
271
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
272
// between static and dynamic specializers.
273
// - Functionality pulled up in abstract classes
274
// - Uniformization of advice methods patterns and names
275
//
276
// Revision 1.9 2003/03/04 18:36:42 popovici
277
// Organization of imprts
278
//
279
// Revision 1.8 2003/03/04 11:26:05 popovici
280
// Important refactorization step (march):
281
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
282
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
283
// structures
284
//
285
// Revision 1.7 2002/11/26 17:15:46 pschoch
286
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
287
// ProseSystem now owns and starts the Aspect interface.
288
// ProseSystem now containes a 'test' AspectManager
289
// AspectManager now owns the JoinPointManager.
290
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
291
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
292
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
293
// Documentation updated accordingly.
294
//
295
// Revision 1.6 2002/10/31 18:26:47 pschoch
296
// Capability of crosscutting Exceptions added to prose.
297
//
298
// Revision 1.5 2002/10/25 07:42:29 popovici
299
// Undo Chnages Phillippe
300
//
301
// Revision 1.3 2002/03/12 09:49:33 popovici
302
// Join Point listener now abstract class (performance reasons)
303
//
304
// Revision 1.2 2002/02/21 13:03:07 popovici
305
// Updated to new performance-optimized design: Crosscuts receive joinpoints, no Event notification, etc
306
//
307
// Revision 1.1 2002/02/05 11:16:16 smarkwal
308
// AbstractJoinPointManagerTest renamed to JoinPointManagerTest and changed to support new JVMAI-based JoinPointManager
309
//
310
// Revision 1.1.1.1 2001/11/29 18:13:33 popovici
311
// Sources from runes
312
//
313
// Revision 1.1.2.2 2001/11/22 09:42:25 popovici
314
// Indentation, minor non-semantic changes, message improvements,
315
// method name harmonization
316
//
317
// Revision 1.1.2.1 2000/10/30 15:58:15 popovici
318
// Initial Revision; Renaming from 'JoinPointMangerImpl'
319
//
320
// Revision 1.1.2.2 2000/10/24 18:10:13 popovici
321
// Minor documentation fix.
322
//
323
// Revision 1.1.2.1 2000/10/18 19:33:48 popovici
324
// Useless System.err.println removed.
325
//
326
// Revision 1.1 2000/10/16 11:42:53 popovici
327
// Initial Revision
328
//
329
Popular Tags