001: //$Id$
002: //=====================================================================
003: //
004: //(history at end)
005: //
006:
007: package ch.ethz.prose;
008:
009: import java.lang.reflect.Field;
010:
011: import junit.framework.TestCase;
012: import junit.framework.Test;
013: import junit.framework.TestSuite;
014: import junit.framework.Assert;
015: import ch.ethz.prose.crosscut.*;
016: import ch.ethz.prose.filter.PointCutter;
017: import ch.ethz.prose.filter.Within;
018:
019: /**
020: * JUnit test for method redefinition.
021: *
022: * @version $Revision$
023: * @author Angela Nicoara
024: */
025:
026: public class MethodRedefineTest extends TestCase {
027:
028: public MethodRedefineTest(String name) {
029: super (name);
030: }
031:
032: protected void setUp() throws SystemStartupException {
033: ProseSystem.startup();
034: }
035:
036: protected void tearDown() throws SystemTeardownException {
037: ProseSystem.teardown();
038: }
039:
040: public void test_010_basic_functionality() {
041: Point p = new Point();
042: DefaultAspect aspect = new SimpleMethodRedefineAspect();
043:
044: ProseSystem.getAspectManager().insert(aspect);
045: p.setX(42);
046: assertEquals("Field x is negative", -42, p.getX());
047:
048: ProseSystem.getAspectManager().withdraw(aspect);
049: p.setX(39);
050: assertEquals("Field x is set normally again", 39, p.getX());
051: }
052:
053: public void test_020_private_field_access() {
054: Point p = new Point();
055: DefaultAspect aspect = new PrivateFieldMethodRedefineAspect();
056:
057: ProseSystem.getAspectManager().insert(aspect);
058: p.setY(17);
059: assertEquals("Private field y is negative", -17, p.getY());
060:
061: ProseSystem.getAspectManager().withdraw(aspect);
062: p.setY(81);
063: assertEquals("Private field y is set normally again", 81, p
064: .getY());
065: }
066:
067: public void test_030_redefine_simple_by_complex_method() {
068: Pojo p = new Pojo();
069: DefaultAspect aspect = new SimpleToComplexMethodRedefineAspect();
070:
071: ProseSystem.getAspectManager().insert(aspect);
072: p.simple();
073: assertTrue("Simple method was redefined by complex",
074: p.calledAndRedefined);
075: }
076:
077: public void test_040_redefine_complex_by_simple_method() {
078: Pojo p = new Pojo();
079: DefaultAspect aspect = new ComplexToSimpleMethodRedefineAspect();
080:
081: ProseSystem.getAspectManager().insert(aspect);
082: p.complex("Hello");
083: assertTrue("Complex method was redefined by simple",
084: p.calledAndRedefined);
085: }
086:
087: public void test_050_redefine_with_rest() {
088: Pojo p = new Pojo();
089: DefaultAspect aspect = new RestMethodRedefineAspect();
090:
091: ProseSystem.getAspectManager().insert(aspect);
092: p.one_argument(42);
093: assertTrue("Pojo.simple() was redefined", p.calledAndRedefined);
094: }
095:
096: public void test_060_redefine_with_return_type() {
097: Pojo p = new Pojo();
098: DefaultAspect aspect = new ReturnTypeMethodRedefineAspect();
099:
100: ProseSystem.getAspectManager().insert(aspect);
101:
102: p.calledAndRedefined = false;
103: p.simple();
104: assertTrue("Pojo.simple() was not redefined",
105: !p.calledAndRedefined);
106:
107: p.calledAndRedefined = false;
108: p.simpleInt();
109: assertTrue("Pojo.simpleInt() was not redefined",
110: !p.calledAndRedefined);
111:
112: p.calledAndRedefined = false;
113: p.simpleString();
114: assertTrue("Pojo.simpleString() was redefined",
115: p.calledAndRedefined);
116:
117: p.calledAndRedefined = false;
118: p.simpleObject();
119: assertTrue("Pojo.simpleObject() was redefined",
120: p.calledAndRedefined);
121: }
122:
123: class SimpleMethodRedefineAspect extends DefaultAspect {
124:
125: public Crosscut c1 = new MethodRedefineCut() {
126:
127: public void METHOD_ARGS(Point target, int x) {
128: target.x = -x;
129: }
130:
131: protected PointCutter pointCutter() {
132: return Within.method("setX");
133: }
134:
135: };
136:
137: }
138:
139: class PrivateFieldMethodRedefineAspect extends DefaultAspect {
140:
141: public Crosscut c1 = new MethodRedefineCut() {
142:
143: public void METHOD_ARGS(Point target, int x) {
144: try {
145: Class point_class = target.getClass();
146: Field y_field = point_class.getDeclaredFields()[1];
147:
148: y_field.setInt(target, -x);
149: } catch (Exception e) {
150: throw new RuntimeException("Oops.", e);
151: }
152: }
153:
154: protected PointCutter pointCutter() {
155: return Within.method("setY");
156: }
157:
158: };
159:
160: }
161:
162: class Pojo {
163:
164: boolean calledAndRedefined;
165:
166: void simple() {
167: }
168:
169: int simpleInt() {
170: return 1;
171: }
172:
173: String simpleString() {
174: return null;
175: }
176:
177: Object simpleObject() {
178: return null;
179: }
180:
181: String complex(String arg) {
182: try {
183: int i = 17;
184: i++;
185: String msg = "Message " + i + " from " + this + ": "
186: + arg;
187: throw new RuntimeException(msg);
188: } catch (RuntimeException e) {
189: e.getStackTrace();
190: }
191: return "Ignore";
192: }
193:
194: void one_argument(int x) {
195: }
196:
197: }
198:
199: class ComplexToSimpleMethodRedefineAspect extends DefaultAspect {
200:
201: public Crosscut c1 = new MethodRedefineCut() {
202:
203: public String METHOD_ARGS(Pojo target, String x) {
204: target.calledAndRedefined = true;
205: return "Ignore, too";
206: }
207:
208: protected PointCutter pointCutter() {
209: return Within.method("complex");
210: }
211:
212: };
213:
214: }
215:
216: class SimpleToComplexMethodRedefineAspect extends DefaultAspect {
217:
218: public Crosscut c1 = new MethodRedefineCut() {
219:
220: public void METHOD_ARGS(Pojo target) {
221: try {
222: int i = 17;
223: i++;
224: String msg = "Message " + i + " from " + target;
225: throw new RuntimeException(msg);
226: } catch (RuntimeException e) {
227: int j = 34;
228: if (j == 17)
229: j--;
230: else
231: j++;
232:
233: }
234:
235: target.calledAndRedefined = true;
236: }
237:
238: protected PointCutter pointCutter() {
239: return Within.method("simple");
240: }
241:
242: };
243:
244: }
245:
246: class RestMethodRedefineAspect extends DefaultAspect {
247:
248: public Crosscut c1 = new MethodRedefineCut() {
249:
250: public void METHOD_ARGS(Pojo target, int x, REST y) {
251: target.calledAndRedefined = true;
252: }
253:
254: protected PointCutter pointCutter() {
255: return Within.method("one_argument");
256: }
257:
258: };
259:
260: }
261:
262: class ReturnTypeMethodRedefineAspect extends DefaultAspect {
263:
264: public Crosscut c1 = new MethodRedefineCut() {
265:
266: public String METHOD_ARGS(Pojo target) {
267: target.calledAndRedefined = true;
268:
269: return null;
270: }
271:
272: protected PointCutter pointCutter() {
273: return Within.method("simple*");
274: }
275:
276: };
277:
278: }
279:
280: /**
281: * Test suite.
282: * @return test instance
283: */
284: public static Test suite() {
285: return new TestSuite(MethodRedefineTest.class);
286: }
287:
288: }
289:
290: //======================================================================
291: //
292: // $Log$
293: //
|