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