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: import java.util.Comparator;
017: import java.util.Iterator;
018: import java.util.Map;
019: import java.util.TreeMap;
020: import java.util.Map.Entry;
021:
022: public class TreeMapTestApp extends AbstractTransparentApp {
023:
024: // plain old TreeMap
025: private final TreeMap map = new TreeMap();
026:
027: // Use a comparator with a shared TreeMap too. If the comparator doesn't make it across VMs,
028: // then we should get some ClassCastExceptions
029: private final TreeMap map2 = new TreeMap(
030: new WrappedStringComparator());
031:
032: private final CyclicBarrier barrier;
033:
034: private final SubMapKey subMapKeyRoot = new SubMapKey(0);
035:
036: private final int loopcount = 200;
037:
038: public TreeMapTestApp(String appId, ApplicationConfig cfg,
039: ListenerProvider listenerProvider) {
040: super (appId, cfg, listenerProvider);
041: barrier = new CyclicBarrier(getParticipantCount());
042: }
043:
044: public void run() {
045: try {
046: for (int i = 0; i < loopcount; ++i) {
047: System.out.println("*** TreeMap LoopCount:" + i);
048: clear();
049: run0();
050: run1();
051: }
052: } catch (Throwable t) {
053: notifyError(t);
054: }
055: }
056:
057: private void run0() throws Exception {
058: String me = getApplicationId();
059:
060: synchronized (map) {
061: map.put(me, me + "-value");
062: }
063:
064: synchronized (map2) {
065: map2.put(new WrappedString(me), me + "-value");
066: }
067:
068: barrier.barrier();
069:
070: validate(map, getParticipantCount(), null);
071: validate(map2, getParticipantCount(),
072: new WrappedStringComparator());
073:
074: barrier.barrier();
075:
076: synchronized (map) {
077: Object removed = map.remove(me);
078: Assert.assertNotNull(removed);
079: }
080:
081: synchronized (map2) {
082: Object removed = map2.remove(new WrappedString(me));
083: Assert.assertNotNull(removed);
084: }
085:
086: barrier.barrier();
087:
088: Assert.assertEquals(0, getMapSize(map));
089: Assert.assertEquals(0, getMapSize(map2));
090:
091: barrier.barrier();
092:
093: synchronized (map) {
094: map.put(me, me + "-value");
095: }
096:
097: synchronized (map2) {
098: map2.put(new WrappedString(me), me + "-value");
099: }
100:
101: barrier.barrier();
102:
103: synchronized (map) {
104: if (map.size() == getParticipantCount()) {
105: map.clear();
106: } else {
107: Assert.assertEquals(0, map.size());
108: }
109: }
110:
111: synchronized (map2) {
112: if (map2.size() == getParticipantCount()) {
113: map2.clear();
114: } else {
115: Assert.assertEquals(0, map2.size());
116: }
117: }
118:
119: barrier.barrier();
120:
121: synchronized (map) {
122: map.put(me, "initial");
123: }
124:
125: synchronized (map2) {
126: map2.put(new WrappedString(me), "initial");
127: }
128:
129: synchronized (map) {
130: Object prev = map.put(me, "replaced");
131: Assert.assertEquals("initial", prev);
132: }
133:
134: synchronized (map2) {
135: Object prev = map2.put(new WrappedString(me), "replaced");
136: Assert.assertEquals("initial", prev);
137: }
138:
139: barrier.barrier();
140:
141: Assert.assertEquals(getParticipantCount(), getMapSize(map));
142: Assert.assertEquals(getParticipantCount(), getMapSize(map2));
143:
144: // synchronization here to have dso previous transactions completed for the object.
145: synchronized (map) {
146: for (Iterator i = map.values().iterator(); i.hasNext();) {
147: Assert.assertEquals("replaced", i.next());
148: }
149: }
150:
151: synchronized (map2) {
152: for (Iterator i = map2.values().iterator(); i.hasNext();) {
153: Assert.assertEquals("replaced", i.next());
154: }
155: }
156:
157: barrier.barrier();
158: }
159:
160: private void run1() throws Exception {
161: clear();
162: initializeMaps();
163:
164: // subMap() testing.
165: synchronized (map) {
166: if (map.size() == getParticipantCount()) {
167: if (map.size() > 2) {
168: Object fromKey = new Integer(0);
169: Object toKey = new Integer(10);
170: Map subMap = map.subMap(fromKey, toKey);
171: subMap.put(new Integer(1), "subMap-value");
172: }
173: } else {
174: Assert.assertEquals(getParticipantCount() + 1, map
175: .size());
176: Assert.assertTrue(map.get(new Integer(1)).equals(
177: "subMap-value"));
178: }
179: }
180:
181: synchronized (map2) {
182: if (map2.size() == getParticipantCount()) {
183: if (map2.size() > 2) {
184: Object fromKey = new WrappedString(0);
185: Object toKey = new WrappedString(10);
186: Map subMap = map2.subMap(fromKey, toKey);
187: subMap.put(new WrappedString(1), "subMap-value");
188: }
189: } else {
190: Assert.assertEquals(getParticipantCount() + 1, map2
191: .size());
192: Assert.assertTrue(map2.get(new WrappedString(1))
193: .equals("subMap-value"));
194: }
195: }
196:
197: barrier.barrier();
198:
199: clear();
200: initializeMaps();
201:
202: // headMap() testing.
203: synchronized (map) {
204: if (map.size() == getParticipantCount()) {
205: if (map.size() > 2) {
206: Object toKey = new Integer(2);
207: Map headMap = map.headMap(toKey);
208: headMap.put(new Integer(1), "subMap-value");
209: }
210: } else {
211: Assert.assertEquals(getParticipantCount() + 1, map
212: .size());
213: Assert.assertTrue(map.get(new Integer(1)).equals(
214: "subMap-value"));
215: }
216: }
217:
218: synchronized (map2) {
219: if (map2.size() == getParticipantCount()) {
220: if (map2.size() > 2) {
221: Object toKey = new WrappedString(2);
222: Map headMap = map2.headMap(toKey);
223: headMap.put(new WrappedString(1), "subMap-value");
224: }
225: } else {
226: Assert.assertEquals(getParticipantCount() + 1, map2
227: .size());
228: Assert.assertTrue(map2.get(new WrappedString(1))
229: .equals("subMap-value"));
230: }
231: }
232:
233: barrier.barrier();
234:
235: clear();
236: initializeMaps();
237:
238: // tailMap() testing.
239: synchronized (map) {
240: if (map.size() == getParticipantCount()) {
241: if (map.size() > 2) {
242: Object fromKey = new Integer(0);
243: Map tailMap = map.tailMap(fromKey);
244: tailMap.put(new Integer(1), "subMap-value");
245: }
246: } else {
247: Assert.assertEquals(getParticipantCount() + 1, map
248: .size());
249: Assert.assertTrue(map.get(new Integer(1)).equals(
250: "subMap-value"));
251: }
252: }
253:
254: synchronized (map2) {
255: if (map2.size() == getParticipantCount()) {
256: if (map2.size() > 2) {
257: Object fromKey = new WrappedString(0);
258: Map tailMap = map2.tailMap(fromKey);
259: tailMap.put(new WrappedString(1), "subMap-value");
260: }
261: } else {
262: Assert.assertEquals(getParticipantCount() + 1, map2
263: .size());
264: Assert.assertTrue(map2.get(new WrappedString(1))
265: .equals("subMap-value"));
266: }
267: }
268:
269: barrier.barrier();
270:
271: clear();
272: initializeMaps();
273:
274: // tailMap() clear testing.
275: synchronized (map) {
276: if (map.size() == getParticipantCount()) {
277: Object fromKey = new Integer(0);
278: Map tailMap = map.tailMap(fromKey);
279: tailMap.clear();
280: } else {
281: Assert.assertEquals(0, map.size());
282: }
283: }
284:
285: synchronized (map2) {
286: if (map2.size() == getParticipantCount()) {
287: Object fromKey = new WrappedString(0);
288: Map tailMap = map2.tailMap(fromKey);
289: tailMap.clear();
290: } else {
291: Assert.assertEquals(0, map2.size());
292: }
293: }
294:
295: barrier.barrier();
296:
297: }
298:
299: private int getMapSize(TreeMap m) {
300: synchronized (m) {
301: return (m.size());
302: }
303: }
304:
305: private void clear() throws Exception {
306: synchronized (map) {
307: map.clear();
308: }
309: synchronized (map2) {
310: map2.clear();
311: }
312: barrier.barrier();
313: }
314:
315: private void initializeMaps() throws Exception {
316: String me = getApplicationId();
317:
318: synchronized (subMapKeyRoot) {
319: if (subMapKeyRoot.getKey() != 0) {
320: subMapKeyRoot.setKey(0);
321: }
322: }
323: barrier.barrier();
324:
325: synchronized (map) {
326: int key = subMapKeyRoot.getKey();
327: map.put(new Integer(key), me + "-value");
328: subMapKeyRoot.setKey(key + 2);
329: }
330: barrier.barrier();
331: synchronized (subMapKeyRoot) {
332: if (subMapKeyRoot.getKey() != 0) {
333: subMapKeyRoot.setKey(0);
334: }
335: }
336: barrier.barrier();
337: synchronized (map2) {
338: int key = subMapKeyRoot.getKey();
339: map2.put(new WrappedString(key), me + "-value");
340: subMapKeyRoot.setKey(key + 2);
341: }
342: barrier.barrier();
343: }
344:
345: private static void validate(Map map, int count,
346: Comparator comparator) {
347: int expect = count;
348: Assert.assertEquals(expect, map.size());
349:
350: TreeMap compare = comparator == null ? new TreeMap()
351: : new TreeMap(comparator);
352: compare.putAll(map);
353:
354: Iterator sharedIter = map.entrySet().iterator();
355: Iterator localIter = compare.entrySet().iterator();
356:
357: while (true) {
358: Entry sharedEntry = (Entry) sharedIter.next();
359: Entry localEntry = (Entry) localIter.next();
360:
361: Object sharedKey = sharedEntry.getKey();
362: Object localKey = localEntry.getKey();
363: Assert.assertEquals(localKey, sharedKey);
364:
365: Object sharedValue = sharedEntry.getValue();
366: Object localValue = localEntry.getValue();
367: Assert.assertEquals(localValue, sharedValue);
368:
369: if (sharedIter.hasNext()) {
370: Assert.assertTrue(localIter.hasNext());
371: } else {
372: break;
373: }
374: }
375:
376: }
377:
378: public static void visitL1DSOConfig(ConfigVisitor visitor,
379: DSOClientConfigHelper config) {
380: TransparencyClassSpec spec = config
381: .getOrCreateSpec(CyclicBarrier.class.getName());
382: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
383: + "*.*(..)");
384:
385: String testClass = TreeMapTestApp.class.getName();
386: spec = config.getOrCreateSpec(testClass);
387:
388: config.addIncludePattern(testClass + "$*");
389:
390: String methodExpression = "* " + testClass + "*.getMapSize(..)";
391: config.addReadAutolock(methodExpression);
392: // methodExpression = "* " + testClass + "*.*(..)";
393: // config.addWriteAutolock(methodExpression);
394: methodExpression = "* " + testClass + ".run*(..)";
395: config.addWriteAutolock(methodExpression);
396: methodExpression = "* " + testClass + ".clear(..)";
397: config.addWriteAutolock(methodExpression);
398: methodExpression = "* " + testClass + ".initializeMaps(..)";
399: config.addWriteAutolock(methodExpression);
400:
401: spec.addRoot("map", "map");
402: spec.addRoot("map2", "map2");
403: spec.addRoot("barrier", "barrier");
404: spec.addRoot("subMapKeyRoot", "subMapKeyRoot");
405:
406: config.addIncludePattern(WrappedStringComparator.class
407: .getName());
408: config.addIncludePattern(WrappedString.class.getName());
409: }
410:
411: // The main purpose of this class is that it does NOT implement Comparable
412: private static class WrappedString {
413: private final String string;
414:
415: WrappedString(String string) {
416: this .string = string;
417: }
418:
419: WrappedString(int i) {
420: this .string = String.valueOf(i);
421: }
422:
423: String getString() {
424: return this .string;
425: }
426: }
427:
428: private static class WrappedStringComparator implements Comparator {
429:
430: public int compare(Object o1, Object o2) {
431: WrappedString ws1 = (WrappedString) o1;
432: WrappedString ws2 = (WrappedString) o2;
433: return ws1.getString().compareTo(ws2.getString());
434: }
435:
436: }
437:
438: /**
439: * The main purpose of this class is for to generate the key for the subMap(), headMap(), and tailMap() testing.
440: */
441: private static class SubMapKey {
442: private int key;
443:
444: public SubMapKey(int key) {
445: this .key = key;
446: }
447:
448: public int getKey() {
449: return key;
450: }
451:
452: public void setKey(int key) {
453: this.key = key;
454: }
455: }
456: }
|