KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tx > ClientTransactionManagerTest


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.tc.object.tx;
5
6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef;
7
8 import com.tc.management.beans.tx.MockClientTxMonitor;
9 import com.tc.net.protocol.tcm.TestChannelIDProvider;
10 import com.tc.object.MockTCObject;
11 import com.tc.object.ObjectID;
12 import com.tc.object.TestClientObjectManager;
13 import com.tc.object.lockmanager.api.LockLevel;
14 import com.tc.object.lockmanager.api.TestLockManager;
15 import com.tc.object.lockmanager.impl.ThreadLockManagerImpl;
16 import com.tc.object.logging.NullRuntimeLogger;
17
18 import junit.framework.TestCase;
19
20 public class ClientTransactionManagerTest extends TestCase {
21   ClientTransactionFactory clientTxnFactory;
22   TestRemoteTransactionManager rmtTxnMgr;
23   TestClientObjectManager objMgr;
24   TestLockManager lockMgr;
25   ClientTransactionManagerImpl clientTxnMgr;
26   MyObject pojo;
27   SynchronizedRef error = new SynchronizedRef(null);
28
29   public void setUp() throws Exception JavaDoc {
30     clientTxnFactory = new ClientTransactionFactoryImpl(new NullRuntimeLogger(), new TestChannelIDProvider());
31     rmtTxnMgr = new TestRemoteTransactionManager();
32     objMgr = new TestClientObjectManager();
33     lockMgr = new TestLockManager();
34     clientTxnMgr = new ClientTransactionManagerImpl(new TestChannelIDProvider(), objMgr,
35                                                     new ThreadLockManagerImpl(lockMgr), clientTxnFactory, rmtTxnMgr,
36                                                     new NullRuntimeLogger(), new MockClientTxMonitor());
37
38     pojo = new MyObject();
39   }
40
41   public void tearDown() throws Exception JavaDoc {
42     if (error.get() != null) { throw new RuntimeException JavaDoc((Throwable JavaDoc) error.get()); }
43   }
44
45   public void testCheckWriteAccess() {
46     // Test that we get an exception when we have no TXN started
47
try {
48       clientTxnMgr.checkWriteAccess(new Object JavaDoc());
49       fail();
50     } catch (UnlockedSharedObjectException usoe) {
51       // expected
52
}
53
54     // Test that we get an exception when checking while only holding a read lock
55
clientTxnMgr.begin("lock", LockLevel.READ);
56     try {
57       clientTxnMgr.checkWriteAccess(new Object JavaDoc());
58       fail();
59     } catch (ReadOnlyException roe) {
60       // expected
61
}
62     clientTxnMgr.commit("lock");
63
64     clientTxnMgr.begin("lock", LockLevel.WRITE);
65     clientTxnMgr.checkWriteAccess(new Object JavaDoc());
66     clientTxnMgr.commit("lock");
67     
68     clientTxnMgr.begin("lock", LockLevel.SYNCHRONOUS_WRITE);
69     clientTxnMgr.checkWriteAccess(new Object JavaDoc());
70     clientTxnMgr.commit("lock");
71
72     clientTxnMgr.begin("lock", LockLevel.CONCURRENT);
73     clientTxnMgr.checkWriteAccess(new Object JavaDoc());
74     clientTxnMgr.commit("lock");
75   }
76
77   public void testDoIllegalReadChange() {
78     clientTxnMgr.begin("lock", LockLevel.READ);
79
80     try {
81       clientTxnMgr.fieldChanged(new MockTCObject(new ObjectID(1), new Object JavaDoc()), null, null, null, -1);
82       assertFalse(true);
83     } catch (ReadOnlyException e) {
84       // expected
85

86       // System.out.println("THIS IS A GOOD THING");
87
// e.printStackTrace();
88
// System.out.println("THIS IS A GOOD THING");
89
}
90
91     clientTxnMgr.commit("lock");
92   }
93
94   private static class MyObject {
95     public int count = 0;
96   }
97 }
98
Popular Tags