KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TransparentTHashMapTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.object.config.TransparencyClassSpec;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tc.util.Assert;
12 import com.tctest.runner.AbstractTransparentApp;
13
14 import gnu.trove.THashMap;
15 import gnu.trove.TObjectObjectProcedure;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 public class TransparentTHashMapTestApp extends AbstractTransparentApp {
23   private THashMap tmaproot = new THashMap();
24   private Set JavaDoc steps = new HashSet JavaDoc();
25
26   public TransparentTHashMapTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
27     super(appId, cfg, listenerProvider);
28
29   }
30
31   public void run() {
32     System.out.println("Running...");
33     synchronized (tmaproot) {
34       switch (steps.size()) {
35         case 0:
36           stage1();
37           break;
38         case 1:
39           stage2();
40           break;
41         case 2:
42           Assert.eval(tmaproot.isEmpty());
43           break;
44         case 3:
45           stage3();
46           break;
47         case 4:
48           stage4();
49           break;
50         case 5:
51           stage5();
52           tmaproot.put("DONE", "DONE");
53           tmaproot.notifyAll();
54           break;
55       }
56
57       steps.add(new Object JavaDoc());
58       System.out.println("Stage: " + steps.size());
59
60       // This bit of wierdness is to make sure this test is actually running all the stages
61
while (!tmaproot.containsKey("DONE")) {
62         try {
63           tmaproot.wait();
64         } catch (InterruptedException JavaDoc e) {
65           notifyError(e);
66         }
67       }
68     }
69   }
70
71   private void stage5() {
72     Assert.eval(tmaproot.get("hello").equals(new TestObject("6")));
73     Assert.eval(tmaproot.keySet().size() == 1);
74   }
75
76   private void stage2() {
77     // System.out.println("Size: " + tmaproot.keySet().size());
78
Assert.eval(tmaproot.keySet().size() == 1);
79     Assert.eval(tmaproot.get(new TestObject("1")).equals(new TestObject("3")));
80     Assert.eval(tmaproot.get(new TestObject("4")) == null);
81     tmaproot.clear();
82   }
83
84   private void stage3() {
85     Map tm = new HashMap();
86     tm.put("hello", new TestObject("6"));
87     tm.put(new TestObject("7"), new TestObject("8"));
88     tmaproot.putAll(tm);
89   }
90
91   private void stage4() {
92     tmaproot.retainEntries(new TObjectObjectProcedure() {
93
94       public boolean execute(Object JavaDoc arg0, Object JavaDoc arg1) {
95         return (arg0.equals("hello"));
96       }
97     });
98   }
99
100   private void stage1() {
101     TestObject to1 = new TestObject("1");
102     TestObject to2 = new TestObject("2");
103     tmaproot.put(to1, to2);
104     tmaproot.put(new TestObject("1"), new TestObject("3"));
105     tmaproot.put(new TestObject("4"), new TestObject("5"));
106     tmaproot.remove(new TestObject("4"));
107   }
108
109   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
110     String JavaDoc testClass = TransparentTHashMapTestApp.class.getName();
111     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
112
113     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
114     config.addWriteAutolock(methodExpression);
115     spec.addRoot("tmaproot", "tmaproot");
116     spec.addRoot("steps", "steps");
117     
118     config.addIncludePattern(TestObject.class.getName());
119   }
120
121   private static class TestObject {
122     private String JavaDoc value;
123
124     public TestObject(String JavaDoc value) {
125       this.value = value;
126     }
127
128     public int hashCode() {
129       return value.hashCode();
130     }
131
132     public boolean equals(Object JavaDoc obj) {
133       if (obj instanceof TestObject) {
134         TestObject to = (TestObject) obj;
135         return this.value.equals(to.value);
136       }
137       return false;
138     }
139   }
140 }
141
Popular Tags