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