1 package org.jboss.cache.api; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.CacheImpl; 5 import org.jboss.cache.Fqn; 6 import org.jboss.cache.Node; 7 import org.jboss.cache.config.Configuration; 8 import org.jboss.cache.factories.DefaultCacheFactory; 9 10 import javax.transaction.TransactionManager ; 11 import java.util.Map ; 12 13 19 public class NodeAPITest extends TestCase 20 { 21 private Node rootNode; 22 23 private CacheImpl cache; 24 25 private TransactionManager tm; 26 27 private static final Fqn A = Fqn.fromString("/a"), B = Fqn.fromString("/b"), C = Fqn.fromString("/c"), D = Fqn 28 .fromString("/d"); 29 30 protected boolean optimistic = false; 31 32 protected void setUp() throws Exception 33 { 34 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml", false); 36 cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC); 37 cache.start(); 38 rootNode = cache.getRoot(); 39 tm = cache.getTransactionManager(); 40 } 41 42 protected void tearDown() 43 { 44 if (cache != null) 45 { 46 cache.stop(); 47 } 48 if (rootNode != null) 49 { 50 rootNode = null; 51 } 52 } 53 54 public void testAddingData() 55 { 56 Node nodeA = rootNode.addChild(A); 57 nodeA.put("key", "value"); 58 59 assertEquals("value", nodeA.get("key")); 60 } 61 62 public void testAddingDataTx() throws Exception 63 { 64 tm.begin(); 65 Node nodeA = rootNode.addChild(A); 66 nodeA.put("key", "value"); 67 68 assertEquals("value", nodeA.get("key")); 69 tm.commit(); 70 } 71 72 73 76 public void testParentsAndChildren() 77 { 78 Node nodeA = rootNode.addChild(A); 79 Node nodeB = nodeA.addChild(B); 80 Node nodeC = nodeA.addChild(C); 81 Node nodeD = rootNode.addChild(D); 82 83 assertEquals(rootNode, nodeA.getParent()); 84 assertEquals(nodeA, nodeB.getParent()); 85 assertEquals(nodeA, nodeC.getParent()); 86 assertEquals(rootNode, nodeD.getParent()); 87 88 assertTrue(rootNode.hasChild(A)); 89 assertFalse(rootNode.hasChild(B)); 90 assertFalse(rootNode.hasChild(C)); 91 assertTrue(rootNode.hasChild(D)); 92 93 assertTrue(nodeA.hasChild(B)); 94 assertTrue(nodeA.hasChild(C)); 95 96 assertEquals(nodeA, rootNode.getChild(A)); 97 assertEquals(nodeD, rootNode.getChild(D)); 98 assertEquals(nodeB, nodeA.getChild(B)); 99 assertEquals(nodeC, nodeA.getChild(C)); 100 101 assertTrue(nodeA.getChildren().contains(nodeB)); 102 assertTrue(nodeA.getChildren().contains(nodeC)); 103 assertEquals(2, nodeA.getChildren().size()); 104 105 assertTrue(rootNode.getChildren().contains(nodeA)); 106 assertTrue(rootNode.getChildren().contains(nodeD)); 107 assertEquals(2, rootNode.getChildren().size()); 108 109 rootNode.removeChild(A); 110 assertFalse(rootNode.getChildren().contains(nodeA)); 111 assertTrue(rootNode.getChildren().contains(nodeD)); 112 assertEquals(1, rootNode.getChildren().size()); 113 } 114 115 public void testLocking() throws Exception 116 { 117 tm.begin(); 118 Node nodeA = rootNode.addChild(A); 119 Node nodeB = nodeA.addChild(B); 120 Node nodeC = nodeB.addChild(C); 121 122 if (!optimistic) 123 { 124 assertEquals(3, cache.getNumberOfNodes()); 125 assertEquals(4, cache.getNumberOfLocksHeld()); 126 } 127 tm.commit(); 128 129 tm.begin(); 130 assertEquals(0, cache.getNumberOfLocksHeld()); 131 nodeC.put("key", "value"); 132 if (!optimistic) assertEquals(4, cache.getNumberOfLocksHeld()); 133 tm.commit(); 134 } 135 136 public void testImmutabilityOfData() 137 { 138 rootNode.put("key", "value"); 139 Map m = rootNode.getData(); 140 try 141 { 142 m.put("x", "y"); 143 fail("Map should be immutable!!"); 144 } 145 catch (Exception e) 146 { 147 } 149 150 try 151 { 152 rootNode.getKeys().add(new Object ()); 153 fail("Key set should be immutable"); 154 } 155 catch (Exception e) 156 { 157 } 159 } 160 161 public void testImmutabilityOfChildren() 162 { 163 rootNode.addChild(A); 164 165 try 166 { 167 rootNode.getChildren().clear(); 168 fail("Collection of child nodes returned in getChildrenDirect() should be immutable"); 169 } 170 catch (Exception e) 171 { 172 } 174 } 175 176 public void testGetChildrenUnderTx() throws Exception 177 { 178 tm.begin(); 179 cache.put(new Fqn(A, B), "1", "1"); 180 cache.put(new Fqn(A, C), "2", "2"); 181 182 if (!optimistic) 183 { 184 assertEquals(3, cache.getNumberOfNodes()); 185 assertEquals(4, cache.getNumberOfLocksHeld()); 186 } 187 assertEquals("Number of child", 2, cache.getRoot().getChild(A).getChildren().size()); 188 tm.commit(); 189 } 190 191 public void testGetChildAPI() 192 { 193 rootNode.addChild(A).addChild(B).addChild(C); 195 196 rootNode.getChild(A).put("key", "value"); 197 rootNode.getChild(A).getChild(B).put("key", "value"); 198 rootNode.getChild(A).getChild(B).getChild(C).put("key", "value"); 199 200 assertEquals("value", rootNode.getChild(A).get("key")); 201 assertEquals("value", rootNode.getChild(A).getChild(B).get("key")); 202 assertEquals("value", rootNode.getChild(A).getChild(B).getChild(C).get("key")); 203 204 assertNull(rootNode.getChild(new Fqn("nonexistent"))); 205 } 206 207 public void testClearingData() 208 { 209 rootNode.put("k", "v"); 210 rootNode.put("k2", "v2"); 211 assertEquals(2, rootNode.getKeys().size()); 212 rootNode.clearData(); 213 assertEquals(0, rootNode.getKeys().size()); 214 assertTrue(rootNode.getData().isEmpty()); 215 } 216 217 public void testClearingDataTx() throws Exception 218 { 219 tm.begin(); 220 rootNode.put("k", "v"); 221 rootNode.put("k2", "v2"); 222 assertEquals(2, rootNode.getKeys().size()); 223 rootNode.clearData(); 224 assertEquals(0, rootNode.getKeys().size()); 225 assertTrue(rootNode.getData().isEmpty()); 226 tm.commit(); 227 assertTrue(rootNode.getData().isEmpty()); 228 } 229 230 } 231 | Popular Tags |