1 7 package ch.ethz.prose; 8 9 import java.lang.reflect.Field ; 11 12 import junit.framework.*; 13 import ch.ethz.jvmai.FieldAccessJoinPoint; 14 import ch.ethz.jvmai.FieldModificationJoinPoint; 15 import ch.ethz.prose.crosscut.*; 16 import ch.ethz.prose.filter.PointCutter; 17 import ch.ethz.prose.filter.Within; 18 19 20 27 public class FieldNotificationTest extends TestCase { 28 29 static final boolean WRITE = false; 31 static final boolean READ = true; 32 33 static class TestClass { 34 int initial; 35 int undefined; 36 37 int[] array; 38 39 Object o; 40 41 public TestClass() 42 { 43 fieldName = "o"; 44 o = null; 45 } 46 } 47 48 public static class TestExtension extends DefaultAspect { 49 50 public Crosscut c1 = new GetCut() 51 { 52 public void GET_ARGS() 53 { 54 try 55 { 56 Field f = ((FieldAccessJoinPoint)thisJoinPoint()).getField(); 57 assertTrue("unexpected read access to watched field " + 58 f, f.getName().equals(fieldName)); 59 assertTrue("unexpected read access to watched field " + 60 f + ", write access expected.", 61 readwriteAccess == READ); 62 } 63 catch ( Exception e ) 64 { 65 e.printStackTrace(); 66 } 67 } 68 69 protected PointCutter pointCutter() { 70 return Within.type(TestClass.class); 71 } 72 73 protected Class [] potentialCrosscutClasses() 75 { 76 Class [] result = {TestClass.class}; 77 return result; 78 } 79 }; 80 81 public Crosscut c = new SetCut() 82 { 83 public void SET_ARGS() 84 { 85 Field f = ((FieldModificationJoinPoint)thisJoinPoint()).getField(); 86 try { 87 assertTrue("unexpected write access to watched field " + 88 f, f.getName().equals(fieldName)); 89 assertTrue("unexpected write access to watched field " + 90 f + ", read access expected.", 91 readwriteAccess == WRITE); 92 } 93 catch ( Exception e ) { 94 e.printStackTrace(); 95 } 96 } 97 98 protected Class [] potentialCrosscutClasses() 100 { 101 Class [] result = {TestClass.class}; 102 return result; 103 } 104 105 protected PointCutter pointCutter() { 106 return Within.type(TestClass.class); 107 } 108 }; 109 } 110 111 TestClass test; 112 113 static String fieldName; 114 static boolean readwriteAccess; 115 116 117 121 public FieldNotificationTest(String name) { 122 super(name); 123 } 124 125 126 129 protected void setUp() { 130 try { 131 ProseSystem.startup(); 132 } 133 catch ( Exception e ) { 134 Assert.fail("ProseSystem.startup() failed."); 135 } 136 } 137 138 141 protected void tearDown() { 142 try { 143 ProseSystem.teardown(); 144 } 145 catch ( Exception e ) { 146 Assert.fail("ProseSystem.teardown() failed."); 147 } 148 } 149 150 151 154 public void testInstantiation() throws Exception { 155 156 try 157 { 158 ProseSystem.getAspectManager().insert(new TestExtension()); 159 160 runTestInstantiation(); 161 } 162 catch (RuntimeException e) 163 { 164 e.printStackTrace(); 165 throw e; 166 } 167 } 168 169 protected void runTestInstantiation() { 170 int[] arr = { 1, 2, 3, 4, 5 }; 171 172 readwriteAccess = WRITE; 173 fieldName = "initial"; 174 test = new TestClass(); 175 176 fieldName = "undefined"; 177 test.undefined = -5; 178 179 fieldName = "o"; 180 test.o = new Object (); 181 182 fieldName = "array"; 183 test.array = arr; 184 185 readwriteAccess = READ; 186 arr[0] = 0; 187 188 readwriteAccess = WRITE; 189 test.array = arr; 190 191 readwriteAccess = READ; 192 test.array[1] = 9; 193 test.array[3] = 1; 194 test.array[4] = 10; 195 } 196 197 201 public static 202 Test suite() { 203 return new TestSuite(FieldNotificationTest.class); 204 } 205 206 } 207 208 209
| Popular Tags
|