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 EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.TransparencyClassSpec;
011: import com.tc.object.config.spec.CyclicBarrierSpec;
012: import com.tc.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tc.util.Assert;
015: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: public class OnLoadTestApp extends AbstractErrorCatchingTransparentApp {
021: private final CyclicBarrier barrier;
022:
023: private MyObject root;
024: private MyObject1 root1;
025: private MyObject2 root2;
026: private MyObject3 root3;
027:
028: public OnLoadTestApp(String appId, ApplicationConfig cfg,
029: ListenerProvider listenerProvider) {
030: super (appId, cfg, listenerProvider);
031: this .barrier = new CyclicBarrier(getParticipantCount());
032: }
033:
034: protected void runTest() throws Throwable {
035: // force the outcome of the race to create (as opposed to faulting) the roots
036: boolean creator = (barrier.barrier() == 0);
037:
038: if (creator) {
039: createRoots();
040: }
041:
042: barrier.barrier();
043:
044: synchronized (root) {
045: Assert.eval(root.getSize() == 0);
046: Assert.eval(root.ok);
047:
048: Assert.eval(root1.getSize() == 0);
049:
050: try {
051: root2.getSize();
052: Assert.eval(creator);
053: } catch (NullPointerException npe) {
054: Assert.eval(!creator);
055: // success
056: System.out.println("YEAH! GOT NPE");
057: }
058: }
059:
060: // Test case to test for error in OnLoad script.
061: synchronized (root3) {
062: try {
063: root3.getSize();
064: Assert.eval(creator);
065: } catch (NullPointerException npe) {
066: Assert.eval(!creator);
067: // success
068: System.out.println("YEAH! GOT NPE");
069: }
070: }
071: }
072:
073: private void createRoots() {
074: root = new MyObject(null);
075: root1 = new MyObject1(null);
076: root2 = new MyObject2(null);
077: root3 = new MyObject3();
078: }
079:
080: public static void visitL1DSOConfig(ConfigVisitor visitor,
081: DSOClientConfigHelper config) {
082: new CyclicBarrierSpec().visit(visitor, config);
083:
084: String testClass = OnLoadTestApp.class.getName();
085: TransparencyClassSpec spec = config
086: .getOrCreateSpec(MyObject.class.getName());
087: spec.setHonorTransient(true);
088: // We want to make sure that all objects are resolved.
089: spec
090: .setExecuteScriptOnLoad("if(self.mine == null){self.ok = false;}else{self.ok = true;} self.list = new ArrayList();");
091:
092: spec = config.getOrCreateSpec(MyObject1.class.getName());
093: spec.setHonorTransient(true);
094: spec.setCallMethodOnLoad("initialize");
095:
096: spec = config.getOrCreateSpec(MyObject2.class.getName());
097: spec.setHonorTransient(true);
098:
099: spec = config.getOrCreateSpec(MyObject3.class.getName());
100: spec.setHonorTransient(true);
101: spec.setExecuteScriptOnLoad("<![CDATA[***********;]]>");
102:
103: spec = config.getOrCreateSpec(testClass);
104:
105: String methodExpression = "* " + testClass + "*.*(..)";
106: config.addWriteAutolock(methodExpression);
107: spec.addRoot("root", "root");
108: spec.addRoot("root1", "root1");
109: spec.addRoot("root2", "root2");
110: spec.addRoot("root3", "root3");
111: spec.addRoot("barrier", "barrier");
112: }
113:
114: private static class MyObject {
115: private transient List list = new ArrayList();
116: private MyObject1 mine = new MyObject1(null);
117: public volatile transient boolean ok = true;
118:
119: public MyObject(Object o) {
120: // I don't want a null constructor to exist so I created this.
121: System.out.println(mine);
122: }
123:
124: public int getSize() {
125: return list.size();
126: }
127:
128: }
129:
130: private static class MyObject1 {
131: private transient List list;
132:
133: public MyObject1(Object o) {
134: initialize();
135: }
136:
137: public void initialize() {
138: list = new ArrayList();
139: }
140:
141: public int getSize() {
142: return list.size();
143: }
144: }
145:
146: private static class MyObject2 {
147: private transient List list = new ArrayList();
148:
149: public MyObject2(Object o) {
150: //
151: }
152:
153: public int getSize() {
154: return list.size();
155: }
156: }
157:
158: /**
159: * This class is used for testing onLoad script with parsing error.
160: */
161: private static class MyObject3 {
162: private transient List list = new ArrayList();
163: public volatile transient boolean ok = true;
164:
165: public MyObject3() {
166: super ();
167: }
168:
169: public int getSize() {
170: return list.size();
171: }
172:
173: }
174: }
|