1 7 package org.jboss.cache.api; 8 9 import junit.framework.TestCase; 10 import org.jboss.cache.CacheSPI; 11 import org.jboss.cache.Fqn; 12 import org.jboss.cache.Node; 13 import org.jboss.cache.config.Configuration; 14 import org.jboss.cache.factories.DefaultCacheFactory; 15 16 import javax.transaction.TransactionManager ; 17 18 public class NodeReplicatedMoveTest extends TestCase 19 { 20 protected Node rootNode, nodeA, nodeB, nodeC, nodeD, nodeE; 21 protected CacheSPI[] cache; 22 protected TransactionManager tm; 23 protected static final Fqn A = Fqn.fromString("/a"), B = Fqn.fromString("/b"), C = Fqn.fromString("/c"), D = Fqn.fromString("/d"), E = Fqn.fromString("/e"); 24 protected Object k = "key", vA = "valueA", vB = "valueB", vC = "valueC", vD = "valueD", vE = "valueE"; 25 26 protected boolean optimistic = false; 27 28 29 protected void setUp() throws Exception 30 { 31 cache = new CacheSPI[2]; 32 33 cache[0] = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/replSync-service.xml", false); 35 cache[0].getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC); 36 cache[0].start(); 37 rootNode = cache[0].getRoot(); 38 tm = cache[0].getTransactionManager(); 39 40 cache[1] = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/replSync-service.xml", false); 42 cache[1].getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC); 43 cache[1].start(); 44 } 45 46 protected void tearDown() 47 { 48 if (cache != null) 49 { 50 if (cache[0] != null) cache[0].stop(); 51 if (cache[1] != null) cache[1].stop(); 52 cache = null; 53 } 54 if (rootNode != null) rootNode = null; 55 } 56 57 public void testReplicatability() 58 { 59 nodeA = rootNode.addChild(A); 60 nodeB = nodeA.addChild(B); 61 62 nodeA.put(k, vA); 63 nodeB.put(k, vB); 64 65 assertEquals(vA, cache[0].getRoot().getChild(A).get(k)); 66 assertEquals(vB, cache[0].getRoot().getChild(A).getChild(B).get(k)); 67 68 assertEquals(vA, cache[1].getRoot().getChild(A).get(k)); 69 assertEquals(vB, cache[1].getRoot().getChild(A).getChild(B).get(k)); 70 71 cache[0].move(nodeB.getFqn(), Fqn.ROOT); 73 74 assertEquals(vA, cache[0].getRoot().getChild(A).get(k)); 75 assertEquals(vB, cache[0].getRoot().getChild(B).get(k)); 76 77 assertEquals(vA, cache[1].getRoot().getChild(A).get(k)); 78 assertEquals(vB, cache[1].getRoot().getChild(B).get(k)); 79 } 80 81 public void testInvalidations() throws Exception 82 { 83 cache[0].stop(); 84 cache[1].stop(); 85 cache[0].getConfiguration().setCacheMode(Configuration.CacheMode.INVALIDATION_SYNC); 86 cache[1].getConfiguration().setCacheMode(Configuration.CacheMode.INVALIDATION_SYNC); 87 cache[0].start(); 88 cache[1].start(); 89 90 nodeA = rootNode.addChild(A); 91 nodeB = nodeA.addChild(B); 92 93 nodeA.put(k, vA); 94 nodeB.put(k, vB); 95 96 assertEquals(vA, cache[0].getRoot().getChild(A).get(k)); 97 assertEquals(vB, cache[0].getRoot().getChild(A).getChild(B).get(k)); 98 99 assertNull(cache[1].getRoot().getChild(A)); 100 assertNull(cache[1].getRoot().getChild(B)); 101 102 cache[0].move(nodeB.getFqn(), Fqn.ROOT); 104 105 assertEquals(vA, cache[0].getRoot().getChild(A).get(k)); 106 assertEquals(vB, cache[0].getRoot().getChild(B).get(k)); 107 108 assertNull(cache[1].getRoot().getChild(A)); 109 assertNull(cache[1].getRoot().getChild(B)); 110 111 cache[1].getRoot().addChild(A); 113 114 try 115 { 116 cache[0].move(cache[0].getRoot().getChild(B).getFqn(), cache[0].getRoot().getChild(A).getFqn()); fail("Expecting an exception"); 118 } 119 catch (Exception e) 120 { 121 } 123 } 124 125 public void testReplTxCommit() throws Exception 126 { 127 Fqn A_B = new Fqn(A, B); 128 nodeA = rootNode.addChild(A); 129 nodeB = nodeA.addChild(B); 130 131 nodeA.put(k, vA); 132 nodeB.put(k, vB); 133 134 assertEquals(vA, cache[0].getRoot().getChild(A).get(k)); 135 assertEquals(vB, cache[0].getRoot().getChild(A).getChild(B).get(k)); 136 137 assertEquals(vA, cache[1].getRoot().getChild(A).get(k)); 138 assertEquals(vB, cache[1].getRoot().getChild(A).getChild(B).get(k)); 139 140 tm.begin(); 142 cache[0].move(nodeB.getFqn(), Fqn.ROOT); 143 144 assertEquals(vA, cache[0].get(A, k)); 145 assertNull(cache[0].get(A_B, k)); 146 assertEquals(vB, cache[0].get(B, k)); 147 tm.commit(); 148 149 assertEquals(vA, cache[0].getRoot().getChild(A).get(k)); 150 assertEquals(vB, cache[0].getRoot().getChild(B).get(k)); 151 assertEquals(vA, cache[1].getRoot().getChild(A).get(k)); 152 assertEquals(vB, cache[1].getRoot().getChild(B).get(k)); 153 154 } 155 156 public void testReplTxRollback() throws Exception 157 { 158 nodeA = rootNode.addChild(A); 159 nodeB = nodeA.addChild(B); 160 161 nodeA.put(k, vA); 162 nodeB.put(k, vB); 163 164 assertEquals(vA, cache[0].getRoot().getChild(A).get(k)); 165 assertEquals(vB, cache[0].getRoot().getChild(A).getChild(B).get(k)); 166 assertEquals(vA, cache[1].getRoot().getChild(A).get(k)); 167 assertEquals(vB, cache[1].getRoot().getChild(A).getChild(B).get(k)); 168 169 tm.begin(); 171 cache[0].move(nodeB.getFqn(), Fqn.ROOT); 172 173 assertEquals(vA, cache[0].get(A, k)); 174 assertEquals(vB, cache[0].get(B, k)); 175 176 tm.rollback(); 177 178 assertEquals(vA, cache[0].getRoot().getChild(A).get(k)); 179 assertEquals(vB, cache[0].getRoot().getChild(A).getChild(B).get(k)); 180 assertEquals(vA, cache[1].getRoot().getChild(A).get(k)); 181 assertEquals(vB, cache[1].getRoot().getChild(A).getChild(B).get(k)); 182 } 183 184 public void testReplConcurrency() 185 { 186 } 189 190 } 191 | Popular Tags |