KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > MapOfMapsTestApp


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.BrokenBarrierException;
7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
8 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
9
10 import com.tc.object.config.ConfigVisitor;
11 import com.tc.object.config.DSOClientConfigHelper;
12 import com.tc.object.config.TransparencyClassSpec;
13 import com.tc.object.config.spec.CyclicBarrierSpec;
14 import com.tc.object.config.spec.SynchronizedIntSpec;
15 import com.tc.simulator.app.ApplicationConfig;
16 import com.tc.simulator.listener.ListenerProvider;
17 import com.tc.util.Assert;
18 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
19
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.IdentityHashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedHashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.TreeMap JavaDoc;
32 import java.util.Map.Entry;
33
34 public class MapOfMapsTestApp extends AbstractErrorCatchingTransparentApp {
35
36   private static final int LOOP_COUNT = 10;
37   private static final int DEPTH_COUNT = 5;
38   private static final int BREATH_COUNT = 3;
39
40   final Map JavaDoc root = new HashMap JavaDoc();
41   final SynchronizedInt uid = new SynchronizedInt(0);
42   CyclicBarrier barrier;
43
44   public MapOfMapsTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
45     super(appId, cfg, listenerProvider);
46   }
47
48   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
49     String JavaDoc testClass = MapOfMapsTestApp.class.getName();
50     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
51     spec.addRoot("root", "root");
52     spec.addRoot("uid", "uid");
53     spec.addRoot("barrier", "barrier");
54     String JavaDoc methodExpression = "* " + testClass + ".read(..)";
55     config.addReadAutolock(methodExpression);
56     methodExpression = "* " + testClass + ".add2Root(..)";
57     config.addWriteAutolock(methodExpression);
58     methodExpression = "* " + testClass + ".populateMyRoot(..)";
59     config.addWriteAutolock(methodExpression);
60     new SynchronizedIntSpec().visit(visitor, config);
61     new CyclicBarrierSpec().visit(visitor, config);
62   }
63
64   public void runTest() throws BrokenBarrierException, InterruptedException JavaDoc {
65     setCyclicBarrier();
66     int myid = uid.increment();
67     if (myid == 1) {
68       // Writer
69
runCreateMaps();
70     } else {
71       // readers
72
runReadMaps();
73     }
74   }
75
76   private void runReadMaps() throws BrokenBarrierException, InterruptedException JavaDoc {
77     int count = 0;
78     int mapCountNo = calculateMapCount(DEPTH_COUNT, BREATH_COUNT);
79     while (count++ < LOOP_COUNT) {
80       barrier.barrier();
81       log("Readers : Loop Count : " + count);
82       Map JavaDoc myRoot = read(String.valueOf(count), root);
83       int mapCount = countMaps(myRoot);
84       log("Readers : No Of Maps = " + mapCount);
85       Assert.assertEquals(mapCountNo, mapCount);
86     }
87   }
88
89   private int calculateMapCount(int depth, int breath) {
90     int pow = breath;
91     int count = 0;
92     for (int i = 0; i <= depth; i++) {
93       count += pow;
94       pow *= breath;
95     }
96     return count;
97   }
98
99   private int countMaps(Map JavaDoc m) {
100
101     int count = 0;
102     for (Iterator JavaDoc i = m.entrySet().iterator(); i.hasNext();) {
103       Map.Entry JavaDoc e = (Entry) i.next();
104       if (e.getValue() instanceof Map JavaDoc) {
105         count = count + 1 + countMaps((Map JavaDoc) e.getValue());
106       }
107     }
108     return count;
109   }
110
111   private void runCreateMaps() throws BrokenBarrierException, InterruptedException JavaDoc {
112     int count = 0;
113     while (count++ < LOOP_COUNT) {
114       log("Writer : Loop Count : " + count);
115       Map JavaDoc myRoot = new HashMap JavaDoc();
116       add2Root(String.valueOf(count), myRoot);
117       populateMyRoot(myRoot, DEPTH_COUNT, BREATH_COUNT);
118       barrier.barrier();
119     }
120   }
121
122   private void populateMyRoot(Map JavaDoc myRoot, int depth, int breath) {
123     List JavaDoc childs = new ArrayList JavaDoc();
124     int b = breath;
125     synchronized (myRoot) {
126       while (b-- > 0) {
127         childs.add(addToMap(String.valueOf(b), myRoot));
128       }
129     }
130     if (depth-- > 0) {
131       for (Iterator JavaDoc i = childs.iterator(); i.hasNext();) {
132         Map JavaDoc child = (Map JavaDoc) i.next();
133         populateMyRoot(child, depth, breath);
134       }
135     }
136   }
137
138   private void setCyclicBarrier() {
139     int participationCount = getParticipantCount();
140     log("Participation Count = " + participationCount);
141     barrier = new CyclicBarrier(participationCount);
142   }
143
144   private void add2Root(String JavaDoc id, Map JavaDoc myRoot) {
145     synchronized (root) {
146       root.put(id, myRoot);
147     }
148   }
149
150   static DateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("hh:mm:ss,S");
151
152   private static void log(String JavaDoc message) {
153     System.err.println(Thread.currentThread().getName() + " :: "
154                        + formatter.format(new Date JavaDoc(System.currentTimeMillis())) + " : " + message);
155   }
156
157   private Map JavaDoc addToMap(String JavaDoc id, Map JavaDoc m) {
158     Map JavaDoc child = getChild(m);
159     m.put(id, child);
160     return child;
161   }
162
163   /**
164    * HashMap, Hashtable and LinkedHashMap supports partial collection and IdentityHashMap and TreeMap doesnt support it.
165    * So both should be tested
166    */

167   private Map JavaDoc getChild(Map JavaDoc m) {
168     if (m instanceof HashMap JavaDoc) {
169       return getPopulatedMap(new Hashtable JavaDoc());
170     } else if (m instanceof Hashtable JavaDoc) {
171       return getPopulatedMap(new LinkedHashMap JavaDoc());
172     } else if (m instanceof LinkedHashMap JavaDoc) {
173       return getPopulatedMap(new IdentityHashMap JavaDoc());
174     } else if (m instanceof IdentityHashMap JavaDoc) {
175       return getPopulatedMap(new TreeMap JavaDoc());
176     } else if (m instanceof TreeMap JavaDoc) {
177       return getPopulatedMap(new HashMap JavaDoc());
178     } else {
179       throw new AssertionError JavaDoc("Should never get here");
180     }
181   }
182
183   private Map JavaDoc getPopulatedMap(Map JavaDoc m) {
184     for (int i = 0; i < 10; i++) {
185       m.put("Hello - " + i, new String JavaDoc("Hehehehehehehehehehehehehehehehehheheheheheheheheheheheheheheheheh-" + i));
186     }
187     return m;
188   }
189
190   private Map JavaDoc read(String JavaDoc id, Map JavaDoc m) {
191     synchronized (m) {
192       return (Map JavaDoc) m.get(id);
193     }
194   }
195
196 }
197
Popular Tags