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.ConfigVisitor;
007: import com.tc.object.config.DSOApplicationConfig;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.Root;
010: import com.tc.simulator.app.ApplicationConfig;
011: import com.tc.simulator.listener.ListenerProvider;
012: import com.tc.util.Assert;
013: import com.tctest.runner.AbstractTransparentApp;
014:
015: import java.util.ArrayList;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.LinkedList;
019: import java.util.List;
020: import java.util.Map;
021:
022: /**
023: *
024: */
025: public class TransparentLinkedListTestApp extends
026: AbstractTransparentApp {
027:
028: private static final int INITIAL_STAGE = 0;
029: private static final int ADD_COMPLETE_STAGE = 1;
030: private static final int ASSERT_MAX_COUNT_SIZE_STAGE = 2;
031: private static final int REMOVE_COMPLETE_STAGE = 3;
032: private static final int ASSERT_REMOVE_SIZE_STAGE = 4;
033:
034: private LinkedList list = new LinkedList();
035:
036: public TransparentLinkedListTestApp(String applicationId,
037: ApplicationConfig cfg, ListenerProvider listenerProvider) {
038: super (applicationId, cfg, listenerProvider);
039: }
040:
041: public void run() {
042: int maxCount = getParticipantCount() * getIntensity();
043: List testObjects = new ArrayList();
044: moveToStage(INITIAL_STAGE);
045: for (int i = 0; i < getIntensity(); i++) {
046: TestObject to = new TestObject(getApplicationId(), i);
047: testObjects.add(to);
048: synchronized (list) {
049: int size = list.size();
050: list.add(to);
051: Assert.eval(list.size() == size + 1);
052: }
053: }
054: moveToStageAndWait(ADD_COMPLETE_STAGE);
055:
056: checkSize(maxCount);
057:
058: moveToStageAndWait(ASSERT_MAX_COUNT_SIZE_STAGE);
059:
060: int removeCount = getIntensity() / 2;
061: for (int i = 0; i < removeCount; i++) {
062: synchronized (list) {
063: try {
064: int size = list.size();
065: boolean wasRemoved = list
066: .remove(testObjects.get(i));
067: Assert.eval(
068: "Test object should have been removed but wasn't: "
069: + testObjects.get(i), wasRemoved);
070: Assert.eval(list.size() == size - 1);
071: } catch (Throwable t) {
072: t.printStackTrace();
073: }
074: }
075: }
076:
077: moveToStageAndWait(REMOVE_COMPLETE_STAGE);
078:
079: checkSize(maxCount - getParticipantCount() * removeCount);
080:
081: moveToStageAndWait(ASSERT_REMOVE_SIZE_STAGE);
082:
083: synchronized (list) {
084: list.clear();
085: Assert.eval(list.size() == 0);
086: }
087:
088: checkSize(0);
089: notifyResult(Boolean.TRUE);
090: }
091:
092: private void checkSize(int s) {
093: synchronized (list) {
094: if (list.size() != s) {
095: System.out.println("list:" + list.size()
096: + " expecting:" + s);
097: Map res = new HashMap();
098: for (Iterator i = list.iterator(); i.hasNext();) {
099: TestObject to = (TestObject) i.next();
100: String key = to.getId();
101: if (!res.containsKey(key)) {
102: res.put(key, new Long(0));
103: } else {
104: long v = ((Long) res.get(key)).longValue();
105: res.put(key, new Long(++v));
106: }
107: }
108: throw new AssertionError("" + res);
109: }
110: }
111: }
112:
113: public static void visitL1DSOConfig(ConfigVisitor visitor,
114: DSOClientConfigHelper config) {
115: String testClassName = TransparentLinkedListTestApp.class
116: .getName();
117: config.addIncludePattern(testClassName);
118: config.addRoot(new Root(testClassName, "list", "list"), true);
119: config.addWriteAutolock("* " + testClassName + ".*(..)");
120: config.addIncludePattern(TestObject.class.getName());
121: visitor.visit(config, AbstractTransparentApp.class);
122: }
123:
124: public static void visitDSOApplicationConfig(ConfigVisitor visitor,
125: DSOApplicationConfig config) {
126: String classname = TransparentLinkedListTestApp.class.getName();
127: config.addIncludePattern(classname);
128: config.addRoot("list", classname + ".list");
129: config.addWriteAutolock("* " + classname + ".*(..)");
130: config.addIncludePattern(TestObject.class.getName());
131: visitor.visitDSOApplicationConfig(config,
132: AbstractTransparentApp.class);
133: }
134:
135: static class TestObject {
136: private String id;
137: private int count;
138:
139: TestObject(String i, int count) {
140: this .id = i;
141: this .count = count;
142: }
143:
144: public String getId() {
145: return id;
146: }
147:
148: public String toString() {
149: return "TestObject(" + id + "," + count + ")";
150: }
151: }
152:
153: }
|