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.TreeMap;
018:
019: public class TreeMapBuggyComparatorTestApp extends
020: AbstractTransparentApp {
021:
022: private final TreeMap map = new TreeMap(new BuggyComparator());
023: private final CyclicBarrier barrier;
024:
025: public TreeMapBuggyComparatorTestApp(String appId,
026: ApplicationConfig cfg, ListenerProvider listenerProvider) {
027: super (appId, cfg, listenerProvider);
028:
029: barrier = new CyclicBarrier(getParticipantCount());
030: }
031:
032: public void run() {
033: try {
034: run0();
035: } catch (Throwable t) {
036: notifyError(t);
037: }
038: }
039:
040: public void run0() throws Exception {
041: BuggyComparator cmp = (BuggyComparator) map.comparator();
042: cmp.setBuggy(true);
043:
044: final boolean first;
045: synchronized (map) {
046: first = map.isEmpty();
047: if (first) {
048: map.put("key", "value");
049: }
050: }
051:
052: Assert.assertEquals(1, map.size());
053: // Since our comparator is buggy, the map will not think it has the given key
054: Assert.assertFalse(map.containsKey("key"));
055: Assert.assertNull(map.remove("key"));
056:
057: barrier.barrier();
058:
059: cmp.setBuggy(false);
060: Assert.assertTrue(map.containsKey("key"));
061: Assert.assertEquals("value", map.get("key"));
062:
063: barrier.barrier();
064:
065: synchronized (map) {
066: if (!map.isEmpty()) {
067: Object removed = map.remove("key");
068: Assert.assertEquals("value", removed);
069: Assert.assertEquals(0, map.size());
070: }
071: }
072: }
073:
074: private static class BuggyComparator implements Comparator {
075: private boolean buggy;
076:
077: synchronized void setBuggy(boolean b) {
078: this .buggy = b;
079: }
080:
081: public int compare(Object o1, Object o2) {
082: if (buggy) {
083: return 1;
084: } else {
085: return ((Comparable) o1).compareTo(o2);
086: }
087: }
088: }
089:
090: public static void visitL1DSOConfig(ConfigVisitor visitor,
091: DSOClientConfigHelper config) {
092: TransparencyClassSpec spec = config
093: .getOrCreateSpec(CyclicBarrier.class.getName());
094: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
095: + "*.*(..)");
096:
097: String testClass = TreeMapBuggyComparatorTestApp.class
098: .getName();
099: spec = config.getOrCreateSpec(testClass);
100:
101: String methodExpression = "* " + testClass + "*.*(..)";
102: config.addWriteAutolock(methodExpression);
103:
104: spec.addRoot("map", "map");
105: spec.addRoot("barrier", "barrier");
106:
107: config.addIncludePattern(BuggyComparator.class.getName());
108: }
109:
110: }
|