KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TransparentLinkedListTestApp


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.DSOApplicationConfig;
8 import com.tc.object.config.DSOClientConfigHelper;
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.Iterator JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  *
23  */

24 public class TransparentLinkedListTestApp extends AbstractTransparentApp {
25
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 LinkedList JavaDoc list = new LinkedList JavaDoc();
33
34   public TransparentLinkedListTestApp(String JavaDoc applicationId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
35     super(applicationId, 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(), i);
44       testObjects.add(to);
45       synchronized (list) {
46         int size = list.size();
47         list.add(to);
48         Assert.eval(list.size() == size + 1);
49       }
50     }
51     moveToStageAndWait(ADD_COMPLETE_STAGE);
52
53     checkSize(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 (list) {
60         try {
61           int size = list.size();
62           boolean wasRemoved = list.remove(testObjects.get(i));
63           Assert.eval("Test object should have been removed but wasn't: " + testObjects.get(i), wasRemoved);
64           Assert.eval(list.size() == size - 1);
65         } catch (Throwable JavaDoc t) {
66           t.printStackTrace();
67         }
68       }
69     }
70
71     moveToStageAndWait(REMOVE_COMPLETE_STAGE);
72
73     checkSize(maxCount - getParticipantCount() * removeCount);
74
75     moveToStageAndWait(ASSERT_REMOVE_SIZE_STAGE);
76
77     synchronized (list) {
78       list.clear();
79       Assert.eval(list.size() == 0);
80     }
81
82     checkSize(0);
83     notifyResult(Boolean.TRUE);
84   }
85
86   private void checkSize(int s) {
87     synchronized (list) {
88       if (list.size() != s) {
89         System.out.println("list:" + list.size() + " expecting:" + s);
90         Map JavaDoc res = new HashMap JavaDoc();
91         for (Iterator JavaDoc i = list.iterator(); i.hasNext();) {
92           TestObject to = (TestObject) i.next();
93           String JavaDoc key = to.getId();
94           if (!res.containsKey(key)) {
95             res.put(key, new Long JavaDoc(0));
96           } else {
97             long v = ((Long JavaDoc) res.get(key)).longValue();
98             res.put(key, new Long JavaDoc(++v));
99           }
100         }
101         throw new AssertionError JavaDoc("" + res);
102       }
103     }
104   }
105
106   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
107     String JavaDoc testClassName = TransparentLinkedListTestApp.class.getName();
108     config.addIncludePattern(testClassName);
109     config.addRoot(testClassName, "list", "list", true);
110     config.addWriteAutolock("* " + testClassName + ".*(..)");
111     config.addIncludePattern(TestObject.class.getName());
112     visitor.visit(config, AbstractTransparentApp.class);
113   }
114   
115   public static void visitDSOApplicationConfig(ConfigVisitor visitor, DSOApplicationConfig config) {
116     String JavaDoc classname = TransparentLinkedListTestApp.class.getName();
117     config.addIncludePattern(classname);
118     config.addRoot("list", classname + ".list");
119     config.addWriteAutolock("* " + classname + ".*(..)");
120     config.addIncludePattern(TestObject.class.getName());
121     visitor.visitDSOApplicationConfig(config, AbstractTransparentApp.class);
122   }
123
124   static class TestObject {
125     private String JavaDoc id;
126     private int count;
127
128     TestObject(String JavaDoc i, int count) {
129       this.id = i;
130       this.count = count;
131     }
132
133     public String JavaDoc getId() {
134       return id;
135     }
136
137     public String JavaDoc toString() {
138       return "TestObject(" + id + "," + count + ")";
139     }
140   }
141
142 }
Popular Tags