1 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 ; 14 import java.util.Map ; 15 16 public class SynchronizedTestApp extends AbstractTransparentApp 17 { 18 private final MapContainer mapContainer = new MapContainer(); 19 private Object lock = new Object (); 20 21 public SynchronizedTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) 22 { 23 super(appId, cfg, listenerProvider); 24 } 25 26 private static class MapContainer 27 { 28 private Map map = new HashMap (); 29 30 public synchronized void put(Object key, Object value) 31 { 32 putItem(key, value); 33 } 34 35 private void putItem(Object key, Object value) 36 { 37 map.put(key, value); 38 } 39 public synchronized Object get(Object key) 40 { 41 return getItem(key); 42 } 43 44 private Object getItem(Object key) 45 { 46 return map.get(key); 47 } 48 49 public void unsafePut(Object key, Object 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 key = new Object (); 64 Object value = new Object (); 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 synchronized (mapContainer) { 77 synchronized (lock) { 78 mapContainer.unsafePut(new Object (), new Object ()); 79 } 80 } 81 } 82 83 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) 84 { 85 String testClass = SynchronizedTestApp.class.getName(); 86 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 87 88 config.addIncludePattern(testClass + "$*"); 89 90 String methodExpression = "* " + testClass + "*.*(..)"; 91 config.addWriteAutolock(methodExpression); 92 93 spec.addRoot("mapContainer", "mapContainer"); 94 } 95 } 96 | Popular Tags |