001: // $Id: AllFieldsTest.java,v 1.3 2004/05/12 17:26:51 anicoara Exp $
002: // =====================================================================
003: //
004: // (history at end)
005: //
006:
007: package ch.ethz.prose.crosscut;
008:
009: //used packages
010: import junit.framework.*;
011: import ch.ethz.prose.DefaultAspect;
012: import ch.ethz.prose.ProseSystem;
013: import ch.ethz.prose.engine.JoinPointManager;
014: import ch.ethz.prose.filter.Fields;
015: import ch.ethz.prose.filter.PointCutter;
016:
017: /**
018: * JUnit testcase for class AllFields.
019: *
020: * @version $Revision: 1.3 $
021: * @author Gerard Roos
022: */
023: public class AllFieldsTest extends TestCase {
024:
025: static boolean accessed;
026: static boolean modified;
027:
028: public static class ExampleClass {
029: public int publicF = 1;
030: protected boolean protectedF = true;
031: double packageF = 0.5;
032: volatile private String privateF = "geheim";
033:
034: public String readPrivateF() {
035: return privateF;
036: }
037: };
038:
039: public static class UserDefined1 extends DefaultAspect {
040: public Crosscut c = new SetCut() {
041: public void SET_ARGS(ExampleClass cls, float x) {
042: modified = true;
043: } // should not match anything
044:
045: protected PointCutter pointCutter() {
046: return null;
047: }
048:
049: };
050: public Crosscut c2 = new GetCut() {
051: public void GET_ARGS(ExampleClass cls, float x) {
052: accessed = true;
053: } // should not match anythin
054:
055: protected PointCutter pointCutter() {
056: return null;
057: }
058:
059: };
060: }
061:
062: public static class UserDefined2 extends DefaultAspect {
063: public Crosscut c1 = new SetCut() {
064: public void SET_ARGS(ExampleClass cls, int x) {
065: modified = true;
066: }
067:
068: protected PointCutter pointCutter() {
069: return null;
070: }
071:
072: };
073: public Crosscut c2 = new GetCut() {
074: public void GET_ARGS(ExampleClass cls, int x) {
075: accessed = true;
076: }
077:
078: protected PointCutter pointCutter() {
079: return null;
080: }
081: };
082: }
083:
084: public static class UserDefined3 extends DefaultAspect {
085: public Crosscut c1 = new SetCut() {
086: public void SET_ARGS(AllFieldsTest cls, int x) {
087: modified = true;
088: } // should not touch modified
089:
090: protected PointCutter pointCutter() {
091: return null;
092: }
093: };
094:
095: public Crosscut c2 = new GetCut() {
096: public void GET_ARGS(AllFieldsTest cls, int x) {
097: accessed = true;
098: } // should not touch modified
099:
100: protected PointCutter pointCutter() {
101: return null;
102: }
103: };
104: }
105:
106: public static class UserDefined4 extends DefaultAspect {
107: public Crosscut c1 = new SetCut() {
108: public void SET_ARGS(ANY cls, int x) {
109: modified = true;
110: } // should not touch modified
111:
112: protected PointCutter pointCutter() {
113: return (Fields.declaredInClass(ExampleClass.class));
114: }
115:
116: };
117:
118: public Crosscut c2 = new GetCut() {
119: public void GET_ARGS(ANY cls, int x) {
120: accessed = true;
121: } // should not touch modified
122:
123: protected PointCutter pointCutter() {
124: return (Fields.declaredInClass(ExampleClass.class));
125: }
126: };
127: }
128:
129: public static class UserDefined5 extends DefaultAspect {
130: public Crosscut c1 = new SetCut() {
131: public void SET_ARGS(ANY cls, ANY x) {
132: modified = true;
133: } // should not touch modified
134:
135: protected PointCutter pointCutter() {
136: return (Fields.declaredInClass(ExampleClass.class));
137: }
138:
139: };
140:
141: public Crosscut c2 = new GetCut() {
142: public void GET_ARGS(ANY cls, ANY x) {
143: accessed = true;
144: } // should not touch modified
145:
146: protected PointCutter pointCutter() {
147: return (Fields.declaredInClass(ExampleClass.class));
148: }
149: };
150: }
151:
152: // class and crosscut MUST be public!!!
153: public static class TestExtension extends DefaultAspect {
154:
155: public Crosscut c = new SetCut() {
156: public void SET_ARGS() {
157: modified = true;
158: }
159:
160: protected PointCutter pointCutter() {
161: return null;
162: }
163:
164: // restrict to ExampleClass
165: protected Class[] potentialCrosscutClasses() {
166: Class[] result = { ExampleClass.class };
167: return result;
168: }
169: };
170:
171: public Crosscut c2 = new GetCut() {
172: public void GET_ARGS() {
173: accessed = true;
174: }
175:
176: protected PointCutter pointCutter() {
177: return null;
178: }
179:
180: protected Class[] potentialCrosscutClasses() {
181: Class[] result = { AllFieldsTest.ExampleClass.class };
182: return result;
183: }
184: };
185: }
186:
187: // static class testSpecializer extends PointCutter {
188: // public boolean isSpecialRequest(JoinPointRequest evRec) {
189:
190: ExampleClass ex;
191: SetCut testCrosscut = null;
192: JoinPointManager jpm;
193:
194: /**
195: * Construct test with given name.
196: * @param name test name
197: */
198: public AllFieldsTest(String name) {
199: super (name);
200: }
201:
202: /**
203: * Set up fixture.
204: */
205: protected void setUp() {
206: try {
207: ProseSystem.startup();
208: } catch (Exception e) {
209: Assert.fail("ProseSystem.startup() failed.");
210: }
211: ex = new ExampleClass();
212: }
213:
214: protected void tearDown() {
215: try {
216: ProseSystem.teardown();
217: } catch (Exception e) {
218: Assert.fail("ProseSystem.teardown() failed");
219: }
220: }
221:
222: protected void getSetPublic() {
223: ex.publicF = ex.publicF + 1;
224: }
225:
226: protected void getSetPackage() {
227: ex.packageF = ex.packageF + 1.0;
228: }
229:
230: public void testUserDefined1() throws Exception {
231: ProseSystem.getAspectManager().insert(new UserDefined1());
232: accessed = false;
233: modified = false;
234: getSetPublic();
235: assertTrue("UserDefined1: !modified:", !modified);
236: assertTrue("UserDefined1: !accessed:", !accessed);
237: }
238:
239: public void testUserDefined2() throws Exception {
240: UserDefined2 aspect = new UserDefined2();
241: ProseSystem.getAspectManager().insert(aspect);
242:
243: accessed = false;
244: modified = false;
245: getSetPublic();
246: assertTrue("UserDefined2.publicF: modified:", modified);
247: assertTrue("UserDefined2.publicF: accessed:", accessed);
248:
249: accessed = false;
250: modified = false;
251: getSetPackage();
252: assertTrue("UserDefined2.packageF: !modified:", !modified);
253: assertTrue("UserDefined2.packageF: !accessed:", !accessed);
254: }
255:
256: public void testUserDefined3() throws Exception {
257: UserDefined3 aspect = new UserDefined3();
258: ProseSystem.getAspectManager().insert(aspect);
259:
260: accessed = false;
261: modified = false;
262: getSetPublic();
263: assertTrue("UserDefined3.publicF: !modified:", !modified);
264: assertTrue("UserDefined3.publicF: !accessed:", !accessed);
265: }
266:
267: public void testUserDefined4() throws Exception {
268: UserDefined4 aspect = new UserDefined4();
269: ProseSystem.getAspectManager().insert(aspect);
270:
271: accessed = false;
272: modified = false;
273: getSetPublic();
274: assertTrue("UserDefined4.publicF: modified:", modified);
275: assertTrue("UserDefined4.publicF: accessed:", accessed);
276:
277: accessed = false;
278: modified = false;
279: getSetPackage();
280: assertTrue("UserDefined4.packageF: !modified:", !modified);
281: assertTrue("UserDefined4.packageF: !accessed:", !accessed);
282: }
283:
284: public void testUserDefined5() throws Exception {
285: UserDefined5 aspect = new UserDefined5();
286: ProseSystem.getAspectManager().insert(aspect);
287:
288: accessed = false;
289: modified = false;
290: getSetPublic();
291: assertTrue("UserDefined5.publicF: modified:", modified);
292: assertTrue("UserDefined5.publicF: accessed:", accessed);
293:
294: accessed = false;
295: modified = false;
296: getSetPackage();
297: assertTrue("UserDefined5.packageF: modified:", modified);
298: assertTrue("UserDefined5.packageF: accessed:", accessed);
299: }
300:
301: public void testExtension() throws Exception {
302:
303: try {
304: ProseSystem.getAspectManager().insert(new TestExtension());
305: runTestExtension();
306: } catch (RuntimeException e) {
307: e.printStackTrace();
308: throw e;
309: }
310: }
311:
312: protected void runTestExtension() {
313: // access public field:
314: accessed = false;
315: modified = false;
316: assertTrue(ex.publicF == 1);
317: assertTrue("1", accessed);
318: assertTrue("2", !modified);
319:
320: // access protected field:
321: accessed = false;
322: modified = false;
323: assertTrue(ex.protectedF == true);
324: assertTrue("3", accessed);
325: assertTrue("4", !modified);
326:
327: // access package field:
328: accessed = false;
329: modified = false;
330: assertTrue(ex.packageF == 0.5);
331: assertTrue("5", accessed);
332: assertTrue("6", !modified);
333:
334: // modify public field:
335: accessed = false;
336: modified = false;
337: ex.publicF = 3;
338: assertTrue("9", !accessed);
339: assertTrue("10", modified);
340: assertTrue(ex.publicF == 3);
341:
342: // modify protected field:
343: accessed = false;
344: modified = false;
345: ex.protectedF = false;
346: assertTrue("11", !accessed);
347: assertTrue("12", modified);
348: assertTrue(ex.protectedF == false);
349:
350: // modify package field:
351: accessed = false;
352: modified = false;
353: ex.packageF = 9.9;
354: assertTrue("13", !accessed);
355: assertTrue("14", modified);
356: assertTrue(ex.packageF == 9.9);
357:
358: // modify private field:
359: accessed = false;
360: modified = false;
361: ex.privateF = "foo";
362: assertTrue("15", !accessed);
363: assertTrue("16", modified);
364: assertTrue(ex.privateF == "foo");
365:
366: // access and modify
367: accessed = false;
368: modified = false;
369: ex.publicF++;
370: assertTrue("17", accessed);
371: assertTrue("18", modified);
372: assertTrue(ex.publicF++ == 4);
373:
374: // access private field:
375: accessed = false;
376: modified = false;
377: Object x = ex.privateF;
378: assertTrue("8", !modified);
379: assertTrue("7", accessed);
380: }
381:
382: /**
383: * Test suite.
384: * @return test instance
385: */
386: public static Test suite() {
387: return new TestSuite(AllFieldsTest.class);
388: }
389:
390: }
391:
392: //======================================================================
393: //
394: // $Log: AllFieldsTest.java,v $
395: // Revision 1.3 2004/05/12 17:26:51 anicoara
396: // Adapt Junit tests to 3.8.1 version and the new package structure
397: //
398: // Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
399: // Imported from ETH Zurich
400: //
401: // Revision 1.1 2003/05/05 14:03:28 popovici
402: // renaming from runes to prose
403: //
404: // Revision 1.16 2003/04/27 13:08:55 popovici
405: // Specializers renamed to PointCutter
406: //
407: // Revision 1.15 2003/04/25 15:15:17 popovici
408: // FieldS renamed to 'Fields'
409: //
410: // Revision 1.14 2003/04/17 15:15:16 popovici
411: // Extension->Aspect renaming
412: //
413: // Revision 1.13 2003/04/17 12:49:42 popovici
414: // Refactoring of the crosscut package
415: // ExceptionCut renamed to ThrowCut
416: // McutSignature is now SignaturePattern
417: //
418: // Revision 1.12 2003/04/17 08:46:46 popovici
419: // Important functionality additions
420: // - Cflow specializers
421: // - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
422: // - Transactional capabilities
423: // - Total refactoring of Specializer evaluation, which permits fine-grained distinction
424: // between static and dynamic specializers.
425: // - Functionality pulled up in abstract classes
426: // - Uniformization of advice methods patterns and names
427: //
428: // Revision 1.11 2003/03/04 18:36:41 popovici
429: // Organization of imprts
430: //
431: // Revision 1.10 2003/03/04 11:25:58 popovici
432: // Important refactorization step (march):
433: // - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
434: // - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
435: // structures
436: //
437: // Revision 1.9 2002/11/27 17:09:33 popovici
438: // asertions more verbose, changes to show exaclty where the
439: // exception bug appears in 1.4.
440: //
441: // Revision 1.8 2002/11/26 17:15:35 pschoch
442: // RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
443: // ProseSystem now owns and starts the Aspect interface.
444: // ProseSystem now containes a 'test' AspectManager
445: // AspectManager now owns the JoinPointManager.
446: // ExtensionManger can be 'connected' to the JVM, or disconnected. The
447: // JoinPointManager of a connected Ext.Mgr enables joinpoints; the
448: // JoinPointManger of a disconnected Ext.Mgr never enables join-points
449: // Documentation updated accordingly.
450: //
451: // Revision 1.7 2002/10/26 07:19:12 popovici
452: // Changes for adapting prose to JDK 1.4:\
453: // More robust hashing for Method and fields in VM (used to be based on methodId only, now both class & methodId
454: // Bug fix in ByteCodeInfoImpl & DebuggerInfoInterface, where the wrong class was passed down
455: // to the ProseVM.
456: // Rewrote 'getFrameId' in prosevm.c. Used to work in 1.2, because 1.2 specific behavior. Now specification compliant.
457: //
458: // Revision 1.6 2002/06/06 14:39:49 popovici
459: // Renamings: FunctionalCrosscut->MethodCut
460: // AllFields->SetCut
461: // SetCu.fieldModiticationAdvice -> SetCut.setAdvice
462: //
463: // Revision 1.5 2002/06/06 12:01:47 popovici
464: // fieldAccessAdvice removed from AllFields; tests and usage of AllFields's
465: // ability to intercept gets moved to 'GetCut'
466: // Minor bug fixes;
467: //
468: // Revision 1.4 2002/06/05 12:03:49 popovici
469: // thisJoinPoint() updated everywhere. The 'fieldModificationAdvice is now parameterless'; older implemnentations now
470: // use 'thisJoinPoint()'
471: //
472: // Revision 1.3 2002/06/05 09:28:01 popovici
473: // thisJoinPoint() introduced in Abstract Crosscut and subclasses;
474: //
475: // Revision 1.2 2002/02/05 11:19:16 smarkwal
476: // modifications to test JVMAI-based implementation
477: //
478: // Revision 1.1.1.1 2001/11/29 18:13:31 popovici
479: // Sources from runes
480: //
481: // Revision 1.1.2.2 2001/02/22 16:51:17 popovici
482: // ProseSystem.setup replaced with startup; teardown introduced
483: //
484: // Revision 1.1.2.1 2000/11/28 16:56:39 groos
485: // Initial Revision.
486: //
|