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:
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.Map;
017: import java.util.Set;
018: import java.util.Stack;
019:
020: public class StackMutateValidateTestApp extends
021: AbstractMutateValidateTransparentApp {
022: private static final boolean MUTATE = true;
023: private static final boolean VALIDATE = false;
024:
025: private final String myAppId;
026: private final Map myMapOfStacks;
027:
028: // ROOT
029: private Map allMaps = new HashMap();
030:
031: public StackMutateValidateTestApp(String appId,
032: ApplicationConfig cfg, ListenerProvider listenerProvider) {
033: super (appId, cfg, listenerProvider);
034: this .myAppId = appId;
035: myMapOfStacks = new HashMap();
036: }
037:
038: protected void mutate() throws Throwable {
039: testEmpty(MUTATE, null);
040: testPush(MUTATE, null);
041: testPeek(MUTATE, null);
042: testPop(MUTATE, null);
043: testSearch(MUTATE, null);
044:
045: synchronized (allMaps) {
046: allMaps.put(myAppId, myMapOfStacks);
047: }
048: }
049:
050: protected void validate() throws Throwable {
051: synchronized (allMaps) {
052: Set appIds = allMaps.keySet();
053: for (Iterator iter = appIds.iterator(); iter.hasNext();) {
054: String appId = (String) iter.next();
055: Map allStacks = (Map) allMaps.get(appId);
056: testEmpty(VALIDATE, allStacks);
057: testPush(VALIDATE, allStacks);
058: testPeek(VALIDATE, allStacks);
059: testPop(VALIDATE, allStacks);
060: testSearch(VALIDATE, allStacks);
061: }
062: }
063: }
064:
065: private void testEmpty(boolean mutate, Map allStacks) {
066: final String key = "testEmpty";
067:
068: if (mutate) {
069: Stack myStack = new Stack();
070: Assert.assertTrue(myStack.add(new FooObject("James", 53,
071: true)));
072: Assert.assertTrue(myStack.remove(new FooObject("James", 53,
073: true)));
074: myMapOfStacks.put(key, myStack);
075: } else {
076: Stack stack = (Stack) allStacks.get(key);
077: Assert.assertTrue(stack.isEmpty());
078: }
079: }
080:
081: private Stack getPopulatedStack() {
082: FooObject fooObject_1 = new FooObject("James", 53, true);
083: FooObject fooObject_2 = new FooObject("Susan", 29, true);
084: FooObject fooObject_3 = new FooObject("Erin", 87, false);
085: Stack stack = new Stack();
086: stack.push(fooObject_1);
087: stack.push(fooObject_2);
088: stack.push(fooObject_3);
089: return stack;
090: }
091:
092: private void testPush(boolean mutate, Map allStacks) {
093: final String key = "testPush";
094:
095: if (mutate) {
096: Stack myStack = getPopulatedStack();
097: myMapOfStacks.put(key, myStack);
098: } else {
099: Stack stack = (Stack) allStacks.get(key);
100: Assert.assertEquals(stack.search(new FooObject("James", 53,
101: true)), 3);
102: Assert.assertEquals(stack.search(new FooObject("Susan", 29,
103: true)), 2);
104: Assert.assertEquals(stack.search(new FooObject("Erin", 87,
105: false)), 1);
106: }
107: }
108:
109: private void testPeek(boolean mutate, Map allStacks) {
110: final String key = "testPeek";
111:
112: if (mutate) {
113: Stack myStack = getPopulatedStack();
114: Assert.assertEquals(myStack.peek(), new FooObject("Erin",
115: 87, false));
116: myMapOfStacks.put(key, myStack);
117: } else {
118: Stack stack = (Stack) allStacks.get(key);
119: Assert.assertEquals(stack.search(new FooObject("James", 53,
120: true)), 3);
121: Assert.assertEquals(stack.search(new FooObject("Susan", 29,
122: true)), 2);
123: Assert.assertEquals(stack.search(new FooObject("Erin", 87,
124: false)), 1);
125: }
126: }
127:
128: private void testPop(boolean mutate, Map allStacks) {
129: final String key = "testPop";
130:
131: if (mutate) {
132: Stack myStack = getPopulatedStack();
133: Assert.assertEquals(myStack.pop(), new FooObject("Erin",
134: 87, false));
135: myMapOfStacks.put(key, myStack);
136: } else {
137: Stack stack = (Stack) allStacks.get(key);
138: Assert.assertEquals(stack.search(new FooObject("James", 53,
139: true)), 2);
140: Assert.assertEquals(stack.search(new FooObject("Susan", 29,
141: true)), 1);
142: Assert.assertEquals(stack.search(new FooObject("Erin", 87,
143: false)), -1);
144: }
145: }
146:
147: private void testSearch(boolean mutate, Map allStacks) {
148: final String key = "testSearch";
149:
150: if (mutate) {
151: Stack myStack = getPopulatedStack();
152: Assert.assertEquals(myStack.search(new FooObject("James",
153: 53, true)), 3);
154: Assert.assertEquals(myStack.search(new FooObject("Susan",
155: 29, true)), 2);
156: Assert.assertEquals(myStack.search(new FooObject("Erin",
157: 87, false)), 1);
158: myMapOfStacks.put(key, myStack);
159: } else {
160: Stack stack = (Stack) allStacks.get(key);
161: Assert.assertEquals(stack.search(new FooObject("James", 53,
162: true)), 3);
163: Assert.assertEquals(stack.search(new FooObject("Susan", 29,
164: true)), 2);
165: Assert.assertEquals(stack.search(new FooObject("Erin", 87,
166: false)), 1);
167: }
168: }
169:
170: public static void visitL1DSOConfig(ConfigVisitor visitor,
171: DSOClientConfigHelper config) {
172: String testClass = StackMutateValidateTestApp.class.getName();
173: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
174: String methodExpression = "* " + testClass + "*.*(..)";
175: config.addWriteAutolock(methodExpression);
176: spec.addRoot("allMaps", "allMaps");
177: config.getOrCreateSpec(FooObject.class.getName());
178: }
179:
180: private static final class FooObject {
181: private final String name;
182: private final boolean playsBasketball;
183: private final int age;
184:
185: public FooObject(String name, int age, boolean playsBasketball) {
186: this .name = name;
187: this .age = age;
188: this .playsBasketball = playsBasketball;
189: }
190:
191: public String getName() {
192: return name;
193: }
194:
195: public int getAge() {
196: return age;
197: }
198:
199: public boolean playsBasketball() {
200: return playsBasketball;
201: }
202:
203: public boolean equals(Object foo) {
204: if (foo == null) {
205: return false;
206: }
207: if (((FooObject) foo).getName().equals(name)
208: && ((FooObject) foo).getAge() == age
209: && ((FooObject) foo).playsBasketball() == playsBasketball) {
210: return true;
211: }
212: return false;
213: }
214: }
215:
216: }
|