KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > LinkedHashMapTestApp


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

5 package com.tctest;
6
7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
8
9 import com.tc.object.config.ConfigVisitor;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.TransparencyClassSpec;
12 import com.tc.object.tx.ReadOnlyException;
13 import com.tc.simulator.app.ApplicationConfig;
14 import com.tc.simulator.listener.ListenerProvider;
15 import com.tc.util.Assert;
16 import com.tc.util.DebugUtil;
17 import com.tctest.restart.system.ObjectDataRestartTestApp;
18 import com.tctest.runner.AbstractTransparentApp;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 public class LinkedHashMapTestApp extends AbstractTransparentApp {
27   public static final String JavaDoc SYNCHRONOUS_WRITE = "synch-write";
28
29   private final MapRoot root = new MapRoot();
30
31   private final CyclicBarrier barrier;
32
33   public LinkedHashMapTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
34     super(appId, cfg, listenerProvider);
35     barrier = new CyclicBarrier(getParticipantCount());
36   }
37
38   public void run() {
39     try {
40       putTesting();
41       getTesting();
42       getROTesting();
43       unsharedGetTesting();
44     } catch (Throwable JavaDoc t) {
45       notifyError(t);
46     }
47   }
48
49   private void clear() throws Exception JavaDoc {
50     synchronized (root) {
51       root.clear();
52     }
53     barrier.barrier();
54   }
55
56   private void initialize() throws Exception JavaDoc {
57     synchronized (root) {
58       if (root.getIndex() == 0) {
59         root.getMap().putAll(getInitialMapData());
60         root.setIndex(root.getIndex() + 1);
61       }
62     }
63     barrier.barrier();
64   }
65
66   private Map JavaDoc getInitialMapData() {
67     Map JavaDoc map = new HashMap JavaDoc();
68     map.put("January", "Jan");
69     map.put("February", "Feb");
70     map.put("March", "Mar");
71     map.put("April", "Apr");
72     return map;
73   }
74
75   private void getTesting() throws Exception JavaDoc {
76     clear();
77     initialize();
78
79     LinkedHashMap JavaDoc map = root.getMap();
80     synchronized (map) {
81       if (root.getIndex() != 0) {
82         map.get("February");
83         map.get("April");
84         root.setIndex(0);
85       }
86     }
87
88     barrier.barrier();
89
90     LinkedHashMap JavaDoc expect = new LinkedHashMap JavaDoc(10, 0.75f, true);
91     expect.putAll(getInitialMapData());
92     expect.get("February");
93     expect.get("April");
94
95     assertMappings(expect, map);
96
97     barrier.barrier();
98   }
99
100   /**
101    * The goal of this test is that it will not throw a ReadOnlyException during the get() operation since the
102    * LinkedHashMap is unshared.
103    */

104   private void unsharedGetTesting() throws Exception JavaDoc {
105     LinkedHashMap JavaDoc map = new LinkedHashMap JavaDoc(10, 0.75f, true);
106     map.putAll(getInitialMapData());
107     map.get("February");
108     map.get("April");
109
110     barrier.barrier();
111   }
112
113   private void getROTesting() throws Exception JavaDoc {
114     clear();
115     initialize();
116     try {
117       tryReadOnlyGetTesting();
118       throw new AssertionError JavaDoc("I should have thrown an ReadOnlyException.");
119     } catch (Throwable JavaDoc t) {
120       if (ReadOnlyException.class.getName().equals(t.getClass().getName())) {
121         // ignore ReadOnlyException in read only tests.
122
} else {
123         throw new RuntimeException JavaDoc(t);
124       }
125     }
126     barrier.barrier();
127   }
128
129   private void tryReadOnlyGetTesting() {
130     LinkedHashMap JavaDoc map = root.getMap();
131     synchronized (map) {
132       if (root.getIndex() != 0) {
133         map.get("February");
134         map.get("April");
135         root.setIndex(0);
136       }
137     }
138   }
139
140   private void putTesting() throws Exception JavaDoc {
141     clear();
142     initialize();
143
144     LinkedHashMap JavaDoc expect = new LinkedHashMap JavaDoc(10, 0.75f, true);
145     expect.putAll(getInitialMapData());
146
147     assertMappings(expect, root.getMap());
148
149     barrier.barrier();
150
151   }
152
153   void assertMappings(Map JavaDoc expect, Map JavaDoc actual) {
154     Assert.assertEquals(expect.size(), actual.size());
155
156     Set JavaDoc expectEntries = expect.entrySet();
157     Set JavaDoc actualEntries = actual.entrySet();
158     for (Iterator JavaDoc iExpect = expectEntries.iterator(), iActual = actualEntries.iterator(); iExpect.hasNext();) {
159       Assert.assertEquals(iExpect.next(), iActual.next());
160     }
161   }
162
163   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
164     visitL1DSOConfig(visitor, config, new HashMap JavaDoc());
165   }
166
167   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config, Map JavaDoc optionalAttributes) {
168     DebugUtil.DEBUG = true;
169
170     boolean isSynchronousWrite = false;
171     if (optionalAttributes.size() > 0) {
172       isSynchronousWrite = Boolean.valueOf((String JavaDoc) optionalAttributes.get(ObjectDataRestartTestApp.SYNCHRONOUS_WRITE))
173           .booleanValue();
174     }
175
176     TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName());
177     addWriteAutolock(config, isSynchronousWrite, "* " + CyclicBarrier.class.getName() + "*.*(..)");
178
179     String JavaDoc testClass = LinkedHashMapTestApp.class.getName();
180     spec = config.getOrCreateSpec(testClass);
181
182     config.addIncludePattern(MapRoot.class.getName());
183
184     String JavaDoc writeAllowdMethodExpression = "* " + testClass + "*.*(..)";
185     addWriteAutolock(config, isSynchronousWrite, writeAllowdMethodExpression);
186     String JavaDoc readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)";
187     config.addReadAutolock(readOnlyMethodExpression);
188
189     spec.addRoot("root", "root");
190     spec.addRoot("barrier", "barrier");
191
192     DebugUtil.DEBUG = false;
193   }
194
195   private static void addWriteAutolock(DSOClientConfigHelper config, boolean isSynchronousWrite, String JavaDoc methodPattern) {
196     if (isSynchronousWrite) {
197       config.addSynchronousWriteAutolock(methodPattern);
198       debugPrintln("***** doing a synchronous write");
199     } else {
200       config.addWriteAutolock(methodPattern);
201     }
202   }
203
204   private static void debugPrintln(String JavaDoc s) {
205     if (DebugUtil.DEBUG) {
206       System.err.println(s);
207     }
208   }
209
210   private static class MapRoot {
211     private final LinkedHashMap JavaDoc map = new LinkedHashMap JavaDoc(10, 0.75f, true);
212     private int index;
213
214     public MapRoot() {
215       index = 0;
216     }
217
218     public int getIndex() {
219       return index;
220     }
221
222     public void setIndex(int index) {
223       this.index = index;
224     }
225
226     public LinkedHashMap JavaDoc getMap() {
227       return map;
228     }
229
230     public void clear() {
231       map.clear();
232       index = 0;
233     }
234   }
235 }
236
Popular Tags