KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > filter > CflowSTest


1 // $Id: CflowSTest.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.filter;
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.crosscut.*;
14
15 /**
16  * JUnit testcase for class XXX.
17  *
18  * @version $Revision: 1.2 $
19  * @author Andrei Popovici
20  */

21 public
22 class CflowSTest extends TestCase {
23
24     // a simple chaing of tree nested invocations
25
String JavaDoc ffoo;
26     String JavaDoc fbar;
27     int fbaz;
28
29     int alternative(int a)
30     {
31     bar("a" + a, "b" + a);
32     return 1;
33     }
34
35     int foo (String JavaDoc x, String JavaDoc y, String JavaDoc z)
36     {
37     bar (x+y,z);
38     ffoo=z;
39     return 1;
40
41     }
42
43     int bar(String JavaDoc x, String JavaDoc y)
44     {
45     baz(1,x+y);
46     fbar=x;
47     return 1;
48     }
49
50     Object JavaDoc baz(int x, Object JavaDoc y)
51     {
52     try
53         {
54         throw new IllegalStateException JavaDoc();
55         }
56     catch(IllegalStateException JavaDoc e)
57         {
58         fbaz=x;
59         }
60     return y;
61     }
62
63
64     public static MethodCut m1 = new MethodCut()
65     {
66         public void METHOD_ARGS(ANY x, REST y)
67         { }
68
69         protected PointCutter pointCutter()
70         {
71         return Within.method("foo");
72         }
73     };
74
75     public static MethodCut m2 = new MethodCut()
76     {
77         public void METHOD_ARGS(ANY x, REST y)
78         {}
79
80         protected PointCutter pointCutter()
81         {
82         return Within.method("nosuchMethod");
83         }
84
85     };
86
87     static boolean setBazCutViz = false;
88
89     public static class SetBazCut extends DefaultAspect
90     {
91         public Crosscut c1 = new SetCut()
92         {
93             public void SET_ARGS()
94             {setBazCutViz = true;}
95
96             protected PointCutter pointCutter()
97             {
98             return (Fields.named("fbaz")). AND
99                 (Within.method("baz")).AND
100                 (Cflow.below(m1));
101             }
102         };
103
104     };
105
106     public static class SetBazCut1 extends DefaultAspect
107     {
108         public Crosscut c1 = new SetCut()
109         {
110             public void SET_ARGS()
111             {
112             System.err.println("visiting the mood");
113             //new Exception("VTM").printStackTrace();
114
setBazCutViz = true;
115             }
116
117             protected PointCutter pointCutter()
118             {
119             return
120                 (Fields.named("fbaz")). AND
121                 (Within.method("baz")).AND
122                 (Cflow.below(m2));
123             }
124         };
125
126     }
127
128     SetBazCut setBazCut;
129     SetBazCut1 setBazCut1;
130
131   /**
132    * Construct test with given name.
133    * @param name test name
134    */

135   public CflowSTest(String JavaDoc name)
136   {
137     super(name);
138   }
139
140   /**
141    * Set up fixture.
142    */

143   protected
144   void setUp() throws Exception JavaDoc
145   {
146       try
147       {
148           ProseSystem.startup();
149           setBazCut = new SetBazCut();
150           setBazCut1 = new SetBazCut1();
151           setBazCutViz = false;
152       }
153        catch (Exception JavaDoc e)
154       {
155           e.printStackTrace();
156           throw e;
157       }
158   }
159
160     protected void
161     tearDown() throws Exception JavaDoc
162     {
163     try
164         {
165         ProseSystem.teardown();
166         }
167     catch (Exception JavaDoc e)
168         {
169           e.printStackTrace();
170           throw e;
171       }
172     }
173
174     public void testBelowFoo()
175     {
176
177
178     ProseSystem.getAspectManager().insert(setBazCut);
179     foo("a","b","c"); // should trigger the exectution of the advice
180
ProseSystem.getAspectManager().withdraw(setBazCut);
181     assertTrue("SetBazCut was visited since baz below foo",setBazCutViz);
182     }
183
184     public void testBelowNothing()
185     {
186     try
187         {
188         ProseSystem.getAspectManager().insert(setBazCut1);
189         foo("a","b","c");
190         ProseSystem.getAspectManager().withdraw(setBazCut1);
191         assertTrue("SetBazCut was not visited since baz below not below XXX",!setBazCutViz);
192         }
193     catch (RuntimeException JavaDoc e)
194         {
195         e.printStackTrace();
196         throw e;
197         }
198     }
199
200   /**
201    * Test suite.
202    * @return test instance
203    */

204   public static
205   Test suite()
206   {
207     return new TestSuite(CflowSTest.class);
208   }
209
210 }
211
212
213 //======================================================================
214
//
215
// $Log: CflowSTest.java,v $
216
// Revision 1.2 2004/05/12 17:26:52 anicoara
217
// Adapt Junit tests to 3.8.1 version and the new package structure
218
//
219
// Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
220
// Imported from ETH Zurich
221
//
222
// Revision 1.1 2003/05/05 14:03:01 popovici
223
// renaming from runes to prose
224
//
225
// Revision 1.7 2003/04/27 13:09:01 popovici
226
// Specializers renamed to PointCutter
227
//
228
// Revision 1.6 2003/04/25 15:15:12 popovici
229
// FieldS renamed to 'Fields'
230
//
231
// Revision 1.5 2003/04/17 15:14:59 popovici
232
// Extension->Aspect renaming
233
//
234
// Revision 1.4 2003/04/17 14:52:28 popovici
235
// CflowS renamed to Cflow
236
//
237
// Revision 1.3 2003/04/17 13:54:32 popovici
238
// Refactorization of 'ExecutionS' into 'Within' and 'Executions'.
239
// Method names refer now to 'types'
240
//
241
// Revision 1.2 2003/04/17 12:49:29 popovici
242
// Refactoring of the crosscut package
243
// ExceptionCut renamed to ThrowCut
244
// McutSignature is now SignaturePattern
245
//
246
// Revision 1.1 2003/04/17 08:46:50 popovici
247
// Important functionality additions
248
// - Cflow specializers
249
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
250
// - Transactional capabilities
251
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
252
// between static and dynamic specializers.
253
// - Functionality pulled up in abstract classes
254
// - Uniformization of advice methods patterns and names
255
//
256
Popular Tags