KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > NullLiteralReferencesTest


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest;
6
7 import org.apache.commons.lang.builder.EqualsBuilder;
8 import org.apache.commons.lang.builder.HashCodeBuilder;
9 import org.apache.commons.lang.builder.ToStringBuilder;
10
11 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
12
13 import com.tc.object.config.ConfigVisitor;
14 import com.tc.object.config.DSOClientConfigHelper;
15 import com.tc.object.config.TransparencyClassSpec;
16 import com.tc.object.config.spec.CyclicBarrierSpec;
17 import com.tc.simulator.app.ApplicationConfig;
18 import com.tc.simulator.listener.ListenerProvider;
19 import com.tc.util.Assert;
20 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
21
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 public class NullLiteralReferencesTest extends TransparentTestBase {
26
27   private static final int NODE_COUNT = 3;
28
29   public void setUp() throws Exception JavaDoc {
30     super.setUp();
31     getTransparentAppConfig().setClientCount(NODE_COUNT).setIntensity(1);
32     initializeTestRunner();
33   }
34
35   protected Class JavaDoc getApplicationClass() {
36     return NullLiteralReferencesTestApp.class;
37   }
38
39   public static class NullLiteralReferencesTestApp extends AbstractErrorCatchingTransparentApp {
40
41     private final CyclicBarrier barrier;
42     private final Map JavaDoc root = new LinkedHashMap JavaDoc();
43
44     public NullLiteralReferencesTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
45       super(appId, cfg, listenerProvider);
46       barrier = new CyclicBarrier(getParticipantCount());
47     }
48
49     protected void runTest() throws Throwable JavaDoc {
50       int index = barrier.barrier();
51
52       if (index == 0) {
53         Holder h1 = new Holder(true);
54         Holder h2 = new Holder(false);
55
56         // these two put()'s need to be done in separate TXNs to ensure that
57
// the state object on the server be deterministic. More over, the
58
// version that has non-null reference literals (h2) must be in the
59
// first transaction
60
synchronized (root) {
61           root.put("null", h2);
62         }
63
64         synchronized (root) {
65           root.put("not-null", h1);
66         }
67
68         h1.setNonNull();
69         h2.setNull();
70       }
71
72       barrier.barrier();
73
74       Holder compareNull = new Holder(true);
75       Holder compareNotNull = new Holder(false);
76
77       synchronized (root) {
78         Assert.assertEquals(compareNull, root.get("null"));
79         Assert.assertEquals(compareNotNull, root.get("not-null"));
80       }
81     }
82
83     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
84       new CyclicBarrierSpec().visit(visitor, config);
85
86       String JavaDoc testClass;
87       TransparencyClassSpec spec;
88       String JavaDoc methodExpression;
89
90       testClass = Holder.class.getName();
91       spec = config.getOrCreateSpec(testClass);
92       methodExpression = "* " + testClass + ".*(..)";
93       config.addWriteAutolock(methodExpression);
94
95       testClass = NullLiteralReferencesTestApp.class.getName();
96       spec = config.getOrCreateSpec(testClass);
97       methodExpression = "* " + testClass + ".*(..)";
98       config.addWriteAutolock(methodExpression);
99       spec.addRoot("barrier", "barrier");
100       spec.addRoot("root", "root");
101     }
102   }
103
104   private static class Holder {
105     Holder(boolean setNull) {
106       if (setNull) {
107         setNull();
108       } else {
109         setNonNull();
110       }
111     }
112
113     Byte JavaDoc byteRef;
114     Boolean JavaDoc booleanRef;
115     Character JavaDoc characterRef;
116     Double JavaDoc doubleRef;
117     Float JavaDoc floatRef;
118     Integer JavaDoc integerRef;
119     Long JavaDoc longRef;
120     Short JavaDoc shortRef;
121
122     Class JavaDoc clazz;
123     StackTraceElement JavaDoc stack;
124     String JavaDoc str;
125
126     byte b = 1;
127     boolean z = true;
128     char c = 'e';
129     double d = Math.PI;
130     float f = "floater".length();
131     int i = -9;
132     long l = 2342342344324234L;
133     short s = 3;
134
135     synchronized void setNonNull() {
136       byteRef = new Byte JavaDoc((byte) 1);
137       booleanRef = new Boolean JavaDoc(true);
138       characterRef = new Character JavaDoc('q');
139       doubleRef = new Double JavaDoc(3.14);
140       floatRef = new Float JavaDoc(2.78);
141       integerRef = new Integer JavaDoc(42);
142       longRef = new Long JavaDoc(666);
143       shortRef = new Short JavaDoc((short) "steve".length());
144
145       clazz = getClass();
146       stack = new Throwable JavaDoc().getStackTrace()[0];
147       str = "timmy";
148     }
149
150     synchronized void setNull() {
151       byteRef = null;
152       booleanRef = null;
153       characterRef = null;
154       doubleRef = null;
155       floatRef = null;
156       integerRef = null;
157       longRef = null;
158       shortRef = null;
159
160       clazz = null;
161       stack = null;
162       str = null;
163     }
164
165     // this method sync'd to have a common shared memory barrier with the mutate methods
166
public synchronized boolean equals(Object JavaDoc o) {
167       return EqualsBuilder.reflectionEquals(this, o);
168     }
169
170     public synchronized String JavaDoc toString() {
171       return ToStringBuilder.reflectionToString(this);
172     }
173
174     public synchronized int hashCode() {
175       return HashCodeBuilder.reflectionHashCode(this);
176     }
177
178   }
179
180 }
181
Popular Tags