KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > LogicalIteratorTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.ConfigLockLevel;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.TransparencyClassSpec;
12 import com.tc.object.config.spec.CyclicBarrierSpec;
13 import com.tc.object.tx.ReadOnlyException;
14 import com.tc.simulator.app.ApplicationConfig;
15 import com.tc.simulator.listener.ListenerProvider;
16 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 public class LogicalIteratorTestApp extends AbstractErrorCatchingTransparentApp {
31
32   private final Set JavaDoc set = new HashSet JavaDoc();
33   private final Map JavaDoc map = new HashMap JavaDoc();
34   private final List JavaDoc llist = new LinkedList JavaDoc();
35   private final List JavaDoc alist = new ArrayList JavaDoc();
36   private final Vector JavaDoc vector = new Vector JavaDoc();
37   private final Hashtable JavaDoc hashtable = new Hashtable JavaDoc();
38   private final CyclicBarrier barrier;
39
40   public LogicalIteratorTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
41     super(appId, cfg, listenerProvider);
42     this.barrier = new CyclicBarrier(getParticipantCount());
43   }
44
45   protected void runTest() throws Throwable JavaDoc {
46     String JavaDoc me = getApplicationId();
47     init(me);
48     barrier.barrier();
49     readOnly();
50     barrier.barrier();
51     remove(me);
52     barrier.barrier();
53     verifySize();
54   }
55
56   private void verifySize() {
57     synchronized (set) {
58       if (vector.size() != 0) { throw new RuntimeException JavaDoc("vector not empty: " + vector.size()); }
59       if (map.size() != 0) { throw new RuntimeException JavaDoc("map not empty: " + map.size()); }
60       if (alist.size() != 0) { throw new RuntimeException JavaDoc("alist not empty" + alist.size()); }
61       if (set.size() != 0) { throw new RuntimeException JavaDoc("set not empty: " + set.size()); }
62       if (llist.size() != 0) { throw new RuntimeException JavaDoc("llist not empty: " + llist.size()); }
63       if (hashtable.size() != 0) { throw new RuntimeException JavaDoc("hashtable not empty: " + hashtable.size()); }
64     }
65   }
66
67   private void init(String JavaDoc id) {
68     synchronized (barrier) {
69       vector.add(id);
70       map.put("key" + id, id);
71       map.put(id, "value" + id);
72       alist.add(id);
73       set.add(id);
74       llist.add(id);
75       hashtable.put("key" + id, id);
76       hashtable.put(id, "value" + id);
77     }
78   }
79
80   private void remove(String JavaDoc id) {
81     synchronized (barrier) {
82       remove(vector.iterator(), id);
83       remove(map.keySet().iterator(), "key" + id);
84       remove(map.values().iterator(), "value" + id);
85       remove(alist.iterator(), id);
86       remove(set.iterator(), id);
87       remove(llist.iterator(), id);
88       remove(hashtable.keySet().iterator(), "key" + id);
89       remove(hashtable.values().iterator(), "value" + id);
90     }
91   }
92
93   private void remove(Iterator JavaDoc iter, String JavaDoc id) {
94     while (iter.hasNext()) {
95       if (id.equals(iter.next())) {
96         iter.remove();
97         return;
98       }
99     }
100   }
101
102   private void readOnly() {
103     synchronized (barrier) {
104       attemptRemove(map.keySet());
105       attemptRemove(map.values());
106       attemptRemove(alist);
107       attemptRemove(set);
108       attemptRemove(llist);
109     }
110   }
111
112   private void attemptRemove(Collection JavaDoc collection) {
113     String JavaDoc type = collection.getClass().getName();
114     final int startSize = collection.size();
115
116     Iterator JavaDoc iter = collection.iterator();
117     iter.next();
118
119     try {
120       iter.remove();
121       throw new RuntimeException JavaDoc("Iterator remove() allowed in read-only transaction on type " + type);
122     } catch (ReadOnlyException roe) {
123       // expected
124
}
125
126     int endSize = collection.size();
127     if (startSize != endSize) {
128       // collection size changed
129
throw new AssertionError JavaDoc("Collection of type " + type + " changed size during read-only TXN: (" + startSize
130                                + " != " + endSize + ")");
131     }
132
133   }
134
135   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
136     String JavaDoc testClass = LogicalIteratorTestApp.class.getName();
137     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
138
139     new CyclicBarrierSpec().visit(visitor, config);
140
141     String JavaDoc method = "* " + testClass + ".init(..)";
142     config.addWriteAutolock(method);
143
144     method = "* " + testClass + ".readOnly(..)";
145     config.addAutolock(method, ConfigLockLevel.READ);
146
147     method = "* " + testClass + ".remove(..)";
148     config.addWriteAutolock(method);
149
150     method = "* " + testClass + ".runTest()";
151     config.addWriteAutolock(method);
152
153     method = "* " + testClass + ".verifySize(..)";
154     config.addWriteAutolock(method);
155
156     spec.addRoot("hashtable", "hashtableLock");
157     spec.addRoot("set", "setLock");
158     spec.addRoot("vector", "vectorLock");
159     spec.addRoot("map", "mapLock");
160     spec.addRoot("llist", "llistLock");
161     spec.addRoot("alist", "alistLock");
162     spec.addRoot("barrier", "barrierLock");
163   }
164
165 }
166
Popular Tags