001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import org.apache.commons.lang.builder.EqualsBuilder;
008: import org.apache.commons.lang.builder.HashCodeBuilder;
009: import org.apache.commons.lang.builder.ToStringBuilder;
010:
011: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
012:
013: import com.tc.object.config.ConfigVisitor;
014: import com.tc.object.config.DSOClientConfigHelper;
015: import com.tc.object.config.TransparencyClassSpec;
016: import com.tc.object.config.spec.CyclicBarrierSpec;
017: import com.tc.simulator.app.ApplicationConfig;
018: import com.tc.simulator.listener.ListenerProvider;
019: import com.tc.util.Assert;
020: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
021:
022: import java.util.LinkedHashMap;
023: import java.util.Map;
024:
025: public class NullLiteralReferencesTest extends TransparentTestBase {
026:
027: private static final int NODE_COUNT = 3;
028:
029: public void setUp() throws Exception {
030: super .setUp();
031: getTransparentAppConfig().setClientCount(NODE_COUNT)
032: .setIntensity(1);
033: initializeTestRunner();
034: }
035:
036: protected Class getApplicationClass() {
037: return NullLiteralReferencesTestApp.class;
038: }
039:
040: public static class NullLiteralReferencesTestApp extends
041: AbstractErrorCatchingTransparentApp {
042:
043: private final CyclicBarrier barrier;
044: private final Map root = new LinkedHashMap();
045:
046: public NullLiteralReferencesTestApp(String appId,
047: ApplicationConfig cfg, ListenerProvider listenerProvider) {
048: super (appId, cfg, listenerProvider);
049: barrier = new CyclicBarrier(getParticipantCount());
050: }
051:
052: protected void runTest() throws Throwable {
053: int index = barrier.barrier();
054:
055: if (index == 0) {
056: Holder h1 = new Holder(true);
057: Holder h2 = new Holder(false);
058:
059: // these two put()'s need to be done in separate TXNs to ensure that
060: // the state object on the server be deterministic. More over, the
061: // version that has non-null reference literals (h2) must be in the
062: // first transaction
063: synchronized (root) {
064: root.put("null", h2);
065: }
066:
067: synchronized (root) {
068: root.put("not-null", h1);
069: }
070:
071: h1.setNonNull();
072: h2.setNull();
073: }
074:
075: barrier.barrier();
076:
077: Holder compareNull = new Holder(true);
078: Holder compareNotNull = new Holder(false);
079:
080: synchronized (root) {
081: Assert.assertEquals(compareNull, root.get("null"));
082: Assert.assertEquals(compareNotNull, root
083: .get("not-null"));
084: }
085: }
086:
087: public static void visitL1DSOConfig(ConfigVisitor visitor,
088: DSOClientConfigHelper config) {
089: new CyclicBarrierSpec().visit(visitor, config);
090:
091: String testClass;
092: TransparencyClassSpec spec;
093: String methodExpression;
094:
095: testClass = Holder.class.getName();
096: spec = config.getOrCreateSpec(testClass);
097: methodExpression = "* " + testClass + ".*(..)";
098: config.addWriteAutolock(methodExpression);
099:
100: testClass = NullLiteralReferencesTestApp.class.getName();
101: spec = config.getOrCreateSpec(testClass);
102: methodExpression = "* " + testClass + ".*(..)";
103: config.addWriteAutolock(methodExpression);
104: spec.addRoot("barrier", "barrier");
105: spec.addRoot("root", "root");
106: }
107: }
108:
109: private static class Holder {
110: Holder(boolean setNull) {
111: if (setNull) {
112: setNull();
113: } else {
114: setNonNull();
115: }
116: }
117:
118: Byte byteRef;
119: Boolean booleanRef;
120: Character characterRef;
121: Double doubleRef;
122: Float floatRef;
123: Integer integerRef;
124: Long longRef;
125: Short shortRef;
126:
127: Class clazz;
128: StackTraceElement stack;
129: String str;
130:
131: byte b = 1;
132: boolean z = true;
133: char c = 'e';
134: double d = Math.PI;
135: float f = "floater".length();
136: int i = -9;
137: long l = 2342342344324234L;
138: short s = 3;
139:
140: synchronized void setNonNull() {
141: byteRef = new Byte((byte) 1);
142: booleanRef = new Boolean(true);
143: characterRef = new Character('q');
144: doubleRef = new Double(3.14);
145: floatRef = new Float(2.78);
146: integerRef = new Integer(42);
147: longRef = new Long(666);
148: shortRef = new Short((short) "steve".length());
149:
150: clazz = getClass();
151: stack = new Throwable().getStackTrace()[0];
152: str = "timmy";
153: }
154:
155: synchronized void setNull() {
156: byteRef = null;
157: booleanRef = null;
158: characterRef = null;
159: doubleRef = null;
160: floatRef = null;
161: integerRef = null;
162: longRef = null;
163: shortRef = null;
164:
165: clazz = null;
166: stack = null;
167: str = null;
168: }
169:
170: // this method sync'd to have a common shared memory barrier with the mutate methods
171: public synchronized boolean equals(Object o) {
172: return EqualsBuilder.reflectionEquals(this , o);
173: }
174:
175: public synchronized String toString() {
176: return ToStringBuilder.reflectionToString(this );
177: }
178:
179: public synchronized int hashCode() {
180: return HashCodeBuilder.reflectionHashCode(this);
181: }
182:
183: }
184:
185: }
|