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