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: import com.tc.util.TIMUtil;
014:
015: import java.net.Socket;
016: import java.util.ArrayList;
017: import java.util.Iterator;
018: import java.util.LinkedList;
019: import java.util.List;
020: import java.util.ListIterator;
021: import java.util.Stack;
022: import java.util.Vector;
023: import java.util.concurrent.CyclicBarrier;
024:
025: /**
026: * Test to make sure local object state is preserved when TC throws:
027: *
028: * UnlockedSharedObjectException ReadOnlyException TCNonPortableObjectError
029: *
030: * List version
031: *
032: * INT-186
033: *
034: * @author hhuynh
035: */
036: public class ListLocalStateTestApp extends GenericLocalStateTestApp {
037: private List<Wrapper> root = new ArrayList<Wrapper>();
038: private CyclicBarrier barrier;
039: private Class[] listClasses = new Class[] { ArrayList.class,
040: Vector.class, LinkedList.class, Stack.class };
041:
042: public ListLocalStateTestApp(String appId, ApplicationConfig cfg,
043: ListenerProvider listenerProvider) {
044: super (appId, cfg, listenerProvider);
045: barrier = new CyclicBarrier(cfg.getGlobalParticipantCount());
046: }
047:
048: protected void runTest() throws Throwable {
049: if (await() == 0) {
050: createLists();
051: }
052: await();
053:
054: for (LockMode lockMode : LockMode.values()) {
055: for (Wrapper w : root) {
056: testMutate(w, lockMode, new AddMutator());
057: testMutate(w, lockMode, new AddAllMutator());
058: testMutate(w, lockMode, new RemoveMutator());
059: testMutate(w, lockMode, new ClearMutator());
060: testMutate(w, lockMode, new RemoveAllMutator());
061: testMutate(w, lockMode, new RetainAllMutator());
062: testMutate(w, lockMode, new IteratorRemoveMutator());
063: testMutate(w, lockMode, new IteratorAddMutator());
064: testMutate(w, lockMode, new ListIteratorRemoveMutator());
065: // failing - DEV-844
066: // testMutate(w, lockMode, new AddAllNonPortableMutator());
067: }
068: }
069: }
070:
071: protected void validate(int oldSize, Wrapper wrapper,
072: LockMode lockMode, Mutator mutator) throws Throwable {
073: int newSize = wrapper.size();
074: switch (lockMode) {
075: case NONE:
076: case READ:
077: Assert.assertEquals("Type: "
078: + wrapper.getObject().getClass() + ", lock: "
079: + lockMode, oldSize, newSize);
080: break;
081: case WRITE:
082: // nothing yet
083: default:
084: throw new RuntimeException("Shouldn't happen");
085: }
086:
087: if (mutator instanceof AddAllNonPortableMutator) {
088: for (Iterator it = ((List) wrapper.getObject()).iterator(); it
089: .hasNext();) {
090: Object o = it.next();
091: Assert.assertFalse("Type: "
092: + wrapper.getObject().getClass() + ", lock: "
093: + lockMode + ", " + o.getClass(),
094: o instanceof Socket);
095: }
096: }
097: }
098:
099: private void createLists() throws Exception {
100: List data = new ArrayList();
101: data.add("v1");
102: data.add("v2");
103: data.add("v3");
104:
105: synchronized (root) {
106: for (Class k : listClasses) {
107: Wrapper cw = new CollectionWrapper(k, List.class);
108: ((List) cw.getObject()).addAll(data);
109: root.add(cw);
110: }
111: }
112: }
113:
114: protected int await() {
115: try {
116: return barrier.await();
117: } catch (Exception e) {
118: throw new RuntimeException(e);
119: }
120: }
121:
122: public static void visitL1DSOConfig(ConfigVisitor visitor,
123: DSOClientConfigHelper config) {
124: config.addModule(TIMUtil.COMMONS_COLLECTIONS_3_1, TIMUtil
125: .getVersion(TIMUtil.COMMONS_COLLECTIONS_3_1));
126:
127: String testClass = ListLocalStateTestApp.class.getName();
128: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
129:
130: config.addIncludePattern(testClass + "$*");
131: config.addIncludePattern(GenericLocalStateTestApp.class
132: .getName()
133: + "$*");
134:
135: config.addWriteAutolock("* " + testClass + "*.createLists()");
136: config.addWriteAutolock("* " + testClass + "*.validate()");
137: config.addReadAutolock("* " + testClass + "*.runTest()");
138:
139: spec.addRoot("root", "root");
140: spec.addRoot("barrier", "barrier");
141:
142: config.addReadAutolock("* " + Handler.class.getName()
143: + "*.invokeWithReadLock(..)");
144: config.addWriteAutolock("* " + Handler.class.getName()
145: + "*.invokeWithWriteLock(..)");
146: config.addWriteAutolock("* " + Handler.class.getName()
147: + "*.setLockMode(..)");
148: }
149:
150: private static class AddMutator implements Mutator {
151: public void doMutate(Object o) {
152: List l = (List) o;
153: l.add("v4");
154: }
155: }
156:
157: private static class AddAllMutator implements Mutator {
158: public void doMutate(Object o) {
159: List l = (List) o;
160: List anotherList = new ArrayList();
161: anotherList.add("v");
162: l.addAll(anotherList);
163: }
164: }
165:
166: private static class RemoveMutator implements Mutator {
167: public void doMutate(Object o) {
168: List l = (List) o;
169: l.remove("v1");
170: }
171: }
172:
173: private static class ClearMutator implements Mutator {
174: public void doMutate(Object o) {
175: List l = (List) o;
176: l.clear();
177: }
178: }
179:
180: private static class RemoveAllMutator implements Mutator {
181: public void doMutate(Object o) {
182: List l = (List) o;
183: List a = new ArrayList();
184: a.add("v1");
185: a.add("v2");
186: l.removeAll(a);
187: }
188: }
189:
190: private static class RetainAllMutator implements Mutator {
191: public void doMutate(Object o) {
192: List l = (List) o;
193: List a = new ArrayList();
194: a.add("v1");
195: a.add("v2");
196: l.retainAll(a);
197: }
198: }
199:
200: private static class IteratorRemoveMutator implements Mutator {
201: public void doMutate(Object o) {
202: List l = (List) o;
203: for (Iterator it = l.iterator(); it.hasNext();) {
204: it.next();
205: it.remove();
206: }
207: }
208: }
209:
210: private static class ListIteratorRemoveMutator implements Mutator {
211: public void doMutate(Object o) {
212: List l = (List) o;
213: for (ListIterator it = l.listIterator(); it.hasNext();) {
214: it.next();
215: it.remove();
216: }
217: }
218: }
219:
220: private static class IteratorAddMutator implements Mutator {
221: public void doMutate(Object o) {
222: List l = (List) o;
223: ListIterator it = l.listIterator();
224: it.add("v");
225: }
226: }
227:
228: private static class AddAllNonPortableMutator implements Mutator {
229: public void doMutate(Object o) {
230: List l = (List) o;
231: List anotherList = new ArrayList();
232: anotherList.add("v4");
233: anotherList.add("v5");
234: anotherList.add(new Socket());
235: anotherList.add("v6");
236: l.addAll(anotherList);
237: }
238: }
239: }
|