001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.object.bytecode.Manager;
008: import com.tc.object.bytecode.ManagerUtil;
009: import com.tc.object.config.ConfigLockLevel;
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.TransparencyClassSpec;
013: import com.tc.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
016:
017: public class IllegalMonitorStateTest extends TransparentTestBase {
018:
019: public void setUp() throws Exception {
020: super .setUp();
021: getTransparentAppConfig().setClientCount(1).setIntensity(1);
022: initializeTestRunner();
023: }
024:
025: protected Class getApplicationClass() {
026: return App.class;
027: }
028:
029: public static class App extends AbstractErrorCatchingTransparentApp {
030:
031: public App(String appId, ApplicationConfig cfg,
032: ListenerProvider listenerProvider) {
033: super (appId, cfg, listenerProvider);
034: }
035:
036: private static Object root1 = new Object();
037: private static Object root2 = new Object();
038:
039: protected void runTest() throws Throwable {
040: testMethods(root1);
041:
042: wrongLockHeld();
043: readOnlyLockHeld();
044: concurrentLockHeld();
045: }
046:
047: private void wrongLockHeld() throws InterruptedException {
048: // At the time of writing this test, there was a different error condition when the calling thread held at least
049: // one lock before performing the bad monitor operation -- thus this code
050: synchronized (root2) {
051: assertTrue(ManagerUtil.isLocked(root2,
052: Manager.LOCK_TYPE_WRITE));
053: testMethods(root1);
054: }
055: }
056:
057: private void readOnlyLockHeld() throws InterruptedException {
058: synchronized (root1) {
059: assertTrue(ManagerUtil.isLocked(root1,
060: Manager.LOCK_TYPE_READ));
061: testMethods(root1);
062: }
063: }
064:
065: private void concurrentLockHeld() throws InterruptedException {
066: synchronized (root1) {
067: assertTrue(ManagerUtil.isLocked(root1,
068: Manager.LOCK_TYPE_CONCURRENT));
069: testMethods(root1);
070: }
071: }
072:
073: private void testMethods(Object o) throws InterruptedException {
074: try {
075: o.wait();
076: } catch (IllegalMonitorStateException ise) {
077: // expected
078: }
079:
080: try {
081: o.wait(500);
082: } catch (IllegalMonitorStateException ise) {
083: // expected
084: }
085:
086: try {
087: o.wait(1000, 42);
088: } catch (IllegalMonitorStateException ise) {
089: // expected
090: }
091:
092: try {
093: o.notify();
094: } catch (IllegalMonitorStateException ise) {
095: // expected
096: }
097:
098: try {
099: o.notifyAll();
100: } catch (IllegalMonitorStateException ise) {
101: // expected
102: }
103: }
104:
105: public static void visitL1DSOConfig(ConfigVisitor visitor,
106: DSOClientConfigHelper config) {
107: String testClass = App.class.getName();
108: TransparencyClassSpec spec = config
109: .getOrCreateSpec(testClass);
110:
111: config.addWriteAutolock("* " + testClass
112: + ".wrongLockHeld()");
113: config.addReadAutolock("* " + testClass
114: + ".readOnlyLockHeld()");
115: config.addAutolock("* " + testClass
116: + ".concurrentLockHeld()",
117: ConfigLockLevel.CONCURRENT);
118:
119: spec.addRoot("root1", "root1");
120: spec.addRoot("root2", "root2");
121: spec.addRoot("objectRoot", "objectRoot");
122: }
123:
124: }
125:
126: }
|