KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: FieldNotificationTest.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;
8
9 //used packages
10
import java.lang.reflect.Field JavaDoc;
11
12 import junit.framework.*;
13 import ch.ethz.jvmai.FieldAccessJoinPoint;
14 import ch.ethz.jvmai.FieldModificationJoinPoint;
15 import ch.ethz.prose.crosscut.*;
16 import ch.ethz.prose.filter.PointCutter;
17 import ch.ethz.prose.filter.Within;
18
19
20 /**
21 * FieldNotificationTest. Used to find out if fields also get
22 * accessed during instantion time, even if not set to default value.
23 *
24 * @version $Revision: 1.3 $
25 * @author Gerard Roos
26 */

27 public class FieldNotificationTest extends TestCase {
28
29       // fixture
30
static final boolean WRITE = false;
31       static final boolean READ = true;
32
33       static class TestClass {
34         int initial;
35         int undefined;
36
37         int[] array;
38
39         Object JavaDoc o;
40
41         public TestClass()
42         {
43             fieldName = "o";
44             o = null;
45         }
46       }
47
48         public static class TestExtension extends DefaultAspect {
49
50         public Crosscut c1 = new GetCut()
51         {
52               public void GET_ARGS()
53               {
54                   try
55                   {
56                       Field JavaDoc f = ((FieldAccessJoinPoint)thisJoinPoint()).getField();
57                       assertTrue("unexpected read access to watched field " +
58                          f, f.getName().equals(fieldName));
59                       assertTrue("unexpected read access to watched field " +
60                          f + ", write access expected.",
61                          readwriteAccess == READ);
62                   }
63                   catch ( Exception JavaDoc e )
64                   {
65                       e.printStackTrace();
66                   }
67               }
68
69               protected PointCutter pointCutter() {
70                 return Within.type(TestClass.class);
71               }
72
73               // restrict to TestClass
74
protected Class JavaDoc[] potentialCrosscutClasses()
75               {
76                   Class JavaDoc[] result = {TestClass.class};
77                   return result;
78               }
79           };
80
81           public Crosscut c = new SetCut()
82           {
83               public void SET_ARGS()
84               {
85                   Field JavaDoc f = ((FieldModificationJoinPoint)thisJoinPoint()).getField();
86                   try {
87                     assertTrue("unexpected write access to watched field " +
88                            f, f.getName().equals(fieldName));
89                     assertTrue("unexpected write access to watched field " +
90                            f + ", read access expected.",
91                            readwriteAccess == WRITE);
92                   }
93                   catch ( Exception JavaDoc e ) {
94                     e.printStackTrace();
95                   }
96               }
97     
98               // restrict to TestClass
99
protected Class JavaDoc[] potentialCrosscutClasses()
100               {
101                   Class JavaDoc[] result = {TestClass.class};
102                   return result;
103               }
104     
105               protected PointCutter pointCutter() {
106                   return Within.type(TestClass.class);
107               }
108           };
109         }
110
111         TestClass test;
112
113         static String JavaDoc fieldName;
114         static boolean readwriteAccess;
115
116
117       /**
118        * Construct test with given name.
119        * @param name test name
120        */

121       public FieldNotificationTest(String JavaDoc name) {
122         super(name);
123       }
124
125
126       /**
127        * Set up fixture.
128        */

129       protected void setUp() {
130         try {
131           ProseSystem.startup();
132         }
133         catch ( Exception JavaDoc e ) {
134           Assert.fail("ProseSystem.startup() failed.");
135         }
136       }
137
138       /**
139        *
140        */

141       protected void tearDown() {
142         try {
143           ProseSystem.teardown();
144         }
145         catch ( Exception JavaDoc e ) {
146           Assert.fail("ProseSystem.teardown() failed.");
147         }
148       }
149
150
151       /**
152        *
153        */

154       public void testInstantiation() throws Exception JavaDoc {
155
156           try
157           {
158               ProseSystem.getAspectManager().insert(new TestExtension());
159
160               runTestInstantiation();
161           }
162           catch (RuntimeException JavaDoc e)
163           {
164               e.printStackTrace();
165               throw e;
166           }
167       }
168
169       protected void runTestInstantiation() {
170         int[] arr = { 1, 2, 3, 4, 5 };
171
172         readwriteAccess = WRITE;
173         fieldName = "initial";
174         test = new TestClass();
175
176         fieldName = "undefined";
177         test.undefined = -5;
178
179         fieldName = "o";
180         test.o = new Object JavaDoc();
181
182         fieldName = "array";
183         test.array = arr;
184
185         readwriteAccess = READ;
186         arr[0] = 0;
187
188         readwriteAccess = WRITE;
189         test.array = arr;
190
191         readwriteAccess = READ;
192         test.array[1] = 9;
193         test.array[3] = 1;
194         test.array[4] = 10;
195       }
196
197       /**
198        * Test suite.
199        * @return test instance
200        */

201       public static
202       Test suite() {
203         return new TestSuite(FieldNotificationTest.class);
204       }
205
206     }
207
208
209 //======================================================================
210
//
211
// $Log: FieldNotificationTest.java,v $
212
// Revision 1.3 2004/05/12 17:26:51 anicoara
213
// Adapt Junit tests to 3.8.1 version and the new package structure
214
//
215
// Revision 1.1.1.1 2003/07/02 15:30:42 apopovic
216
// Imported from ETH Zurich
217
//
218
// Revision 1.1 2003/05/05 14:02:28 popovici
219
// renaming from runes to prose
220
//
221
// Revision 1.13 2003/04/27 13:08:40 popovici
222
// Specializers renamed to PointCutter
223
//
224
// Revision 1.12 2003/04/17 15:15:03 popovici
225
// Extension->Aspect renaming
226
//
227
// Revision 1.11 2003/04/17 12:49:39 popovici
228
// Refactoring of the crosscut package
229
// ExceptionCut renamed to ThrowCut
230
// McutSignature is now SignaturePattern
231
//
232
// Revision 1.10 2003/04/17 08:46:43 popovici
233
// Important functionality additions
234
// - Cflow specializers
235
// - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
236
// - Transactional capabilities
237
// - Total refactoring of Specializer evaluation, which permits fine-grained distinction
238
// between static and dynamic specializers.
239
// - Functionality pulled up in abstract classes
240
// - Uniformization of advice methods patterns and names
241
//
242
// Revision 1.9 2003/03/04 18:36:10 popovici
243
// Organization of imprts
244
//
245
// Revision 1.8 2003/03/04 11:25:56 popovici
246
// Important refactorization step (march):
247
// - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
248
// - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
249
// structures
250
//
251
// Revision 1.7 2002/11/26 17:15:30 pschoch
252
// RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
253
// ProseSystem now owns and starts the Aspect interface.
254
// ProseSystem now containes a 'test' AspectManager
255
// AspectManager now owns the JoinPointManager.
256
// ExtensionManger can be 'connected' to the JVM, or disconnected. The
257
// JoinPointManager of a connected Ext.Mgr enables joinpoints; the
258
// JoinPointManger of a disconnected Ext.Mgr never enables join-points
259
// Documentation updated accordingly.
260
//
261
// Revision 1.6 2002/06/06 14:39:54 popovici
262
// Renamings: FunctionalCrosscut->MethodCut
263
// AllFields->SetCut
264
// SetCu.fieldModiticationAdvice -> SetCut.setAdvice
265
//
266
// Revision 1.5 2002/06/06 12:01:46 popovici
267
// fieldAccessAdvice removed from AllFields; tests and usage of AllFields's
268
// ability to intercept gets moved to 'GetCut'
269
// Minor bug fixes;
270
//
271
// Revision 1.4 2002/06/05 12:03:49 popovici
272
// thisJoinPoint() updated everywhere. The 'fieldModificationAdvice is now parameterless'; older implemnentations now
273
// use 'thisJoinPoint()'
274
//
275
// Revision 1.3 2002/02/21 13:03:05 popovici
276
// Updated to new performance-optimized design: Crosscuts receive joinpoints, no Event notification, etc
277
//
278
// Revision 1.2 2002/02/05 11:20:36 smarkwal
279
// modifications to test JVMAI-based implementation
280
//
281
// Revision 1.1.1.1 2001/11/29 18:13:30 popovici
282
// Sources from runes
283
//
284
// Revision 1.1.2.2 2001/02/22 16:51:56 popovici
285
// ProseSystem.setup replaced with startup; teardown introduced
286
//
287
// Revision 1.1.2.1 2001/01/17 21:25:17 groos
288
// initial revision.
289
//
290
Popular Tags