01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest;
05:
06: import com.tc.object.config.ConfigVisitor;
07: import com.tc.object.config.DSOClientConfigHelper;
08: import com.tc.object.config.TransparencyClassSpec;
09: 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;
14: import java.util.Map;
15:
16: public class SynchronizedTestApp extends AbstractTransparentApp {
17: private final MapContainer mapContainer = new MapContainer();
18: private Object lock = new Object();
19:
20: public SynchronizedTestApp(String appId, ApplicationConfig cfg,
21: ListenerProvider listenerProvider) {
22: super (appId, cfg, listenerProvider);
23: }
24:
25: private static class MapContainer {
26: private Map map = new HashMap();
27:
28: public synchronized void put(Object key, Object value) {
29: putItem(key, value);
30: }
31:
32: private void putItem(Object key, Object value) {
33: map.put(key, value);
34: }
35:
36: public synchronized Object get(Object key) {
37: return getItem(key);
38: }
39:
40: private Object getItem(Object key) {
41: return map.get(key);
42: }
43:
44: public void unsafePut(Object key, Object value) {
45: map.put(key, value);
46: }
47: }
48:
49: public void run() {
50: testUnsynchronizedMethod();
51: testLocalSynchronized();
52: }
53:
54: private void testUnsynchronizedMethod() {
55: Object key = new Object();
56: Object value = new Object();
57:
58: mapContainer.put(key, value);
59: if (mapContainer.get(key) != value) {
60: notifyError("Wrong Value!");
61: }
62: }
63:
64: private void testLocalSynchronized() {
65: // test to see if we can get a local lock inside a tc lock and make sure
66: // it doesn't get confused
67: synchronized (mapContainer) {
68: synchronized (lock) {
69: mapContainer.unsafePut(new Object(), new Object());
70: }
71: }
72: }
73:
74: public static void visitL1DSOConfig(ConfigVisitor visitor,
75: DSOClientConfigHelper config) {
76: String testClass = SynchronizedTestApp.class.getName();
77: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
78:
79: config.addIncludePattern(testClass + "$*");
80:
81: String methodExpression = "* " + testClass + "*.*(..)";
82: config.addWriteAutolock(methodExpression);
83:
84: spec.addRoot("mapContainer", "mapContainer");
85: }
86: }
|