KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > SynchronizedTestApp


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.tctest.runner.AbstractTransparentApp;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 public class SynchronizedTestApp extends AbstractTransparentApp
17 {
18   private final MapContainer mapContainer = new MapContainer();
19   private Object JavaDoc lock = new Object JavaDoc();
20   
21   public SynchronizedTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider)
22   {
23     super(appId, cfg, listenerProvider);
24   }
25   
26   private static class MapContainer
27   {
28     private Map JavaDoc map = new HashMap JavaDoc();
29     
30     public synchronized void put(Object JavaDoc key, Object JavaDoc value)
31     {
32       putItem(key, value);
33     }
34     
35     private void putItem(Object JavaDoc key, Object JavaDoc value)
36     {
37       map.put(key, value);
38     }
39     public synchronized Object JavaDoc get(Object JavaDoc key)
40     {
41       return getItem(key);
42     }
43     
44     private Object JavaDoc getItem(Object JavaDoc key)
45     {
46       return map.get(key);
47     }
48     
49     public void unsafePut(Object JavaDoc key, Object JavaDoc value)
50     {
51       map.put(key, value);
52     }
53   }
54   
55   public void run()
56   {
57     testUnsynchronizedMethod();
58     testLocalSynchronized();
59   }
60   
61   private void testUnsynchronizedMethod()
62   {
63     Object JavaDoc key = new Object JavaDoc();
64     Object JavaDoc value = new Object JavaDoc();
65     
66     mapContainer.put(key, value);
67     if (mapContainer.get(key) != value) {
68       notifyError("Wrong Value!");
69     }
70   }
71   
72   private void testLocalSynchronized()
73   {
74     // test to see if we can get a local lock inside a tc lock and make sure
75
// it doesn't get confused
76
synchronized (mapContainer) {
77       synchronized (lock) {
78         mapContainer.unsafePut(new Object JavaDoc(), new Object JavaDoc());
79       }
80     }
81   }
82   
83   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config)
84   {
85     String JavaDoc testClass = SynchronizedTestApp.class.getName();
86     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
87
88     config.addIncludePattern(testClass + "$*");
89    
90     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
91     config.addWriteAutolock(methodExpression);
92
93     spec.addRoot("mapContainer", "mapContainer");
94   }
95 }
96
Popular Tags