1 5 package com.tctest; 6 7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 8 9 import com.tc.object.bytecode.Manageable; 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.simulator.app.ApplicationConfig; 15 import com.tc.simulator.listener.ListenerProvider; 16 import com.tc.util.Assert; 17 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 18 19 import java.lang.reflect.Method ; 20 import java.util.HashMap ; 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Map.Entry; 25 26 public class PartialCollectionsCloneTest extends TransparentTestBase { 27 28 private static final int NODE_COUNT = 2; 29 30 public void doSetUp(TransparentTestIface t) throws Exception { 31 t.getTransparentAppConfig().setClientCount(NODE_COUNT); 32 t.initializeTestRunner(); 33 } 34 35 protected Class getApplicationClass() { 36 return PartialCollectionsCloneTestApp.class; 37 } 38 39 public static class PartialCollectionsCloneTestApp extends AbstractErrorCatchingTransparentApp { 40 private static final int MAP_SIZE = 1000; 41 private final CyclicBarrier barrier; 42 private HashMap hashmap; 43 private Hashtable hashtable; 44 45 public PartialCollectionsCloneTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 46 super(appId, cfg, listenerProvider); 47 barrier = new CyclicBarrier(getParticipantCount()); 48 } 49 50 protected void runTest() throws Throwable { 51 boolean rootCreator = barrier.barrier() == 0; 52 53 if (rootCreator) { 54 createRoots(); 55 } 56 57 barrier.barrier(); 58 59 if (!rootCreator) { 60 testMapClone(hashmap); 61 testMapClone(hashtable); 62 } 63 64 barrier.barrier(); 65 } 66 67 private void testMapClone(Map m) { 68 final Map cloned; 69 70 synchronized (m) { 72 cloned = (Map ) cloneIt(m); 73 } 74 75 Assert.assertEquals(MAP_SIZE, cloned.size()); 76 77 if (((Manageable) cloned).__tc_isManaged()) { throw new AssertionError ("cloned object is shared"); } 78 79 for (Iterator i = cloned.entrySet().iterator(); i.hasNext();) { 80 Map.Entry entry = (Entry) i.next(); 81 82 NonLiteralObject nlo = (NonLiteralObject) entry.getValue(); 85 Assert.assertNotNull(nlo); 86 } 87 } 88 89 private Object cloneIt(Object o) { 90 try { 91 Method m = o.getClass().getDeclaredMethod("clone", new Class [] {}); 92 return m.invoke(o, new Object [] {}); 93 } catch (Exception e) { 94 throw new RuntimeException (e); 95 } 96 } 97 98 private void createRoots() { 99 hashmap = new HashMap (); 100 hashtable = new Hashtable (); 101 102 populateMap(hashmap); 103 populateMap(hashtable); 104 } 105 106 private void populateMap(Map m) { 107 synchronized (m) { 108 for (int i = 0; i < MAP_SIZE; i++) { 109 m.put(new Integer (i), new NonLiteralObject()); 110 } 111 } 112 } 113 114 private static class NonLiteralObject { 115 final NonLiteralObject next; 116 117 public NonLiteralObject() { 118 next = new NonLiteralObject(1); 119 } 120 121 private NonLiteralObject(int i) { 122 if (i < 3) { 123 next = new NonLiteralObject(++i); 124 } else { 125 next = null; 126 } 127 } 128 } 129 130 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 131 new CyclicBarrierSpec().visit(visitor, config); 132 String testClass = PartialCollectionsCloneTestApp.class.getName(); 133 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 134 String methodExpr = "* " + testClass + "*.*(..)"; 135 config.addWriteAutolock(methodExpr); 136 config.addIncludePattern(testClass + "*"); 137 138 spec.addRoot("barrier", "barrier"); 139 spec.addRoot("hashmap", "hashmap"); 140 spec.addRoot("hashtable", "hashtable"); 141 } 142 143 } 144 145 } 146 | Popular Tags |