KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TransparentSetTestApp


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 com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.simulator.app.Application;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tc.util.Assert;
12 import com.tctest.runner.AbstractTransparentApp;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 /**
23  * Application for testing DSO sets.
24  */

25 public class TransparentSetTestApp extends AbstractTransparentApp implements Application {
26   private static final int INITIAL_STAGE = 0;
27   private static final int ADD_COMPLETE_STAGE = 1;
28   private static final int ASSERT_MAX_COUNT_SIZE_STAGE = 2;
29   private static final int REMOVE_COMPLETE_STAGE = 3;
30   private static final int ASSERT_REMOVE_SIZE_STAGE = 4;
31
32   private Set JavaDoc set = new HashSet JavaDoc();
33
34   public TransparentSetTestApp(String JavaDoc globalId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
35     super(globalId, cfg, listenerProvider);
36   }
37
38   public void run() {
39     int maxCount = getParticipantCount() * getIntensity();
40     List JavaDoc testObjects = new ArrayList JavaDoc();
41     moveToStage(INITIAL_STAGE);
42     for (int i = 0; i < getIntensity(); i++) {
43       TestObject to = new TestObject(getApplicationId(), 1);
44       testObjects.add(to);
45       synchronized (set) {
46         int size = set.size();
47         set.add(to);
48         Assert.eval(set.size() == size + 1);
49       }
50     }
51     moveToStageAndWait(ADD_COMPLETE_STAGE);
52
53     checkSetSize(maxCount);
54
55     moveToStageAndWait(ASSERT_MAX_COUNT_SIZE_STAGE);
56
57     int removeCount = getIntensity() / 2;
58     for (int i = 0; i < removeCount; i++) {
59       synchronized (set) {
60         int size = set.size();
61         boolean wasRemoved = set.remove(testObjects.get(i));
62         Assert.eval("Test object should have been removed but wasn't: " + testObjects.get(i), wasRemoved);
63         Assert.eval(set.size() == size - 1);
64       }
65     }
66
67     moveToStageAndWait(REMOVE_COMPLETE_STAGE);
68
69     checkSetSize(maxCount - getParticipantCount() * removeCount);
70
71     moveToStageAndWait(ASSERT_REMOVE_SIZE_STAGE);
72
73     synchronized (set) {
74       set.clear();
75       Assert.eval(set.size() == 0);
76     }
77
78     checkSetSize(0);
79     notifyResult(Boolean.TRUE);
80   }
81
82   private void checkSetSize(int s) {
83     synchronized (set) {
84       String JavaDoc error = "set size:" + set.size() + " expecting: " + s + " for: " + this.getApplicationId();
85       System.out.println(error);
86       Map JavaDoc res = new HashMap JavaDoc();
87       for (Iterator JavaDoc i = set.iterator(); i.hasNext();) {
88         TestObject to = (TestObject) i.next();
89         String JavaDoc key = to.getId();
90         if (!res.containsKey(key)) {
91           res.put(key, new Long JavaDoc(0));
92         } else {
93           long v = ((Long JavaDoc) res.get(key)).longValue();
94           res.put(key, new Long JavaDoc(++v));
95         }
96       }
97       if (set.size() != s) {
98
99       throw new AssertionError JavaDoc("" + res + error); }
100     }
101   }
102
103   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
104     String JavaDoc testClassName = TransparentSetTestApp.class.getName();
105     config.addRoot(testClassName, "set", "set", true);
106     String JavaDoc methodExpression = "* " + testClassName + ".*(..)";
107     System.err.println("Adding autolock for: " + methodExpression);
108
109     config.addWriteAutolock(methodExpression);
110
111     config.addIncludePattern(TestObject.class.getName());
112   }
113
114   static class TestObject {
115
116     private String JavaDoc id;
117     private int count;
118
119     TestObject(String JavaDoc id, int count) {
120       this.id = id;
121       this.count = count;
122     }
123
124     public String JavaDoc getId() {
125       return id;
126     }
127
128     public String JavaDoc toString() {
129       return "TestObject(" + id + "," + count + ")";
130     }
131   }
132
133 }
Popular Tags