1 package ch.ethz.prose.jikesrvm; 2 3 import java.lang.reflect.Field ; 4 5 import junit.framework.TestCase; 6 import ch.ethz.prose.*; 7 import ch.ethz.prose.crosscut.*; 8 import ch.ethz.prose.filter.PointCutter; 9 import ch.ethz.prose.filter.Within; 10 11 public class MethodRedefineTest extends TestCase { 12 13 public MethodRedefineTest(String name) { 14 super(name); 15 } 16 17 protected void setUp() throws SystemStartupException { 18 ProseSystem.startup(); 19 } 20 21 protected void tearDown() throws SystemTeardownException { 22 ProseSystem.teardown(); 23 } 24 25 public void test_010_basic_functionality() { 26 Point p = new Point(); 27 DefaultAspect aspect = new SimpleMethodRedefineAspect(); 28 29 ProseSystem.getAspectManager().insert(aspect); 30 p.setX(42); 31 assertEquals("Field x is negative", -42, p.getX()); 32 33 ProseSystem.getAspectManager().withdraw(aspect); 34 p.setX(39); 35 assertEquals("Field x is set normally again", 39, p.getX()); 36 } 37 38 public void test_020_private_field_access() { 39 Point p = new Point(); 40 DefaultAspect aspect = new PrivateFieldMethodRedefineAspect(); 41 42 ProseSystem.getAspectManager().insert(aspect); 43 p.setY(17); 44 assertEquals("Private field y is negative", -17, p.getY()); 45 46 ProseSystem.getAspectManager().withdraw(aspect); 47 p.setY(81); 48 assertEquals("Private field y is set normally again", 81, p.getY()); 49 } 50 51 public void test_030_redefine_simple_by_complex_method() { 52 Pojo p = new Pojo(); 53 DefaultAspect aspect = new SimpleToComplexMethodRedefineAspect(); 54 55 ProseSystem.getAspectManager().insert(aspect); 56 p.simple(); 57 assertTrue("Simple method was redefined by complex", p.calledAndRedefined); 58 } 59 60 public void test_040_redefine_complex_by_simple_method() { 61 Pojo p = new Pojo(); 62 DefaultAspect aspect = new ComplexToSimpleMethodRedefineAspect(); 63 64 ProseSystem.getAspectManager().insert(aspect); 65 p.complex("Hello"); 66 assertTrue("Complex method was redefined by simple", p.calledAndRedefined); 67 } 68 69 public void test_050_redefine_with_rest() { 70 Pojo p = new Pojo(); 71 DefaultAspect aspect = new RestMethodRedefineAspect(); 72 73 ProseSystem.getAspectManager().insert(aspect); 74 p.one_argument(42); 75 assertTrue("Pojo.simple() was redefined", p.calledAndRedefined); 76 } 77 78 public void test_060_redefine_with_return_type() { 79 Pojo p = new Pojo(); 80 DefaultAspect aspect = new ReturnTypeMethodRedefineAspect(); 81 82 ProseSystem.getAspectManager().insert(aspect); 83 84 p.calledAndRedefined = false; 85 p.simple(); 86 assertTrue("Pojo.simple() was not redefined", !p.calledAndRedefined); 87 88 p.calledAndRedefined = false; 89 p.simpleInt(); 90 assertTrue("Pojo.simpleInt() was not redefined", !p.calledAndRedefined); 91 92 p.calledAndRedefined = false; 93 p.simpleString(); 94 assertTrue("Pojo.simpleString() was redefined", p.calledAndRedefined); 95 96 p.calledAndRedefined = false; 97 p.simpleObject(); 98 assertTrue("Pojo.simpleObject() was redefined", p.calledAndRedefined); 99 } 100 101 class SimpleMethodRedefineAspect extends DefaultAspect { 102 103 public Crosscut c1 = new MethodRedefineCut() { 104 105 public void METHOD_ARGS(Point target, int x) { 106 target.x = -x; 107 } 108 109 protected PointCutter pointCutter() { 110 return Within.method("setX"); 111 } 112 113 }; 114 115 } 116 117 class PrivateFieldMethodRedefineAspect extends DefaultAspect { 118 119 public Crosscut c1 = new MethodRedefineCut() { 120 121 public void METHOD_ARGS(Point target, int x) { 122 try { 123 Class point_class = target.getClass(); 124 Field y_field = point_class.getDeclaredFields()[1]; 125 126 y_field.setInt(target, -x); 127 } catch (Exception e) { 128 throw new RuntimeException ("Oops.", e); 129 } 130 } 131 132 protected PointCutter pointCutter() { 133 return Within.method("setY"); 134 } 135 136 }; 137 138 } 139 140 class Pojo { 141 142 boolean calledAndRedefined; 143 144 void simple() {} 145 146 int simpleInt() { 147 return 1; 148 } 149 150 String simpleString() { 151 return null; 152 } 153 154 Object simpleObject() { 155 return null; 156 } 157 158 String complex(String arg) { 159 try { 160 int i = 17; 161 i++; 162 String msg = "Message " + i + " from " + this +": " + arg; 163 throw new RuntimeException (msg); 164 } catch (RuntimeException e) { 165 e.getStackTrace(); 166 } 167 return "Ignore"; 168 } 169 170 void one_argument(int x) {} 171 172 } 173 174 class ComplexToSimpleMethodRedefineAspect extends DefaultAspect { 175 176 public Crosscut c1 = new MethodRedefineCut() { 177 178 public String METHOD_ARGS(Pojo target, String x) { 179 target.calledAndRedefined = true; 180 return "Ignore, too"; 181 } 182 183 protected PointCutter pointCutter() { 184 return Within.method("complex"); 185 } 186 187 }; 188 189 } 190 191 class SimpleToComplexMethodRedefineAspect extends DefaultAspect { 192 193 public Crosscut c1 = new MethodRedefineCut() { 194 195 public void METHOD_ARGS(Pojo target) { 196 try { 197 int i = 17; 198 i++; 199 String msg = "Message " + i + " from " + target; 200 throw new RuntimeException (msg); 201 } catch (RuntimeException e) { 202 int j = 34; 203 if (j == 17) 204 j--; 205 else 206 j++; 207 208 } 209 210 target.calledAndRedefined = true; 211 } 212 213 protected PointCutter pointCutter() { 214 return Within.method("simple"); 215 } 216 217 }; 218 219 } 220 221 class RestMethodRedefineAspect extends DefaultAspect { 222 223 public Crosscut c1 = new MethodRedefineCut() { 224 225 public void METHOD_ARGS(Pojo target, int x, REST y) { 226 target.calledAndRedefined = true; 227 } 228 229 protected PointCutter pointCutter() { 230 return Within.method("one_argument"); 231 } 232 233 }; 234 235 } 236 237 class ReturnTypeMethodRedefineAspect extends DefaultAspect { 238 239 public Crosscut c1 = new MethodRedefineCut() { 240 241 public String METHOD_ARGS(Pojo target) { 242 target.calledAndRedefined = true; 243 244 return null; 245 } 246 247 protected PointCutter pointCutter() { 248 return Within.method("simple*"); 249 } 250 251 }; 252 253 } 254 255 } 256 | Popular Tags |