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