KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TreeMapTestApp


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.Comparator JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.TreeMap JavaDoc;
20 import java.util.Map.Entry;
21
22 public class TreeMapTestApp extends AbstractTransparentApp {
23
24   // plain old TreeMap
25
private final TreeMap JavaDoc map = new TreeMap JavaDoc();
26
27   // Use a comparator with a shared TreeMap too. If the comparator doesn't make it across VMs,
28
// then we should get some ClassCastExceptions
29
private final TreeMap JavaDoc map2 = new TreeMap JavaDoc(new WrappedStringComparator());
30
31   private final CyclicBarrier barrier;
32
33   private final SubMapKey subMapKeyRoot = new SubMapKey(0);
34
35   public TreeMapTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
36     super(appId, cfg, listenerProvider);
37     barrier = new CyclicBarrier(getParticipantCount());
38   }
39
40   public void run() {
41     try {
42       run0();
43       run1();
44     } catch (Throwable JavaDoc t) {
45       notifyError(t);
46     }
47   }
48
49   private void run0() throws Exception JavaDoc {
50     String JavaDoc me = getApplicationId();
51
52     synchronized (map) {
53       map.put(me, me + "-value");
54     }
55
56     synchronized (map2) {
57       map2.put(new WrappedString(me), me + "-value");
58     }
59
60     barrier.barrier();
61
62     validate(map, getParticipantCount(), null);
63     validate(map2, getParticipantCount(), new WrappedStringComparator());
64
65     barrier.barrier();
66
67     synchronized (map) {
68       Object JavaDoc removed = map.remove(me);
69       Assert.assertNotNull(removed);
70     }
71
72     synchronized (map2) {
73       Object JavaDoc removed = map2.remove(new WrappedString(me));
74       Assert.assertNotNull(removed);
75     }
76
77     barrier.barrier();
78
79     Assert.assertEquals(0, map.size());
80     Assert.assertEquals(0, map2.size());
81
82     barrier.barrier();
83
84     synchronized (map) {
85       map.put(me, me + "-value");
86     }
87
88     synchronized (map2) {
89       map2.put(new WrappedString(me), me + "-value");
90     }
91
92     barrier.barrier();
93
94     synchronized (map) {
95       if (map.size() == getParticipantCount()) {
96         map.clear();
97       } else {
98         Assert.assertEquals(0, map.size());
99       }
100     }
101
102     synchronized (map2) {
103       if (map2.size() == getParticipantCount()) {
104         map2.clear();
105       } else {
106         Assert.assertEquals(0, map2.size());
107       }
108     }
109
110     barrier.barrier();
111
112     synchronized (map) {
113       map.put(me, "initial");
114     }
115
116     synchronized (map2) {
117       map2.put(new WrappedString(me), "initial");
118     }
119
120     synchronized (map) {
121       Object JavaDoc prev = map.put(me, "replaced");
122       Assert.assertEquals("initial", prev);
123     }
124
125     synchronized (map2) {
126       Object JavaDoc prev = map2.put(new WrappedString(me), "replaced");
127       Assert.assertEquals("initial", prev);
128     }
129
130     barrier.barrier();
131
132     Assert.assertEquals(getParticipantCount(), map.size());
133     Assert.assertEquals(getParticipantCount(), map2.size());
134
135     for (Iterator JavaDoc i = map.values().iterator(); i.hasNext();) {
136       Assert.assertEquals("replaced", i.next());
137     }
138
139     for (Iterator JavaDoc i = map2.values().iterator(); i.hasNext();) {
140       Assert.assertEquals("replaced", i.next());
141     }
142
143     barrier.barrier();
144   }
145
146   private void run1() throws Exception JavaDoc {
147     clear();
148     initializeMaps();
149
150     // subMap() testing.
151
synchronized (map) {
152       if (map.size() == getParticipantCount()) {
153         if (map.size() > 2) {
154           Object JavaDoc fromKey = new Integer JavaDoc(0);
155           Object JavaDoc toKey = new Integer JavaDoc(10);
156           Map JavaDoc subMap = map.subMap(fromKey, toKey);
157           subMap.put(new Integer JavaDoc(1), "subMap-value");
158         }
159       } else {
160         Assert.assertEquals(getParticipantCount() + 1, map.size());
161         Assert.assertTrue(map.get(new Integer JavaDoc(1)).equals("subMap-value"));
162       }
163     }
164
165     synchronized (map2) {
166       if (map2.size() == getParticipantCount()) {
167         if (map2.size() > 2) {
168           Object JavaDoc fromKey = new WrappedString(0);
169           Object JavaDoc toKey = new WrappedString(10);
170           Map JavaDoc subMap = map2.subMap(fromKey, toKey);
171           subMap.put(new WrappedString(1), "subMap-value");
172         }
173       } else {
174         Assert.assertEquals(getParticipantCount() + 1, map2.size());
175         Assert.assertTrue(map2.get(new WrappedString(1)).equals("subMap-value"));
176       }
177     }
178
179     barrier.barrier();
180
181     clear();
182     initializeMaps();
183
184     // headMap() testing.
185
synchronized (map) {
186       if (map.size() == getParticipantCount()) {
187         if (map.size() > 2) {
188           Object JavaDoc toKey = new Integer JavaDoc(2);
189           Map JavaDoc headMap = map.headMap(toKey);
190           headMap.put(new Integer JavaDoc(1), "subMap-value");
191         }
192       } else {
193         Assert.assertEquals(getParticipantCount() + 1, map.size());
194         Assert.assertTrue(map.get(new Integer JavaDoc(1)).equals("subMap-value"));
195       }
196     }
197
198     synchronized (map2) {
199       if (map2.size() == getParticipantCount()) {
200         if (map2.size() > 2) {
201           Object JavaDoc toKey = new WrappedString(2);
202           Map JavaDoc headMap = map2.headMap(toKey);
203           headMap.put(new WrappedString(1), "subMap-value");
204         }
205       } else {
206         Assert.assertEquals(getParticipantCount() + 1, map2.size());
207         Assert.assertTrue(map2.get(new WrappedString(1)).equals("subMap-value"));
208       }
209     }
210
211     barrier.barrier();
212
213     clear();
214     initializeMaps();
215
216     // tailMap() testing.
217
synchronized (map) {
218       if (map.size() == getParticipantCount()) {
219         if (map.size() > 2) {
220           Object JavaDoc fromKey = new Integer JavaDoc(0);
221           Map JavaDoc tailMap = map.tailMap(fromKey);
222           tailMap.put(new Integer JavaDoc(1), "subMap-value");
223         }
224       } else {
225         Assert.assertEquals(getParticipantCount() + 1, map.size());
226         Assert.assertTrue(map.get(new Integer JavaDoc(1)).equals("subMap-value"));
227       }
228     }
229
230     synchronized (map2) {
231       if (map2.size() == getParticipantCount()) {
232         if (map2.size() > 2) {
233           Object JavaDoc fromKey = new WrappedString(0);
234           Map JavaDoc tailMap = map2.tailMap(fromKey);
235           tailMap.put(new WrappedString(1), "subMap-value");
236         }
237       } else {
238         Assert.assertEquals(getParticipantCount() + 1, map2.size());
239         Assert.assertTrue(map2.get(new WrappedString(1)).equals("subMap-value"));
240       }
241     }
242
243     barrier.barrier();
244
245     clear();
246     initializeMaps();
247
248     // tailMap() clear testing.
249
synchronized (map) {
250       if (map.size() == getParticipantCount()) {
251         Object JavaDoc fromKey = new Integer JavaDoc(0);
252         Map JavaDoc tailMap = map.tailMap(fromKey);
253         tailMap.clear();
254       } else {
255         Assert.assertEquals(0, map.size());
256       }
257     }
258
259     synchronized (map2) {
260       if (map2.size() == getParticipantCount()) {
261         Object JavaDoc fromKey = new WrappedString(0);
262         Map JavaDoc tailMap = map2.tailMap(fromKey);
263         tailMap.clear();
264       } else {
265         Assert.assertEquals(0, map2.size());
266       }
267     }
268
269     barrier.barrier();
270
271   }
272
273   private void clear() throws Exception JavaDoc {
274     synchronized (map) {
275       map.clear();
276     }
277     synchronized (map2) {
278       map2.clear();
279     }
280     barrier.barrier();
281   }
282
283   private void initializeMaps() throws Exception JavaDoc {
284     String JavaDoc me = getApplicationId();
285
286     synchronized (subMapKeyRoot) {
287       if (subMapKeyRoot.getKey() != 0) {
288         subMapKeyRoot.setKey(0);
289       }
290     }
291     barrier.barrier();
292
293     synchronized (map) {
294       int key = subMapKeyRoot.getKey();
295       map.put(new Integer JavaDoc(key), me + "-value");
296       subMapKeyRoot.setKey(key + 2);
297     }
298     barrier.barrier();
299     synchronized (subMapKeyRoot) {
300       if (subMapKeyRoot.getKey() != 0) {
301         subMapKeyRoot.setKey(0);
302       }
303     }
304     barrier.barrier();
305     synchronized (map2) {
306       int key = subMapKeyRoot.getKey();
307       map2.put(new WrappedString(key), me + "-value");
308       subMapKeyRoot.setKey(key + 2);
309     }
310     barrier.barrier();
311   }
312
313   private static void validate(Map JavaDoc map, int count, Comparator JavaDoc comparator) {
314     int expect = count;
315     Assert.assertEquals(expect, map.size());
316
317     TreeMap JavaDoc compare = comparator == null ? new TreeMap JavaDoc() : new TreeMap JavaDoc(comparator);
318     compare.putAll(map);
319
320     Iterator JavaDoc sharedIter = map.entrySet().iterator();
321     Iterator JavaDoc localIter = compare.entrySet().iterator();
322
323     while (true) {
324       Entry sharedEntry = (Entry) sharedIter.next();
325       Entry localEntry = (Entry) localIter.next();
326
327       Object JavaDoc sharedKey = sharedEntry.getKey();
328       Object JavaDoc localKey = localEntry.getKey();
329       Assert.assertEquals(localKey, sharedKey);
330
331       Object JavaDoc sharedValue = sharedEntry.getValue();
332       Object JavaDoc localValue = localEntry.getValue();
333       Assert.assertEquals(localValue, sharedValue);
334
335       if (sharedIter.hasNext()) {
336         Assert.assertTrue(localIter.hasNext());
337       } else {
338         break;
339       }
340     }
341
342   }
343
344   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
345     TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName());
346     config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)");
347
348     String JavaDoc testClass = TreeMapTestApp.class.getName();
349     spec = config.getOrCreateSpec(testClass);
350     
351     config.addIncludePattern(testClass + "$*");
352
353     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
354     config.addWriteAutolock(methodExpression);
355
356     spec.addRoot("map", "map");
357     spec.addRoot("map2", "map2");
358     spec.addRoot("barrier", "barrier");
359     spec.addRoot("subMapKeyRoot", "subMapKeyRoot");
360     
361     config.addIncludePattern(WrappedStringComparator.class.getName());
362     config.addIncludePattern(WrappedString.class.getName());
363   }
364
365   // The main purpose of this class is that it does NOT implement Comparable
366
private static class WrappedString {
367     private final String JavaDoc string;
368
369     WrappedString(String JavaDoc string) {
370       this.string = string;
371     }
372
373     WrappedString(int i) {
374       this.string = String.valueOf(i);
375     }
376
377     String JavaDoc getString() {
378       return this.string;
379     }
380   }
381
382   private static class WrappedStringComparator implements Comparator JavaDoc {
383
384     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
385       WrappedString ws1 = (WrappedString) o1;
386       WrappedString ws2 = (WrappedString) o2;
387       return ws1.getString().compareTo(ws2.getString());
388     }
389
390   }
391
392   /**
393    * The main purpose of this class is for to generate the key for the subMap(), headMap(), and tailMap() testing.
394    */

395   private static class SubMapKey {
396     private int key;
397
398     public SubMapKey(int key) {
399       this.key = key;
400     }
401
402     public int getKey() {
403       return key;
404     }
405
406     public void setKey(int key) {
407       this.key = key;
408     }
409   }
410 }
411
Popular Tags