001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.tx;
005:
006: import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef;
007:
008: import com.tc.management.beans.tx.MockClientTxMonitor;
009: import com.tc.net.protocol.tcm.TestChannelIDProvider;
010: import com.tc.object.MockTCObject;
011: import com.tc.object.ObjectID;
012: import com.tc.object.TestClientObjectManager;
013: import com.tc.object.lockmanager.api.LockLevel;
014: import com.tc.object.lockmanager.api.TestLockManager;
015: import com.tc.object.lockmanager.impl.ThreadLockManagerImpl;
016: import com.tc.object.logging.NullRuntimeLogger;
017: import com.tc.object.util.ReadOnlyException;
018:
019: import junit.framework.TestCase;
020:
021: public class ClientTransactionManagerTest extends TestCase {
022: ClientTransactionFactory clientTxnFactory;
023: TestRemoteTransactionManager rmtTxnMgr;
024: TestClientObjectManager objMgr;
025: TestLockManager lockMgr;
026: ClientTransactionManagerImpl clientTxnMgr;
027: MyObject pojo;
028: SynchronizedRef error = new SynchronizedRef(null);
029:
030: public void setUp() throws Exception {
031: clientTxnFactory = new ClientTransactionFactoryImpl(
032: new NullRuntimeLogger());
033: rmtTxnMgr = new TestRemoteTransactionManager();
034: objMgr = new TestClientObjectManager();
035: lockMgr = new TestLockManager();
036: clientTxnMgr = new ClientTransactionManagerImpl(
037: new TestChannelIDProvider(), objMgr,
038: new ThreadLockManagerImpl(lockMgr), clientTxnFactory,
039: rmtTxnMgr, new NullRuntimeLogger(),
040: new MockClientTxMonitor());
041:
042: pojo = new MyObject();
043: }
044:
045: public void tearDown() throws Exception {
046: if (error.get() != null) {
047: throw new RuntimeException((Throwable) error.get());
048: }
049: }
050:
051: public void testCheckWriteAccess() {
052: // Test that we get an exception when we have no TXN started
053: try {
054: clientTxnMgr.checkWriteAccess(new Object());
055: fail();
056: } catch (UnlockedSharedObjectException usoe) {
057: // expected
058: }
059:
060: // Test that we get an exception when checking while only holding a read lock
061: clientTxnMgr.begin("lock", LockLevel.READ);
062: try {
063: clientTxnMgr.checkWriteAccess(new Object());
064: fail();
065: } catch (ReadOnlyException roe) {
066: // expected
067: }
068: clientTxnMgr.commit("lock");
069:
070: clientTxnMgr.begin("lock", LockLevel.WRITE);
071: clientTxnMgr.checkWriteAccess(new Object());
072: clientTxnMgr.commit("lock");
073:
074: clientTxnMgr.begin("lock", LockLevel.SYNCHRONOUS_WRITE);
075: clientTxnMgr.checkWriteAccess(new Object());
076: clientTxnMgr.commit("lock");
077:
078: clientTxnMgr.begin("lock", LockLevel.CONCURRENT);
079: clientTxnMgr.checkWriteAccess(new Object());
080: clientTxnMgr.commit("lock");
081: }
082:
083: public void testDoIllegalReadChange() {
084: clientTxnMgr.begin("lock", LockLevel.READ);
085:
086: try {
087: clientTxnMgr.fieldChanged(new MockTCObject(new ObjectID(1),
088: new Object()), null, null, null, -1);
089: assertFalse(true);
090: } catch (ReadOnlyException e) {
091: // expected
092:
093: // System.out.println("THIS IS A GOOD THING");
094: // e.printStackTrace();
095: // System.out.println("THIS IS A GOOD THING");
096: }
097:
098: clientTxnMgr.commit("lock");
099: }
100:
101: private static class MyObject {
102: public int count = 0;
103: }
104: }
|