1 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.DSOClientConfigHelper; 10 import com.tc.object.config.TransparencyClassSpec; 11 import com.tc.object.config.spec.CyclicBarrierSpec; 12 import com.tc.simulator.app.ApplicationConfig; 13 import com.tc.simulator.listener.ListenerProvider; 14 import com.tc.util.Assert; 15 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 16 17 import java.util.ArrayList ; 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.ListIterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.SortedMap ; 30 import java.util.SortedSet ; 31 import java.util.TreeMap ; 32 import java.util.TreeSet ; 33 import java.util.Map.Entry; 34 35 public class CollectionsWrappersTest extends TransparentTestBase { 36 37 private static final int NODE_COUNT = 3; 38 39 public CollectionsWrappersTest() { 40 disableAllUntil("2023-04-01"); 41 } 42 43 public void setUp() throws Exception { 44 super.setUp(); 45 getTransparentAppConfig().setClientCount(NODE_COUNT).setIntensity(1); 46 initializeTestRunner(); 47 } 48 49 protected Class getApplicationClass() { 50 return CollectionsWrappersTestApp.class; 51 } 52 53 public static class CollectionsWrappersTestApp extends AbstractErrorCatchingTransparentApp { 54 55 private static final Map root = new HashMap (); 56 private final CyclicBarrier barrier; 57 58 private final LinkedList c = new LinkedList (Arrays.asList(new Object [] { "item" })); 59 private final ArrayList l = new ArrayList (Arrays.asList(new Object [] { "timmy" })); 60 private final HashMap m = new HashMap (); 61 private final HashSet s = new HashSet (); 62 private final TreeMap sm = new TreeMap (); 63 private final TreeSet ss = new TreeSet (); 64 { 65 m.put("yer", "mom"); 66 s.add("it"); 67 sm.put("swing", "low"); 68 ss.add("sweet chariot"); 69 } 70 71 public CollectionsWrappersTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 72 super(appId, cfg, listenerProvider); 73 barrier = new CyclicBarrier(getParticipantCount()); 74 } 75 76 protected void runTest() throws Throwable { 77 int n = barrier.barrier(); 78 79 if (n == 0) { 80 synchronized (root) { 81 root.put("synch collection", Collections.synchronizedCollection(new LinkedList ())); 86 root.put("synch list", Collections.synchronizedList(new ArrayList ())); 87 root.put("synch map", Collections.synchronizedMap(new HashMap ())); 88 root.put("synch set", Collections.synchronizedSet(new HashSet ())); 89 root.put("synch sorted map", Collections.synchronizedSortedMap(new TreeMap ())); 90 root.put("synch sorted set", Collections.synchronizedSortedSet(new TreeSet ())); 91 92 root.put("empty list", Collections.EMPTY_LIST); 93 root.put("empty set", Collections.EMPTY_SET); 94 root.put("empty map", Collections.EMPTY_MAP); 95 96 root.put("singleton", Collections.singleton("item")); 97 root.put("singleton list", Collections.singletonList("item")); 98 root.put("singleton map", Collections.singletonMap("key", "value")); 99 100 root.put("unmod collection", Collections.unmodifiableCollection((Collection ) c.clone())); 101 root.put("unmod list", Collections.unmodifiableList((List ) l.clone())); 102 root.put("unmod map", Collections.unmodifiableMap((Map ) m.clone())); 103 root.put("unmod set", Collections.unmodifiableSet((Set ) s.clone())); 104 root.put("unmod sorted map", Collections.unmodifiableSortedMap((SortedMap ) sm.clone())); 105 root.put("unmod sorted set", Collections.unmodifiableSortedSet((SortedSet ) ss.clone())); 106 } 107 108 for (Iterator i = root.keySet().iterator(); i.hasNext();) { 111 String key = (String ) i.next(); 112 if (key.startsWith("synch ")) { 113 Object o = root.get(key); 114 if (o instanceof Collection ) { 115 Collection c1 = (Collection ) o; 116 c1.add("value"); 118 } else if (o instanceof Map ) { 119 Map m1 = (Map ) o; 120 m1.put("key", "value"); 121 } else { 122 throw new AssertionError ("unknown type: " + o.getClass()); 123 } 124 } 125 } 126 } 127 128 barrier.barrier(); 129 130 verify(); 131 } 132 133 private void verify() { 134 for (Iterator i = root.keySet().iterator(); i.hasNext();) { 135 String key = (String ) i.next(); 136 if (key.startsWith("synch ")) { 137 Object o = root.get(key); 138 if (o instanceof Collection ) { 139 Collection c1 = (Collection ) o; 140 Assert.assertEquals(1, c1.size()); 141 Assert.assertEquals("value", c1.iterator().next()); 142 } else if (o instanceof Map ) { 143 Map m1 = (Map ) o; 144 Assert.assertEquals(1, m1.size()); 145 Assert.assertEquals("value", m1.values().iterator().next()); 146 Assert.assertEquals("key", m1.keySet().iterator().next()); 147 } else { 148 throw new AssertionError ("unknown type: " + o.getClass()); 149 } 150 } 151 } 152 153 assertEquals(Collections.EMPTY_LIST, root.get("empty list")); 154 assertEquals(Collections.EMPTY_SET, root.get("empty set")); 155 assertEquals(Collections.EMPTY_MAP, root.get("empty map")); 156 157 assertEquals(Collections.singleton("item"), root.get("singleton")); 158 assertEquals(Collections.singletonList("item"), root.get("singleton list")); 159 assertEquals(Collections.singletonMap("key", "value"), root.get("singleton map")); 160 161 assertTrue(Arrays.equals(Collections.unmodifiableCollection(c).toArray(), ((Collection ) root 162 .get("unmod collection")).toArray())); 163 assertEquals(Collections.unmodifiableList(l), root.get("unmod list")); 164 assertEquals(Collections.unmodifiableMap(m), root.get("unmod map")); 165 assertEquals(Collections.unmodifiableSet(s), root.get("unmod set")); 166 assertEquals(Collections.unmodifiableSortedMap(sm), root.get("unmod sorted map")); 167 assertEquals(Collections.unmodifiableSortedSet(ss), root.get("unmod sorted set")); 168 169 for (Iterator i = root.values().iterator(); i.hasNext();) { 170 exercise(i.next()); 171 } 172 } 173 174 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 175 visitor.visit(config, new CyclicBarrierSpec()); 176 177 TransparencyClassSpec spec; 178 String testClass; 179 180 testClass = CollectionsWrappersTestApp.class.getName(); 181 spec = config.getOrCreateSpec(testClass); 182 183 String methodExpression = "* " + testClass + ".*(..)"; 184 config.addWriteAutolock(methodExpression); 185 186 spec.addRoot("root", "root"); 187 spec.addRoot("barrier", "barrier"); 188 } 189 190 private static void exercise(Object o) { 191 194 if (o instanceof SortedMap ) { 195 exerciseSortedMap((SortedMap ) o); 196 } else if (o instanceof SortedSet ) { 197 exerciseSortedSet((SortedSet ) o); 198 } else if (o instanceof Map ) { 199 exerciseMap((Map ) o); 200 } else if (o instanceof Set ) { 201 exerciseSet((Set ) o); 202 } else if (o instanceof List ) { 203 exerciseList((List ) o); 204 } else if (o instanceof Collection ) { 205 exerciseCollection((Collection ) o); 206 } else { 207 throw new AssertionError ("unknown type: " + o.getClass().getName()); 208 } 209 } 210 211 private static void exerciseCollection(Collection coll) { 212 coll.contains(coll); 213 coll.containsAll(coll); 214 coll.equals(coll); 215 coll.isEmpty(); 216 for (Iterator i = coll.iterator(); i.hasNext();) { 217 i.next(); 218 } 219 coll.hashCode(); 220 coll.size(); 221 coll.toArray(); 222 coll.toArray(new Object [] {}); 223 coll.toString(); 224 } 225 226 private static void exerciseList(List list) { 227 list.contains(list); 228 list.containsAll(list); 229 list.equals(list); 230 if (list.size() > 0) { 231 list.get(0); 232 } 233 list.indexOf(list); 234 list.isEmpty(); 235 list.hashCode(); 236 for (Iterator i = list.iterator(); i.hasNext();) { 237 i.next(); 238 } 239 list.lastIndexOf(list); 240 for (ListIterator i = list.listIterator(); i.hasNext();) { 241 i.hasPrevious(); 242 i.hasNext(); 243 i.nextIndex(); 244 i.next(); 245 i.previousIndex(); 246 i.previous(); 247 i.next(); 248 } 249 list.listIterator(0); 250 list.size(); 251 list.subList(0, 0); 252 list.toArray(); 253 list.toArray(new Object [] {}); 254 list.toString(); 255 } 256 257 private static void exerciseSet(Set set) { 258 set.contains("darth"); 259 set.containsAll(set); 260 set.equals(set); 261 set.hashCode(); 262 set.isEmpty(); 263 for (Iterator i = set.iterator(); i.hasNext();) { 264 i.next(); 265 } 266 set.size(); 267 set.toArray(); 268 set.toArray(new Object [] {}); 269 set.toString(); 270 } 271 272 private static void exerciseSortedMap(SortedMap map) { 273 exerciseMap(map); 274 map.comparator(); 275 map.firstKey(); 276 map.headMap(map.firstKey()); 277 map.lastKey(); 278 map.subMap(map.firstKey(), map.lastKey()); 279 map.tailMap(map.firstKey()); 280 } 281 282 private static void exerciseSortedSet(SortedSet set) { 283 } 285 286 private static void exerciseMap(Map map) { 287 map.containsKey("bob"); 288 map.containsValue(map); 289 exerciseSet(map.entrySet()); 290 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 291 Map.Entry entry = (Entry) i.next(); 292 entry.getKey(); 293 entry.getValue(); 294 } 295 map.equals(map); 296 map.get("fanny"); 297 map.hashCode(); 298 map.isEmpty(); 299 exerciseSet(map.keySet()); 300 map.size(); 301 map.toString(); 302 exerciseCollection(map.values()); 303 } 304 305 } 306 } 307 | Popular Tags |