001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.transparency;
006:
007: import org.apache.commons.collections.FastHashMap;
008:
009: import EDU.oswego.cs.dl.util.concurrent.BrokenBarrierException;
010: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
011:
012: import com.tc.object.config.ConfigVisitor;
013: import com.tc.object.config.DSOClientConfigHelper;
014: import com.tc.object.config.TransparencyClassSpec;
015: import com.tc.object.config.spec.CyclicBarrierSpec;
016: import com.tc.simulator.app.ApplicationConfig;
017: import com.tc.simulator.listener.ListenerProvider;
018: import com.tc.util.Assert;
019: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
020:
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Vector;
025:
026: public class InstrumentEverythingTestApp extends
027: AbstractErrorCatchingTransparentApp {
028:
029: private static final int INITIAL = 0;
030: private static final int INTERMEDIATE = 1;
031: private static final int END = 2;
032:
033: final List root = new ArrayList();
034: final CyclicBarrier barrier;
035:
036: public InstrumentEverythingTestApp(String appId,
037: ApplicationConfig cfg, ListenerProvider listenerProvider) {
038: super (appId, cfg, listenerProvider);
039: barrier = new CyclicBarrier(getParticipantCount());
040: }
041:
042: public static void visitL1DSOConfig(ConfigVisitor visitor,
043: DSOClientConfigHelper config) {
044: String testClass = InstrumentEverythingTestApp.class.getName();
045: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
046: String methodExpression = "* " + testClass + "*.*(..)";
047: config.addWriteAutolock(methodExpression);
048: spec.addRoot("root", "root");
049: spec.addRoot("barrier", "barrier");
050:
051: CyclicBarrierSpec cbspec = new CyclicBarrierSpec();
052: cbspec.visit(visitor, config);
053:
054: // config.addExcludePattern("*..SubClassA");
055:
056: // Include everything to be instrumented.
057: config.addIncludePattern("*..*", false);
058: }
059:
060: public void runTest() throws BrokenBarrierException,
061: InterruptedException {
062: int n = barrier.barrier();
063: moveToStage(INITIAL);
064: if (n == 0) {
065: synchronized (root) {
066: addALotOfObjects(root);
067: }
068: moveToStage(INTERMEDIATE);
069: } else {
070: List local = new ArrayList();
071: addALotOfObjects(local);
072: moveToStageAndWait(INTERMEDIATE);
073: synchronized (root) {
074: verify(local, root);
075: }
076: }
077: printDetails();
078: moveToStage(END);
079: }
080:
081: private void verify(List expected, List actual) {
082: Assert.assertEquals(expected, actual);
083: }
084:
085: private void addALotOfObjects(List l) {
086: HashMap map = new HashMap();
087: map.put("hello", "saro");
088: map.put(new Integer(10), new Vector());
089: l.add(map);
090: l.add(new ArrayList());
091: l.add(new SubClassA());
092: l.add(new SubClassB());
093: l.add(new SubClassB());
094: l.add(new SubClassC());
095: l.add(new SubClassD());
096: // l.add(new StringBuffer("hello there"));
097: addFastHashMaps(l);
098: addClonedObjects(l);
099: }
100:
101: private void addFastHashMaps(List l) {
102: // This is added to test fasthashmap's clone method. It uses clone() to put() objects in the
103: // map and earlier this used to cause problem. esp. when fast=true !!
104: FastHashMap fslow = new FastHashMap();
105: l.add(fslow);
106: fslow.put("key1", "value1");
107: fslow.put("key2", "value2");
108: fslow.put("key3", "value3");
109: fslow.put("key4", "value4");
110: fslow.put("key5", "value5");
111:
112: FastHashMap freallyslow = new FastHashMap();
113: l.add(freallyslow);
114: freallyslow.setFast(true);
115: freallyslow.put("key1", "value1");
116: freallyslow.put("key2", "value2");
117: freallyslow.put("key3", "value3");
118: freallyslow.put("key4", "value4");
119: freallyslow.put("key5", "value5");
120: }
121:
122: // test to make sure clonedObjects gets shared properly.
123: private void addClonedObjects(List l) {
124: SubClassA a = new SubClassA();
125: synchronizedAdd(l, a);
126: synchronizedAdd(l, a.getCopy());
127: SubClassB b = new SubClassB();
128: b.method1();
129: synchronizedAdd(l, b);
130: synchronizedAdd(l, b.getCopy());
131: SubClassC c = new SubClassC();
132: synchronizedAdd(l, c);
133: synchronizedAdd(l, c);
134: synchronizedAdd(l, c);
135: synchronizedAdd(l, c.getCopy());
136: synchronizedAdd(l, c.getCopy());
137: synchronizedAdd(l, c.clone());
138: synchronizedAdd(l, c.clone());
139: synchronizedAdd(l, c.clone());
140: SubClassD d = new SubClassD();
141: synchronizedAdd(l, d.clone());
142: synchronizedAdd(l, d.clone());
143: synchronizedAdd(l, d);
144: synchronizedAdd(l, d);
145: synchronizedAdd(l, d.clone());
146: synchronizedAdd(l, d.clone());
147: }
148:
149: private void synchronizedAdd(List l, Object o) {
150: synchronized (l) {
151: l.add(o);
152: }
153: }
154:
155: private void printDetails() {
156: synchronized (root) {
157: System.err.println(Thread.currentThread().getName()
158: + ": Root size() = " + root.size());
159: }
160: }
161:
162: }
|