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 com.tc.object.config.ConfigLockLevel;
007: import com.tc.object.config.ConfigVisitor;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.simulator.app.ApplicationConfig;
010: import com.tc.simulator.listener.ListenerProvider;
011: import com.tc.util.Assert;
012:
013: import java.util.ArrayList;
014: import java.util.Arrays;
015: import java.util.Enumeration;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.ListIterator;
019: import java.util.Stack;
020: import java.util.Vector;
021:
022: public class AutoLockVectorTestApp extends GenericTestApp {
023:
024: public AutoLockVectorTestApp(String appId, ApplicationConfig cfg,
025: ListenerProvider listenerProvider) {
026: super (appId, cfg, listenerProvider, Vector.class);
027: }
028:
029: protected Object getTestObject(String testName) {
030: List lists = (List) sharedMap.get("lists");
031: return lists.iterator();
032: }
033:
034: protected void setupTestObject(String testName) {
035: List lists = new ArrayList();
036: lists.add(new Vector());
037: lists.add(new Stack());
038:
039: sharedMap.put("lists", lists);
040: sharedMap.put("arrayforVector", new Object[4]);
041: sharedMap.put("arrayforStack", new Object[4]);
042: }
043:
044: private void initialize(Vector vector) {
045: vector.addAll(getInitialData());
046: }
047:
048: private List getInitialData() {
049: List data = new ArrayList();
050: data.add("January");
051: data.add("February");
052: data.add("March");
053: data.add("April");
054: return data;
055: }
056:
057: void testAddAll(Vector vector, boolean validate) {
058: if (validate) {
059: assertListsEqual(getInitialData(), vector);
060: } else {
061: initialize(vector);
062: }
063: }
064:
065: void testAdd(Vector vector, boolean validate) {
066: if (validate) {
067: assertListsEqual(getInitialData(), vector);
068: } else {
069: vector.add("January");
070: vector.add("February");
071: vector.add("March");
072: vector.add("April");
073: }
074: }
075:
076: void testClear(Vector vector, boolean validate) {
077: if (validate) {
078: Assert.assertEquals(0, vector.size());
079: } else {
080: initialize(vector);
081: assertListsEqual(getInitialData(), vector);
082: vector.clear();
083: }
084: }
085:
086: void testIteratorRemove(Vector vector, boolean validate) {
087: if (validate) {
088: List expect = getInitialData();
089: Iterator expectIterator = expect.iterator();
090: expectIterator.next();
091: expectIterator.next();
092: expectIterator.remove();
093: assertListsEqual(expect, vector);
094: } else {
095: initialize(vector);
096: Iterator iterator = vector.iterator();
097: iterator.next();
098: iterator.next();
099: iterator.remove();
100: }
101: }
102:
103: void testListIteratorAdd(Vector vector, boolean validate) {
104: if (validate) {
105: List expect = getInitialData();
106: expect.add(2, "May");
107: assertListsEqual(expect, vector);
108: } else {
109: initialize(vector);
110: ListIterator iterator = vector.listIterator();
111: iterator.next();
112: iterator.next();
113: iterator.add("May");
114: }
115: }
116:
117: void testListIteratorRemove(Vector vector, boolean validate) {
118: if (validate) {
119: List expect = getInitialData();
120: expect.remove(1);
121: assertListsEqual(expect, vector);
122: } else {
123: initialize(vector);
124: ListIterator iterator = vector.listIterator();
125: iterator.next();
126: iterator.next();
127: iterator.remove();
128: }
129: }
130:
131: void testRemove(Vector vector, boolean validate) {
132: if (validate) {
133: List expect = getInitialData();
134: expect.remove("February");
135: assertListsEqual(expect, vector);
136: } else {
137: initialize(vector);
138: vector.remove("February");
139: }
140: }
141:
142: void testRemoveAt(Vector vector, boolean validate) {
143: if (validate) {
144: List expect = getInitialData();
145: expect.remove("February");
146: assertListsEqual(expect, vector);
147: } else {
148: initialize(vector);
149: vector.remove(1);
150: }
151: }
152:
153: void testSetSizeGrow(Vector vector, boolean validate) {
154: int size = 5;
155:
156: if (validate) {
157: Assert.assertEquals("January", vector.get(0));
158: for (int i = 1; i < size; i++) {
159: Object val = vector.get(i);
160: Assert.assertNull("element " + i + " is " + val, val);
161: }
162: Assert.assertEquals("July", vector.get(size));
163: } else {
164: vector.add("January");
165: vector.setSize(size);
166: vector.add("July");
167: }
168: }
169:
170: void testSetSizeShrink(Vector vector, boolean validate) {
171: if (validate) {
172: Assert.assertEquals("January", vector.get(0));
173: Assert.assertEquals("February", vector.get(1));
174: } else {
175: vector.add("January");
176: vector.add("ho hum");
177: vector.add("ho hum2");
178: vector.add("ho hum3");
179: vector.setSize(1);
180: vector.add("February");
181: }
182: }
183:
184: void testAddElement(Vector vector, boolean validate) {
185: if (validate) {
186: assertListsEqual(Arrays.asList(new Object[] { "January",
187: "February" }), vector);
188: } else {
189: vector.addElement("January");
190: vector.addElement("February");
191: }
192: }
193:
194: void testRetainAll(Vector vector, boolean validate) {
195: if (validate) {
196: assertListsEqual(Arrays.asList(new Object[] { "February",
197: "April" }), vector);
198: } else {
199: initialize(vector);
200: List toRetain = new ArrayList();
201: toRetain.add("February");
202: toRetain.add("April");
203: vector.retainAll(toRetain);
204: }
205: }
206:
207: void testRemoveAll(Vector vector, boolean validate) {
208: if (validate) {
209: assertListsEqual(Arrays.asList(new Object[] { "January",
210: "March" }), vector);
211: } else {
212: initialize(vector);
213: List toRemove = new ArrayList();
214: toRemove.add("February");
215: toRemove.add("April");
216: vector.removeAll(toRemove);
217: }
218: }
219:
220: void testListIteratorSet(Vector vector, boolean validate) {
221: if (validate) {
222: List expect = getInitialData();
223: expect.set(1, "February modified");
224: assertListsEqual(expect, vector);
225: } else {
226: initialize(vector);
227: ListIterator iterator = vector.listIterator();
228: iterator.next();
229: iterator.next();
230: iterator.set("February modified");
231: }
232: }
233:
234: void testToArray(Vector vector, boolean validate) {
235: Object[] array = getArray(vector);
236:
237: if (validate) {
238: assertListsEqual(Arrays.asList(array), vector);
239: } else {
240: initialize(vector);
241: synchronized (array) {
242: Object[] returnArray = vector.toArray(array);
243: Assert.assertTrue(returnArray == array);
244: }
245: }
246: }
247:
248: void testCopyInto(Vector vector, boolean validate) {
249: Object[] array = getArray(vector);
250:
251: if (validate) {
252: assertListsEqual(Arrays.asList(array), vector);
253: } else {
254: initialize(vector);
255: synchronized (array) {
256: vector.copyInto(array);
257: }
258: }
259: }
260:
261: void testSetElementAt(Vector vector, boolean validate) {
262: if (validate) {
263: assertListsEqual(Arrays.asList(new Object[] { "January",
264: "February Modified", "March", "April" }), vector);
265: } else {
266: initialize(vector);
267: vector.setElementAt("February Modified", 1);
268: }
269: }
270:
271: void testSubList(Vector vector, boolean validate) {
272: if (validate) {
273: assertListsEqual(Arrays.asList(new Object[] { "January",
274: "May", "February", "March", "April" }), vector);
275: } else {
276: initialize(vector);
277: List subList = vector.subList(1, 2);
278: ListIterator listIterator = subList.listIterator();
279: listIterator.add("May");
280: }
281: }
282:
283: void testElements(Vector vector, boolean validate) {
284: if (validate) {
285: List expect = getInitialData();
286: int i = 0;
287: Enumeration elements = vector.elements();
288: while (elements.hasMoreElements()) {
289: Object expectObj = expect.get(i++);
290: Object actualObj = elements.nextElement();
291: Assert.assertEquals(expectObj, actualObj);
292: }
293: } else {
294: initialize(vector);
295: }
296: }
297:
298: void testStackPush(Vector vector, boolean validate) {
299: if (!(vector instanceof Stack)) {
300: return;
301: }
302:
303: if (validate) {
304: assertListsEqual(Arrays.asList(new Object[] { "January",
305: "February" }), vector);
306: } else {
307: Stack s = (Stack) vector;
308: s.push("January");
309: s.push("February");
310: }
311: }
312:
313: void testStackPop(Vector vector, boolean validate) {
314: if (!(vector instanceof Stack)) {
315: return;
316: }
317:
318: if (validate) {
319: assertListsEqual(Arrays.asList(new Object[] { "January" }),
320: vector);
321: } else {
322: Stack s = (Stack) vector;
323: s.push("January");
324: s.push("February");
325:
326: Object o = s.pop();
327: Assert.assertEquals("February", o);
328: }
329: }
330:
331: private Object[] getArray(Vector vector) {
332: if (vector instanceof Stack) {
333: return (Object[]) sharedMap.get("arrayforStack");
334: }
335: return (Object[]) sharedMap.get("arrayforVector");
336: }
337:
338: private void assertListsEqual(List expect, List actual) {
339: Assert.assertEquals(expect.size(), actual.size());
340:
341: Assert.assertTrue(expect.containsAll(actual));
342: Assert.assertTrue(actual.containsAll(expect));
343:
344: for (int i = 0, n = expect.size(); i < n; i++) {
345: Assert.assertEquals(expect.get(i), actual.get(i));
346: }
347:
348: if (expect.isEmpty()) {
349: Assert.assertTrue(actual.isEmpty());
350: } else {
351: Assert.assertFalse(actual.isEmpty());
352: }
353:
354: for (Iterator iExpect = expect.iterator(), iActual = actual
355: .iterator(); iExpect.hasNext();) {
356: Assert.assertEquals(iExpect.next(), iActual.next());
357: }
358: }
359:
360: public static void visitL1DSOConfig(ConfigVisitor visitor,
361: DSOClientConfigHelper config) {
362: String testClass = AutoLockVectorTestApp.class.getName();
363: config.getOrCreateSpec(testClass);
364:
365: config.addIncludePattern("java.util.Vector");
366: config.addAutolock("* java.util.Vector.*(..)",
367: ConfigLockLevel.WRITE);
368: }
369:
370: }
|