001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
008:
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.tx.UnlockedSharedObjectException;
012: import com.tc.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tc.util.Assert;
015: import com.tctest.runner.AbstractTransparentApp;
016:
017: import java.lang.reflect.AccessibleObject;
018: import java.lang.reflect.Constructor;
019: import java.lang.reflect.Field;
020: import java.lang.reflect.Method;
021:
022: public class AccessibleObjectTestApp extends AbstractTransparentApp {
023: private CyclicBarrier barrier;
024: private final DataRoot root = new DataRoot();
025:
026: public AccessibleObjectTestApp(String appId, ApplicationConfig cfg,
027: ListenerProvider listenerProvider) {
028: super (appId, cfg, listenerProvider);
029: barrier = new CyclicBarrier(getParticipantCount());
030: }
031:
032: public void run() {
033: try {
034: int index = barrier.barrier();
035:
036: testField(index);
037: testConstructor(index);
038: testMethod(index);
039:
040: } catch (Throwable t) {
041: notifyError(t);
042: }
043: }
044:
045: private void testField(int index) throws Exception {
046: basicFieldTest(index);
047: super FieldTest(index);
048: fieldSetAccessibleTest(index);
049: }
050:
051: private void basicFieldTest(int index) throws Exception {
052: if (index == 0) {
053: Field f = Superclass.class.getDeclaredField("superString");
054: f.setAccessible(true);
055: root.setF1(f);
056: }
057:
058: barrier.barrier();
059:
060: if (index == 1) {
061: Superclass sc = new Superclass();
062: Field f = root.getF1();
063: f.set(sc, "Basic Field Test");
064: Assert
065: .assertEquals("Basic Field Test", sc
066: .getSuperString());
067: root.setSharedObject(sc);
068: }
069:
070: barrier.barrier();
071:
072: Field f = root.getF1();
073: Object o = f.get(root.getSharedObject());
074: Assert.assertEquals("Basic Field Test", o);
075:
076: barrier.barrier();
077: }
078:
079: private void super FieldTest(int index) throws Exception {
080: if (index == 0) {
081: Field f = Subclass.class.getField("subString");
082: f.setAccessible(true);
083: root.setF1(f);
084: }
085:
086: barrier.barrier();
087:
088: if (index == 1) {
089: Superclass sc = new Subclass();
090: Field f = root.getF1();
091: f.set(sc, "Super Field Test");
092: Assert.assertEquals("Super Field Test", sc.getSubString());
093: root.setSharedObject(sc);
094: }
095:
096: barrier.barrier();
097:
098: Field f = root.getF1();
099: Object o = f.get(root.getSharedObject());
100: Assert.assertEquals("Super Field Test", o);
101:
102: barrier.barrier();
103: }
104:
105: private void fieldSetAccessibleTest(int index) throws Exception {
106: if (index == 0) {
107: Field f = Superclass.class.getDeclaredField("superString");
108: root.setF1(f);
109: }
110:
111: barrier.barrier();
112:
113: if (index == 1) {
114: Field f = root.getF1();
115: synchronized (root) {
116: f.setAccessible(true);
117: }
118: }
119:
120: barrier.barrier();
121:
122: Assert.assertTrue(root.getF1().isAccessible());
123:
124: barrier.barrier();
125:
126: if (index == 0) {
127: Field f = root.getF1();
128: synchronized (root) {
129: f.setAccessible(false);
130: }
131: }
132:
133: barrier.barrier();
134:
135: Assert.assertFalse(root.getF1().isAccessible());
136:
137: barrier.barrier();
138:
139: if (index == 1) {
140: synchronized (root) {
141: AccessibleObject.setAccessible(
142: new AccessibleObject[] { root.getF1() }, true);
143: }
144: }
145:
146: barrier.barrier();
147:
148: Assert.assertTrue(root.getF1().isAccessible());
149:
150: barrier.barrier();
151: }
152:
153: private void testConstructor(int index) throws Exception {
154: basicConstructorTest(index);
155: constructorSetAccessibleTest(index);
156: }
157:
158: private void basicConstructorTest(int index) throws Exception {
159: if (index == 0) {
160: Constructor c = Superclass.class
161: .getDeclaredConstructor(new Class[] { Integer.TYPE,
162: Long.TYPE, String.class });
163: root.setC1(c);
164: }
165:
166: barrier.barrier();
167:
168: if (index == 1) {
169: Constructor c = root.getC1();
170: Superclass o = (Superclass) c.newInstance(new Object[] {
171: new Integer(1), new Long(10),
172: "Basic Constructor Test" });
173: Assert.assertEquals(1, o.getI());
174: Assert.assertEquals(10, o.getL());
175: Assert.assertEquals("Basic Constructor Test", o
176: .getSuperString());
177: }
178:
179: barrier.barrier();
180: }
181:
182: private void constructorSetAccessibleTest(int index)
183: throws Exception {
184: if (index == 0) {
185: Constructor c = Superclass.class
186: .getDeclaredConstructor(new Class[] { Integer.TYPE,
187: Long.TYPE, String.class });
188: root.setC1(c);
189: }
190:
191: barrier.barrier();
192:
193: if (index == 1) {
194: Constructor c = root.getC1();
195: synchronized (root) {
196: c.setAccessible(true);
197: }
198: }
199:
200: barrier.barrier();
201:
202: Assert.assertTrue(root.getC1().isAccessible());
203:
204: barrier.barrier();
205:
206: if (index == 0) {
207: Constructor c = root.getC1();
208: synchronized (root) {
209: c.setAccessible(false);
210: }
211: }
212:
213: barrier.barrier();
214:
215: Assert.assertFalse(root.getC1().isAccessible());
216:
217: barrier.barrier();
218:
219: if (index == 1) {
220: synchronized (root) {
221: AccessibleObject.setAccessible(
222: new AccessibleObject[] { root.getC1() }, true);
223: }
224: }
225:
226: barrier.barrier();
227:
228: Assert.assertTrue(root.getC1().isAccessible());
229:
230: barrier.barrier();
231: }
232:
233: private void testMethod(int index) throws Exception {
234: basicSetMethodTest(index);
235: subclassSetMethodTest1(index);
236: subclassSetMethodTest2(index);
237: subclassSetMethodTest3(index);
238: subclassSetMethodTest4(index);
239: subclassSetMethodTestStaticArray(index);
240: subclassSetMethodTestUnlocked(index);
241: basicGetMethodTest(index);
242: subclassGetMethodTest1(index);
243: subclassGetMethodTest2(index);
244: subclassGetMethodTest3(index);
245: subclassGetMethodTest4(index);
246: }
247:
248: private void subclassSetMethodTestStaticArray(int index)
249: throws Exception {
250: if (index == 0) {
251: Method m1 = Superclass.class.getDeclaredMethod(
252: "setSuperString", new Class[] { String.class });
253: root.setM1(m1);
254: Method m2 = Superclass.class.getDeclaredMethod("setL",
255: new Class[] { Long.TYPE });
256: root.setM2(m2);
257: }
258:
259: barrier.barrier();
260:
261: if (index == 1) {
262: Method m1 = root.getM1();
263: Assert.assertFalse(m1.isAccessible());
264: Method m2 = root.getM2();
265: Assert.assertFalse(m2.isAccessible());
266: }
267:
268: barrier.barrier();
269:
270: if (index == 0) {
271: synchronized (root) {
272: Method m1 = root.getM1();
273: Method m2 = root.getM2();
274: AccessibleObject.setAccessible(new AccessibleObject[] {
275: m1, m2 }, true);
276: }
277: }
278:
279: barrier.barrier();
280:
281: if (index == 1) {
282: Method m1 = root.getM1();
283: Assert.assertTrue(m1.isAccessible());
284: Method m2 = root.getM2();
285: Assert.assertTrue(m2.isAccessible());
286:
287: Subclass sub = new Subclass();
288: m1.invoke(sub, new Object[] { "sample super string" });
289: m2.invoke(sub, new Object[] { new Long(773648L) });
290: Assert.assertEquals("sample super string", sub
291: .getSuperString());
292: Assert.assertEquals(773648L, sub.getL());
293: }
294:
295: barrier.barrier();
296: }
297:
298: private void subclassSetMethodTestUnlocked(int index)
299: throws Exception {
300: if (index == 0) {
301: Method m = Superclass.class.getDeclaredMethod(
302: "setSuperString", new Class[] { String.class });
303: root.setM1(m);
304: }
305:
306: barrier.barrier();
307:
308: if (index == 1) {
309: Method m = root.getM1();
310: Assert.assertFalse(m.isAccessible());
311: }
312:
313: barrier.barrier();
314:
315: if (index == 0) {
316: Method m = root.getM1();
317: try {
318: m.setAccessible(true);
319: Assert.fail();
320: } catch (UnlockedSharedObjectException e) {
321: Assert.assertFalse(m.isAccessible());
322: }
323: }
324:
325: barrier.barrier();
326: }
327:
328: private void subclassSetMethodTest4(int index) throws Exception {
329: if (index == 0) {
330: Method m = Superclass.class.getDeclaredMethod(
331: "setSuperString", new Class[] { String.class });
332: root.setM1(m);
333: }
334:
335: barrier.barrier();
336:
337: if (index == 1) {
338: Method m = root.getM1();
339: Assert.assertFalse(m.isAccessible());
340: }
341:
342: barrier.barrier();
343:
344: if (index == 0) {
345: synchronized (root) {
346: Method m = root.getM1();
347: m.setAccessible(true);
348: }
349: }
350:
351: barrier.barrier();
352:
353: if (index == 1) {
354: Method m = root.getM1();
355: Assert.assertTrue(m.isAccessible());
356: Subclass sub = new Subclass();
357: m.invoke(sub, new Object[] { "sample super string" });
358: Assert.assertEquals("sample super string", sub
359: .getSuperString());
360: }
361:
362: barrier.barrier();
363: }
364:
365: private void subclassSetMethodTest3(int index) throws Exception {
366: if (index == 0) {
367: Method m = Subclass.class.getMethod("setS",
368: new Class[] { String.class });
369: root.setM1(m);
370: }
371:
372: barrier.barrier();
373:
374: if (index == 1) {
375: Method m = root.getM1();
376: Subclass sub = new Subclass();
377: m.invoke(sub, new Object[] { "sample string" });
378: Assert.assertEquals("sample string", sub.getS());
379: }
380:
381: barrier.barrier();
382: }
383:
384: private void subclassSetMethodTest2(int index) throws Exception {
385: if (index == 0) {
386: Method m = Subclass.class.getMethod("setI",
387: new Class[] { Integer.TYPE });
388: root.setM1(m);
389: }
390:
391: barrier.barrier();
392:
393: if (index == 1) {
394: Method m = root.getM1();
395: Subclass sub = new Subclass();
396: m.invoke(sub, new Object[] { new Integer(10) });
397: Assert.assertEquals(10, sub.getI());
398: }
399:
400: barrier.barrier();
401: }
402:
403: private void subclassSetMethodTest1(int index) throws Exception {
404: if (index == 0) {
405: Method m = Superclass.class.getDeclaredMethod("setI",
406: new Class[] { Integer.TYPE });
407: root.setM1(m);
408: }
409:
410: barrier.barrier();
411:
412: if (index == 1) {
413: Method m = root.getM1();
414: Subclass sub = new Subclass();
415: m.invoke(sub, new Object[] { new Integer(10) });
416: Assert.assertEquals(10, sub.getI());
417: }
418:
419: barrier.barrier();
420: }
421:
422: private void subclassGetMethodTest4(int index) throws Exception {
423: if (index == 0) {
424: Method m = Superclass.class.getDeclaredMethod(
425: "getSuperString", new Class[] {});
426: root.setM1(m);
427: }
428:
429: barrier.barrier();
430:
431: if (index == 1) {
432: Method m = root.getM1();
433: synchronized (root) {
434: m.setAccessible(true);
435: }
436: Subclass sub = new Subclass();
437: sub.setSuperString("sample super string");
438: String value = (String) m.invoke(sub, new Object[] {});
439: Assert.assertEquals("sample super string", value);
440: }
441:
442: barrier.barrier();
443: }
444:
445: private void subclassGetMethodTest3(int index) throws Exception {
446: if (index == 0) {
447: Method m = Subclass.class.getMethod("getS", new Class[] {});
448: root.setM1(m);
449: }
450:
451: barrier.barrier();
452:
453: if (index == 1) {
454: Method m = root.getM1();
455: Subclass sub = new Subclass();
456: sub.setS("sample string");
457: String value = (String) m.invoke(sub, new Object[] {});
458: Assert.assertEquals("sample string", value);
459: }
460:
461: barrier.barrier();
462: }
463:
464: private void subclassGetMethodTest2(int index) throws Exception {
465: if (index == 0) {
466: Method m = Subclass.class.getMethod("getI", new Class[] {});
467: root.setM1(m);
468: }
469:
470: barrier.barrier();
471:
472: if (index == 1) {
473: Method m = root.getM1();
474: Subclass sub = new Subclass();
475: sub.setI(20);
476: Integer value = (Integer) m.invoke(sub, new Object[] {});
477: Assert.assertEquals(20, value.intValue());
478: }
479:
480: barrier.barrier();
481: }
482:
483: private void subclassGetMethodTest1(int index) throws Exception {
484: if (index == 0) {
485: Method m = Superclass.class.getDeclaredMethod("getI",
486: new Class[] {});
487: root.setM1(m);
488: }
489:
490: barrier.barrier();
491:
492: if (index == 1) {
493: Method m = root.getM1();
494: Subclass sub = new Subclass();
495: sub.setI(20);
496: Integer value = (Integer) m.invoke(sub, new Object[] {});
497: Assert.assertEquals(20, value.intValue());
498: }
499:
500: barrier.barrier();
501: }
502:
503: private void basicGetMethodTest(int index) throws Exception {
504: if (index == 0) {
505: Method m = Superclass.class.getDeclaredMethod("getI",
506: new Class[] {});
507: root.setM1(m);
508: }
509:
510: barrier.barrier();
511:
512: if (index == 1) {
513: Method m = root.getM1();
514: Superclass sc = new Superclass();
515: sc.setI(20);
516: Integer value = (Integer) m.invoke(sc, new Object[] {});
517: Assert.assertEquals(20, value.intValue());
518: }
519:
520: barrier.barrier();
521: }
522:
523: private void basicSetMethodTest(int index) throws Exception {
524: if (index == 0) {
525: Method m = Superclass.class.getDeclaredMethod("setI",
526: new Class[] { Integer.TYPE });
527: root.setM1(m);
528: }
529:
530: barrier.barrier();
531:
532: if (index == 1) {
533: Method m = root.getM1();
534: Superclass sc = new Superclass();
535: m.invoke(sc, new Object[] { new Integer(10) });
536: Assert.assertEquals(10, sc.getI());
537: }
538:
539: barrier.barrier();
540: }
541:
542: public static void visitL1DSOConfig(ConfigVisitor visitor,
543: DSOClientConfigHelper config) {
544: config.getOrCreateSpec(CyclicBarrier.class.getName());
545: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
546: + "*.*(..)");
547: String testClass = AccessibleObjectTestApp.class.getName();
548: config.getOrCreateSpec(testClass).addRoot("barrier", "barrier")
549: .addRoot("root", "root");
550: String writeAllowedMethodExpression = "* " + testClass
551: + "*.*(..)";
552: config.addWriteAutolock(writeAllowedMethodExpression);
553: config.addIncludePattern(testClass + "$*");
554: }
555:
556: private static class Superclass {
557: private int i;
558: private long l;
559: private String super String;
560: public String subString;
561:
562: public Superclass() {
563: super ();
564: }
565:
566: public Superclass(int i, long l, String super String) {
567: this .i = i;
568: this .l = l;
569: this .super String = super String;
570: }
571:
572: public int getI() {
573: return i;
574: }
575:
576: public void setI(int i) {
577: this .i = i;
578: }
579:
580: protected String getSuperString() {
581: return super String;
582: }
583:
584: protected void setSuperString(String super String) {
585: this .super String = super String;
586: }
587:
588: public synchronized String getSubString() {
589: return subString;
590: }
591:
592: public synchronized void setSubString(String subString) {
593: this .subString = subString;
594: }
595:
596: protected long getL() {
597: return l;
598: }
599:
600: protected void setL(long l) {
601: this .l = l;
602: }
603: }
604:
605: private static class Subclass extends Superclass {
606: private String s;
607:
608: public String getS() {
609: return s;
610: }
611:
612: public void setS(String s) {
613: this .s = s;
614: }
615: }
616:
617: private static class DataRoot {
618: private Method m1;
619: private Method m2;
620: private Constructor c1;
621: private Field f1;
622: private Superclass sharedObject;
623:
624: public synchronized Method getM1() {
625: return m1;
626: }
627:
628: public synchronized void setM1(Method m) {
629: this .m1 = m;
630: }
631:
632: public synchronized Method getM2() {
633: return m2;
634: }
635:
636: public synchronized void setM2(Method m) {
637: this .m2 = m;
638: }
639:
640: public synchronized Superclass getSharedObject() {
641: return sharedObject;
642: }
643:
644: public synchronized void setSharedObject(Superclass sharedObject) {
645: this .sharedObject = sharedObject;
646: }
647:
648: public synchronized Constructor getC1() {
649: return c1;
650: }
651:
652: public synchronized void setC1(Constructor c1) {
653: this .c1 = c1;
654: }
655:
656: public synchronized Field getF1() {
657: return f1;
658: }
659:
660: public synchronized void setF1(Field f1) {
661: this.f1 = f1;
662: }
663: }
664: }
|