001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.object.config.ConfigVisitor;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.TransparencyClassSpec;
010: import com.tc.simulator.app.ApplicationConfig;
011: import com.tc.simulator.listener.ListenerProvider;
012: import com.tc.util.Assert;
013:
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.Map;
017: import java.util.Set;
018: import java.util.TreeSet;
019:
020: public class TreeSetMutateValidateTestApp extends
021: AbstractMutateValidateTransparentApp {
022:
023: private static final boolean MUTATE = true;
024: private static final boolean VALIDATE = false;
025:
026: private final String myAppId;
027: private final Map myMapOfTreeSets;
028:
029: // ROOT
030: private Map allMaps = new HashMap();
031:
032: public TreeSetMutateValidateTestApp(String appId,
033: ApplicationConfig cfg, ListenerProvider listenerProvider) {
034: super (appId, cfg, listenerProvider);
035: this .myAppId = appId;
036: myMapOfTreeSets = new HashMap();
037: }
038:
039: protected void mutate() throws Throwable {
040: testAdd(MUTATE, null);
041: testAddAll(MUTATE, null);
042: testClear(MUTATE, null);
043: testRemove(MUTATE, null);
044: testFirst(MUTATE, null);
045: testLast(MUTATE, null);
046:
047: synchronized (allMaps) {
048: allMaps.put(myAppId, myMapOfTreeSets);
049: }
050: }
051:
052: protected void validate() throws Throwable {
053: synchronized (allMaps) {
054: Set appIds = allMaps.keySet();
055: for (Iterator iter = appIds.iterator(); iter.hasNext();) {
056: String appId = (String) iter.next();
057: Map allTreeSets = (Map) allMaps.get(appId);
058: testAdd(VALIDATE, allTreeSets);
059: testAddAll(VALIDATE, allTreeSets);
060: testClear(VALIDATE, allTreeSets);
061: testRemove(VALIDATE, allTreeSets);
062: testFirst(VALIDATE, allTreeSets);
063: testLast(VALIDATE, allTreeSets);
064: }
065: }
066: }
067:
068: private TreeSet getPopulatedTreeSet() {
069: FooObject fooObject_1 = new FooObject("James", 53, true);
070: FooObject fooObject_2 = new FooObject("Susan", 29, true);
071: FooObject fooObject_3 = new FooObject("Erin", 87, false);
072: TreeSet treeSet = new TreeSet(new NullTolerantComparator());
073: Assert.assertTrue(treeSet.add(fooObject_1));
074: Assert.assertTrue(treeSet.add(fooObject_2));
075: Assert.assertTrue(treeSet.add(fooObject_3));
076: return treeSet;
077: }
078:
079: private void testAdd(boolean mutate, Map allTreeSets) {
080: final String key = "testAdd";
081:
082: if (mutate) {
083: TreeSet myTreeSet = getPopulatedTreeSet();
084: myMapOfTreeSets.put(key, myTreeSet);
085: } else {
086: TreeSet treeSet = (TreeSet) allTreeSets.get(key);
087: Assert.assertEquals(treeSet.size(), 3);
088: Assert.assertTrue(treeSet.contains(new FooObject("James",
089: 53, true)));
090: Assert.assertTrue(treeSet.contains(new FooObject("Susan",
091: 29, true)));
092: Assert.assertTrue(treeSet.contains(new FooObject("Erin",
093: 87, false)));
094: }
095: }
096:
097: private void testAddAll(boolean mutate, Map allTreeSets) {
098: final String key = "testAddAll";
099:
100: if (mutate) {
101: TreeSet treeSet = getPopulatedTreeSet();
102: TreeSet myTreeSet = new TreeSet(
103: new NullTolerantComparator());
104: myTreeSet.addAll(treeSet);
105: myMapOfTreeSets.put(key, myTreeSet);
106: } else {
107: TreeSet treeSet = (TreeSet) allTreeSets.get(key);
108: Assert.assertEquals(treeSet.size(), 3);
109: Assert.assertTrue(treeSet.contains(new FooObject("James",
110: 53, true)));
111: Assert.assertTrue(treeSet.contains(new FooObject("Susan",
112: 29, true)));
113: Assert.assertTrue(treeSet.contains(new FooObject("Erin",
114: 87, false)));
115: }
116: }
117:
118: private void testClear(boolean mutate, Map allTreeSets) {
119: final String key = "testClear";
120:
121: if (mutate) {
122: TreeSet myTreeSet = getPopulatedTreeSet();
123: myTreeSet.clear();
124: myMapOfTreeSets.put(key, myTreeSet);
125: } else {
126: TreeSet treeSet = (TreeSet) allTreeSets.get(key);
127: Assert.assertTrue(treeSet.isEmpty());
128: }
129: }
130:
131: private void testRemove(boolean mutate, Map allTreeSets) {
132: final String key = "testRemove";
133:
134: if (mutate) {
135: TreeSet myTreeSet = getPopulatedTreeSet();
136: myTreeSet.remove(new FooObject("Susan", 29, true));
137: myMapOfTreeSets.put(key, myTreeSet);
138: } else {
139: TreeSet treeSet = (TreeSet) allTreeSets.get(key);
140: Assert.assertEquals(treeSet.size(), 2);
141: Assert.assertTrue(treeSet.contains(new FooObject("James",
142: 53, true)));
143: Assert.assertTrue(treeSet.contains(new FooObject("Erin",
144: 87, false)));
145: }
146: }
147:
148: private void testFirst(boolean mutate, Map allTreeSets) {
149: final String key = "testFirst";
150:
151: if (mutate) {
152: TreeSet myTreeSet = getPopulatedTreeSet();
153: Assert.assertEquals(myTreeSet.first(), new FooObject(
154: "Susan", 29, true));
155: myMapOfTreeSets.put(key, myTreeSet);
156: } else {
157: TreeSet treeSet = (TreeSet) allTreeSets.get(key);
158: Assert.assertEquals(treeSet.size(), 3);
159: Assert.assertTrue(treeSet.contains(new FooObject("James",
160: 53, true)));
161: Assert.assertTrue(treeSet.contains(new FooObject("Susan",
162: 29, true)));
163: Assert.assertTrue(treeSet.contains(new FooObject("Erin",
164: 87, false)));
165: }
166: }
167:
168: private void testLast(boolean mutate, Map allTreeSets) {
169: final String key = "testLast";
170:
171: if (mutate) {
172: TreeSet myTreeSet = getPopulatedTreeSet();
173: Assert.assertEquals(myTreeSet.last(), new FooObject("Erin",
174: 87, false));
175: myMapOfTreeSets.put(key, myTreeSet);
176: } else {
177: TreeSet treeSet = (TreeSet) allTreeSets.get(key);
178: Assert.assertEquals(treeSet.size(), 3);
179: Assert.assertTrue(treeSet.contains(new FooObject("James",
180: 53, true)));
181: Assert.assertTrue(treeSet.contains(new FooObject("Susan",
182: 29, true)));
183: Assert.assertTrue(treeSet.contains(new FooObject("Erin",
184: 87, false)));
185: }
186: }
187:
188: public static void visitL1DSOConfig(ConfigVisitor visitor,
189: DSOClientConfigHelper config) {
190: String testClass = TreeSetMutateValidateTestApp.class.getName();
191: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
192: String methodExpression = "* " + testClass + "*.*(..)";
193: config.addWriteAutolock(methodExpression);
194: spec.addRoot("allMaps", "allMaps");
195: config.getOrCreateSpec(FooObject.class.getName());
196: config.getOrCreateSpec(NullTolerantComparator.class.getName());
197: }
198:
199: private static final class FooObject implements Comparable {
200: private final String name;
201: private final boolean playsBasketball;
202: private final int age;
203:
204: public FooObject(String name, int age, boolean playsBasketball) {
205: this .name = name;
206: this .age = age;
207: this .playsBasketball = playsBasketball;
208: }
209:
210: public String getName() {
211: return name;
212: }
213:
214: public int getAge() {
215: return age;
216: }
217:
218: public boolean playsBasketball() {
219: return playsBasketball;
220: }
221:
222: public boolean equals(Object foo) {
223: if (foo == null) {
224: return false;
225: }
226: if (((FooObject) foo).getName().equals(name)
227: && ((FooObject) foo).getAge() == age
228: && ((FooObject) foo).playsBasketball() == playsBasketball) {
229: return true;
230: }
231: return false;
232: }
233:
234: public int compareTo(Object o) {
235: int othersAge = ((FooObject) o).getAge();
236: if (age < othersAge) {
237: return -1;
238: } else if (age == othersAge) {
239: return 0;
240: } else {
241: return 1;
242: }
243: }
244: }
245:
246: }
|