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.simulator.app.ApplicationConfig;
009: import com.tc.simulator.listener.ListenerProvider;
010: import com.tc.util.Assert;
011: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
012:
013: import java.util.concurrent.atomic.AtomicInteger;
014:
015: /**
016: * Test that makes sure "shadowed" roots work correctly with DSO
017: * @author hhuynh
018: */
019: public class ShadowRootTestApp extends
020: AbstractErrorCatchingTransparentApp {
021:
022: private BaseClass base = new BaseClass();
023: private DerivedClass derived = new DerivedClass();
024:
025: public ShadowRootTestApp(String appId, ApplicationConfig cfg,
026: ListenerProvider listenerProvider) {
027: super (appId, cfg, listenerProvider);
028: }
029:
030: public static void visitL1DSOConfig(ConfigVisitor visitor,
031: DSOClientConfigHelper config) {
032: String testClass = ShadowRootTestApp.class.getName();
033: config.addIncludePattern(testClass);
034: config.addIncludePattern(BaseClass.class.getName());
035: config.addIncludePattern(DerivedClass.class.getName());
036:
037: String methodExpression = "* " + testClass + "*.*(..)";
038: config.addWriteAutolock(methodExpression);
039:
040: String baseClass = BaseClass.class.getName();
041: config.addRoot(baseClass + ".sum1", baseClass + ".sum1");
042: config.addRoot(baseClass + ".sum2", baseClass + ".sum2");
043: config.addRoot(baseClass + ".sum3", baseClass + ".sum3");
044:
045: String derivedClass = DerivedClass.class.getName();
046: config.addRoot(derivedClass + ".sum1", derivedClass + ".sum1");
047: config.addRoot(derivedClass + ".sum2", derivedClass + ".sum2");
048: config.addRoot(derivedClass + ".sum3", derivedClass + ".sum3");
049:
050: }
051:
052: protected void runTest() throws Throwable {
053: Assert.assertEquals(-1, base.get1());
054: Assert.assertEquals(-1, base.get2());
055: Assert.assertEquals(-1, base.get3());
056:
057: Assert.assertEquals(10, derived.get1());
058: Assert.assertEquals(10, derived.get2());
059: Assert.assertEquals(10, derived.get3());
060:
061: base.set1(0);
062: base.set2(0);
063: base.set3(0);
064:
065: derived.set1(10);
066: derived.set2(10);
067: derived.set3(10);
068:
069: Assert.assertEquals(1, base.addAndGet1(1));
070: Assert.assertEquals(1, base.addAndGet2(1));
071: Assert.assertEquals(1, base.addAndGet3(1));
072:
073: Assert.assertEquals(11, derived.addAndGet1(1));
074: Assert.assertEquals(11, derived.addAndGet2(1));
075: Assert.assertEquals(11, derived.addAndGet3(1));
076: }
077:
078: private static class BaseClass {
079: protected AtomicInteger sum1 = new AtomicInteger(-1);
080: public AtomicInteger sum2 = new AtomicInteger(-1);
081: protected int sum3 = -1;
082:
083: public int addAndGet1(int delta) {
084: return sum1.addAndGet(delta);
085: }
086:
087: public int addAndGet2(int delta) {
088: return sum2.addAndGet(delta);
089: }
090:
091: public int addAndGet3(int delta) {
092: sum3 += delta;
093: return sum3;
094: }
095:
096: public int get1() {
097: return sum1.get();
098: }
099:
100: public int get2() {
101: return sum2.get();
102: }
103:
104: public int get3() {
105: return sum3;
106: }
107:
108: public void set1(int value) {
109: sum1.set(value);
110: }
111:
112: public void set2(int value) {
113: sum2.set(value);
114: }
115:
116: public void set3(int value) {
117: sum3 = value;
118: }
119:
120: }
121:
122: private static class DerivedClass extends BaseClass {
123: public AtomicInteger sum1 = new AtomicInteger(10);
124: protected AtomicInteger sum2 = new AtomicInteger(10);
125: public int sum3 = 10;
126:
127: public int addAndGet1(int delta) {
128: return sum1.addAndGet(delta);
129: }
130:
131: public int addAndGet2(int delta) {
132: return sum2.addAndGet(delta);
133: }
134:
135: public int addAndGet3(int delta) {
136: sum3 += delta;
137: return sum3;
138: }
139:
140: public void set1(int value) {
141: sum1.set(value);
142: }
143:
144: public void set2(int value) {
145: sum2.set(value);
146: }
147:
148: public void set3(int value) {
149: sum3 = value;
150: }
151:
152: public int get1() {
153: return sum1.get();
154: }
155:
156: public int get2() {
157: return sum2.get();
158: }
159:
160: public int get3() {
161: return sum3;
162: }
163: }
164:
165: }
|