KickJava   Java API By Example, From Geeks To Geeks.

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


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

7 package ch.ethz.prose;
8
9 //used packages
10
import java.util.*;
11
12 import junit.framework.*;
13 import ch.ethz.jvmai.MethodEntryJoinPoint;
14 import ch.ethz.prose.crosscut.*;
15 import ch.ethz.prose.engine.JoinPointRequest;
16 import ch.ethz.prose.filter.PointCutter;
17 import ch.ethz.prose.filter.Within;
18
19 /**
20  * JUnit testcase// integration test for
21  * DefaultAspect,ProseSystem and Functional Crosscut.
22  *
23  * @version $Revision: 1.3 $
24  * @author Andrei Popovici */

25 public
26 class ExtensionInsertionTest extends TestCase {
27
28   // fixture
29
static class TestClass {
30     public void hello(String JavaDoc stuff)
31     {
32       int a =1;
33     }
34
35     public void byebye(String JavaDoc stuff, String JavaDoc stuf1)
36     {
37       int b =1;
38     }
39   }
40
41
42   static class TestClassWithInt {
43     int receivedI = 0;
44     public void hello(int i)
45     {
46         receivedI = i;
47     }
48   }
49
50   static class ModParExtension extends DefaultAspect
51   {
52     Crosscut myCC1 = new MethodCut()
53     {
54         public void METHOD_ARGS(TestClassWithInt receiver, int i)
55         {
56             if (thisJoinPoint() instanceof MethodEntryJoinPoint)
57                 ((MethodEntryJoinPoint)thisJoinPoint()).setArg(0,new Integer JavaDoc(20));
58         }
59     
60         protected PointCutter pointCutter()
61         {
62             return null;
63         }
64     };
65   }
66
67   static class NonPubExtension extends DefaultAspect
68   {
69     public boolean called = false;
70     Crosscut pc1 = new MethodCut()
71     {
72       public void METHOD_ARGS(TestClass cls, String JavaDoc stuff, REST params)
73       {
74         called = true;
75       }
76       protected PointCutter pointCutter() { return null;}
77     };
78   }
79
80
81
82   public static class SimpleExtension extends DefaultAspect
83   {
84     public static List lastArgumentCalls = new Vector();
85
86     public static class Crosscut1 extends MethodCut
87     {
88       public void METHOD_ARGS(TestClass cls, String JavaDoc stuff, REST params)
89         {
90           lastArgumentCalls.add("C1 received " + stuff);
91         }
92
93         protected PointCutter pointCutter() { return null;}
94     }
95
96     public static class Crosscut2 extends MethodCut
97     {
98       public void METHOD_ARGS(ANY thisObj,String JavaDoc stuff, String JavaDoc stuff1)
99       {
100         lastArgumentCalls.add("C2 received " + stuff);
101       }
102
103       protected PointCutter pointCutter() { return Within.method("byebye");}
104     }
105
106     Crosscut1 c1 = new Crosscut1();
107     Crosscut2 c2 = new Crosscut2();
108   }
109
110
111     static abstract class AbstractMethodCut extends MethodCut
112     {
113         public abstract void METHOD_ARGS(TestClass t, REST r);
114     
115         protected PointCutter pointCutter()
116         {
117             return Within.method("hello").OR(Within.method("byebye"));
118         }
119     };
120
121     static boolean concVisited = false;
122     static Vector visitedBy = new Vector();
123
124     static class ConcreteMethodCut1 extends AbstractMethodCut
125     {
126         public void METHOD_ARGS(TestClass t, REST r)
127         {
128             concVisited = true;
129             visitedBy.add(getOwner());
130         }
131     };
132
133     static class ConcreteMethodCut2 extends ConcreteMethodCut1
134     {
135         protected PointCutter pointCutter()
136         {
137             return Within.method("byebye"). AND(super.pointCutter());
138         }
139     };
140
141     static class ConcreteExtension extends DefaultAspect
142     {
143       Crosscut c = new ConcreteMethodCut1();
144     };
145
146     static class ConcreteExtension1 extends DefaultAspect
147     {
148         Crosscut c = new ConcreteMethodCut2();
149     };
150
151     TestClass testObject = null;
152     SimpleExtension testExtension = null;
153     NonPubExtension npExtension = null;
154     ConcreteExtension concExtension = null;
155     ConcreteExtension1 concExtension1 = null;
156
157    /**
158    * Construct test with given name.
159    * @param name test name
160    */

161   public ExtensionInsertionTest(String JavaDoc name)
162   {
163     super(name);
164   }
165
166
167   /**
168    * Set up fixture.
169    */

170   protected void setUp() {
171 // System.err.println("setting up" + this);
172
try {
173       ProseSystem.startup();
174     }
175     catch ( Exception JavaDoc e ) {
176         e.printStackTrace();
177           Assert.fail("ProseSystem.startup() failed.");
178     }
179     testExtension = new SimpleExtension();
180     npExtension = new NonPubExtension();
181     testObject = new TestClass();
182     concExtension = new ConcreteExtension();
183     concExtension1 = new ConcreteExtension1();
184   }
185
186   /**
187    *
188    */

189   protected void tearDown() {
190     try {
191       ProseSystem.teardown();
192     }
193     catch ( Exception JavaDoc e ) {
194         e.printStackTrace();
195     }
196   }
197
198
199
200   public void _testRegisterListener() throws Exception JavaDoc
201   {
202     try {
203         Iterator i1 = testExtension.c1.createRequest().iterator();
204         while (i1.hasNext())
205         {
206             JoinPointRequest crtReq = (JoinPointRequest)(i1.next());
207     
208             ProseSystem.getAspectManager().getJoinPointManager().registerListener(testExtension.c1,crtReq);
209         }
210
211         i1 = testExtension.c2.createRequest().iterator();
212         while (i1.hasNext())
213         {
214             JoinPointRequest crtReq = (JoinPointRequest)(i1.next());
215             ProseSystem.getAspectManager().getJoinPointManager().registerListener(testExtension.c1,crtReq);
216         }
217     }
218     catch (Exception JavaDoc e) {
219         e.printStackTrace();
220     }
221   }
222
223   /** this tests inserts our example extension and
224    * then calls 5 times each method of the example
225    * class. the first crosscut (C1) should receive
226    * 5 * 2 on behalf of hello and 5 *2 on behalf of
227    * byebye; the seccond crosscut would receive 5*2
228    * messages for byeye. We test that our crosscuts
229    * receive 30 messages.
230    */

231   public void testExtensionInsertion() throws Exception JavaDoc
232   {
233     try {
234         ProseSystem.getAspectManager().insert(testExtension);
235
236         for(int i=0; i < 5; i++)
237           testObject.hello("hello invocation [ " + i + " ]");
238
239         for(int i=5; i < 10; i++)
240           testObject.byebye("byebye invocation [" + i + "]","foo");
241
242         assertTrue("thirty notifications", SimpleExtension.lastArgumentCalls.size()==30);
243         ProseSystem.getAspectManager().withdraw(testExtension);
244     }
245     catch (Throwable JavaDoc e) {
246         e.printStackTrace();
247         throw (Exception JavaDoc)e;
248     }
249   }
250
251   /** Until recently, crosscuts had to be declared as public
252    * static local classes of extensions. This is no longer required.
253    * We test here that a real inner class is callable.
254    */

255   public void testInnerCrosscutInsertion() throws Exception JavaDoc
256   {
257     ProseSystem.getAspectManager().insert(npExtension);
258     testObject.byebye("byebye", "invocation");
259     assertTrue("Inner class method called", npExtension.called);
260     ProseSystem.getAspectManager().withdraw(npExtension);
261   }
262
263     /** This test checks whether after the call of the ProseSystem.teardown()
264      * method all joinpoints have been correctly cleaned up.
265      */

266     public void testTeardownQuality()
267     {
268         ProseSystem.getAspectManager().insert(npExtension);
269         try
270             { ProseSystem.teardown();}
271         catch (SystemTeardownException e)
272             { Assert.fail("ProseSystem.teardown() failed");}
273         testObject.byebye("byebye","invocation");
274         assertTrue("Inner class method not called",!npExtension.called);
275     }
276
277
278     public void testConcreteExtension()
279     {
280         ProseSystem.getAspectManager().insert(concExtension);
281         concVisited = false;
282         testObject.hello("");
283         assertTrue("conc was visted by hello",concVisited);
284         concVisited = false;
285         testObject.byebye("","");
286         assertTrue("conc was visted by bybye",concVisited);
287     }
288
289
290     public void testConcreteExtension1()
291     {
292         ProseSystem.getAspectManager().insert(concExtension1);
293         concVisited = false;
294         testObject.hello("");
295         assertTrue("conc was NOT visted by hello",!concVisited);
296         concVisited = false;
297         testObject.byebye("","");
298         assertTrue("conc was visted by bybye",concVisited);
299     }
300
301
302   public void testExtensionUniqueness()
303     {
304         ConcreteExtension ce1 = new ConcreteExtension();
305         ce1.associateTo("first");
306         ConcreteExtension ce2 = new ConcreteExtension();
307         ce2.associateTo("first");
308
309         ProseSystem.getAspectManager().insert(ce1);
310
311         // FIXME: this crashes the VM!
312
// if (true) throw new Error();
313
try {
314           ProseSystem.getAspectManager().insert(ce2); // should fail
315
Assert.fail("was able to insert second aspect with the same id");
316         }
317         catch (Throwable JavaDoc e)
318         {
319           // take a break
320
}
321     }
322
323   public void testParamModification()
324   {
325       ModParExtension mpe = new ModParExtension();
326       ProseSystem.getAspectManager().insert(mpe);
327       TestClassWithInt objWithInt = new TestClassWithInt();
328       objWithInt.hello(10);
329       assertEquals("ModParExtension has doubled the value",20,objWithInt.receivedI);
330   }
331
332   public void testNotificationOrder()
333   {
334       visitedBy = new Vector();
335       ConcreteExtension ce1 = new ConcreteExtension();
336       ce1.setPriority(1);
337       ce1.associateTo("first");
338       ConcreteExtension ce2 = new ConcreteExtension();
339       ce2.associateTo("second");
340       ce2.setPriority(2);
341
342       ProseSystem.getAspectManager().insert(ce1);
343       ProseSystem.getAspectManager().insert(ce2);
344       testObject.byebye("","");
345
346       Vector firstOrderVisitedBy = visitedBy;
347       visitedBy = new Vector();
348       ProseSystem.getAspectManager().withdraw(ce1);
349       ProseSystem.getAspectManager().withdraw(ce2);
350
351       ce1.setPriority(2);
352       ce2.setPriority(1);
353       ProseSystem.getAspectManager().insert(ce1);
354       ProseSystem.getAspectManager().insert(ce2);
355       testObject.byebye("","");
356
357
358       assertTrue("vectors differ in ordering",!firstOrderVisitedBy.equals(visitedBy));
359   }
360
361   /**
362    * Test suite.
363    * @return test instance
364    */

365   public static
366   Test suite()
367   {
368     return new TestSuite(ExtensionInsertionTest.class);
369   }
370
371 }
372
373
374 //======================================================================
375
//
376
// $Log: ExtensionInsertionTest.java,v $
377
// Revision 1.3 2004/05/12 17:26:50 anicoara
378
// Adapt Junit tests to 3.8.1 version and the new package structure
379
//
380
// Revision 1.1.1.1 2003/07/02 15:30:42 apopovic
381
// Imported from ETH Zurich
382
//
383
// Revision 1.2 2003/05/06 15:13:34 popovici
384
// aspectId renamed to 'associated object'
385
//
386
// Revision 1.1 2003/05/05 14:02:33 popovici
387
// renaming from runes to prose
388
//
389
// Revision 1.14 2003/04/30 14:50:10 popovici
390
// junit test for checking parameter modification added; tests for checking parameters modification (old) updated; they used to fail silently
391
//
392
// Revision 1.13 2003/04/29 12:41:07 popovici
393
// Feature added:
394
// - the 'setPriority' in class insertable allows now Aspects and Crosscuts to have a priority.
395
// Notitification is done from low int priorities to high int priorities.
396
// - the 'setAspectID' introduced to replace constuctor; used to be cumberstone for subclasses
397
//
398
// Revision 1.12 2003/04/27 13:08:40 popovici
399
// Specializers renamed to PointCutter
400
//
401
// Revision 1.11 2003/04/17 15:15:02 popovici
402
// Extension->Aspect renaming
403
//
404
// Revision 1.10 2003/04/17 13:54:34 popovici
405
// Refactorization of 'ExecutionS' into 'Within' and 'Executions'.
406
// Method names refer now to 'types'
407
//
408
// Revision 1.9 2003/04/17 12:49:38 popovici
409
// Refactoring of the crosscut package
410
// ExceptionCut renamed to ThrowCut
411
// McutSignature is now SignaturePattern
412
//
413
// Revision 1.8 2003/04/17 08:46:43 popovici
414
// Important functionality additions
415
// - Cflow specializers
416
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
417
// - Transactional capabilities
418
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
419
// between static and dynamic specializers.
420
// - Functionality pulled up in abstract classes
421
// - Uniformization of advice methods patterns and names
422
//
423
// Revision 1.7 2003/03/05 08:31:23 popovici
424
// Bug fix afeer import organization
425
//
426
// Revision 1.6 2003/03/04 18:36:07 popovici
427
// Organization of imprts
428
//
429
// Revision 1.5 2003/03/04 11:25:55 popovici
430
// Important refactorization step (march):
431
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
432
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
433
// structures
434
//
435
// Revision 1.4 2002/11/26 17:15:29 pschoch
436
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
437
// ProseSystem now owns and starts the Aspect interface.
438
// ProseSystem now containes a 'test' AspectManager
439
// AspectManager now owns the JoinPointManager.
440
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
441
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
442
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
443
// Documentation updated accordingly.
444
//
445
// Revision 1.3 2002/06/06 14:39:54 popovici
446
// Renamings: FunctionalCrosscut->MethodCut
447
// AllFields->SetCut
448
// SetCu.fieldModiticationAdvice -> SetCut.setAdvice
449
//
450
// Revision 1.2 2002/02/05 11:20:17 smarkwal
451
// modifications to test JVMAI-based implementation
452
//
453
// Revision 1.1.1.1 2001/11/29 18:13:30 popovici
454
// Sources from runes
455
//
456
// Revision 1.1.2.6 2001/06/01 12:17:12 popovici
457
// Methods 'hello' und 'byebye' now not-void.
458
//
459
// Revision 1.1.2.5 2001/03/23 12:32:23 popovici
460
// Minor correction in 'testTeardownQuality'. A call to the method
461
// to dispatch is now performed after teardown. The test used tu yield
462
// otherwise true by default.
463
//
464
// Revision 1.1.2.4 2001/02/22 16:56:06 popovici
465
// ProseSystem.setup replaced with startup; teardown introduced
466
// - test 'testTeardownQuality' introduced
467
//
468
// Revision 1.1.2.3 2001/02/07 12:03:41 popovici
469
// ProseSystem.insertExtension() now throws an exception. 'testInnerCrosscutInsert'
470
// adapted.
471
//
472
// Revision 1.1.2.2 2000/12/01 09:47:50 popovici
473
// Test 'testInnerCrosscutInsertion' added.
474
//
475
// Revision 1.1.2.1 2000/10/25 09:38:52 popovici
476
// Initial Revision
477
//
478
Popular Tags