1 5 package com.tctest; 6 7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 8 9 import com.tc.object.config.ConfigVisitor; 10 import com.tc.object.config.DSOClientConfigHelper; 11 import com.tc.simulator.app.ApplicationConfig; 12 import com.tc.simulator.listener.ListenerProvider; 13 import com.tc.util.Assert; 14 import com.tctest.runner.AbstractTransparentApp; 15 16 import java.lang.reflect.Method ; 17 18 public class AccessibleObjectTestApp extends AbstractTransparentApp { 19 private CyclicBarrier barrier; 20 private final DataRoot root = new DataRoot(); 21 22 public AccessibleObjectTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 23 super(appId, cfg, listenerProvider); 24 barrier = new CyclicBarrier(getParticipantCount()); 25 } 26 27 public void run() { 28 try { 29 int index = barrier.barrier(); 30 31 testMethod(index); 32 } catch (Throwable t) { 33 notifyError(t); 34 } 35 } 36 37 private void testMethod(int index) throws Exception { 38 basicSetMethodTest(index); 39 subclassSetMethodTest1(index); 40 subclassSetMethodTest2(index); 41 subclassSetMethodTest3(index); 42 subclassSetMethodTest4(index); 43 basicGetMethodTest(index); 44 subclassGetMethodTest1(index); 45 subclassGetMethodTest2(index); 46 subclassGetMethodTest3(index); 47 subclassGetMethodTest4(index); 48 } 49 50 private void subclassSetMethodTest4(int index) throws Exception { 51 if (index == 0) { 52 Method m = Superclass.class.getDeclaredMethod("setSuperString", new Class []{String .class}); 53 root.setM(m); 54 } 55 56 barrier.barrier(); 57 58 if (index == 1) { 59 Method m = root.getM(); 60 Assert.assertFalse(m.isAccessible()); 61 } 62 63 barrier.barrier(); 64 65 if (index == 0) { 66 synchronized(root) { 67 Method m = root.getM(); 68 m.setAccessible(true); 69 } 70 } 71 72 barrier.barrier(); 73 74 if (index == 1) { 75 Method m = root.getM(); 76 Assert.assertTrue(m.isAccessible()); 77 Subclass sub = new Subclass(); 78 m.invoke(sub, new Object []{"sample super string"}); 79 Assert.assertEquals("sample super string", sub.getSuperString()); 80 } 81 82 barrier.barrier(); 83 } 84 85 private void subclassSetMethodTest3(int index) throws Exception { 86 if (index == 0) { 87 Method m = Subclass.class.getMethod("setS", new Class []{String .class}); 88 root.setM(m); 89 } 90 91 barrier.barrier(); 92 93 if (index == 1) { 94 Method m = root.getM(); 95 Subclass sub = new Subclass(); 96 m.invoke(sub, new Object []{"sample string"}); 97 Assert.assertEquals("sample string", sub.getS()); 98 } 99 100 barrier.barrier(); 101 } 102 103 private void subclassSetMethodTest2(int index) throws Exception { 104 if (index == 0) { 105 Method m = Subclass.class.getMethod("setI", new Class []{Integer.TYPE}); 106 root.setM(m); 107 } 108 109 barrier.barrier(); 110 111 if (index == 1) { 112 Method m = root.getM(); 113 Subclass sub = new Subclass(); 114 m.invoke(sub, new Object []{new Integer (10)}); 115 Assert.assertEquals(10, sub.getI()); 116 } 117 118 barrier.barrier(); 119 } 120 121 private void subclassSetMethodTest1(int index) throws Exception { 122 if (index == 0) { 123 Method m = Superclass.class.getDeclaredMethod("setI", new Class []{Integer.TYPE}); 124 root.setM(m); 125 } 126 127 barrier.barrier(); 128 129 if (index == 1) { 130 Method m = root.getM(); 131 Subclass sub = new Subclass(); 132 m.invoke(sub, new Object []{new Integer (10)}); 133 Assert.assertEquals(10, sub.getI()); 134 } 135 136 barrier.barrier(); 137 } 138 139 private void subclassGetMethodTest4(int index) throws Exception { 140 if (index == 0) { 141 Method m = Superclass.class.getDeclaredMethod("getSuperString", new Class []{}); 142 root.setM(m); 143 } 144 145 barrier.barrier(); 146 147 if (index == 1) { 148 Method m = root.getM(); 149 synchronized(root) { 150 m.setAccessible(true); 151 } 152 Subclass sub = new Subclass(); 153 sub.setSuperString("sample super string"); 154 String value = (String )m.invoke(sub, new Object []{}); 155 Assert.assertEquals("sample super string", value); 156 } 157 158 barrier.barrier(); 159 } 160 161 private void subclassGetMethodTest3(int index) throws Exception { 162 if (index == 0) { 163 Method m = Subclass.class.getMethod("getS", new Class []{}); 164 root.setM(m); 165 } 166 167 barrier.barrier(); 168 169 if (index == 1) { 170 Method m = root.getM(); 171 Subclass sub = new Subclass(); 172 sub.setS("sample string"); 173 String value = (String )m.invoke(sub, new Object []{}); 174 Assert.assertEquals("sample string", value); 175 } 176 177 barrier.barrier(); 178 } 179 180 private void subclassGetMethodTest2(int index) throws Exception { 181 if (index == 0) { 182 Method m = Subclass.class.getMethod("getI", new Class []{}); 183 root.setM(m); 184 } 185 186 barrier.barrier(); 187 188 if (index == 1) { 189 Method m = root.getM(); 190 Subclass sub = new Subclass(); 191 sub.setI(20); 192 Integer value = (Integer )m.invoke(sub, new Object []{}); 193 Assert.assertEquals(20, value.intValue()); 194 } 195 196 barrier.barrier(); 197 } 198 199 private void subclassGetMethodTest1(int index) throws Exception { 200 if (index == 0) { 201 Method m = Superclass.class.getDeclaredMethod("getI", new Class []{}); 202 root.setM(m); 203 } 204 205 barrier.barrier(); 206 207 if (index == 1) { 208 Method m = root.getM(); 209 Subclass sub = new Subclass(); 210 sub.setI(20); 211 Integer value = (Integer )m.invoke(sub, new Object []{}); 212 Assert.assertEquals(20, value.intValue()); 213 } 214 215 barrier.barrier(); 216 } 217 218 private void basicGetMethodTest(int index) throws Exception { 219 if (index == 0) { 220 Method m = Superclass.class.getDeclaredMethod("getI", new Class []{}); 221 root.setM(m); 222 } 223 224 barrier.barrier(); 225 226 if (index == 1) { 227 Method m = root.getM(); 228 Superclass sc = new Superclass(); 229 sc.setI(20); 230 Integer value = (Integer )m.invoke(sc, new Object []{}); 231 Assert.assertEquals(20, value.intValue()); 232 } 233 234 barrier.barrier(); 235 } 236 237 238 private void basicSetMethodTest(int index) throws Exception { 239 if (index == 0) { 240 Method m = Superclass.class.getDeclaredMethod("setI", new Class []{Integer.TYPE}); 241 root.setM(m); 242 } 243 244 barrier.barrier(); 245 246 if (index == 1) { 247 Method m = root.getM(); 248 Superclass sc = new Superclass(); 249 m.invoke(sc, new Object []{new Integer (10)}); 250 Assert.assertEquals(10, sc.getI()); 251 } 252 253 barrier.barrier(); 254 } 255 256 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 257 config.getOrCreateSpec(CyclicBarrier.class.getName()); 258 config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)"); 259 String testClass = AccessibleObjectTestApp.class.getName(); 260 config.getOrCreateSpec(testClass).addRoot("barrier", "barrier").addRoot("root", "root"); 261 String writeAllowedMethodExpression = "* " + testClass + "*.*(..)"; 262 config.addWriteAutolock(writeAllowedMethodExpression); 263 config.addIncludePattern(testClass + "$*"); 264 } 265 266 private static class Superclass { 267 private int i; 268 private String superString; 269 270 public int getI() { 271 return i; 272 } 273 274 public void setI(int i) { 275 this.i = i; 276 } 277 278 protected String getSuperString() { 279 return superString; 280 } 281 282 protected void setSuperString(String superString) { 283 this.superString = superString; 284 } 285 } 286 287 private static class Subclass extends Superclass { 288 private String s; 289 290 public String getS() { 291 return s; 292 } 293 294 public void setS(String s) { 295 this.s = s; 296 } 297 } 298 299 private static class DataRoot { 300 private Method m; 301 private Superclass sharedObject; 302 303 public synchronized Method getM() { 304 return m; 305 } 306 307 public synchronized void setM(Method m) { 308 this.m = m; 309 } 310 311 public Superclass getSharedObject() { 312 return sharedObject; 313 } 314 315 public void setSharedObject(Superclass sharedObject) { 316 this.sharedObject = sharedObject; 317 } 318 } 319 } 320 | Popular Tags |