KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > LiteralValueRootTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.TransparencyClassSpec;
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 public class LiteralValueRootTestApp extends AbstractTransparentApp {
17
18   private final SyncRoot syncRoot = new SyncRoot();
19   private final SyncRoot referenceRoot = new SyncRoot();
20
21   private String JavaDoc stringRoot = null;
22   private Integer JavaDoc integerRoot = null;
23   private Long JavaDoc longRoot = null;
24   private Double JavaDoc doubleRoot = null;
25   private Float JavaDoc floatRoot = null;
26   private Byte JavaDoc byteRoot = null;
27   private Boolean JavaDoc booleanRoot = null;
28   private Character JavaDoc characterRoot = null;
29
30   private Class JavaDoc classRoot = null;
31
32   private Integer JavaDoc nonSharedIntegerObject = new Integer JavaDoc(10);
33
34   private int nonDefaultPrimitiveIntRoot = 5;
35   private Integer JavaDoc nonDefaultIntegerRoot = new Integer JavaDoc(5);
36   private Integer JavaDoc sharedIntegerObject = new Integer JavaDoc(50);
37
38   private int readBeforeSetTestIntRoot;
39   private Integer JavaDoc readBeforeSetTestIntegerRoot;
40
41   private long primitiveLongRoot;
42   private int primitiveIntRoot;
43   private float primitiveFloatRoot;
44   private double primitiveDoubleRoot;
45   private boolean primitiveBooleanRoot;
46   private byte primitiveByteRoot;
47   private char primitiveCharRoot;
48
49   private final CyclicBarrier barrier;
50
51   public LiteralValueRootTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
52     super(appId, cfg, listenerProvider);
53     barrier = new CyclicBarrier(getParticipantCount());
54   }
55
56   public void run() {
57     try {
58       testGetBeforeSetObject();
59       testGetBeforeSetPrimitive();
60
61       testDefaultPrimitiveValues();
62       testMultipleRootSetting();
63
64       testSharedPrimitiveIntRoot();
65       testSharedPrimitiveLongRoot();
66       testSharedPrimitiveDoubleRoot();
67       testSharedPrimitiveFloatRoot();
68       testSharedPrimitiveBooleanRoot();
69       testSharedPrimitiveByteRoot();
70       testSharedPrimitiveCharRoot();
71
72       testSharedClassRoot();
73       testSharedStringRoot();
74       testSharedIntegerRoot();
75       testSharedLongRoot();
76       testSharedFloatRoot();
77       testSharedDoubleRoot();
78       testSharedByteRoot();
79       testSharedBooleanRoot();
80       testSharedCharacterRoot();
81       testReferenceInequality();
82
83       // testReferenceEquality();
84
} catch (Throwable JavaDoc t) {
85       notifyError(t);
86     }
87   }
88
89   private void testMultipleRootSetting() throws Exception JavaDoc {
90     clear();
91
92     int index = -1;
93     synchronized (syncRoot) {
94       index = syncRoot.getIndex();
95       if (index == 0) {
96         syncRoot.setIndex(1);
97       }
98     }
99
100     barrier.barrier();
101
102     synchronized (syncRoot) {
103       if (index == 0) {
104         nonDefaultPrimitiveIntRoot = 10;
105       }
106     }
107
108     barrier.barrier();
109
110     Assert.assertEquals(10, nonDefaultPrimitiveIntRoot);
111
112     barrier.barrier();
113
114     synchronized (syncRoot) {
115       if (index == 0) {
116         nonDefaultIntegerRoot = new Integer JavaDoc(10);
117       }
118     }
119
120     barrier.barrier();
121
122     Assert.assertEquals(new Integer JavaDoc(10), nonDefaultIntegerRoot);
123
124     barrier.barrier();
125
126     synchronized (syncRoot) {
127       if (index == 0) {
128         nonDefaultIntegerRoot = sharedIntegerObject;
129       }
130     }
131
132     barrier.barrier();
133
134     Assert.assertEquals(new Integer JavaDoc(50), nonDefaultIntegerRoot);
135
136     barrier.barrier();
137
138   }
139
140   private void testDefaultPrimitiveValues() throws Exception JavaDoc {
141     Assert.assertEquals(0, primitiveIntRoot);
142     Assert.assertEquals(0L, primitiveLongRoot);
143     Assert.assertEquals(0.0D, primitiveDoubleRoot);
144     Assert.assertEquals(0.0F, primitiveFloatRoot);
145     Assert.assertEquals((byte) 0, primitiveByteRoot);
146     Assert.assertFalse(primitiveBooleanRoot);
147     barrier.barrier();
148   }
149
150   private void testGetBeforeSetObject() throws Exception JavaDoc {
151     clear();
152
153     int index = -1;
154     synchronized (syncRoot) {
155       index = syncRoot.getIndex();
156       if (index == 0) {
157         syncRoot.setIndex(1);
158       }
159     }
160
161     barrier.barrier();
162
163     Assert.assertNull(readBeforeSetTestIntegerRoot);
164
165     barrier.barrier();
166
167     if (index == 0) {
168       while (readBeforeSetTestIntegerRoot == null) {
169         if (readBeforeSetTestIntegerRoot != null) {
170           break;
171         }
172       }
173     } else {
174       synchronized (syncRoot) {
175         readBeforeSetTestIntegerRoot = new Integer JavaDoc(100);
176       }
177     }
178
179     barrier.barrier();
180
181     Assert.assertEquals(new Integer JavaDoc(100), readBeforeSetTestIntegerRoot);
182
183     barrier.barrier();
184   }
185
186   private void testGetBeforeSetPrimitive() throws Exception JavaDoc {
187     clear();
188
189     int index = -1;
190     synchronized (syncRoot) {
191       index = syncRoot.getIndex();
192       if (index == 0) {
193         syncRoot.setIndex(1);
194       }
195     }
196
197     barrier.barrier();
198
199     Assert.assertEquals(0, readBeforeSetTestIntRoot);
200
201     barrier.barrier();
202
203     if (index == 0) {
204       while (readBeforeSetTestIntRoot == 0) {
205         if (readBeforeSetTestIntRoot != 0) {
206           break;
207         }
208       }
209     } else {
210       synchronized (syncRoot) {
211         readBeforeSetTestIntRoot = 10;
212       }
213     }
214
215     barrier.barrier();
216
217     Assert.assertEquals(10, readBeforeSetTestIntRoot);
218
219     barrier.barrier();
220   }
221
222   private void testSharedPrimitiveLongRoot() throws Exception JavaDoc {
223     clear();
224
225     int index = -1;
226     synchronized (syncRoot) {
227       index = syncRoot.getIndex();
228       if (index == 0) {
229         syncRoot.setIndex(1);
230       }
231     }
232
233     barrier.barrier();
234
235     synchronized (syncRoot) {
236       if (index == 0) {
237         primitiveLongRoot = 32L;
238       }
239     }
240
241     barrier.barrier();
242
243     Assert.assertEquals(32L, primitiveLongRoot);
244
245     barrier.barrier();
246
247   }
248
249   private void testSharedPrimitiveIntRoot() throws Exception JavaDoc {
250     clear();
251
252     int index = -1;
253     synchronized (syncRoot) {
254       index = syncRoot.getIndex();
255       if (index == 0) {
256         syncRoot.setIndex(1);
257       }
258     }
259
260     barrier.barrier();
261
262     synchronized (syncRoot) {
263       if (index == 0) {
264         primitiveIntRoot = 32;
265       }
266     }
267
268     barrier.barrier();
269
270     Assert.assertEquals(32, primitiveIntRoot);
271
272     barrier.barrier();
273
274     synchronized (syncRoot) {
275       if (index == 0) {
276         primitiveIntRoot++;
277       }
278     }
279
280     barrier.barrier();
281
282     Assert.assertEquals(33, primitiveIntRoot);
283
284     barrier.barrier();
285   }
286
287   private void testSharedPrimitiveDoubleRoot() throws Exception JavaDoc {
288     clear();
289
290     int index = -1;
291     synchronized (syncRoot) {
292       index = syncRoot.getIndex();
293       if (index == 0) {
294         syncRoot.setIndex(1);
295       }
296     }
297
298     barrier.barrier();
299
300     synchronized (syncRoot) {
301       if (index == 0) {
302         primitiveDoubleRoot = 2e4;
303       }
304     }
305
306     barrier.barrier();
307
308     Assert.assertEquals(2e4, primitiveDoubleRoot);
309
310     barrier.barrier();
311
312   }
313
314   private void testSharedPrimitiveFloatRoot() throws Exception JavaDoc {
315     clear();
316
317     int index = -1;
318     synchronized (syncRoot) {
319       index = syncRoot.getIndex();
320       if (index == 0) {
321         syncRoot.setIndex(1);
322       }
323     }
324
325     barrier.barrier();
326
327     synchronized (syncRoot) {
328       if (index == 0) {
329         primitiveFloatRoot = 0.3f;
330       }
331     }
332
333     barrier.barrier();
334
335     Assert.assertEquals(0.3f, primitiveFloatRoot);
336
337     barrier.barrier();
338
339   }
340
341   private void testSharedPrimitiveByteRoot() throws Exception JavaDoc {
342     clear();
343
344     int index = -1;
345     synchronized (syncRoot) {
346       index = syncRoot.getIndex();
347       if (index == 0) {
348         syncRoot.setIndex(1);
349       }
350     }
351
352     barrier.barrier();
353
354     synchronized (syncRoot) {
355       if (index == 0) {
356         primitiveByteRoot = (byte) 10;
357       }
358     }
359
360     barrier.barrier();
361
362     Assert.assertEquals((byte) 10, primitiveByteRoot);
363
364     barrier.barrier();
365
366   }
367
368   private void testSharedPrimitiveBooleanRoot() throws Exception JavaDoc {
369     clear();
370
371     int index = -1;
372     synchronized (syncRoot) {
373       index = syncRoot.getIndex();
374       if (index == 0) {
375         syncRoot.setIndex(1);
376       }
377     }
378
379     barrier.barrier();
380
381     synchronized (syncRoot) {
382       if (index == 0) {
383         primitiveBooleanRoot = true;
384       }
385     }
386
387     barrier.barrier();
388
389     Assert.assertTrue(primitiveBooleanRoot);
390
391     barrier.barrier();
392
393   }
394
395   private void testSharedPrimitiveCharRoot() throws Exception JavaDoc {
396     clear();
397
398     int index = -1;
399     synchronized (syncRoot) {
400       index = syncRoot.getIndex();
401       if (index == 0) {
402         syncRoot.setIndex(1);
403       }
404     }
405
406     barrier.barrier();
407
408     synchronized (syncRoot) {
409       if (index == 0) {
410         primitiveCharRoot = 'c';
411       }
412     }
413
414     barrier.barrier();
415
416     Assert.assertEquals('c', primitiveCharRoot);
417
418     barrier.barrier();
419
420   }
421
422   private void testSharedClassRoot() throws Exception JavaDoc {
423     clear();
424
425     int index = -1;
426     synchronized (syncRoot) {
427       index = syncRoot.getIndex();
428       if (index == 0) {
429         syncRoot.setIndex(1);
430       }
431     }
432
433     barrier.barrier();
434
435     synchronized (syncRoot) {
436       if (index == 0) {
437         classRoot = Float JavaDoc.class;
438       }
439     }
440
441     barrier.barrier();
442
443     Assert.assertEquals("java.lang.Float", classRoot.getName());
444
445     barrier.barrier();
446   }
447
448   private void clear() throws Exception JavaDoc {
449     synchronized (syncRoot) {
450       syncRoot.setIndex(0);
451       referenceRoot.clear();
452     }
453
454     barrier.barrier();
455   }
456
457   private void testSharedStringRoot() throws Exception JavaDoc {
458     clear();
459
460     int index = -1;
461     synchronized (syncRoot) {
462       index = syncRoot.getIndex();
463       if (index == 0) {
464         syncRoot.setIndex(1);
465       }
466     }
467
468     barrier.barrier();
469
470     synchronized (syncRoot) {
471       if (index == 0) {
472         stringRoot = "Shared value";
473       }
474     }
475
476     barrier.barrier();
477
478     Assert.assertEquals("Shared value", stringRoot);
479
480     barrier.barrier();
481   }
482
483   private void testSharedIntegerRoot() throws Exception JavaDoc {
484     clear();
485
486     int index = -1;
487     synchronized (syncRoot) {
488       index = syncRoot.getIndex();
489       if (index == 0) {
490         syncRoot.setIndex(1);
491       }
492     }
493
494     barrier.barrier();
495
496     synchronized (syncRoot) {
497       if (index == 0) {
498         integerRoot = new Integer JavaDoc(4);
499       }
500     }
501
502     barrier.barrier();
503
504     Assert.assertEquals(new Integer JavaDoc(4), integerRoot);
505
506     barrier.barrier();
507   }
508
509   private void testSharedLongRoot() throws Exception JavaDoc {
510     clear();
511
512     int index = -1;
513     synchronized (syncRoot) {
514       index = syncRoot.getIndex();
515       if (index == 0) {
516         syncRoot.setIndex(1);
517       }
518     }
519
520     barrier.barrier();
521
522     synchronized (syncRoot) {
523       if (index == 0) {
524         longRoot = new Long JavaDoc(10L);
525       }
526     }
527
528     barrier.barrier();
529
530     Assert.assertEquals(new Long JavaDoc(10L), longRoot);
531
532     barrier.barrier();
533   }
534
535   private void testSharedFloatRoot() throws Exception JavaDoc {
536     clear();
537
538     int index = -1;
539     synchronized (syncRoot) {
540       index = syncRoot.getIndex();
541       if (index == 0) {
542         syncRoot.setIndex(1);
543       }
544     }
545
546     barrier.barrier();
547
548     synchronized (syncRoot) {
549       if (index == 0) {
550         floatRoot = new Float JavaDoc(3.2D);
551       }
552     }
553
554     barrier.barrier();
555
556     Assert.assertEquals(new Float JavaDoc(3.2D), floatRoot);
557
558     barrier.barrier();
559   }
560
561   private void testSharedDoubleRoot() throws Exception JavaDoc {
562     clear();
563
564     int index = -1;
565     synchronized (syncRoot) {
566       index = syncRoot.getIndex();
567       if (index == 0) {
568         syncRoot.setIndex(1);
569       }
570     }
571
572     barrier.barrier();
573
574     synchronized (syncRoot) {
575       if (index == 0) {
576         doubleRoot = new Double JavaDoc(3.2e4);
577       }
578     }
579
580     barrier.barrier();
581
582     Assert.assertEquals(new Double JavaDoc(3.2e4), doubleRoot);
583
584     barrier.barrier();
585   }
586
587   private void testSharedByteRoot() throws Exception JavaDoc {
588     clear();
589
590     int index = -1;
591     synchronized (syncRoot) {
592       index = syncRoot.getIndex();
593       if (index == 0) {
594         syncRoot.setIndex(1);
595       }
596     }
597
598     barrier.barrier();
599
600     synchronized (syncRoot) {
601       if (index == 0) {
602         byteRoot = new Byte JavaDoc((byte) 5);
603       }
604     }
605
606     barrier.barrier();
607
608     Assert.assertEquals(new Byte JavaDoc((byte) 5), byteRoot);
609
610     barrier.barrier();
611   }
612
613   private void testSharedBooleanRoot() throws Exception JavaDoc {
614     clear();
615
616     int index = -1;
617     synchronized (syncRoot) {
618       index = syncRoot.getIndex();
619       if (index == 0) {
620         syncRoot.setIndex(1);
621       }
622     }
623
624     barrier.barrier();
625
626     synchronized (syncRoot) {
627       if (index == 0) {
628         booleanRoot = Boolean.TRUE;
629       }
630     }
631
632     barrier.barrier();
633
634     Assert.assertTrue(booleanRoot.booleanValue());
635
636     barrier.barrier();
637   }
638
639   private void testSharedCharacterRoot() throws Exception JavaDoc {
640     clear();
641
642     int index = -1;
643     synchronized (syncRoot) {
644       index = syncRoot.getIndex();
645       if (index == 0) {
646         syncRoot.setIndex(1);
647       }
648     }
649
650     barrier.barrier();
651
652     synchronized (syncRoot) {
653       if (index == 0) {
654         characterRoot = new Character JavaDoc('c');
655       }
656     }
657
658     barrier.barrier();
659
660     Assert.assertEquals(new Character JavaDoc('c'), characterRoot);
661
662     barrier.barrier();
663   }
664
665   private void testReferenceInequality() throws Exception JavaDoc {
666     clear();
667
668     int index = -1;
669     synchronized (syncRoot) {
670       index = syncRoot.getIndex();
671       if (index == 0) {
672         syncRoot.setIndex(1);
673       }
674     }
675
676     barrier.barrier();
677
678     synchronized (syncRoot) {
679       if (index == 0) {
680         referenceRoot.setObj(nonSharedIntegerObject);
681       }
682     }
683
684     barrier.barrier();
685
686     if (index != 0) {
687       Assert.assertEquals(nonSharedIntegerObject, referenceRoot.getObj());
688       Assert.assertFalse(referenceRoot.isSameReferencedObject(nonSharedIntegerObject));
689     }
690
691     barrier.barrier();
692   }
693
694   // TODO: Needs to make this reference equality work
695
/*
696   private void testReferenceEquality() throws Exception {
697     clear();
698     int index = -1;
699     synchronized (syncRoot) {
700       index = syncRoot.getIndex();
701       if (index == 0) {
702         syncRoot.setIndex(1);
703       }
704     }
705     barrier.barrier();
706     synchronized (syncRoot) {
707       if (index == 0) {
708         integerRoot = new Integer(20);
709       }
710     }
711     barrier.barrier();
712     if (index != 0) {
713       Assert.assertEquals(new Integer(20), integerRoot);
714     }
715     barrier.barrier();
716     synchronized (syncRoot) {
717       if (index == 0) {
718         referenceRoot.setObj(integerRoot);
719       }
720     }
721     barrier.barrier();
722     if (index != 0) {
723       Assert.assertEquals(integerRoot, referenceRoot.getObj());
724       Assert.assertTrue(referenceRoot.isSameReferencedObject(integerRoot));
725     }
726     barrier.barrier();
727   }
728   */

