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