KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TransparentVectorTestApp


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.CyclicBarrier;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.spec.CyclicBarrierSpec;
11 import com.tc.simulator.app.ApplicationConfig;
12 import com.tc.simulator.listener.ListenerProvider;
13 import com.tc.util.Assert;
14 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
15
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 /**
23  *
24  */

25 public class TransparentVectorTestApp extends AbstractErrorCatchingTransparentApp {
26
27   public TransparentVectorTestApp(String JavaDoc globalId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
28     super(globalId, cfg, listenerProvider);
29     barrier = new CyclicBarrier(getParticipantCount());
30   }
31
32   private static final int INITIAL_STAGE = 0;
33   private static final int ADD_COMPLETE_STAGE = 1;
34   private static final int ASSERT_MAX_COUNT_SIZE_STAGE = 2;
35   private static final int REMOVE_COMPLETE_STAGE = 3;
36   private static final int ASSERT_REMOVE_SIZE_STAGE = 4;
37
38   private final Vector JavaDoc vector = new Vector JavaDoc();
39   // private List subList;
40

41   private final CyclicBarrier barrier;
42
43   public void moveToStageAndWait(int stage) {
44     super.moveToStageAndWait(stage);
45   }
46
47   protected void runTest() throws Throwable JavaDoc {
48     int maxCount = getParticipantCount() * getIntensity();
49     List JavaDoc testObjects = new Vector JavaDoc();
50     moveToStage(INITIAL_STAGE);
51     for (int i = 0; i < getIntensity(); i++) {
52       TestObject to = new TestObject(getApplicationId(), i);
53       testObjects.add(to);
54       synchronized (vector) {
55         int size = vector.size();
56         vector.add(to);
57         Assert.eval(vector.size() == size + 1);
58       }
59     }
60     moveToStageAndWait(ADD_COMPLETE_STAGE);
61
62     checkSize(maxCount);
63
64     moveToStageAndWait(ASSERT_MAX_COUNT_SIZE_STAGE);
65     int removeCount = getIntensity() / 2;
66     for (int i = 0; i < removeCount; i++) {
67       synchronized (vector) {
68         int size = vector.size();
69         Object JavaDoc toRemove = testObjects.get(i);
70         boolean wasRemoved = vector.remove(toRemove);
71         if (!wasRemoved) {
72           System.out.println("List:" + vector + " list hash:" + System.identityHashCode(vector));
73           Assert.eval("Test object should have been removed but wasn't: " + testObjects.get(i), wasRemoved);
74           Assert.eval(vector.size() == size - 1);
75         }
76       }
77     }
78
79     moveToStageAndWait(REMOVE_COMPLETE_STAGE);
80
81     checkSize(maxCount - getParticipantCount() * removeCount);
82
83     moveToStageAndWait(ASSERT_REMOVE_SIZE_STAGE);
84
85     synchronized (vector) {
86       vector.clear();
87       Assert.eval(vector.size() == 0);
88     }
89
90     checkSize(0);
91
92     int num = barrier.barrier();
93
94     if (num == 0) {
95       synchronized (vector) {
96         vector.setSize(5);
97       }
98     }
99
100     barrier.barrier();
101
102     checkSize(5);
103
104     for (int i = 0; i < 5; i++) {
105       Object JavaDoc val = vector.get(i);
106       if (val != null) {
107         notifyError("Expected null at index " + i + ", but found " + val);
108         return;
109       }
110     }
111
112     barrier.barrier();
113
114     // subList = vector.subList(1, 3);
115
//
116
// Assert.assertNotNull(subList);
117
// Assert.assertEquals(2, subList.size());
118

119     notifyResult(Boolean.TRUE);
120   }
121
122   private void checkSize(int s) {
123     synchronized (vector) {
124       if (vector.size() != s) {
125         Map JavaDoc res = new HashMap JavaDoc();
126         for (Iterator JavaDoc i = vector.iterator(); i.hasNext();) {
127           TestObject to = (TestObject) i.next();
128           String JavaDoc key = to.getId();
129           if (!res.containsKey(key)) {
130             res.put(key, new Long JavaDoc(0));
131           } else {
132             long v = ((Long JavaDoc) res.get(key)).longValue();
133             res.put(key, new Long JavaDoc(++v));
134           }
135         }
136         throw new AssertionError JavaDoc("" + res);
137       }
138     }
139   }
140
141   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
142     new CyclicBarrierSpec().visit(visitor, config);
143     String JavaDoc testClassName = TransparentVectorTestApp.class.getName();
144     config.addRoot(testClassName, "vector", "vector", true);
145     config.addRoot(testClassName, "subList", "subList", true);
146     config.addRoot(testClassName, "barrier", "barrier", true);
147     String JavaDoc methodExpression = "* " + testClassName + ".*(..)";
148     System.err.println("Adding autolock for: " + methodExpression);
149     config.addWriteAutolock(methodExpression);
150     config.addIncludePattern(TestObject.class.getName());
151   }
152
153   static class TestObject {
154     private String JavaDoc id;
155     private int count;
156
157     TestObject(String JavaDoc id, int count) {
158       this.id = id;
159       this.count = count;
160     }
161
162     public String JavaDoc getId() {
163       return id;
164     }
165
166     public String JavaDoc toString() {
167       return "TestObject(" + id + "," + count + ")";
168     }
169   }
170
171 }
Popular Tags