KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > query > SurrogateTest


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

7 package ch.ethz.prose.query;
8
9 // used packages
10
import java.lang.reflect.Field JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.util.TreeMap JavaDoc;
13
14 import junit.framework.*;
15 import ch.ethz.jvmai.*;
16 import ch.ethz.prose.*;
17 import ch.ethz.prose.crosscut.Crosscut;
18 import ch.ethz.prose.crosscut.ThrowCut;
19 import ch.ethz.prose.engine.*;
20 import ch.ethz.prose.filter.Exceptions;
21 import ch.ethz.prose.filter.PointCutter;
22
23 /**
24  * JUnit testcase for classes ClassSurrogate, MethodSurrogate, FieldSurogate
25  * and JoinPointRequestSurrogate.
26  *
27  * @version $Revision: 1.2 $
28  * @author Philippe Schoch
29  */

30 public
31 class SurrogateTest extends TestCase {
32
33   // fixture
34

35   public static abstract class MyTestClass
36   {
37     public static int fa;
38     public boolean fb;
39     public static String JavaDoc fc;
40     public AspectManager fd;
41     protected int[] fe;
42     private Object JavaDoc ff;
43
44     public static void ma() {}
45     public String JavaDoc mb(byte a, short b, int c, long d, float e, double f, boolean g) { return null; }
46     public static int mc(Object JavaDoc a, String JavaDoc b, AspectManager c, TreeMap JavaDoc d, boolean e, double[] f, Throwable JavaDoc[][][] g) { return 0; }
47     public abstract Class JavaDoc[] md();
48     protected void me() {}
49     private boolean mf() { return true; }
50   }
51
52   public static class MyExtension extends DefaultAspect
53   {
54     public Crosscut ca = new ThrowCut()
55     {
56       public void THROW_ARGS() { }
57       public Class JavaDoc[] potentialCrosscutClasses()
58     { Class JavaDoc[] result = { LinkageError JavaDoc.class,
59                  IllegalStateException JavaDoc.class,
60                  InstantiationError JavaDoc.class,
61                  NullPointerException JavaDoc.class,
62                  RuntimeException JavaDoc.class,
63                  ClassFormatError JavaDoc.class,
64                  UnknownError JavaDoc.class};
65     return result; }
66
67     protected PointCutter pointCutter()
68     { return ( (Exceptions.type("InstantiationError")) . OR (Exceptions.type("UnknownError")) );}
69     };
70
71     public Crosscut cb = new ThrowCut()
72     {
73       public void THROW_ARGS() { }
74       public Class JavaDoc[] potentialCrosscutClasses()
75         { Class JavaDoc[] result = { LinkageError JavaDoc.class,
76                      IllegalStateException JavaDoc.class,
77                      InstantiationError JavaDoc.class,
78                      NullPointerException JavaDoc.class,
79                      RuntimeException JavaDoc.class,
80                      ClassFormatError JavaDoc.class,
81                      UnknownError JavaDoc.class};
82         return result; }
83       protected PointCutter pointCutter()
84     { return ( (Exceptions.type("NullPointerException")) . OR (Exceptions.type("RuntimeException")) );}
85     };
86
87     public Crosscut cc = new ThrowCut()
88     {
89       public void THROW_ARGS() { }
90       public Class JavaDoc[] potentialCrosscutClasses()
91         { Class JavaDoc[] result = { LinkageError JavaDoc.class,
92                      IllegalStateException JavaDoc.class,
93                      InstantiationError JavaDoc.class,
94                      NullPointerException JavaDoc.class,
95                      RuntimeException JavaDoc.class,
96                      ClassFormatError JavaDoc.class,
97                      UnknownError JavaDoc.class}; return result; }
98       protected PointCutter pointCutter()
99       { return ( (Exceptions.type("ClassFormatError")) . OR (Exceptions.type("UnknownError")) );}
100     };
101   }
102
103
104   MyTestClass tstClsObj = null;
105   Class JavaDoc tstCls = null;
106   JVMAspectInterface ai = null;
107
108   /**
109    * Construct test with given name.
110    * @param name test name
111    */

112   public SurrogateTest(String JavaDoc name)
113   {
114     super(name);
115   }
116
117   /**
118    * Set up fixture.
119    */

120   protected
121   void setUp()
122     {
123       try
124     {
125       ProseSystem.startup();
126     }
127       catch (SystemStartupException e)
128     {
129       Assert.fail("could not start ProseSystem");
130     }
131       tstClsObj = new MyTestClass() { public Class JavaDoc[] md() { return null; } };
132       tstCls = SurrogateTest.MyTestClass.class;
133       ai = ProseSystem.getAspectManager().getJoinPointManager().getAspectInterface();
134
135     }
136
137   public
138   void tearDown()
139     {
140       try
141         {
142           ProseSystem.teardown();
143         }
144           catch (SystemTeardownException e)
145         {
146           Assert.fail("could not teardown ProseSystem");
147         }
148     }
149
150   // create a methodSurrogate and recreate the Method.
151
public void test0010_MethodSurrogate()
152     {
153       try
154         {
155           Method JavaDoc m;
156           MethodSurrogate sur;
157           Class JavaDoc[] paramTypes;
158
159           Method JavaDoc[] mArr = tstCls.getDeclaredMethods();
160           for (int j=0; j < mArr.length; j++)
161             {
162               m = mArr[j];
163               paramTypes = m.getParameterTypes();
164               sur = new MethodSurrogate(m);
165                   assertTrue("["+j+"] name of Surrogate and method are the same", sur.getName().equals(m.getName()));
166                   assertTrue("["+j+"] recreated method is still the same", sur.toRealInstance(tstCls.getName()).equals(m));
167             }
168         }
169       catch (Exception JavaDoc e)
170         {
171           e.printStackTrace();
172         }
173     }
174
175   // create a fieldSurrogate and recreate the Field.
176
public void test0020_FieldSurrogate()
177     {
178       try
179         {
180           Field JavaDoc f;
181           FieldSurrogate sur;
182           Class JavaDoc type;
183     
184           Field JavaDoc[] fArr = tstCls.getDeclaredFields();
185           for (int j=0; j < fArr.length; j++)
186             {
187               f = fArr[j];
188               type = f.getType();
189               sur = new FieldSurrogate(f);
190               assertTrue("["+j+"] name of Surrogate and field are the same", sur.getName().equals(f.getName()));
191               assertTrue("["+j+"] recreated field is still the same", sur.toRealInstance(tstCls.getName()).equals(f));
192             }
193         }
194       catch (Exception JavaDoc e)
195         {
196           e.printStackTrace();
197         }
198     }
199
200   // create a classSurrogate and recreate the Class.
201
public void test0030_ClassSurrogate()
202     {
203       try
204         {
205           Class JavaDoc c;
206           ClassSurrogate sur;
207
208           Class JavaDoc[] cArr = new Class JavaDoc[] {MyTestClass.class, MyExtension.class, SurrogateTest.class};
209           for (int j=0; j < cArr.length; j++)
210             {
211               c = cArr[j];
212               sur = new ClassSurrogate(c);
213               assertTrue(c.getName()+": class names are equal", sur.getName().equals(c.getName()));
214               assertTrue(c.getName()+" class is still the same", sur.toRealInstance().equals(c));
215             }
216         }
217       catch (Exception JavaDoc e)
218         {
219         e.printStackTrace();
220         }
221     }
222
223   // create JoinPointRequestSurrogates and recreate the JoinPointRequests.
224
public void test0040_JoinPointRequestSurrogate()
225     {
226       try
227         {
228       JoinPointManager jpm;
229       JoinPointRequest jpr;
230       JoinPointRequestSurrogate sur;
231       Method JavaDoc m; Field JavaDoc f; Class JavaDoc c;
232       jpm = ProseSystem.getAspectManager().getJoinPointManager();
233       QueryManager qm = new QueryManager(ProseSystem.getAspectManager());
234
235       Method JavaDoc[] mArr = tstCls.getDeclaredMethods();
236       for (int j=0; j < mArr.length; j++)
237         {
238           m = mArr[j];
239
240               jpr = (MethodEntryRequest)jpm.createJoinPointRequest(MethodEntryJoinPoint.KIND,m);
241           sur = new JoinPointRequestSurrogate(jpr);
242           assertTrue("["+j+"] recreated MethodEntryRequest is still the same",
243              qm.reconstructRequest(sur).equals(jpr));
244
245           jpr = (MethodExitRequest)jpm.createJoinPointRequest(MethodExitJoinPoint.KIND,m);
246           sur = new JoinPointRequestSurrogate(jpr);
247           assertTrue("["+j+"] recreated MethodExitRequest is still the same",
248              qm.reconstructRequest(sur).equals(jpr));
249         }
250
251       Field JavaDoc[] fArr = tstCls.getDeclaredFields();
252       for (int j=0; j < fArr.length; j++)
253         {
254           f = fArr[j];
255
256           jpr = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f);
257           sur = new JoinPointRequestSurrogate(jpr);
258           assertTrue("["+j+"] recreated FieldAccessRequest is still the same",
259              qm.reconstructRequest(sur).equals(jpr));
260
261           jpr = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f);
262           sur = new JoinPointRequestSurrogate(jpr);
263           assertTrue("["+j+"] recreated FieldModificationRequest is still the same",
264              qm.reconstructRequest(sur).equals(jpr));
265         }
266
267       Class JavaDoc[] cArr = new Class JavaDoc[] {Error JavaDoc.class, SystemStartupException.class, ArrayIndexOutOfBoundsException JavaDoc.class};
268       for (int j=0; j < cArr.length; j++)
269         {
270           c = cArr[j];
271
272           jpr = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,c);
273           sur = new JoinPointRequestSurrogate(jpr);
274           assertTrue("["+j+"] recreated ExceptionThrowRequest is still the same",
275              qm.reconstructRequest(sur).equals(jpr));
276
277         }
278     }
279       catch (Exception JavaDoc e)
280         {
281       e.printStackTrace();
282     }
283     }
284
285
286   public void test0050_equals()
287     {
288       Method JavaDoc m; Field JavaDoc f; Class JavaDoc c; Aspect ext = new MyExtension();
289       Crosscut cr = new ThrowCut()
290       {
291           public void THROW_ARGS() { }
292           public Class JavaDoc[] potentialCrosscutClasses()
293           { Class JavaDoc[] result = { LinkageError JavaDoc.class,
294                    IllegalStateException JavaDoc.class,
295                    InstantiationError JavaDoc.class,
296                    NullPointerException JavaDoc.class,
297                    RuntimeException JavaDoc.class,
298                    ClassFormatError JavaDoc.class,
299                    UnknownError JavaDoc.class}
300           ; return result; }
301           protected PointCutter pointCutter()
302           { return ( (Exceptions.type("InstantiationError")) . OR (Exceptions.type("UnknownError")) );}
303       };
304
305       JoinPointManager jpm = ProseSystem.getAspectManager().getJoinPointManager();
306       try
307         {
308       Class JavaDoc c1 = MyTestClass.class;
309       Class JavaDoc c2 = MyTestClass.class;
310       ClassSurrogate s1 = new ClassSurrogate(c1);
311       ClassSurrogate s2 = new ClassSurrogate(c2);
312       assertEquals("ClassSurrogate equals", s1, s2);
313
314           MethodSurrogate sur1, sur2;
315       Class JavaDoc[] paramTypes;
316       Method JavaDoc[] mArr = tstCls.getDeclaredMethods();
317       for (int j=0; j < mArr.length; j++)
318         {
319           m = mArr[j];
320           paramTypes = m.getParameterTypes();
321           sur1 = new MethodSurrogate(m);
322           sur2 = new MethodSurrogate(m);
323           assertEquals("MethodSurrogate equals", sur1, sur2);
324         }
325
326       FieldSurrogate fsur1, fsur2;
327       Class JavaDoc type;
328       Field JavaDoc[] fArr = tstCls.getDeclaredFields();
329       for (int j=0; j < fArr.length; j++)
330         {
331           f = fArr[j];
332           type = f.getType();
333           fsur1 = new FieldSurrogate(f);
334           fsur2 = new FieldSurrogate(f);
335           assertEquals("FieldSurrogate equals", fsur1, fsur2);
336         }
337
338       JoinPointRequest jpr;
339       JoinPointRequestSurrogate jsur1, jsur2;
340
341       mArr = tstCls.getDeclaredMethods();
342       for (int j=0; j < mArr.length; j++)
343         {
344           m = mArr[j];
345
346               jpr = (MethodEntryRequest)jpm.createJoinPointRequest(MethodEntryJoinPoint.KIND,m);
347           jsur1 = new JoinPointRequestSurrogate(jpr);
348           jsur2 = new JoinPointRequestSurrogate(jpr);
349           assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
350           assertEquals("Tupel equals"+jsur1.getKind(),
351                new Tuple(new AspectSurrogate(ext), new CrosscutSurrogate(new AspectSurrogate(ext),cr), jsur1),
352                new Tuple(new AspectSurrogate(ext), new CrosscutSurrogate(new AspectSurrogate(ext),cr), jsur2));
353
354           jpr = (MethodExitRequest)jpm.createJoinPointRequest(MethodExitJoinPoint.KIND,m);
355           jsur1 = new JoinPointRequestSurrogate(jpr);
356           jsur2 = new JoinPointRequestSurrogate(jpr);
357           assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
358         }
359
360       fArr = tstCls.getDeclaredFields();
361       for (int j=0; j < fArr.length; j++)
362         {
363           f = fArr[j];
364
365           jpr = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f);
366           jsur1 = new JoinPointRequestSurrogate(jpr);
367           jsur2 = new JoinPointRequestSurrogate(jpr);
368           assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
369
370           jpr = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f);
371           jsur1 = new JoinPointRequestSurrogate(jpr);
372           jsur2 = new JoinPointRequestSurrogate(jpr);
373           assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
374         }
375
376       Class JavaDoc[] cArr = new Class JavaDoc[] {Error JavaDoc.class, SystemStartupException.class, ArrayIndexOutOfBoundsException JavaDoc.class};
377       for (int j=0; j < cArr.length; j++)
378         {
379           c = cArr[j];
380
381           jpr = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,c);
382           jsur1 = new JoinPointRequestSurrogate(jpr);
383           jsur2 = new JoinPointRequestSurrogate(jpr);
384           assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
385         }
386     }
387       catch (Exception JavaDoc e)
388         {
389         e.printStackTrace();
390         }
391     }
392
393
394   /**
395    * Test suite.
396    * @return test instance
397    */

