KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > AtomicIntegerTestApp


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 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
17
18 public class AtomicIntegerTestApp extends AbstractTransparentApp {
19
20   private final CyclicBarrier barrier;
21
22   private final DataRoot root = new DataRoot();
23
24   public AtomicIntegerTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
25     super(appId, cfg, listenerProvider);
26     barrier = new CyclicBarrier(getParticipantCount());
27   }
28
29   public void run() {
30     try {
31       atomicIntegerTesting();
32     } catch (Throwable JavaDoc t) {
33       notifyError(t);
34     }
35   }
36
37   private void atomicIntegerTesting() throws Exception JavaDoc {
38     basicSetTesting();
39     basicGetTesting();
40     addAndGetTesting();
41     compareAndSetTesting();
42     weakCompareAndSetTesting();
43     getAndIncrementTesting();
44     getAndDecrementTesting();
45     getAndSetTesting();
46     getAndAddTesting();
47     incrementAndGetTesting();
48     decrementAndGetTesting();
49   }
50
51   private void clear() throws Exception JavaDoc {
52     synchronized (root) {
53       root.clear();
54     }
55
56     barrier.barrier();
57   }
58
59   private void initialize() throws Exception JavaDoc {
60
61     int index = barrier.barrier();
62
63     if (index == 0) {
64       root.getIntValue().set(10);
65     }
66
67     barrier.barrier();
68
69   }
70
71   private void basicSetTesting() throws Exception JavaDoc {
72     clear();
73     initialize();
74
75     Assert.assertEquals(10, root.getIntValue().get());
76
77     barrier.barrier();
78   }
79
80   private void basicGetTesting() throws Exception JavaDoc {
81     clear();
82     initialize();
83
84     Assert.assertEquals(10, root.getIntValue().get());
85     Assert.assertEquals(10.0D, root.getIntValue().doubleValue());
86     Assert.assertEquals((byte) 10, root.getIntValue().byteValue());
87     Assert.assertEquals(10.0f, root.getIntValue().floatValue());
88     Assert.assertEquals(10, root.getIntValue().intValue());
89     Assert.assertEquals(10L, root.getIntValue().longValue());
90     Assert.assertEquals(10, root.getIntValue().shortValue());
91
92     barrier.barrier();
93   }
94
95   private void addAndGetTesting() throws Exception JavaDoc {
96     clear();
97     initialize();
98
99     int index = barrier.barrier();
100
101     if (index == 0) {
102       int val = root.getIntValue().addAndGet(4);
103       Assert.assertEquals(14, val);
104     }
105
106     barrier.barrier();
107
108     Assert.assertEquals(14, root.getIntValue().get());
109
110     barrier.barrier();
111   }
112
113   private void compareAndSetTesting() throws Exception JavaDoc {
114     clear();
115     initialize();
116
117     int index = barrier.barrier();
118     if (index == 0) {
119       root.getIntValue().compareAndSet(10, 18);
120     }
121
122     barrier.barrier();
123
124     Assert.assertEquals(18, root.getIntValue().get());
125
126     barrier.barrier();
127   }
128
129   private void weakCompareAndSetTesting() throws Exception JavaDoc {
130     clear();
131     initialize();
132
133     int index = barrier.barrier();
134     if (index == 0) {
135       root.getIntValue().weakCompareAndSet(10, 20);
136     }
137
138     barrier.barrier();
139
140     Assert.assertEquals(20, root.getIntValue().get());
141
142     barrier.barrier();
143   }
144
145   private void getAndIncrementTesting() throws Exception JavaDoc {
146     clear();
147     initialize();
148
149     int index = barrier.barrier();
150     if (index == 0) {
151       int val = root.getIntValue().getAndIncrement();
152       Assert.assertEquals(10, val);
153     }
154
155     barrier.barrier();
156
157     Assert.assertEquals(11, root.getIntValue().get());
158
159     barrier.barrier();
160   }
161
162   private void getAndDecrementTesting() throws Exception JavaDoc {
163     clear();
164     initialize();
165
166     int index = barrier.barrier();
167     if (index == 0) {
168       int val = root.getIntValue().getAndDecrement();
169       Assert.assertEquals(10, val);
170     }
171
172     barrier.barrier();
173
174     Assert.assertEquals(9, root.getIntValue().get());
175
176     barrier.barrier();
177   }
178
179   private void getAndSetTesting() throws Exception JavaDoc {
180     clear();
181     initialize();
182
183     int index = barrier.barrier();
184     if (index == 0) {
185       int val = root.getIntValue().getAndSet(200);
186       Assert.assertEquals(10, val);
187     }
188
189     barrier.barrier();
190
191     Assert.assertEquals(200, root.getIntValue().get());
192
193     barrier.barrier();
194   }
195
196   private void getAndAddTesting() throws Exception JavaDoc {
197     clear();
198     initialize();
199
200     int index = barrier.barrier();
201     if (index == 0) {
202       int val = root.getIntValue().getAndAdd(5);
203       Assert.assertEquals(10, val);
204     }
205
206     barrier.barrier();
207
208     Assert.assertEquals(15, root.getIntValue().get());
209
210     barrier.barrier();
211   }
212
213   private void incrementAndGetTesting() throws Exception JavaDoc {
214     clear();
215     initialize();
216
217     int index = barrier.barrier();
218     if (index == 0) {
219       int val = root.getIntValue().incrementAndGet();
220       Assert.assertEquals(11, val);
221     }
222
223     barrier.barrier();
224
225     Assert.assertEquals(11, root.getIntValue().get());
226
227     barrier.barrier();
228   }
229
230   private void decrementAndGetTesting() throws Exception JavaDoc {
231     clear();
232     initialize();
233
234     int index = barrier.barrier();
235     if (index == 0) {
236       int val = root.getIntValue().decrementAndGet();
237       Assert.assertEquals(9, val);
238     }
239
240     barrier.barrier();
241
242     Assert.assertEquals(9, root.getIntValue().get());
243
244     barrier.barrier();
245   }
246
247   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
248     TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName());
249     config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)");
250
251     String JavaDoc testClass = AtomicIntegerTestApp.class.getName();
252     spec = config.getOrCreateSpec(testClass);
253
254     config.addIncludePattern(testClass + "$*");
255
256     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
257     config.addWriteAutolock(methodExpression);
258
259     spec.addRoot("barrier", "barrier");
260     spec.addRoot("root", "root");
261   }
262
263   private static class DataRoot {
264     private AtomicInteger JavaDoc intValue = new AtomicInteger JavaDoc(0);
265
266     public DataRoot() {
267       super();
268     }
269
270     public AtomicInteger JavaDoc getIntValue() {
271       return intValue;
272     }
273
274     public void setIntValue(AtomicInteger JavaDoc intValue) {
275       this.intValue = intValue;
276     }
277
278     public void clear() {
279       intValue.set(0);
280     }
281   }
282 }
283
Popular Tags