KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > NullReferenceTestApp


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.object.config.TransparencyClassSpec;
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.IdentityHashMap JavaDoc;
18 import java.util.LinkedList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 public class NullReferenceTestApp extends AbstractTransparentApp {
25
26   private final List JavaDoc nodes = new ArrayList JavaDoc();
27   private Holder holder;
28
29   public NullReferenceTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
30     super(appId, cfg, listenerProvider);
31   }
32
33   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
34     String JavaDoc testClass = NullReferenceTestApp.class.getName();
35     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
36     spec.addRoot("nodes", "nodesLock");
37     spec.addRoot("holder", "holderLock");
38
39     String JavaDoc methodExpression;
40
41     methodExpression = "* " + testClass + ".init()";
42     config.addWriteAutolock(methodExpression);
43
44     methodExpression = "* " + testClass + ".check()";
45     config.addWriteAutolock(methodExpression);
46
47     methodExpression = "* " + testClass + "$Holder.*(..)";
48     config.addWriteAutolock(methodExpression);
49     
50     config.addIncludePattern(Holder.class.getName());
51   }
52
53   public void run() {
54     init();
55     check();
56     finish();
57   }
58
59   private void finish() {
60     holder.mod();
61   }
62
63   private void check() {
64     holder.check();
65
66     synchronized (nodes) {
67       nodes.remove(0);
68       nodes.notifyAll();
69
70       while (nodes.size() > 0) {
71         try {
72           nodes.wait();
73         } catch (InterruptedException JavaDoc e) {
74           throw new RuntimeException JavaDoc(e);
75         }
76       }
77     }
78   }
79
80   private void init() {
81     synchronized (nodes) {
82       Holder h = new Holder();
83       this.holder = h;
84
85       // Make sure we're actually using the distributed version of the "holder" object.
86
// This nonsense wouldn't be necessary if GETFIELD on roots was instrumented
87
if (nodes.size() == 0) {
88         Assert.assertTrue(this.holder == h);
89       } else {
90         Assert.assertTrue(this.holder != h);
91       }
92
93       nodes.add(new Object JavaDoc());
94       nodes.notifyAll();
95
96       while (nodes.size() != getParticipantCount()) {
97         try {
98           nodes.wait();
99         } catch (InterruptedException JavaDoc e) {
100           throw new RuntimeException JavaDoc(e);
101         }
102       }
103     }
104
105   }
106
107   private static class Holder {
108     Object JavaDoc reference = null;
109     Object JavaDoc[] array = new Object JavaDoc[] { null, new Object JavaDoc(), null };
110
111     Map JavaDoc map1 = new HashMap JavaDoc();
112     Map JavaDoc map2 = new IdentityHashMap JavaDoc();
113
114     List JavaDoc list1 = new LinkedList JavaDoc();
115     List JavaDoc list2 = new ArrayList JavaDoc();
116     List JavaDoc list3 = new Vector JavaDoc();
117
118     Set JavaDoc set1 = new HashSet JavaDoc();
119
120     private int count = 0;
121
122     Holder() {
123       mod();
124     }
125
126     public void check() {
127       Assert.assertNull(reference);
128       Assert.assertNull(array[0]);
129       Assert.assertNotNull(array[1]);
130       Assert.assertNull(array[2]);
131
132       Assert.assertEquals(1, list1.size());
133       Assert.assertEquals(1, list2.size());
134       Assert.assertEquals(1, list3.size());
135       Assert.assertTrue(list1.contains(null));
136       Assert.assertTrue(list2.contains(null));
137       Assert.assertTrue(list3.contains(null));
138
139       Assert.assertEquals(2, map1.size());
140       Assert.assertEquals(2, map2.size());
141       Assert.assertTrue(map1.containsKey(null));
142       Assert.assertTrue(map2.containsKey(null));
143       Assert.assertTrue(map1.containsValue(null));
144       Assert.assertTrue(map2.containsValue(null));
145
146       Assert.assertEquals(1, set1.size());
147       Assert.assertTrue(set1.contains(null));
148     }
149
150     synchronized void mod() {
151       modSets(new Set JavaDoc[] { set1 });
152       modLists(new List JavaDoc[] { list1, list2, list3 });
153       modMaps(new Map JavaDoc[] { map1, map2 });
154       reference = null;
155       array[0] = null;
156     }
157
158     void modLists(List JavaDoc[] lists) {
159       for (int i = 0; i < lists.length; i++) {
160         lists[i].add(null);
161       }
162     }
163
164     void modSets(Set JavaDoc[] sets) {
165       for (int i = 0; i < sets.length; i++) {
166         sets[i].add(null);
167       }
168     }
169
170     void modMaps(Map JavaDoc[] maps) {
171       for (int i = 0; i < maps.length; i++) {
172         maps[i].put(null, "value for null key");
173         maps[i].put("key" + count + " for null value", null);
174       }
175       count++;
176     }
177
178   }
179
180 }
181
Popular Tags