KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > PartialCollectionsCloneTest


1 /*
2  * All content copyright (c) 2003-2007 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.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 JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
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 JavaDoc {
31     t.getTransparentAppConfig().setClientCount(NODE_COUNT);
32     t.initializeTestRunner();
33   }
34
35   protected Class JavaDoc 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 JavaDoc hashmap;
43     private Hashtable JavaDoc hashtable;
44
45     public PartialCollectionsCloneTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
46       super(appId, cfg, listenerProvider);
47       barrier = new CyclicBarrier(getParticipantCount());
48     }
49
50     protected void runTest() throws Throwable JavaDoc {
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 JavaDoc m) {
68       final Map JavaDoc cloned;
69
70       // read the map in the same lock under which is was mutated
71
synchronized (m) {
72         cloned = (Map JavaDoc) cloneIt(m);
73       }
74
75       Assert.assertEquals(MAP_SIZE, cloned.size());
76
77       if (((Manageable) cloned).__tc_isManaged()) { throw new AssertionError JavaDoc("cloned object is shared"); }
78
79       for (Iterator JavaDoc i = cloned.entrySet().iterator(); i.hasNext();) {
80         Map.Entry JavaDoc entry = (Entry) i.next();
81
82         // Before the fix for clone() of partial maps, this would fail with ClassCastException since unresolved
83
// types are still in the map values
84
NonLiteralObject nlo = (NonLiteralObject) entry.getValue();
85         Assert.assertNotNull(nlo);
86       }
87     }
88
89     private Object JavaDoc cloneIt(Object JavaDoc o) {
90       try {
91         Method JavaDoc m = o.getClass().getDeclaredMethod("clone", new Class JavaDoc[] {});
92         return m.invoke(o, new Object JavaDoc[] {});
93       } catch (Exception JavaDoc e) {
94         throw new RuntimeException JavaDoc(e);
95       }
96     }
97
98     private void createRoots() {
99       hashmap = new HashMap JavaDoc();
100       hashtable = new Hashtable JavaDoc();
101
102       populateMap(hashmap);
103       populateMap(hashtable);
104     }
105
106     private void populateMap(Map JavaDoc m) {
107       synchronized (m) {
108         for (int i = 0; i < MAP_SIZE; i++) {
109           m.put(new Integer JavaDoc(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 JavaDoc testClass = PartialCollectionsCloneTestApp.class.getName();
133       TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
134       String JavaDoc 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