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