1 package org.jboss.cache.invocationcontext; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.CacheSPI; 5 import org.jboss.cache.Fqn; 6 import org.jboss.cache.factories.DefaultCacheFactory; 7 8 import javax.transaction.TransactionManager ; 9 import java.util.Map ; 10 11 16 public class TransactionTest extends TestCase 17 { 18 private CacheSPI cache; 19 private TransactionManager tm; 20 21 protected void setUp() 22 { 23 cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml"); 24 tm = cache.getTransactionManager(); 25 } 26 27 protected void tearDown() 28 { 29 if (cache != null) 30 { 31 cache.stop(); 32 cache = null; 33 } 34 } 35 36 public void testTxExistenceAfterWrite() throws Exception 37 { 38 tm.begin(); 40 41 assertNull("Tx should not have been set up yet", cache.getInvocationContext().getTransaction()); 42 assertNull("Gtx should not have been set up yet", cache.getInvocationContext().getGlobalTransaction()); 43 44 cache.getRoot().put("k", "v"); 46 Map data = cache.getRoot().getData(); 47 assertEquals("Data map should not empty", 1, data.size()); 48 49 assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction()); 51 assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction()); 52 assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction()); 53 54 tm.commit(); 55 } 56 57 public void testTxExistenceAfterRead() throws Exception 58 { 59 tm.begin(); 61 62 assertNull("Tx should not have been set up yet", cache.getInvocationContext().getTransaction()); 63 assertNull("Gtx should not have been set up yet", cache.getInvocationContext().getGlobalTransaction()); 64 65 Object value = cache.get(Fqn.ROOT, "k"); 67 assertNull("Value should be null", value); 68 69 assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction()); 71 assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction()); 72 assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction()); 73 74 tm.commit(); 75 } 76 77 public void testScrubbingAfterCommit() throws Exception 78 { 79 doScrubbingTest(true); 80 } 81 82 public void testScrubbingAfterRollback() throws Exception 83 { 84 doScrubbingTest(false); 85 } 86 87 private void doScrubbingTest(boolean commit) throws Exception 88 { 89 tm.begin(); 90 cache.getRoot().put("key", "value"); 91 assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction()); 92 assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction()); 93 assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction()); 94 95 if (commit) tm.commit(); 96 else tm.rollback(); 97 98 assertNull("Tx should have been scrubbed", cache.getInvocationContext().getTransaction()); 99 assertNull("Gtx should have been scrubbed", cache.getInvocationContext().getGlobalTransaction()); 100 } 101 } 102 | Popular Tags |