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.object.config.spec.CyclicBarrierSpec;
012: import com.tc.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tc.util.Assert;
015: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
016:
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.ListIterator;
027: import java.util.Map;
028: import java.util.Set;
029: import java.util.SortedMap;
030: import java.util.SortedSet;
031: import java.util.TreeMap;
032: import java.util.TreeSet;
033: import java.util.Map.Entry;
034:
035: public class CollectionsWrappersTest extends TransparentTestBase {
036:
037: private static final int NODE_COUNT = 3;
038:
039: public void setUp() throws Exception {
040: super .setUp();
041: getTransparentAppConfig().setClientCount(NODE_COUNT)
042: .setIntensity(1);
043: initializeTestRunner();
044: }
045:
046: protected Class getApplicationClass() {
047: return CollectionsWrappersTestApp.class;
048: }
049:
050: @SuppressWarnings("unchecked")
051: public static class CollectionsWrappersTestApp extends
052: AbstractErrorCatchingTransparentApp {
053:
054: private static final Map root = new HashMap();
055: private final CyclicBarrier barrier;
056:
057: private final LinkedList c = new LinkedList(Arrays
058: .asList(new Object[] { "item" }));
059: private final ArrayList l = new ArrayList(Arrays
060: .asList(new Object[] { "timmy" }));
061: private final HashMap m = new HashMap();
062: private final HashSet s = new HashSet();
063: private final TreeMap sm = new TreeMap();
064: private final TreeSet ss = new TreeSet();
065: {
066: m.put("yer", "mom");
067: s.add("it");
068: sm.put("swing", "low");
069: ss.add("sweet chariot");
070: }
071:
072: public CollectionsWrappersTestApp(String appId,
073: ApplicationConfig cfg, ListenerProvider listenerProvider) {
074: super (appId, cfg, listenerProvider);
075: barrier = new CyclicBarrier(getParticipantCount());
076: }
077:
078: protected void runTest() throws Throwable {
079: int n = barrier.barrier();
080:
081: if (n == 0) {
082: synchronized (root) {
083: // It's important here to use non-synch collection implelentation underneath the wrapper
084: // This test is making sure that the wrapper acquires the proper write lock before
085: // calling down to the underlying collection instance. If that instance is itself autolocked, then this test
086: // isn't very effective (ie. don't use Hashtable, or Vector, etc)
087: root.put("synch collection", Collections
088: .synchronizedCollection(new LinkedList()));
089: root.put("synch list", Collections
090: .synchronizedList(new ArrayList()));
091: root.put("synch map", Collections
092: .synchronizedMap(new HashMap()));
093: root.put("synch set", Collections
094: .synchronizedSet(new HashSet()));
095: root.put("synch sorted map", Collections
096: .synchronizedSortedMap(new TreeMap()));
097: root.put("synch sorted set", Collections
098: .synchronizedSortedSet(new TreeSet()));
099:
100: root.put("empty list", Collections.EMPTY_LIST);
101: root.put("empty set", Collections.EMPTY_SET);
102: root.put("empty map", Collections.EMPTY_MAP);
103:
104: root
105: .put("singleton", Collections
106: .singleton("item"));
107: root.put("singleton list", Collections
108: .singletonList("item"));
109: root.put("singleton map", Collections.singletonMap(
110: "key", "value"));
111:
112: root.put("unmod collection", Collections
113: .unmodifiableCollection((Collection) c
114: .clone()));
115: root.put("unmod list", Collections
116: .unmodifiableList((List) l.clone()));
117: root.put("unmod map", Collections
118: .unmodifiableMap((Map) m.clone()));
119: root.put("unmod set", Collections
120: .unmodifiableSet((Set) s.clone()));
121: root.put("unmod sorted map", Collections
122: .unmodifiableSortedMap((SortedMap) sm
123: .clone()));
124: root.put("unmod sorted set", Collections
125: .unmodifiableSortedSet((SortedSet) ss
126: .clone()));
127: }
128:
129: // XXX: This test should really excercise all methods to test autolocking, not just
130: // add(Object) and put(Object,Object)
131: for (Iterator i = root.keySet().iterator(); i.hasNext();) {
132: String key = (String) i.next();
133: if (key.startsWith("synch ")) {
134: Object o = root.get(key);
135: if (o instanceof Collection) {
136: Collection c1 = (Collection) o;
137: // attempt modify it
138: c1.add("value");
139: } else if (o instanceof Map) {
140: Map m1 = (Map) o;
141: m1.put("key", "value");
142: } else {
143: throw new AssertionError("unknown type: "
144: + o.getClass());
145: }
146: }
147: }
148: }
149:
150: barrier.barrier();
151:
152: verify();
153: }
154:
155: private void verify() {
156: for (Iterator i = root.keySet().iterator(); i.hasNext();) {
157: String key = (String) i.next();
158: if (key.startsWith("synch ")) {
159: Object o = root.get(key);
160: if (o instanceof Collection) {
161: Collection c1 = (Collection) o;
162: Assert.assertEquals(1, c1.size());
163: Assert.assertEquals("value", c1.iterator()
164: .next());
165: } else if (o instanceof Map) {
166: Map m1 = (Map) o;
167: Assert.assertEquals(1, m1.size());
168: Assert.assertEquals("value", m1.values()
169: .iterator().next());
170: Assert.assertEquals("key", m1.keySet()
171: .iterator().next());
172: } else {
173: throw new AssertionError("unknown type: "
174: + o.getClass());
175: }
176: }
177: }
178:
179: assertEquals(Collections.EMPTY_LIST, root.get("empty list"));
180: assertEquals(Collections.EMPTY_SET, root.get("empty set"));
181: assertEquals(Collections.EMPTY_MAP, root.get("empty map"));
182:
183: assertEquals(Collections.singleton("item"), root
184: .get("singleton"));
185: assertEquals(Collections.singletonList("item"), root
186: .get("singleton list"));
187: assertEquals(Collections.singletonMap("key", "value"), root
188: .get("singleton map"));
189:
190: assertTrue(Arrays.equals(Collections
191: .unmodifiableCollection(c).toArray(),
192: ((Collection) root.get("unmod collection"))
193: .toArray()));
194: assertEquals(Collections.unmodifiableList(l), root
195: .get("unmod list"));
196: assertEquals(Collections.unmodifiableMap(m), root
197: .get("unmod map"));
198: assertEquals(Collections.unmodifiableSet(s), root
199: .get("unmod set"));
200: assertEquals(Collections.unmodifiableSortedMap(sm), root
201: .get("unmod sorted map"));
202: assertEquals(Collections.unmodifiableSortedSet(ss), root
203: .get("unmod sorted set"));
204:
205: for (Iterator i = root.values().iterator(); i.hasNext();) {
206: exercise(i.next());
207: }
208: }
209:
210: public static void visitL1DSOConfig(ConfigVisitor visitor,
211: DSOClientConfigHelper config) {
212: visitor.visit(config, new CyclicBarrierSpec());
213:
214: TransparencyClassSpec spec;
215: String testClass;
216:
217: testClass = CollectionsWrappersTestApp.class.getName();
218: spec = config.getOrCreateSpec(testClass);
219:
220: String methodExpression = "* " + testClass + ".*(..)";
221: config.addWriteAutolock(methodExpression);
222:
223: spec.addRoot("root", "root");
224: spec.addRoot("barrier", "barrier");
225: }
226:
227: private static void exercise(Object o) {
228: // exercise all non-mutator methods (this is to shake out problems with uninstrumented classes, or improper
229: // transients
230:
231: if (o instanceof SortedMap) {
232: exerciseSortedMap((SortedMap) o);
233: } else if (o instanceof SortedSet) {
234: exerciseSortedSet((SortedSet) o);
235: } else if (o instanceof Map) {
236: exerciseMap((Map) o);
237: } else if (o instanceof Set) {
238: exerciseSet((Set) o);
239: } else if (o instanceof List) {
240: exerciseList((List) o);
241: } else if (o instanceof Collection) {
242: exerciseCollection((Collection) o);
243: } else {
244: throw new AssertionError("unknown type: "
245: + o.getClass().getName());
246: }
247: }
248:
249: private static void exerciseCollection(Collection coll) {
250: coll.contains(coll);
251: coll.containsAll(coll);
252: coll.equals(coll);
253: coll.isEmpty();
254: for (Iterator i = coll.iterator(); i.hasNext();) {
255: i.next();
256: }
257: coll.hashCode();
258: coll.size();
259: coll.toArray();
260: coll.toArray(new Object[] {});
261: coll.toString();
262: }
263:
264: private static void exerciseList(List list) {
265: list.contains(list);
266: list.containsAll(list);
267: list.equals(list);
268: if (list.size() > 0) {
269: list.get(0);
270: }
271: list.indexOf(list);
272: list.isEmpty();
273: list.hashCode();
274: for (Iterator i = list.iterator(); i.hasNext();) {
275: i.next();
276: }
277: list.lastIndexOf(list);
278: for (ListIterator i = list.listIterator(); i.hasNext();) {
279: i.hasPrevious();
280: i.hasNext();
281: i.nextIndex();
282: i.next();
283: i.previousIndex();
284: i.previous();
285: i.next();
286: }
287: list.listIterator(0);
288: list.size();
289: list.subList(0, 0);
290: list.toArray();
291: list.toArray(new Object[] {});
292: list.toString();
293: }
294:
295: private static void exerciseSet(Set set) {
296: set.contains("darth");
297: set.containsAll(set);
298: set.equals(set);
299: set.hashCode();
300: set.isEmpty();
301: for (Iterator i = set.iterator(); i.hasNext();) {
302: i.next();
303: }
304: set.size();
305: set.toArray();
306: set.toArray(new Object[] {});
307: set.toString();
308: }
309:
310: private static void exerciseSortedMap(SortedMap map) {
311: exerciseMap(map);
312: map.comparator();
313: map.firstKey();
314: map.headMap(map.firstKey());
315: map.lastKey();
316: map.subMap(map.firstKey(), map.lastKey());
317: map.tailMap(map.firstKey());
318: }
319:
320: private static void exerciseSortedSet(SortedSet set) {
321: //
322: }
323:
324: private static void exerciseMap(Map map) {
325: map.containsKey("bob");
326: map.containsValue(map);
327: exerciseSet(map.entrySet());
328: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
329: Map.Entry entry = (Entry) i.next();
330: entry.getKey();
331: entry.getValue();
332: }
333: map.equals(map);
334: map.get("fanny");
335: map.hashCode();
336: map.isEmpty();
337: exerciseSet(map.keySet());
338: map.size();
339: map.toString();
340: exerciseCollection(map.values());
341: }
342:
343: }
344: }
|