1 |
| package org.jboss.cache.transaction; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestCase; |
5 |
| import junit.framework.TestSuite; |
6 |
| import org.jboss.cache.CacheException; |
7 |
| import org.jboss.cache.CacheImpl; |
8 |
| import org.jboss.cache.DefaultCacheFactory; |
9 |
| import org.jboss.cache.TransactionTable; |
10 |
| |
11 |
| import javax.transaction.NotSupportedException; |
12 |
| import javax.transaction.Synchronization; |
13 |
| import javax.transaction.Transaction; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class PrepareTxTest extends TestCase |
22 |
| { |
23 |
| CacheImpl cache; |
24 |
| |
25 |
2
| protected void setUp() throws Exception
|
26 |
| { |
27 |
2
| super.setUp();
|
28 |
2
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
29 |
2
| cache.getConfiguration().setCacheMode("local");
|
30 |
2
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
31 |
2
| cache.create();
|
32 |
2
| cache.start();
|
33 |
| } |
34 |
| |
35 |
2
| protected void tearDown() throws Exception
|
36 |
| { |
37 |
2
| super.tearDown();
|
38 |
2
| cache.stop();
|
39 |
2
| cache.destroy();
|
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
1
| public void testCacheModificationInBeforeCompletionPhase() throws Exception, NotSupportedException
|
51 |
| { |
52 |
1
| int numLocks = 0;
|
53 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
54 |
1
| mgr.begin();
|
55 |
1
| Transaction tx = mgr.getTransaction();
|
56 |
| |
57 |
| |
58 |
1
| cache.put("/one/two/three", "key1", "val1");
|
59 |
1
| System.out.println("before commit:\n" + cache.printLockInfo());
|
60 |
1
| numLocks = cache.getNumberOfLocksHeld();
|
61 |
1
| assertEquals(4, numLocks);
|
62 |
| |
63 |
| |
64 |
1
| tx.registerSynchronization(new Synchronization()
|
65 |
| { |
66 |
| |
67 |
1
| public void beforeCompletion()
|
68 |
| { |
69 |
1
| try
|
70 |
| { |
71 |
1
| cache.put("/a/b/c", null);
|
72 |
1
| System.out.println("before commit:\n" + cache.printLockInfo());
|
73 |
| } |
74 |
| catch (CacheException e) |
75 |
| { |
76 |
0
| e.printStackTrace();
|
77 |
| } |
78 |
| } |
79 |
| |
80 |
1
| public void afterCompletion(int status)
|
81 |
| { |
82 |
| } |
83 |
| }); |
84 |
| |
85 |
1
| tx.commit();
|
86 |
1
| System.out.println("after commit:\n" + cache.printLockInfo());
|
87 |
1
| numLocks = cache.getNumberOfLocksHeld();
|
88 |
1
| assertEquals(0, numLocks);
|
89 |
| |
90 |
1
| int num_local_txs, num_global_txs;
|
91 |
1
| TransactionTable tx_table = cache.getTransactionTable();
|
92 |
1
| num_local_txs = tx_table.getNumLocalTransactions();
|
93 |
1
| num_global_txs = tx_table.getNumGlobalTransactions();
|
94 |
1
| System.out.println("Number of Transactions: " + num_local_txs + "\nNumber of GlobalTransactions: " +
|
95 |
| num_global_txs + "\nTransactionTable:\n " + tx_table.toString(true)); |
96 |
1
| assertEquals(num_local_txs, num_global_txs);
|
97 |
1
| assertEquals(0, num_local_txs);
|
98 |
| } |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
1
| public void testCacheModificationInAfterCompletionPhase() throws Exception, NotSupportedException
|
109 |
| { |
110 |
1
| int numLocks = 0;
|
111 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
112 |
1
| mgr.begin();
|
113 |
1
| Transaction tx = mgr.getTransaction();
|
114 |
| |
115 |
| |
116 |
1
| cache.put("/one/two/three", "key1", "val1");
|
117 |
1
| System.out.println("before commit:\n" + cache.printLockInfo());
|
118 |
1
| numLocks = cache.getNumberOfLocksHeld();
|
119 |
1
| assertEquals(4, numLocks);
|
120 |
| |
121 |
| |
122 |
1
| tx.registerSynchronization(new Synchronization()
|
123 |
| { |
124 |
| |
125 |
1
| public void beforeCompletion()
|
126 |
| { |
127 |
| } |
128 |
| |
129 |
1
| public void afterCompletion(int status)
|
130 |
| { |
131 |
1
| try
|
132 |
| { |
133 |
1
| cache.put("/a/b/c", null);
|
134 |
1
| System.out.println("before commit:\n" + cache.printLockInfo());
|
135 |
| } |
136 |
| catch (CacheException e) |
137 |
| { |
138 |
0
| e.printStackTrace();
|
139 |
| } |
140 |
| } |
141 |
| }); |
142 |
| |
143 |
1
| tx.commit();
|
144 |
1
| System.out.println("after commit:\n" + cache.printLockInfo());
|
145 |
1
| numLocks = cache.getNumberOfLocksHeld();
|
146 |
1
| assertEquals(0, numLocks);
|
147 |
| |
148 |
1
| int num_local_txs, num_global_txs;
|
149 |
1
| TransactionTable tx_table = cache.getTransactionTable();
|
150 |
1
| num_local_txs = tx_table.getNumLocalTransactions();
|
151 |
1
| num_global_txs = tx_table.getNumGlobalTransactions();
|
152 |
1
| System.out.println("Number of Transactions: " + num_local_txs + "\nNumber of GlobalTransactions: " +
|
153 |
| num_global_txs + "\nTransactionTable:\n " + tx_table.toString(true)); |
154 |
1
| assertEquals(num_local_txs, num_global_txs);
|
155 |
1
| assertEquals(0, num_local_txs);
|
156 |
| } |
157 |
| |
158 |
| |
159 |
1
| public static Test suite()
|
160 |
| { |
161 |
1
| return new TestSuite(PrepareTxTest.class);
|
162 |
| } |
163 |
| |
164 |
0
| public static void main(String[] args)
|
165 |
| { |
166 |
0
| junit.textui.TestRunner.run(suite());
|
167 |
| } |
168 |
| |
169 |
| } |