729
730   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
731     TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName());
732     config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)");
733     String JavaDoc testClass = LiteralValueRootTestApp.class.getName();
734     spec = config.getOrCreateSpec(testClass);
735     config.addIncludePattern(testClass + "$*");
736     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
737     config.addWriteAutolock(methodExpression);
738     spec.addRoot("primitiveIntRoot", "primitiveIntRoot");
739     spec.addRoot("primitiveLongRoot", "primitiveLongRoot");
740     spec.addRoot("primitiveFloatRoot", "primitiveFloatRoot");
741     spec.addRoot("primitiveDoubleRoot", "primitiveDoubleRoot");
742     spec.addRoot("primitiveBooleanRoot", "primitiveBooleanRoot");
743     spec.addRoot("primitiveByteRoot", "primitiveByteRoot");
744     spec.addRoot("primitiveCharRoot", "primitiveCharRoot");
745     spec.addRoot("nonDefaultPrimitiveIntRoot", "nonDefaultPrimitiveIntRoot");
746     spec.addRoot("nonDefaultIntegerRoot", "nonDefaultIntegerRoot", false);
747     spec.addRoot("sharedIntegerObject", "sharedIntegerObject");
748     spec.addRoot("readBeforeSetTestIntRoot", "readBeforeSetTestIntRoot");
749     spec.addRoot("classRoot", "classRoot");
750     spec.addRoot("readBeforeSetTestIntegerRoot", "readBeforeSetTestIntegerRoot");
751     spec.addRoot("stringRoot", "stringRoot");
752     spec.addRoot("integerRoot", "integerRoot");
753     spec.addRoot("longRoot", "longRoot");
754     spec.addRoot("doubleRoot", "doubleRoot");
755     spec.addRoot("floatRoot", "floatRoot");
756     spec.addRoot("byteRoot", "byteRoot");
757     spec.addRoot("booleanRoot", "booleanRoot");
758     spec.addRoot("characterRoot", "characterRoot");
759     spec.addRoot("syncRoot", "syncRoot");
760     spec.addRoot("referenceRoot", "referenceRoot");
761     spec.addRoot("barrier", "barrier");
762   }
763
764   private static class SyncRoot {
765     private int index = 0;
766     private Object JavaDoc obj;
767
768     public SyncRoot() {
769       super();
770     }
771
772     public SyncRoot(Object JavaDoc o) {
773       this.obj = o;
774     }
775
776     public int getIndex() {
777       return index;
778     }
779
780     public void setIndex(int index) {
781       this.index = index;
782     }
783
784     public Object JavaDoc getObj() {
785       return obj;
786     }
787
788     public void setObj(Object JavaDoc obj) {
789       this.obj = obj;
790     }
791
792     public boolean isSameReferencedObject(Object JavaDoc o) {
793       return this.obj == o;
794     }
795
796     public void clear() {
797       this.index = 0;
798       this.obj = null;
799     }
800   }
801
802 }
803
Popular Tags