1 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 ; 21 import java.util.Iterator ; 22 import java.util.LinkedHashMap ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 public class LinkedHashMapTestApp extends AbstractTransparentApp { 27 public static final String SYNCHRONOUS_WRITE = "synch-write"; 28 29 private final MapRoot root = new MapRoot(); 30 31 private final CyclicBarrier barrier; 32 33 public LinkedHashMapTestApp(String 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 t) { 45 notifyError(t); 46 } 47 } 48 49 private void clear() throws Exception { 50 synchronized (root) { 51 root.clear(); 52 } 53 barrier.barrier(); 54 } 55 56 private void initialize() throws Exception { 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 getInitialMapData() { 67 Map map = new HashMap (); 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 { 76 clear(); 77 initialize(); 78 79 LinkedHashMap 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 expect = new LinkedHashMap (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 104 private void unsharedGetTesting() throws Exception { 105 LinkedHashMap map = new LinkedHashMap (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 { 114 clear(); 115 initialize(); 116 try { 117 tryReadOnlyGetTesting(); 118 throw new AssertionError ("I should have thrown an ReadOnlyException."); 119 } catch (Throwable t) { 120 if (ReadOnlyException.class.getName().equals(t.getClass().getName())) { 121 } else { 123 throw new RuntimeException (t); 124 } 125 } 126 barrier.barrier(); 127 } 128 129 private void tryReadOnlyGetTesting() { 130 LinkedHashMap 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 { 141 clear(); 142 initialize(); 143 144 LinkedHashMap expect = new LinkedHashMap (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 expect, Map actual) { 154 Assert.assertEquals(expect.size(), actual.size()); 155 156 Set expectEntries = expect.entrySet(); 157 Set actualEntries = actual.entrySet(); 158 for (Iterator 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 ()); 165 } 166 167 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config, Map optionalAttributes) { 168 DebugUtil.DEBUG = true; 169 170 boolean isSynchronousWrite = false; 171 if (optionalAttributes.size() > 0) { 172 isSynchronousWrite = Boolean.valueOf((String ) 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 testClass = LinkedHashMapTestApp.class.getName(); 180 spec = config.getOrCreateSpec(testClass); 181 182 config.addIncludePattern(MapRoot.class.getName()); 183 184 String writeAllowdMethodExpression = "* " + testClass + "*.*(..)"; 185 addWriteAutolock(config, isSynchronousWrite, writeAllowdMethodExpression); 186 String 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 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 s) { 205 if (DebugUtil.DEBUG) { 206 System.err.println(s); 207 } 208 } 209 210 private static class MapRoot { 211 private final LinkedHashMap map = new LinkedHashMap (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 getMap() { 227 return map; 228 } 229 230 public void clear() { 231 map.clear(); 232 index = 0; 233 } 234 } 235 } 236 | Popular Tags |