001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOClientConfigHelper;
008: import com.tc.object.config.TransparencyClassSpec;
009: import com.tc.simulator.app.ApplicationConfig;
010: import com.tc.simulator.listener.ListenerProvider;
011: import com.tc.util.Assert;
012: import com.tctest.runner.AbstractTransparentApp;
013:
014: import java.util.ArrayList;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.IdentityHashMap;
018: import java.util.LinkedList;
019: import java.util.List;
020: import java.util.Map;
021: import java.util.Set;
022: import java.util.Vector;
023:
024: public class NullReferenceTestApp extends AbstractTransparentApp {
025:
026: private final List nodes = new ArrayList();
027: private Holder holder;
028:
029: public NullReferenceTestApp(String appId, ApplicationConfig cfg,
030: ListenerProvider listenerProvider) {
031: super (appId, cfg, listenerProvider);
032: }
033:
034: public static void visitL1DSOConfig(ConfigVisitor visitor,
035: DSOClientConfigHelper config) {
036: String testClass = NullReferenceTestApp.class.getName();
037: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
038: spec.addRoot("nodes", "nodesLock");
039: spec.addRoot("holder", "holderLock");
040:
041: String methodExpression;
042:
043: methodExpression = "* " + testClass + ".init()";
044: config.addWriteAutolock(methodExpression);
045:
046: methodExpression = "* " + testClass + ".check()";
047: config.addWriteAutolock(methodExpression);
048:
049: methodExpression = "* " + testClass + "$Holder.*(..)";
050: config.addWriteAutolock(methodExpression);
051:
052: config.addIncludePattern(Holder.class.getName());
053: }
054:
055: public void run() {
056: init();
057: check();
058: finish();
059: }
060:
061: private void finish() {
062: holder.mod();
063: }
064:
065: private void check() {
066: holder.check();
067:
068: synchronized (nodes) {
069: nodes.remove(0);
070: nodes.notifyAll();
071:
072: while (nodes.size() > 0) {
073: try {
074: nodes.wait();
075: } catch (InterruptedException e) {
076: throw new RuntimeException(e);
077: }
078: }
079: }
080: }
081:
082: private void init() {
083: synchronized (nodes) {
084: Holder h = new Holder();
085: this .holder = h;
086:
087: // Make sure we're actually using the distributed version of the "holder" object.
088: // This nonsense wouldn't be necessary if GETFIELD on roots was instrumented
089: if (nodes.size() == 0) {
090: Assert.assertTrue(this .holder == h);
091: } else {
092: Assert.assertTrue(this .holder != h);
093: }
094:
095: nodes.add(new Object());
096: nodes.notifyAll();
097:
098: while (nodes.size() != getParticipantCount()) {
099: try {
100: nodes.wait();
101: } catch (InterruptedException e) {
102: throw new RuntimeException(e);
103: }
104: }
105: }
106:
107: }
108:
109: private static class Holder {
110: Object reference = null;
111: Object[] array = new Object[] { null, new Object(), null };
112:
113: Map map1 = new HashMap();
114: Map map2 = new IdentityHashMap();
115:
116: List list1 = new LinkedList();
117: List list2 = new ArrayList();
118: List list3 = new Vector();
119:
120: Set set1 = new HashSet();
121:
122: private int count = 0;
123:
124: Holder() {
125: mod();
126: }
127:
128: public void check() {
129: Assert.assertNull(reference);
130: Assert.assertNull(array[0]);
131: Assert.assertNotNull(array[1]);
132: Assert.assertNull(array[2]);
133:
134: Assert.assertEquals(1, list1.size());
135: Assert.assertEquals(1, list2.size());
136: Assert.assertEquals(1, list3.size());
137: Assert.assertTrue(list1.contains(null));
138: Assert.assertTrue(list2.contains(null));
139: Assert.assertTrue(list3.contains(null));
140:
141: Assert.assertEquals(2, map1.size());
142: Assert.assertEquals(2, map2.size());
143: Assert.assertTrue(map1.containsKey(null));
144: Assert.assertTrue(map2.containsKey(null));
145: Assert.assertTrue(map1.containsValue(null));
146: Assert.assertTrue(map2.containsValue(null));
147:
148: Assert.assertEquals(1, set1.size());
149: Assert.assertTrue(set1.contains(null));
150: }
151:
152: synchronized void mod() {
153: modSets(new Set[] { set1 });
154: modLists(new List[] { list1, list2, list3 });
155: modMaps(new Map[] { map1, map2 });
156: reference = null;
157: array[0] = null;
158: }
159:
160: void modLists(List[] lists) {
161: for (int i = 0; i < lists.length; i++) {
162: lists[i].add(null);
163: }
164: }
165:
166: void modSets(Set[] sets) {
167: for (int i = 0; i < sets.length; i++) {
168: sets[i].add(null);
169: }
170: }
171:
172: void modMaps(Map[] maps) {
173: for (int i = 0; i < maps.length; i++) {
174: maps[i].put(null, "value for null key");
175: maps[i].put("key" + count + " for null value", null);
176: }
177: count++;
178: }
179:
180: }
181:
182: }
|