1 7 8 package org.jboss.cache.api; 9 10 import junit.framework.TestCase; 11 import org.jboss.cache.Cache; 12 import org.jboss.cache.CacheSPI; 13 import org.jboss.cache.Fqn; 14 import org.jboss.cache.InvocationContext; 15 import org.jboss.cache.Node; 16 import org.jboss.cache.config.Option; 17 import org.jboss.cache.factories.DefaultCacheFactory; 18 import org.jboss.cache.misc.TestingUtil; 19 import org.jboss.cache.transaction.DummyTransactionManager; 20 21 import javax.transaction.HeuristicMixedException ; 22 import javax.transaction.HeuristicRollbackException ; 23 import javax.transaction.NotSupportedException ; 24 import javax.transaction.RollbackException ; 25 import javax.transaction.SystemException ; 26 import javax.transaction.Transaction ; 27 28 31 public class SyncReplTxTest extends TestCase 32 { 33 private CacheSPI[] caches; 34 35 protected void setUp() 36 { 37 System.out.println("*** In setUp()"); 38 caches = new CacheSPI[2]; 39 caches[0] = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/replSync-service.xml"); 40 caches[1] = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/replSync-service.xml"); 41 42 TestingUtil.blockUntilViewsReceived(caches, 5000); 43 System.out.println("*** Finished setUp()"); 44 } 45 46 protected void tearDown() 47 { 48 System.out.println("*** In tearDown()"); 49 if (caches != null) 50 { 51 for (CacheSPI c : caches) 52 { 53 if (c != null) 54 { 55 Transaction t; 56 57 try 58 { 59 if ((t = c.getTransactionManager().getTransaction()) != null) 60 { 61 try 62 { 63 t.rollback(); 64 } 65 catch (SystemException e) 66 { 67 } 69 } 70 } 71 catch (SystemException e) 72 { 73 } 75 c.stop(); 76 } 77 } 78 caches = null; 79 } 80 System.out.println("*** Finished tearDown()"); 81 } 82 83 Transaction beginTransaction() throws SystemException , NotSupportedException 84 { 85 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 86 mgr.begin(); 87 return mgr.getTransaction(); 88 } 89 90 public void testBasicOperation() throws SystemException , NotSupportedException , HeuristicMixedException , HeuristicRollbackException , RollbackException 91 { 92 assertClusterSize("Should only be 2 caches in the cluster!!!", 2); 93 assertInvocationContextInitState(); 94 95 Fqn f = Fqn.fromString("/test/data"); 96 String k = "key", v = "value"; 97 98 assertNull("Should be null", caches[0].getRoot().getChild(f)); 99 assertNull("Should be null", caches[1].getRoot().getChild(f)); 100 101 Node node = caches[0].getRoot().addChild(f); 102 103 assertNotNull("Should not be null", node); 104 105 Transaction tx = beginTransaction(); 106 node.put(k, v); 107 Transaction tx1 = caches[0].getInvocationContext().getTransaction(); 108 assertNotNull("Transaction can't be null ", tx1); 109 tx.commit(); 110 111 assertEquals(v, node.get(k)); 112 assertEquals(v, caches[0].get(f, k)); 113 assertEquals("Should have replicated", v, caches[1].get(f, k)); 114 } 115 116 private void assertClusterSize(String message, int size) 117 { 118 for (Cache c : caches) 119 { 120 assertClusterSize(message, size, c); 121 } 122 } 123 124 private void assertClusterSize(String message, int size, Cache c) 125 { 126 assertEquals(message, size, c.getMembers().size()); 127 } 128 129 private void assertInvocationContextInitState() 130 { 131 for (Cache c : caches) 132 { 133 assertInvocationContextInitState(c); 134 } 135 } 136 137 private void assertInvocationContextInitState(Cache c) 138 { 139 InvocationContext ctx = c.getInvocationContext(); 140 InvocationContext control = null; 141 try 142 { 143 control = ctx.clone(); 144 } 145 catch (CloneNotSupportedException e) 146 { 147 fail("Unable to clone InvocationCOntext"); 148 } 149 150 control.reset(); 151 control.setOptionOverrides(new Option()); 152 153 assertEquals("Should be equal", control, ctx); 154 } 155 156 157 } 158 | Popular Tags |