001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.TransparencyClassSpec;
011: import com.tc.simulator.app.ApplicationConfig;
012: import com.tc.simulator.listener.ListenerProvider;
013: import com.tc.util.Assert;
014: import com.tctest.runner.AbstractTransparentApp;
015:
016: public class LiteralValueRootTestApp extends AbstractTransparentApp {
017:
018: private final SyncRoot syncRoot = new SyncRoot();
019: private final SyncRoot referenceRoot = new SyncRoot();
020:
021: private String stringRoot = null;
022: private Integer integerRoot = null;
023: private Long longRoot = null;
024: private Double doubleRoot = null;
025: private Float floatRoot = null;
026: private Byte byteRoot = null;
027: private Boolean booleanRoot = null;
028: private Character characterRoot = null;
029:
030: private Class classRoot = null;
031:
032: private Integer nonSharedIntegerObject = new Integer(10);
033:
034: private int nonDefaultPrimitiveIntRoot = 5;
035: private Integer nonDefaultIntegerRoot = new Integer(5);
036: private Integer sharedIntegerObject = new Integer(50);
037:
038: private int readBeforeSetTestIntRoot;
039: private Integer readBeforeSetTestIntegerRoot;
040:
041: private long primitiveLongRoot;
042: private int primitiveIntRoot;
043: private float primitiveFloatRoot;
044: private double primitiveDoubleRoot;
045: private boolean primitiveBooleanRoot;
046: private byte primitiveByteRoot;
047: private char primitiveCharRoot;
048:
049: private final CyclicBarrier barrier;
050:
051: public LiteralValueRootTestApp(String appId, ApplicationConfig cfg,
052: ListenerProvider listenerProvider) {
053: super (appId, cfg, listenerProvider);
054: barrier = new CyclicBarrier(getParticipantCount());
055: }
056:
057: public void run() {
058: try {
059: testGetBeforeSetObject();
060: testGetBeforeSetPrimitive();
061:
062: testDefaultPrimitiveValues();
063: testMultipleRootSetting();
064:
065: testSharedPrimitiveIntRoot();
066: testSharedPrimitiveLongRoot();
067: testSharedPrimitiveDoubleRoot();
068: testSharedPrimitiveFloatRoot();
069: testSharedPrimitiveBooleanRoot();
070: testSharedPrimitiveByteRoot();
071: testSharedPrimitiveCharRoot();
072:
073: testSharedClassRoot();
074: testSharedStringRoot();
075: testSharedIntegerRoot();
076: testSharedLongRoot();
077: testSharedFloatRoot();
078: testSharedDoubleRoot();
079: testSharedByteRoot();
080: testSharedBooleanRoot();
081: testSharedCharacterRoot();
082: testReferenceInequality();
083:
084: // testReferenceEquality();
085: } catch (Throwable t) {
086: notifyError(t);
087: }
088: }
089:
090: private void testMultipleRootSetting() throws Exception {
091: clear();
092:
093: int index = -1;
094: synchronized (syncRoot) {
095: index = syncRoot.getIndex();
096: if (index == 0) {
097: syncRoot.setIndex(1);
098: }
099: }
100:
101: barrier.barrier();
102:
103: synchronized (syncRoot) {
104: if (index == 0) {
105: nonDefaultPrimitiveIntRoot = 10;
106: }
107: }
108:
109: barrier.barrier();
110:
111: Assert.assertEquals(10, nonDefaultPrimitiveIntRoot);
112:
113: barrier.barrier();
114:
115: synchronized (syncRoot) {
116: if (index == 0) {
117: nonDefaultIntegerRoot = new Integer(10);
118: }
119: }
120:
121: barrier.barrier();
122:
123: Assert.assertEquals(new Integer(10), nonDefaultIntegerRoot);
124:
125: barrier.barrier();
126:
127: synchronized (syncRoot) {
128: if (index == 0) {
129: nonDefaultIntegerRoot = sharedIntegerObject;
130: }
131: }
132:
133: barrier.barrier();
134:
135: Assert.assertEquals(new Integer(50), nonDefaultIntegerRoot);
136:
137: barrier.barrier();
138:
139: }
140:
141: private void testDefaultPrimitiveValues() throws Exception {
142: Assert.assertEquals(0, primitiveIntRoot);
143: Assert.assertEquals(0L, primitiveLongRoot);
144: Assert.assertEquals(0.0D, primitiveDoubleRoot);
145: Assert.assertEquals(0.0F, primitiveFloatRoot);
146: Assert.assertEquals((byte) 0, primitiveByteRoot);
147: Assert.assertFalse(primitiveBooleanRoot);
148: barrier.barrier();
149: }
150:
151: private void testGetBeforeSetObject() throws Exception {
152: clear();
153:
154: int index = -1;
155: synchronized (syncRoot) {
156: index = syncRoot.getIndex();
157: if (index == 0) {
158: syncRoot.setIndex(1);
159: }
160: }
161:
162: barrier.barrier();
163:
164: Assert.assertNull(readBeforeSetTestIntegerRoot);
165:
166: barrier.barrier();
167:
168: if (index == 0) {
169: while (readBeforeSetTestIntegerRoot == null) {
170: if (readBeforeSetTestIntegerRoot != null) {
171: break;
172: }
173: }
174: } else {
175: synchronized (syncRoot) {
176: readBeforeSetTestIntegerRoot = new Integer(100);
177: }
178: }
179:
180: barrier.barrier();
181:
182: Assert.assertEquals(new Integer(100),
183: readBeforeSetTestIntegerRoot);
184:
185: barrier.barrier();
186: }
187:
188: private void testGetBeforeSetPrimitive() throws Exception {
189: clear();
190:
191: int index = -1;
192: synchronized (syncRoot) {
193: index = syncRoot.getIndex();
194: if (index == 0) {
195: syncRoot.setIndex(1);
196: }
197: }
198:
199: barrier.barrier();
200:
201: Assert.assertEquals(0, readBeforeSetTestIntRoot);
202:
203: barrier.barrier();
204:
205: if (index == 0) {
206: while (readBeforeSetTestIntRoot == 0) {
207: if (readBeforeSetTestIntRoot != 0) {
208: break;
209: }
210: }
211: } else {
212: synchronized (syncRoot) {
213: readBeforeSetTestIntRoot = 10;
214: }
215: }
216:
217: barrier.barrier();
218:
219: Assert.assertEquals(10, readBeforeSetTestIntRoot);
220:
221: barrier.barrier();
222: }
223:
224: private void testSharedPrimitiveLongRoot() throws Exception {
225: clear();
226:
227: int index = -1;
228: synchronized (syncRoot) {
229: index = syncRoot.getIndex();
230: if (index == 0) {
231: syncRoot.setIndex(1);
232: }
233: }
234:
235: barrier.barrier();
236:
237: synchronized (syncRoot) {
238: if (index == 0) {
239: primitiveLongRoot = 32L;
240: }
241: }
242:
243: barrier.barrier();
244:
245: Assert.assertEquals(32L, primitiveLongRoot);
246:
247: barrier.barrier();
248:
249: }
250:
251: private void testSharedPrimitiveIntRoot() throws Exception {
252: clear();
253:
254: int index = -1;
255: synchronized (syncRoot) {
256: index = syncRoot.getIndex();
257: if (index == 0) {
258: syncRoot.setIndex(1);
259: }
260: }
261:
262: barrier.barrier();
263:
264: synchronized (syncRoot) {
265: if (index == 0) {
266: primitiveIntRoot = 32;
267: }
268: }
269:
270: barrier.barrier();
271:
272: Assert.assertEquals(32, primitiveIntRoot);
273:
274: barrier.barrier();
275:
276: synchronized (syncRoot) {
277: if (index == 0) {
278: primitiveIntRoot++;
279: }
280: }
281:
282: barrier.barrier();
283:
284: Assert.assertEquals(33, primitiveIntRoot);
285:
286: barrier.barrier();
287: }
288:
289: private void testSharedPrimitiveDoubleRoot() throws Exception {
290: clear();
291:
292: int index = -1;
293: synchronized (syncRoot) {
294: index = syncRoot.getIndex();
295: if (index == 0) {
296: syncRoot.setIndex(1);
297: }
298: }
299:
300: barrier.barrier();
301:
302: synchronized (syncRoot) {
303: if (index == 0) {
304: primitiveDoubleRoot = 2e4;
305: }
306: }
307:
308: barrier.barrier();
309:
310: Assert.assertEquals(2e4, primitiveDoubleRoot);
311:
312: barrier.barrier();
313:
314: }
315:
316: private void testSharedPrimitiveFloatRoot() throws Exception {
317: clear();
318:
319: int index = -1;
320: synchronized (syncRoot) {
321: index = syncRoot.getIndex();
322: if (index == 0) {
323: syncRoot.setIndex(1);
324: }
325: }
326:
327: barrier.barrier();
328:
329: synchronized (syncRoot) {
330: if (index == 0) {
331: primitiveFloatRoot = 0.3f;
332: }
333: }
334:
335: barrier.barrier();
336:
337: Assert.assertEquals(0.3f, primitiveFloatRoot);
338:
339: barrier.barrier();
340:
341: }
342:
343: private void testSharedPrimitiveByteRoot() throws Exception {
344: clear();
345:
346: int index = -1;
347: synchronized (syncRoot) {
348: index = syncRoot.getIndex();
349: if (index == 0) {
350: syncRoot.setIndex(1);
351: }
352: }
353:
354: barrier.barrier();
355:
356: synchronized (syncRoot) {
357: if (index == 0) {
358: primitiveByteRoot = (byte) 10;
359: }
360: }
361:
362: barrier.barrier();
363:
364: Assert.assertEquals((byte) 10, primitiveByteRoot);
365:
366: barrier.barrier();
367:
368: }
369:
370: private void testSharedPrimitiveBooleanRoot() throws Exception {
371: clear();
372:
373: int index = -1;
374: synchronized (syncRoot) {
375: index = syncRoot.getIndex();
376: if (index == 0) {
377: syncRoot.setIndex(1);
378: }
379: }
380:
381: barrier.barrier();
382:
383: synchronized (syncRoot) {
384: if (index == 0) {
385: primitiveBooleanRoot = true;
386: }
387: }
388:
389: barrier.barrier();
390:
391: Assert.assertTrue(primitiveBooleanRoot);
392:
393: barrier.barrier();
394:
395: }
396:
397: private void testSharedPrimitiveCharRoot() throws Exception {
398: clear();
399:
400: int index = -1;
401: synchronized (syncRoot) {
402: index = syncRoot.getIndex();
403: if (index == 0) {
404: syncRoot.setIndex(1);
405: }
406: }
407:
408: barrier.barrier();
409:
410: synchronized (syncRoot) {
411: if (index == 0) {
412: primitiveCharRoot = 'c';
413: }
414: }
415:
416: barrier.barrier();
417:
418: Assert.assertEquals('c', primitiveCharRoot);
419:
420: barrier.barrier();
421:
422: }
423:
424: private void testSharedClassRoot() throws Exception {
425: clear();
426:
427: int index = -1;
428: synchronized (syncRoot) {
429: index = syncRoot.getIndex();
430: if (index == 0) {
431: syncRoot.setIndex(1);
432: }
433: }
434:
435: barrier.barrier();
436:
437: synchronized (syncRoot) {
438: if (index == 0) {
439: classRoot = Float.class;
440: }
441: }
442:
443: barrier.barrier();
444:
445: Assert.assertEquals("java.lang.Float", classRoot.getName());
446:
447: barrier.barrier();
448: }
449:
450: private void clear() throws Exception {
451: synchronized (syncRoot) {
452: syncRoot.setIndex(0);
453: referenceRoot.clear();
454: }
455:
456: barrier.barrier();
457: }
458:
459: private void testSharedStringRoot() throws Exception {
460: clear();
461:
462: int index = -1;
463: synchronized (syncRoot) {
464: index = syncRoot.getIndex();
465: if (index == 0) {
466: syncRoot.setIndex(1);
467: }
468: }
469:
470: barrier.barrier();
471:
472: synchronized (syncRoot) {
473: if (index == 0) {
474: stringRoot = "Shared value";
475: }
476: }
477:
478: barrier.barrier();
479:
480: Assert.assertEquals("Shared value", stringRoot);
481:
482: barrier.barrier();
483: }
484:
485: private void testSharedIntegerRoot() throws Exception {
486: clear();
487:
488: int index = -1;
489: synchronized (syncRoot) {
490: index = syncRoot.getIndex();
491: if (index == 0) {
492: syncRoot.setIndex(1);
493: }
494: }
495:
496: barrier.barrier();
497:
498: synchronized (syncRoot) {
499: if (index == 0) {
500: integerRoot = new Integer(4);
501: }
502: }
503:
504: barrier.barrier();
505:
506: Assert.assertEquals(new Integer(4), integerRoot);
507:
508: barrier.barrier();
509: }
510:
511: private void testSharedLongRoot() throws Exception {
512: clear();
513:
514: int index = -1;
515: synchronized (syncRoot) {
516: index = syncRoot.getIndex();
517: if (index == 0) {
518: syncRoot.setIndex(1);
519: }
520: }
521:
522: barrier.barrier();
523:
524: synchronized (syncRoot) {
525: if (index == 0) {
526: longRoot = new Long(10L);
527: }
528: }
529:
530: barrier.barrier();
531:
532: Assert.assertEquals(new Long(10L), longRoot);
533:
534: barrier.barrier();
535: }
536:
537: private void testSharedFloatRoot() throws Exception {
538: clear();
539:
540: int index = -1;
541: synchronized (syncRoot) {
542: index = syncRoot.getIndex();
543: if (index == 0) {
544: syncRoot.setIndex(1);
545: }
546: }
547:
548: barrier.barrier();
549:
550: synchronized (syncRoot) {
551: if (index == 0) {
552: floatRoot = new Float(3.2D);
553: }
554: }
555:
556: barrier.barrier();
557:
558: Assert.assertEquals(new Float(3.2D), floatRoot);
559:
560: barrier.barrier();
561: }
562:
563: private void testSharedDoubleRoot() throws Exception {
564: clear();
565:
566: int index = -1;
567: synchronized (syncRoot) {
568: index = syncRoot.getIndex();
569: if (index == 0) {
570: syncRoot.setIndex(1);
571: }
572: }
573:
574: barrier.barrier();
575:
576: synchronized (syncRoot) {
577: if (index == 0) {
578: doubleRoot = new Double(3.2e4);
579: }
580: }
581:
582: barrier.barrier();
583:
584: Assert.assertEquals(new Double(3.2e4), doubleRoot);
585:
586: barrier.barrier();
587: }
588:
589: private void testSharedByteRoot() throws Exception {
590: clear();
591:
592: int index = -1;
593: synchronized (syncRoot) {
594: index = syncRoot.getIndex();
595: if (index == 0) {
596: syncRoot.setIndex(1);
597: }
598: }
599:
600: barrier.barrier();
601:
602: synchronized (syncRoot) {
603: if (index == 0) {
604: byteRoot = new Byte((byte) 5);
605: }
606: }
607:
608: barrier.barrier();
609:
610: Assert.assertEquals(new Byte((byte) 5), byteRoot);
611:
612: barrier.barrier();
613: }
614:
615: private void testSharedBooleanRoot() throws Exception {
616: clear();
617:
618: int index = -1;
619: synchronized (syncRoot) {
620: index = syncRoot.getIndex();
621: if (index == 0) {
622: syncRoot.setIndex(1);
623: }
624: }
625:
626: barrier.barrier();
627:
628: synchronized (syncRoot) {
629: if (index == 0) {
630: booleanRoot = Boolean.TRUE;
631: }
632: }
633:
634: barrier.barrier();
635:
636: Assert.assertTrue(booleanRoot.booleanValue());
637:
638: barrier.barrier();
639: }
640:
641: private void testSharedCharacterRoot() throws Exception {
642: clear();
643:
644: int index = -1;
645: synchronized (syncRoot) {
646: index = syncRoot.getIndex();
647: if (index == 0) {
648: syncRoot.setIndex(1);
649: }
650: }
651:
652: barrier.barrier();
653:
654: synchronized (syncRoot) {
655: if (index == 0) {
656: characterRoot = new Character('c');
657: }
658: }
659:
660: barrier.barrier();
661:
662: Assert.assertEquals(new Character('c'), characterRoot);
663:
664: barrier.barrier();
665: }
666:
667: private void testReferenceInequality() throws Exception {
668: clear();
669:
670: int index = -1;
671: synchronized (syncRoot) {
672: index = syncRoot.getIndex();
673: if (index == 0) {
674: syncRoot.setIndex(1);
675: }
676: }
677:
678: barrier.barrier();
679:
680: synchronized (syncRoot) {
681: if (index == 0) {
682: referenceRoot.setObj(nonSharedIntegerObject);
683: }
684: }
685:
686: barrier.barrier();
687:
688: if (index != 0) {
689: Assert.assertEquals(nonSharedIntegerObject, referenceRoot
690: .getObj());
691: Assert.assertFalse(referenceRoot
692: .isSameReferencedObject(nonSharedIntegerObject));
693: }
694:
695: barrier.barrier();
696: }
697:
698: // TODO: Needs to make this reference equality work
699: /*
700: private void testReferenceEquality() throws Exception {
701: clear();
702: int index = -1;
703: synchronized (syncRoot) {
704: index = syncRoot.getIndex();
705: if (index == 0) {
706: syncRoot.setIndex(1);
707: }
708: }
709: barrier.barrier();
710: synchronized (syncRoot) {
711: if (index == 0) {
712: integerRoot = new Integer(20);
713: }
714: }
715: barrier.barrier();
716: if (index != 0) {
717: Assert.assertEquals(new Integer(20), integerRoot);
718: }
719: barrier.barrier();
720: synchronized (syncRoot) {
721: if (index == 0) {
722: referenceRoot.setObj(integerRoot);
723: }
724: }
725: barrier.barrier();
726: if (index != 0) {
727: Assert.assertEquals(integerRoot, referenceRoot.getObj());
728: Assert.assertTrue(referenceRoot.isSameReferencedObject(integerRoot));
729: }
730: barrier.barrier();
731: }
732: */
733:
734: public static void visitL1DSOConfig(ConfigVisitor visitor,
735: DSOClientConfigHelper config) {
736: TransparencyClassSpec spec = config
737: .getOrCreateSpec(CyclicBarrier.class.getName());
738: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
739: + "*.*(..)");
740: String testClass = LiteralValueRootTestApp.class.getName();
741: spec = config.getOrCreateSpec(testClass);
742: config.addIncludePattern(testClass + "$*");
743: String methodExpression = "* " + testClass + "*.*(..)";
744: config.addWriteAutolock(methodExpression);
745: spec.addRoot("primitiveIntRoot", "primitiveIntRoot");
746: spec.addRoot("primitiveLongRoot", "primitiveLongRoot");
747: spec.addRoot("primitiveFloatRoot", "primitiveFloatRoot");
748: spec.addRoot("primitiveDoubleRoot", "primitiveDoubleRoot");
749: spec.addRoot("primitiveBooleanRoot", "primitiveBooleanRoot");
750: spec.addRoot("primitiveByteRoot", "primitiveByteRoot");
751: spec.addRoot("primitiveCharRoot", "primitiveCharRoot");
752: spec.addRoot("nonDefaultPrimitiveIntRoot",
753: "nonDefaultPrimitiveIntRoot");
754: spec.addRoot("nonDefaultIntegerRoot", "nonDefaultIntegerRoot",
755: false);
756: spec.addRoot("sharedIntegerObject", "sharedIntegerObject");
757: spec.addRoot("readBeforeSetTestIntRoot",
758: "readBeforeSetTestIntRoot");
759: spec.addRoot("classRoot", "classRoot");
760: spec.addRoot("readBeforeSetTestIntegerRoot",
761: "readBeforeSetTestIntegerRoot");
762: spec.addRoot("stringRoot", "stringRoot");
763: spec.addRoot("integerRoot", "integerRoot");
764: spec.addRoot("longRoot", "longRoot");
765: spec.addRoot("doubleRoot", "doubleRoot");
766: spec.addRoot("floatRoot", "floatRoot");
767: spec.addRoot("byteRoot", "byteRoot");
768: spec.addRoot("booleanRoot", "booleanRoot");
769: spec.addRoot("characterRoot", "characterRoot");
770: spec.addRoot("syncRoot", "syncRoot");
771: spec.addRoot("referenceRoot", "referenceRoot");
772: spec.addRoot("barrier", "barrier");
773: }
774:
775: private static class SyncRoot {
776: private int index = 0;
777: private Object obj;
778:
779: public SyncRoot() {
780: super ();
781: }
782:
783: public SyncRoot(Object o) {
784: this .obj = o;
785: }
786:
787: public int getIndex() {
788: return index;
789: }
790:
791: public void setIndex(int index) {
792: this .index = index;
793: }
794:
795: public Object getObj() {
796: return obj;
797: }
798:
799: public void setObj(Object obj) {
800: this .obj = obj;
801: }
802:
803: public boolean isSameReferencedObject(Object o) {
804: return this .obj == o;
805: }
806:
807: public void clear() {
808: this .index = 0;
809: this.obj = null;
810: }
811: }
812:
813: }
|