001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test;
008:
009: import junit.framework.TestCase;
010:
011: import java.io.PrintStream;
012:
013: /**
014: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
015: */
016: public class FieldAdviceTest extends TestCase {
017: private static String s_logString = "";
018:
019: private static long s_setStaticFieldAroundAdviced = 0L;
020:
021: private static int s_setStaticFieldPreAdviced = 0;
022:
023: private static String s_setStaticFieldPostAdviced = "string";
024:
025: private static double s_setStaticFieldPrePostAdviced = 0.000D;
026:
027: private static long s_getStaticFieldAroundAdviced = 1L;
028:
029: private static int s_getStaticFieldPreAdviced = 1;
030:
031: private static String s_getStaticFieldPostAdviced = "string";
032:
033: private static double s_getStaticFieldPrePostAdviced = 1.1111D;
034:
035: private long m_setFieldAroundAdviced = 0L;
036:
037: private int m_setFieldAroundAdvicedWithNullAdvice = 0;
038:
039: private String m_setFieldAroundAdvicedObjectWithNullAdvice = new String(
040: "0");
041:
042: private String m_setFieldAroundAdvicedObjectWithAPI = new String(
043: "0");
044:
045: private int m_setFieldAroundAdvicedWithAPI = 0;
046:
047: private int m_setFieldPreAdviced = 0;
048:
049: private String m_setFieldPostAdviced = "string";
050:
051: private double m_setFieldPrePostAdviced = 0.000D;
052:
053: private long m_getFieldAroundAdviced = 1L;
054:
055: private String m_getFieldAroundAdvicedWithNullAdvice = "string";
056:
057: private double m_getFieldPreAdviced = 1.0000D;
058:
059: private int m_getFieldPostAdviced = 1;
060:
061: private int m_getFieldPrePostAdviced = 1;
062:
063: public FieldAdviceTest() {
064: }
065:
066: public FieldAdviceTest(String name) {
067: super (name);
068: }
069:
070: public void testSetMemberFieldAroundAdviced() {
071: s_logString = "";
072: try {
073: setFieldAroundAdviced();
074: assertEquals("before after ", s_logString);
075: assertEquals(187, m_setFieldAroundAdviced);
076: } catch (Exception e) {
077: fail();
078: }
079: }
080:
081: public void testSetMemberFieldAroundAdvicedWithNullAdvice() {
082: s_logString = "";
083: try {
084: setFieldAroundAdvicedWithNullAdvice();
085: assertEquals("before after ", s_logString);
086:
087: //CAUTION: null advice for @Set leave the assigned value
088: //The advice return value is ignored
089: assertEquals(187, m_setFieldAroundAdvicedWithNullAdvice);
090: } catch (Exception e) {
091: fail();
092: }
093: }
094:
095: public void testSetMemberFieldAroundAdvicedObjectWithNullAdvice() {
096: s_logString = "";
097: try {
098: setFieldAroundAdvicedObjectWithNullAdvice();
099: assertEquals("before after ", s_logString);
100:
101: //CAUTION: null advice for @Set leave the assigned value
102: //The advice return value is ignored
103: assertEquals("1",
104: m_setFieldAroundAdvicedObjectWithNullAdvice);
105: } catch (Exception e) {
106: fail();
107: }
108: }
109:
110: //FIXME - activate when proceed(args) will be supported
111:
112: // public void testSetMemberFieldAroundAdvicedObjectWithAPI() {
113: // s_logString = "";
114: // try {
115: // setFieldAroundAdvicedObjectWithAPI();
116: // assertEquals("before after ", s_logString);
117: //
118: // //The advice is using the Signature API to alter the assigned value
119: // assertEquals("byAdvice", m_setFieldAroundAdvicedObjectWithAPI);
120: // } catch (Exception e) {
121: // fail();
122: // }
123: // }
124: //
125: // public void testSetMemberFieldAroundAdvicedWithAPI() {
126: // s_logString = "";
127: // try {
128: // setFieldAroundAdvicedWithAPI();
129: // assertEquals("before after ", s_logString);
130: //
131: // //The advice is using the Signature API to alter the assigned value
132: // assertEquals(3, m_setFieldAroundAdvicedWithAPI);
133: // } catch (Exception e) {
134: // fail();
135: // }
136: // }
137:
138: public void testGetMemberFieldAroundAdviced() {
139: s_logString = "";
140: try {
141: long i = getFieldAroundAdviced(); // int default value
142: assertEquals("before after ", s_logString);
143: assertEquals(1L, i);
144: } catch (Exception e) {
145: fail();
146: }
147: }
148:
149: public void testGetMemberFieldAroundAdvicedWithNullAdvice() {
150: s_logString = "";
151: try {
152: String i = getFieldAroundAdvicedWithNullAdvice();
153: assertEquals("before after ", s_logString);
154: assertEquals(null, i);
155: } catch (Exception e) {
156: fail();
157: }
158: }
159:
160: public void testSetFieldPreAdviced() {
161: s_logString = "";
162: try {
163: setFieldPreAdviced();
164: assertEquals("pre1 pre2 ", s_logString);
165: } catch (Exception e) {
166: fail();
167: }
168: }
169:
170: public void testSetFieldPostAdviced() {
171: s_logString = "";
172: try {
173: setFieldPostAdviced();
174: assertEquals("post2 post1 ", s_logString);
175: } catch (Exception e) {
176: fail();
177: }
178: }
179:
180: public void testSetFieldPrePostAdviced() {
181: s_logString = "";
182: try {
183: setFieldPrePostAdviced();
184: assertEquals("pre1 pre2 post2 post1 ", s_logString);
185: } catch (Exception e) {
186: fail();
187: }
188: }
189:
190: public void testGetFieldPreAdviced() {
191: s_logString = "";
192: try {
193: getFieldPreAdviced();
194: assertEquals("pre1 pre2 ", s_logString);
195: } catch (Exception e) {
196: e.printStackTrace();
197: fail();
198: }
199: }
200:
201: public void testGetFieldPostAdviced() {
202: s_logString = "";
203: try {
204: getFieldPostAdviced();
205: assertEquals("post2 post1 ", s_logString);
206: } catch (Exception e) {
207: fail();
208: }
209: }
210:
211: public void testGetFieldPrePostAdviced() {
212: s_logString = "";
213: try {
214: getFieldPrePostAdviced();
215: assertEquals("pre1 pre2 post2 post1 ", s_logString);
216: } catch (Exception e) {
217: fail();
218: }
219: }
220:
221: public void testSetStaticFieldAroundAdviced() {
222: s_logString = "";
223: try {
224: setStaticFieldAroundAdviced();
225: assertEquals("before after ", s_logString);
226: assertEquals(3, s_setStaticFieldAroundAdviced);
227: } catch (Exception e) {
228: fail();
229: }
230: }
231:
232: public void testGetStaticFieldAroundAdviced() {
233: s_logString = "";
234: try {
235: long i = getStaticFieldAroundAdviced();
236: assertEquals("before after ", s_logString);
237: assertEquals(1L, i);
238: } catch (Exception e) {
239: fail();
240: }
241: }
242:
243: public void testSetStaticFieldPreAdviced() {
244: s_logString = "";
245: try {
246: setStaticFieldPreAdviced();
247: assertEquals("pre1 pre2 ", s_logString);
248: } catch (Exception e) {
249: fail();
250: }
251: }
252:
253: public void testSetStaticFieldPostAdviced() {
254: s_logString = "";
255: try {
256: setStaticFieldPostAdviced();
257: assertEquals("post2 post1 ", s_logString);
258: } catch (Exception e) {
259: fail();
260: }
261: }
262:
263: public void testSetStaticFieldPrePostAdviced() {
264: s_logString = "";
265: try {
266: setStaticFieldPrePostAdviced();
267: assertEquals("pre1 pre2 post2 post1 ", s_logString);
268: } catch (Exception e) {
269: fail();
270: }
271: }
272:
273: public void testGetStaticFieldPreAdviced() {
274: s_logString = "";
275: try {
276: getStaticFieldPreAdviced();
277: assertEquals("pre1 pre2 ", s_logString);
278: } catch (Exception e) {
279: fail();
280: }
281: }
282:
283: public void testGetStaticFieldPostAdviced() {
284: s_logString = "";
285: try {
286: getStaticFieldPostAdviced();
287: assertEquals("post2 post1 ", s_logString);
288: } catch (Exception e) {
289: fail();
290: }
291: }
292:
293: public void testStaticGetFieldPrePostAdviced() {
294: s_logString = "";
295: try {
296: getStaticFieldPrePostAdviced();
297: assertEquals("pre1 pre2 post2 post1 ", s_logString);
298: } catch (Exception e) {
299: fail();
300: }
301: }
302:
303: public void testPublicFieldOutOfWeaverScope() {
304: s_logString = "";
305: PrintStream out = System.out;//field get(* java.lang.System) && withincode ..
306: PrintStream err = System.err;
307: assertEquals("adviceOnPublicField ", s_logString);
308: }
309:
310: public static void main(String[] args) {
311: junit.textui.TestRunner.run(suite());
312: }
313:
314: public static junit.framework.Test suite() {
315: return new junit.framework.TestSuite(FieldAdviceTest.class);
316: }
317:
318: // ==== methods to test ====
319: public static void log(final String wasHere) {
320: s_logString += wasHere;
321: }
322:
323: public void setFieldAroundAdviced() {
324: m_setFieldAroundAdviced = 3 + (23 * 8);
325: }
326:
327: public void setFieldAroundAdvicedWithNullAdvice() {
328: m_setFieldAroundAdvicedWithNullAdvice = 3 + (23 * 8);
329: }
330:
331: public void setFieldAroundAdvicedObjectWithNullAdvice() {
332: m_setFieldAroundAdvicedObjectWithNullAdvice = new String("1");
333: }
334:
335: public void setFieldAroundAdvicedObjectWithAPI() {
336: m_setFieldAroundAdvicedObjectWithAPI = new String("original");
337: }
338:
339: public void setFieldAroundAdvicedWithAPI() {
340: m_setFieldAroundAdvicedWithAPI = 2;
341: }
342:
343: public void setFieldPreAdviced() {
344: m_setFieldPreAdviced = 3 + (23 * 8);
345: }
346:
347: public void setFieldPostAdviced() {
348: m_setFieldPostAdviced = "asdf";
349: }
350:
351: public void setFieldPrePostAdviced() {
352: m_setFieldPrePostAdviced = 3;
353: }
354:
355: public long getFieldAroundAdviced() {
356: return m_getFieldAroundAdviced;
357: }
358:
359: public String getFieldAroundAdvicedWithNullAdvice() {
360: return m_getFieldAroundAdvicedWithNullAdvice;
361: }
362:
363: public double getFieldPreAdviced() {
364: return m_getFieldPreAdviced;
365: }
366:
367: public int getFieldPostAdviced() {
368: return m_getFieldPostAdviced;
369: }
370:
371: public int getFieldPrePostAdviced() {
372: return m_getFieldPrePostAdviced;
373: }
374:
375: public static void setStaticFieldAroundAdviced() {
376: s_setStaticFieldAroundAdviced = 3;
377: }
378:
379: public static void setStaticFieldPreAdviced() {
380: s_setStaticFieldPreAdviced = 3;
381: }
382:
383: public static void setStaticFieldPostAdviced() {
384: s_setStaticFieldPostAdviced = "asdf";
385: }
386:
387: public static void setStaticFieldPrePostAdviced() {
388: s_setStaticFieldPrePostAdviced = 3;
389: }
390:
391: public static long getStaticFieldAroundAdviced() {
392: return s_getStaticFieldAroundAdviced;
393: }
394:
395: public static int getStaticFieldPreAdviced() {
396: return s_getStaticFieldPreAdviced;
397: }
398:
399: public static String getStaticFieldPostAdviced() {
400: return s_getStaticFieldPostAdviced;
401: }
402:
403: public static double getStaticFieldPrePostAdviced() {
404: return s_getStaticFieldPrePostAdviced;
405: }
406: }
|