KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > MutableInstrumentedEnumTest


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

5 package com.tctest;
6
7 import com.tc.object.config.ConfigVisitor;
8 import com.tc.object.config.DSOClientConfigHelper;
9 import com.tc.object.config.TransparencyClassSpec;
10 import com.tc.simulator.app.ApplicationConfig;
11 import com.tc.simulator.listener.ListenerProvider;
12 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.concurrent.CyclicBarrier JavaDoc;
17
18 public class MutableInstrumentedEnumTest extends TransparentTestBase {
19
20   private static final int NODE_COUNT = 3;
21
22   public void doSetUp(TransparentTestIface t) throws Exception JavaDoc {
23     t.getTransparentAppConfig().setClientCount(NODE_COUNT);
24     t.initializeTestRunner();
25   }
26
27   protected Class JavaDoc getApplicationClass() {
28     return MutableInstrumentedEnumTestApp.class;
29   }
30
31   public static class MutableInstrumentedEnumTestApp extends AbstractErrorCatchingTransparentApp {
32
33     private final Map JavaDoc<String JavaDoc, Ref> mapRoot = new HashMap JavaDoc<String JavaDoc, Ref>();
34     private final CyclicBarrier JavaDoc barrier;
35
36     public MutableInstrumentedEnumTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
37       super(appId, cfg, listenerProvider);
38       barrier = new CyclicBarrier JavaDoc(getParticipantCount());
39     }
40
41     protected void runTest() throws Throwable JavaDoc {
42
43       int index = barrier.await();
44
45       MutableEnum e = MutableEnum.THREE;
46
47       if (index == 0) {
48         synchronized (mapRoot) {
49           mapRoot.put("e", new Ref(e));
50         }
51       }
52
53       barrier.await();
54
55       if (index == 0) {
56         synchronized (mapRoot) {
57           e.mutate();
58         }
59       }
60
61       barrier.await();
62
63       if (index != 0) {
64         // acquiring this lock should force the potentially bad TXN (containing the change to the shared mutable enum)
65
// to be flushed
66
synchronized (mapRoot) {
67           mapRoot.clear();
68         }
69       }
70
71       barrier.await();
72
73     }
74
75     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
76       String JavaDoc testClass = MutableInstrumentedEnumTestApp.class.getName();
77       TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
78
79       config.getOrCreateSpec(MutableEnum.class.getName());
80       config.getOrCreateSpec(Ref.class.getName());
81
82       String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
83       config.addWriteAutolock(methodExpression);
84
85       spec.addRoot("barrier", "barrier");
86       spec.addRoot("mapRoot", "mapRoot");
87     }
88
89   }
90
91   public enum MutableEnum {
92     ONE, TWO, THREE;
93
94     private int mutableState;
95
96     int mutate() {
97       return ++mutableState;
98     }
99   }
100
101   private static class Ref {
102
103     private final Object JavaDoc o;
104
105     Ref(Object JavaDoc o) {
106       this.o = o;
107     }
108
109     Object JavaDoc getO() {
110       return o;
111     }
112
113   }
114
115 }
116
Popular Tags