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 EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
008:
009: import com.tc.object.bytecode.Manageable;
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.Root;
013: import com.tc.object.config.TransparencyClassSpec;
014: import com.tc.object.config.spec.CyclicBarrierSpec;
015: import com.tc.simulator.app.ApplicationConfig;
016: import com.tc.simulator.listener.ListenerProvider;
017: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
018:
019: public class TransientFieldsTest extends TransparentTestBase {
020:
021: private static final int NODE_COUNT = 3;
022:
023: public void doSetUp(TransparentTestIface t) throws Exception {
024: t.getTransparentAppConfig().setClientCount(NODE_COUNT);
025: t.initializeTestRunner();
026: }
027:
028: protected Class getApplicationClass() {
029: return App.class;
030: }
031:
032: public static class App extends AbstractErrorCatchingTransparentApp {
033:
034: public App(String appId, ApplicationConfig cfg,
035: ListenerProvider listenerProvider) {
036: super (appId, cfg, listenerProvider);
037: barrier = new CyclicBarrier(getParticipantCount());
038: }
039:
040: public static void visitL1DSOConfig(ConfigVisitor visitor,
041: DSOClientConfigHelper config) {
042: TransparencyClassSpec spec = config
043: .getOrCreateSpec(Foo.class.getName());
044: spec.setHonorTransient(true);
045: config.addTransient(Foo.class.getName(),
046: "declaredTransientInt");
047: config.addTransient(Foo.class.getName(),
048: "declaredTransientObject");
049:
050: config.addRoot(
051: new Root(App.class.getName(), "root", "root"),
052: false);
053: config.addRoot(new Root(App.class.getName(), "barrier",
054: "barrier"), false);
055:
056: new CyclicBarrierSpec().visit(visitor, config);
057: }
058:
059: private final Foo root = new Foo();
060: private final CyclicBarrier barrier;
061:
062: protected void runTest() throws Throwable {
063: final int myId = Integer.parseInt(getApplicationId());
064: final Object myObject = getApplicationId();
065:
066: // make sure our local id values don't happen to be java's defaults for uninitialized fields
067: assertNotNull(myObject);
068: assertFalse(myId == 0);
069:
070: // observe the transient fields as uninitialized
071: assertEquals(0, root.getDeclaredTransientInt());
072: assertEquals(0, root.getHonoredTransientInt());
073: assertEquals(null, root.getDeclaredTransientObject());
074: assertEquals(null, root.getHonoredTransientObject());
075:
076: barrier.barrier();
077:
078: // set the transient fields to local values
079: root.setDeclaredTransientInt(myId);
080: root.setHonoredTransientInt(myId);
081: root.setDeclaredTransientObject(myObject);
082: root.setHonoredTransientObject(myObject);
083:
084: barrier.barrier();
085:
086: // observe the the transient fields values are still what we expect them to be
087: assertEquals(myId, root.getDeclaredTransientInt());
088: assertEquals(myId, root.getHonoredTransientInt());
089: assertEquals(myObject, root.getDeclaredTransientObject());
090: assertEquals(myObject, root.getHonoredTransientObject());
091:
092: barrier.barrier();
093:
094: // make sure reference clearing does not disturb transient fields
095: ((Manageable) root).__tc_managed().clearReferences(10000);
096: assertEquals(myObject, root.getDeclaredTransientObject());
097: assertEquals(myObject, root.getHonoredTransientObject());
098: }
099: }
100:
101: public static class Foo {
102: private transient int honoredTransientInt;
103: private transient Object honoredTransientObject;
104:
105: private int declaredTransientInt;
106: private Object declaredTransientObject;
107:
108: public int getHonoredTransientInt() {
109: return honoredTransientInt;
110: }
111:
112: public void setHonoredTransientInt(int honoredTransientInt) {
113: this .honoredTransientInt = honoredTransientInt;
114: }
115:
116: public Object getHonoredTransientObject() {
117: return honoredTransientObject;
118: }
119:
120: public void setHonoredTransientObject(
121: Object honoredTransientObject) {
122: this .honoredTransientObject = honoredTransientObject;
123: }
124:
125: public int getDeclaredTransientInt() {
126: return declaredTransientInt;
127: }
128:
129: public void setDeclaredTransientInt(int declaredTransientInt) {
130: this .declaredTransientInt = declaredTransientInt;
131: }
132:
133: public Object getDeclaredTransientObject() {
134: return declaredTransientObject;
135: }
136:
137: public void setDeclaredTransientObject(
138: Object declaredTransientObject) {
139: this.declaredTransientObject = declaredTransientObject;
140: }
141:
142: }
143:
144: }
|