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.object.config.TransparencyClassSpec;
009: import com.tc.simulator.app.ApplicationConfig;
010: import com.tc.simulator.listener.ListenerProvider;
011: import com.tc.util.Assert;
012: import com.tctest.runner.AbstractTransparentApp;
013:
014: import gnu.trove.THashMap;
015: import gnu.trove.TObjectObjectProcedure;
016:
017: import java.util.HashMap;
018: import java.util.HashSet;
019: import java.util.Map;
020: import java.util.Set;
021:
022: public class TransparentTHashMapTestApp extends AbstractTransparentApp {
023: private THashMap tmaproot = new THashMap();
024: private Set steps = new HashSet();
025:
026: public TransparentTHashMapTestApp(String appId,
027: ApplicationConfig cfg, ListenerProvider listenerProvider) {
028: super (appId, cfg, listenerProvider);
029:
030: }
031:
032: public void run() {
033: System.out.println("Running...");
034: synchronized (tmaproot) {
035: switch (steps.size()) {
036: case 0:
037: stage1();
038: break;
039: case 1:
040: stage2();
041: break;
042: case 2:
043: Assert.eval(tmaproot.isEmpty());
044: break;
045: case 3:
046: stage3();
047: break;
048: case 4:
049: stage4();
050: break;
051: case 5:
052: stage5();
053: tmaproot.put("DONE", "DONE");
054: tmaproot.notifyAll();
055: break;
056: }
057:
058: steps.add(new Object());
059: System.out.println("Stage: " + steps.size());
060:
061: // This bit of wierdness is to make sure this test is actually running all the stages
062: while (!tmaproot.containsKey("DONE")) {
063: try {
064: tmaproot.wait();
065: } catch (InterruptedException e) {
066: notifyError(e);
067: }
068: }
069: }
070: }
071:
072: private void stage5() {
073: Assert.eval(tmaproot.get("hello").equals(new TestObject("6")));
074: Assert.eval(tmaproot.keySet().size() == 1);
075: }
076:
077: private void stage2() {
078: // System.out.println("Size: " + tmaproot.keySet().size());
079: Assert.eval(tmaproot.keySet().size() == 1);
080: Assert.eval(tmaproot.get(new TestObject("1")).equals(
081: new TestObject("3")));
082: Assert.eval(tmaproot.get(new TestObject("4")) == null);
083: tmaproot.clear();
084: }
085:
086: private void stage3() {
087: Map tm = new HashMap();
088: tm.put("hello", new TestObject("6"));
089: tm.put(new TestObject("7"), new TestObject("8"));
090: tmaproot.putAll(tm);
091: }
092:
093: private void stage4() {
094: tmaproot.retainEntries(new TObjectObjectProcedure() {
095:
096: public boolean execute(Object arg0, Object arg1) {
097: return (arg0.equals("hello"));
098: }
099: });
100: }
101:
102: private void stage1() {
103: TestObject to1 = new TestObject("1");
104: TestObject to2 = new TestObject("2");
105: tmaproot.put(to1, to2);
106: tmaproot.put(new TestObject("1"), new TestObject("3"));
107: tmaproot.put(new TestObject("4"), new TestObject("5"));
108: tmaproot.remove(new TestObject("4"));
109: }
110:
111: public static void visitL1DSOConfig(ConfigVisitor visitor,
112: DSOClientConfigHelper config) {
113: String testClass = TransparentTHashMapTestApp.class.getName();
114: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
115:
116: String methodExpression = "* " + testClass + "*.*(..)";
117: config.addWriteAutolock(methodExpression);
118: spec.addRoot("tmaproot", "tmaproot");
119: spec.addRoot("steps", "steps");
120:
121: config.addIncludePattern(TestObject.class.getName());
122: }
123:
124: private static class TestObject {
125: private String value;
126:
127: public TestObject(String value) {
128: this .value = value;
129: }
130:
131: public int hashCode() {
132: return value.hashCode();
133: }
134:
135: public boolean equals(Object obj) {
136: if (obj instanceof TestObject) {
137: TestObject to = (TestObject) obj;
138: return this .value.equals(to.value);
139: }
140: return false;
141: }
142: }
143: }
|