1 package org.jboss.cache.api; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.CacheSPI; 5 import org.jboss.cache.Fqn; 6 import org.jboss.cache.NodeSPI; 7 import org.jboss.cache.factories.DefaultCacheFactory; 8 9 import java.util.Map ; 10 import java.util.Set ; 11 12 15 public class NodeSPITest extends TestCase 16 { 17 private CacheSPI cache; 18 private NodeSPI root; 19 20 protected void setUp() 21 { 22 cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(); 23 root = cache.getRoot(); 24 } 25 26 protected void tearDown() 27 { 28 if (cache != null) cache.stop(); 29 root = null; 30 cache = null; 31 } 32 33 public void testDeepOperations() throws Exception 34 { 35 Fqn A = Fqn.fromString("/a"); 36 Fqn B = Fqn.fromString("/b"); 37 Fqn A_B = Fqn.fromString("/a/b"); 38 39 NodeSPI nodeA, nodeB; 40 41 cache.put(A, "k", "v"); 42 cache.put(A_B, "k", "v"); 43 44 nodeA = cache.getRoot().getChildDirect(A); nodeB = cache.getRoot().getChildDirect(A_B); 47 assertEquals(A_B, nodeB.getFqn()); 48 49 nodeB = nodeA.getChildDirect(B); assertEquals(A_B, nodeB.getFqn()); 51 cache.getRoot().removeChildDirect(A_B); 53 nodeA.removeChildDirect(B); cache.getRoot().removeChildDirect(A); 56 try 57 { 58 cache.getRoot().addChildDirect(A_B); fail("Should have failed"); 60 } 61 catch (UnsupportedOperationException e) 62 { 63 } 65 nodeA = cache.getRoot().addChildDirect(A); nodeA.addChildDirect(B); } 68 69 public void testDataImmutabilityAndDefensiveCopy() 70 { 71 root.put("k", "v"); 73 Map dataDirect = root.getDataDirect(); 74 Set keysDirect = root.getKeysDirect(); 75 76 try 77 { 78 dataDirect.remove("k"); 79 fail("getDataDirect() should return an unmodifiable collection object"); 80 } 81 catch (UnsupportedOperationException uoe) 82 { 83 } 85 86 try 87 { 88 keysDirect.clear(); 89 fail("getKeysDirect() should return an unmodifiable collection object"); 90 } 91 catch (UnsupportedOperationException uoe) 92 { 93 } 95 96 root.put("k2", "v2"); 98 99 assertTrue("root.put() should have succeeded", root.getDataDirect().containsKey("k2")); 100 assertTrue("getDataDirect() should have made a defensive copy of the data collection object", !dataDirect.containsKey("k2")); 101 assertTrue("getKeysDirect() should have made a defensive copy of the data collection object", !keysDirect.contains("k2")); 102 } 103 104 public void testChildrenImmutabilityAndDefensiveCopy() 105 { 106 Object childName = "childName"; 108 Object newChild = "newChild"; 109 root.addChild(new Fqn(childName)); 110 Map childrenMapDirect = root.getChildrenMapDirect(); 111 Set childrenDirect = root.getChildrenDirect(); 112 Set childrenNamesDirect = root.getChildrenNamesDirect(); 113 114 try 115 { 116 childrenMapDirect.clear(); 117 fail("getChildrenMapDirect() should return an unmodifiable collection object"); 118 } 119 catch (UnsupportedOperationException uoe) 120 { 121 } 123 124 try 125 { 126 childrenDirect.clear(); 127 fail("getChildrenDirect() should return an unmodifiable collection object"); 128 } 129 catch (UnsupportedOperationException uoe) 130 { 131 } 133 134 try 135 { 136 childrenNamesDirect.clear(); 137 fail("getChildrenNamesDirect() should return an unmodifiable collection object"); 138 } 139 catch (UnsupportedOperationException uoe) 140 { 141 } 143 144 root.addChild(new Fqn(newChild)); 146 147 assertTrue("root.addChild() should have succeeded", root.getChildrenNamesDirect().contains(newChild)); 148 assertTrue("getChildrenMapDirect() should have made a defensive copy of the data collection object", !childrenMapDirect.containsKey(newChild)); 149 assertTrue("getChildrenDirect() should have made a defensive copy of the data collection object", !childrenDirect.contains(newChild)); 150 assertTrue("getChildrenNamesDirect() should have made a defensive copy of the data collection object", !childrenNamesDirect.contains(newChild)); 151 152 } 153 154 public void testNullCollections() 155 { 156 assertNotNull("Should not be null", root.getDataDirect()); 158 assertTrue("Should be empty", root.getDataDirect().isEmpty()); 159 160 assertNotNull("Should not be null", root.getKeysDirect()); 161 assertTrue("Should be empty", root.getKeysDirect().isEmpty()); 162 163 assertNotNull("Should not be null", root.getChildrenMapDirect()); 164 assertTrue("Should be empty", root.getChildrenMapDirect().isEmpty()); 165 166 assertNotNull("Should not be null", root.getChildrenDirect()); 167 assertTrue("Should be empty", root.getChildrenDirect().isEmpty()); 168 169 assertNotNull("Should not be null", root.getChildrenNamesDirect()); 170 assertTrue("Should be empty", root.getChildrenNamesDirect().isEmpty()); 171 } 172 173 174 } 175 | Popular Tags |