398   public static
399   Test suite()
400   {
401     return new TestSuite(SurrogateTest.class);
402   }
403
404 }
405
406
407 //======================================================================
408
//
409
// $Log: SurrogateTest.java,v $
410
// Revision 1.2 2004/05/12 17:26:52 anicoara
411
// Adapt Junit tests to 3.8.1 version and the new package structure
412
//
413
// Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
414
// Imported from ETH Zurich
415
//
416
// Revision 1.2 2003/05/20 16:04:57 popovici
417
//
418
// New QueryManager replaces functionality in AspectManager (better Soc)
419
// New 'Surrogate' classes for usage in the QueryManager
420
// The 'RemoteAspectManager' and tools modified to use the Surrogates and the QueryManager
421
//
422
// Revision 1.1 2003/05/05 14:03:42 popovici
423
// renaming from runes to prose
424
//
425
// Revision 1.10 2003/04/27 13:09:03 popovici
426
// Specializers renamed to PointCutter
427
//
428
// Revision 1.9 2003/04/26 18:51:46 popovici
429
// 1 Bug fix which lead to a refactoring step:
430
// 1. the bug: 'JoinPointRequests' used to write to a static list, which survived a startup/teardown;
431
// now this list belongs to the JoinPointManager;
432
// 2. the refactoring: the JoinPointManager now creates (and shares state) with join-points.
433
//
434
// Revision 1.8 2003/04/17 15:15:18 popovici
435
// Extension->Aspect renaming
436
//
437
// Revision 1.7 2003/04/17 14:51:16 popovici
438
// ExceptionS renamed to Exception; method renamings
439
//
440
// Revision 1.6 2003/04/17 12:49:45 popovici
441
// Refactoring of the crosscut package
442
// ExceptionCut renamed to ThrowCut
443
// McutSignature is now SignaturePattern
444
//
445
// Revision 1.5 2003/04/17 08:46:55 popovici
446
// Important functionality additions
447
// - Cflow specializers
448
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
449
// - Transactional capabilities
450
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
451
// between static and dynamic specializers.
452
// - Functionality pulled up in abstract classes
453
// - Uniformization of advice methods patterns and names
454
//
455
// Revision 1.4 2003/03/04 18:36:43 popovici
456
// Organization of imprts
457
//
458
// Revision 1.3 2003/03/04 11:26:06 popovici
459
// Important refactorization step (march):
460
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
461
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
462
// structures
463
//
464
// Revision 1.2 2003/01/27 12:52:23 pschoch
465
// Advice-Method now without parameter
466
//
467
// Revision 1.1 2003/01/17 17:19:28 pschoch
468
// Tests for Surrogate package
469
//
470
Popular Tags