KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > crosscut > AllFieldsTest


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

7 package ch.ethz.prose.crosscut;
8
9 //used packages
10
import junit.framework.*;
11 import ch.ethz.prose.DefaultAspect;
12 import ch.ethz.prose.ProseSystem;
13 import ch.ethz.prose.engine.JoinPointManager;
14 import ch.ethz.prose.filter.Fields;
15 import ch.ethz.prose.filter.PointCutter;
16
17 /**
18 * JUnit testcase for class AllFields.
19 *
20 * @version $Revision: 1.3 $
21 * @author Gerard Roos
22 */

23 public class AllFieldsTest extends TestCase {
24
25       // fixture
26

27        static boolean accessed;
28        static boolean modified;
29
30        public static class ExampleClass
31        {
32            public int publicF = 1;
33            protected boolean protectedF = true;
34            double packageF = 0.5;
35            volatile private String JavaDoc privateF = "geheim";
36
37            public String JavaDoc readPrivateF() { return privateF; }
38         };
39
40         public static class UserDefined1 extends DefaultAspect
41         {
42             public Crosscut c = new SetCut()
43             {
44                 public void SET_ARGS(ExampleClass cls, float x)
45                 { modified = true;} // should not match anything
46

47                 protected PointCutter pointCutter()
48                 {
49                     return null;
50                 }
51     
52             };
53             public Crosscut c2 = new GetCut()
54             {
55                 public void GET_ARGS(ExampleClass cls, float x)
56                 { accessed = true; } // should not match anythin
57

58                 protected PointCutter pointCutter()
59                 {
60                     return null;
61                 }
62     
63             };
64         }
65
66         public static class UserDefined2 extends DefaultAspect
67         {
68             public Crosscut c1 = new SetCut()
69             {
70                 public void SET_ARGS(ExampleClass cls, int x)
71                 { modified = true;}
72     
73                 protected PointCutter pointCutter()
74                 {
75                     return null;
76                 }
77     
78             };
79             public Crosscut c2 = new GetCut()
80             {
81                 public void GET_ARGS(ExampleClass cls, int x)
82                 { accessed = true;}
83     
84                 protected PointCutter pointCutter()
85                 {
86                     return null;
87                 }
88             };
89         }
90
91         public static class UserDefined3 extends DefaultAspect
92         {
93             public Crosscut c1 = new SetCut()
94             {
95                 public void SET_ARGS(AllFieldsTest cls, int x)
96                 { modified = true;} // should not touch modified
97

98                 protected PointCutter pointCutter()
99                 {
100                     return null;
101                 }
102             };
103     
104             public Crosscut c2 = new GetCut()
105             {
106                 public void GET_ARGS(AllFieldsTest cls, int x)
107                 { accessed = true;} // should not touch modified
108

109                 protected PointCutter pointCutter()
110                 {
111                     return null;
112                 }
113             };
114         }
115
116         public static class UserDefined4 extends DefaultAspect
117         {
118             public Crosscut c1 = new SetCut()
119                 {
120                 public void SET_ARGS(ANY cls, int x)
121                 { modified = true;} // should not touch modified
122

123                 protected PointCutter pointCutter()
124                 {
125                     return (Fields.declaredInClass(ExampleClass.class));
126                 }
127     
128     
129                 };
130     
131             public Crosscut c2 = new GetCut()
132             {
133                 public void GET_ARGS(ANY cls, int x)
134                 { accessed = true;} // should not touch modified
135

136                 protected PointCutter pointCutter()
137                 {
138                     return (Fields.declaredInClass(ExampleClass.class));
139                 }
140             };
141         }
142
143         public static class UserDefined5 extends DefaultAspect
144         {
145             public Crosscut c1 = new SetCut()
146             {
147                 public void SET_ARGS(ANY cls, ANY x)
148                 { modified = true;} // should not touch modified
149

150                 protected PointCutter pointCutter()
151                 {
152                     return (Fields.declaredInClass(ExampleClass.class));
153                 }
154     
155             };
156     
157             public Crosscut c2 = new GetCut()
158             {
159                 public void GET_ARGS(ANY cls, ANY x)
160                 { accessed = true;} // should not touch modified
161

162                 protected PointCutter pointCutter()
163                 {
164                     return (Fields.declaredInClass(ExampleClass.class));
165                 }
166             };
167         }
168
169         // class and crosscut MUST be public!!!
170
public static class TestExtension extends DefaultAspect {
171
172             public Crosscut c = new SetCut()
173             {
174                 public void SET_ARGS()
175                 {
176                     modified = true;
177                 }
178     
179                 protected PointCutter pointCutter()
180                 {
181                     return null;
182                 }
183     
184                 // restrict to ExampleClass
185
protected Class JavaDoc[] potentialCrosscutClasses() {
186                     Class JavaDoc[] result = {ExampleClass.class};
187                     return result;
188                 }
189             };
190     
191             public Crosscut c2 = new GetCut()
192             {
193                 public void GET_ARGS()
194                 {
195                     accessed = true;
196                 }
197                 protected PointCutter pointCutter()
198                 {
199                     return null;
200                 }
201     
202                 protected Class JavaDoc[] potentialCrosscutClasses()
203                 {
204                     Class JavaDoc[] result = {AllFieldsTest.ExampleClass.class};
205                     return result;
206                 }
207             };
208         }
209
210 // static class testSpecializer extends PointCutter {
211
// public boolean isSpecialRequest(JoinPointRequest evRec) {
212

213       ExampleClass ex;
214       SetCut testCrosscut = null;
215       JoinPointManager jpm;
216
217       /**
218        * Construct test with given name.
219        * @param name test name
220        */

221       public AllFieldsTest(String JavaDoc name) {
222         super(name);
223       }
224
225       /**
226        * Set up fixture.
227        */

228       protected void setUp()
229       {
230           try
231           {
232               ProseSystem.startup();
233           }
234           catch ( Exception JavaDoc e )
235           {
236               Assert.fail("ProseSystem.startup() failed.");
237           }
238           ex = new ExampleClass();
239       }
240
241       protected void tearDown()
242       {
243           try
244             { ProseSystem.teardown(); }
245               catch (Exception JavaDoc e)
246             { Assert.fail("ProseSystem.teardown() failed"); }
247       }
248
249       protected void getSetPublic() {
250         ex.publicF = ex.publicF + 1;
251       }
252
253       protected void getSetPackage() {
254         ex.packageF = ex.packageF + 1.0;
255       }
256       
257       
258         public void testUserDefined1() throws Exception JavaDoc
259         {
260             ProseSystem.getAspectManager().insert(new UserDefined1());
261             accessed = false; modified = false;
262             getSetPublic();
263             assertTrue("UserDefined1: !modified:",!modified);
264             assertTrue("UserDefined1: !accessed:",!accessed);
265         }
266
267         public void testUserDefined2() throws Exception JavaDoc
268         {
269             UserDefined2 aspect = new UserDefined2();
270             ProseSystem.getAspectManager().insert(aspect);
271     
272             accessed = false; modified = false;
273             getSetPublic();
274             assertTrue("UserDefined2.publicF: modified:",modified);
275             assertTrue("UserDefined2.publicF: accessed:",accessed);
276     
277             accessed = false; modified = false;
278             getSetPackage();
279             assertTrue("UserDefined2.packageF: !modified:",!modified);
280             assertTrue("UserDefined2.packageF: !accessed:",!accessed);
281         }
282
283         public void testUserDefined3() throws Exception JavaDoc
284         {
285             UserDefined3 aspect = new UserDefined3();
286             ProseSystem.getAspectManager().insert(aspect);
287     
288             accessed = false; modified = false;
289             getSetPublic();
290             assertTrue("UserDefined3.publicF: !modified:",!modified);
291             assertTrue("UserDefined3.publicF: !accessed:",!accessed);
292         }
293
294         public void testUserDefined4() throws Exception JavaDoc
295         {
296             UserDefined4 aspect = new UserDefined4();
297             ProseSystem.getAspectManager().insert(aspect);
298     
299             accessed = false; modified = false;
300             getSetPublic();
301             assertTrue("UserDefined4.publicF: modified:",modified);
302             assertTrue("UserDefined4.publicF: accessed:",accessed);
303     
304             accessed = false; modified = false;
305             getSetPackage();
306             assertTrue("UserDefined4.packageF: !modified:",!modified);
307             assertTrue("UserDefined4.packageF: !accessed:",!accessed);
308         }
309
310         public void testUserDefined5() throws Exception JavaDoc
311         {
312             UserDefined5 aspect = new UserDefined5();
313             ProseSystem.getAspectManager().insert(aspect);
314     
315             accessed = false; modified = false;
316             getSetPublic();
317             assertTrue("UserDefined5.publicF: modified:",modified);
318             assertTrue("UserDefined5.publicF: accessed:",accessed);
319     
320             accessed = false; modified = false;
321             getSetPackage();
322             assertTrue("UserDefined5.packageF: modified:",modified);
323             assertTrue("UserDefined5.packageF: accessed:",accessed);
324         }
325
326
327       public void testExtension() throws Exception JavaDoc {
328
329           try
330           {
331               ProseSystem.getAspectManager().insert(new TestExtension());
332               runTestExtension();
333           }
334           catch (RuntimeException JavaDoc e)
335           {
336               e.printStackTrace();
337               throw e;
338           }
339       }
340
341       protected void runTestExtension() {
342         // access public field:
343
accessed = false;
344         modified = false;
345         assertTrue(ex.publicF == 1);
346         assertTrue("1", accessed);
347         assertTrue("2", !modified);
348
349         // access protected field:
350
accessed = false;
351         modified = false;
352         assertTrue(ex.protectedF == true);
353         assertTrue("3", accessed);
354         assertTrue("4", !modified);
355
356         // access package field:
357
accessed = false;
358         modified = false;
359         assertTrue(ex.packageF == 0.5);
360         assertTrue("5", accessed);
361         assertTrue("6", !modified);
362
363         // modify public field:
364
accessed = false;
365         modified = false;
366         ex.publicF = 3;
367         assertTrue("9", !accessed);
368         assertTrue("10", modified);
369         assertTrue(ex.publicF == 3);
370
371         // modify protected field:
372
accessed = false;
373         modified = false;
374         ex.protectedF = false;
375         assertTrue("11", !accessed);
376         assertTrue("12", modified);
377         assertTrue(ex.protectedF == false);
378
379         // modify package field:
380
accessed = false;
381         modified = false;
382         ex.packageF = 9.9;
383         assertTrue("13", !accessed);
384         assertTrue("14", modified);
385         assertTrue(ex.packageF == 9.9);
386
387         // modify private field:
388
accessed = false;
389         modified = false;
390         ex.privateF = "foo";
391         assertTrue("15", !accessed);
392         assertTrue("16", modified);
393         assertTrue(ex.privateF == "foo");
394
395         // access and modify
396
accessed = false;
397         modified = false;
398         ex.publicF++;
399         assertTrue("17", accessed);
400         assertTrue("18", modified);
401         assertTrue(ex.publicF++ == 4);
402
403         // access private field:
404
accessed = false;
405         modified = false;
406         Object JavaDoc x = ex.privateF;
407         assertTrue("8", !modified);
408         assertTrue("7", accessed);
409       }
410
411       /**
412        * Test suite.
413        * @return test instance
414        */

415       public static Test suite() {
416         return new TestSuite(AllFieldsTest.class);
417       }
418
419     }
420
421
422 //======================================================================
423
//
424
// $Log: AllFieldsTest.java,v $
425
// Revision 1.3 2004/05/12 17:26:51 anicoara
426
// Adapt Junit tests to 3.8.1 version and the new package structure
427
//
428
// Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
429
// Imported from ETH Zurich
430
//
431
// Revision 1.1 2003/05/05 14:03:28 popovici
432
// renaming from runes to prose
433
//
434
// Revision 1.16 2003/04/27 13:08:55 popovici
435
// Specializers renamed to PointCutter
436
//
437
// Revision 1.15 2003/04/25 15:15:17 popovici
438
// FieldS renamed to 'Fields'
439
//
440
// Revision 1.14 2003/04/17 15:15:16 popovici
441
// Extension->Aspect renaming
442
//
443
// Revision 1.13 2003/04/17 12:49:42 popovici
444
// Refactoring of the crosscut package
445
// ExceptionCut renamed to ThrowCut
446
// McutSignature is now SignaturePattern
447
//
448
// Revision 1.12 2003/04/17 08:46:46 popovici
449
// Important functionality additions
450
// - Cflow specializers
451
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
452
// - Transactional capabilities
453
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
454
// between static and dynamic specializers.
455
// - Functionality pulled up in abstract classes
456
// - Uniformization of advice methods patterns and names
457
//
458
// Revision 1.11 2003/03/04 18:36:41 popovici
459
// Organization of imprts
460
//
461
// Revision 1.10 2003/03/04 11:25:58 popovici
462
// Important refactorization step (march):
463
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
464
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
465
// structures
466
//
467
// Revision 1.9 2002/11/27 17:09:33 popovici
468
// asertions more verbose, changes to show exaclty where the
469
// exception bug appears in 1.4.
470
//
471
// Revision 1.8 2002/11/26 17:15:35 pschoch
472
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
473
// ProseSystem now owns and starts the Aspect interface.
474
// ProseSystem now containes a 'test' AspectManager
475
// AspectManager now owns the JoinPointManager.
476
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
477
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
478
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
479
// Documentation updated accordingly.
480
//
481
// Revision 1.7 2002/10/26 07:19:12 popovici
482
// Changes for adapting prose to JDK 1.4:\
483
// More robust hashing for Method and fields in VM (used to be based on methodId only, now both class & methodId
484
// Bug fix in ByteCodeInfoImpl & DebuggerInfoInterface, where the wrong class was passed down
485
// to the ProseVM.
486
// Rewrote 'getFrameId' in prosevm.c. Used to work in 1.2, because 1.2 specific behavior. Now specification compliant.
487
//
488
// Revision 1.6 2002/06/06 14:39:49 popovici
489
// Renamings: FunctionalCrosscut->MethodCut
490
// AllFields->SetCut
491
// SetCu.fieldModiticationAdvice -> SetCut.setAdvice
492
//
493
// Revision 1.5 2002/06/06 12:01:47 popovici
494
// fieldAccessAdvice removed from AllFields; tests and usage of AllFields's
495
// ability to intercept gets moved to 'GetCut'
496
// Minor bug fixes;
497
//
498
// Revision 1.4 2002/06/05 12:03:49 popovici
499
// thisJoinPoint() updated everywhere. The 'fieldModificationAdvice is now parameterless'; older implemnentations now
500
// use 'thisJoinPoint()'
501
//
502
// Revision 1.3 2002/06/05 09:28:01 popovici
503
// thisJoinPoint() introduced in Abstract Crosscut and subclasses;
504
//
505
// Revision 1.2 2002/02/05 11:19:16 smarkwal
506
// modifications to test JVMAI-based implementation
507
//
508
// Revision 1.1.1.1 2001/11/29 18:13:31 popovici
509
// Sources from runes
510
//
511
// Revision 1.1.2.2 2001/02/22 16:51:17 popovici
512
// ProseSystem.setup replaced with startup; teardown introduced
513
//
514
// Revision 1.1.2.1 2000/11/28 16:56:39 groos
515
// Initial Revision.
516
//
517
Popular Tags