1 package org.jgroups.tests; 2 3 import org.jgroups.Channel; 4 import org.jgroups.blocks.DistributedHashtable; 5 6 import java.util.HashMap ; 7 import java.util.Map ; 8 9 14 public class DistributedHashtableUnitTest extends ChannelTestBase { 15 16 private static int testCount = 1; 17 18 private DistributedHashtable map1; 19 private DistributedHashtable map2; 20 21 public DistributedHashtableUnitTest(String testName) { 22 super(testName); 23 } 24 25 protected void setUp() throws Exception { 26 super.setUp(); 27 System.out.println("#### Setup Test " + testCount); 28 29 Channel c1=createChannel("A"); 30 this.map1=new DistributedHashtable(c1, false, 5000); 31 c1.connect("demo"); 32 this.map1.start(5000); 33 34 Channel c2=createChannel("A"); 35 this.map2=new DistributedHashtable(c2, false, 5000); 36 c2.connect("demo"); 37 this.map2.start(5000); 38 } 39 40 protected void tearDown() throws Exception { 41 this.map1.stop(); 42 this.map2.stop(); 43 System.out.println("#### TearDown Test " + testCount + "\n\n"); 44 testCount++; 45 super.tearDown(); 46 } 47 48 public void testSize() { 49 assertEquals(0, this.map1.size()); 50 assertEquals(this.map2.size(), this.map1.size()); 51 52 this.map1.put("key1", "value1"); 53 assertEquals(1, this.map1.size()); 54 assertEquals(this.map2.size(), this.map1.size()); 55 56 this.map2.put("key2", "value2"); 57 assertEquals(2, this.map1.size()); 58 assertEquals(this.map2.size(), this.map1.size()); 59 } 60 61 public void testIsEmpty() { 62 assertTrue(this.map1.isEmpty()); 63 assertTrue(this.map2.isEmpty()); 64 65 this.map1.put("key", "value"); 66 67 assertFalse(this.map1.isEmpty()); 68 assertFalse(this.map2.isEmpty()); 69 } 70 71 public void testContainsKey() { 72 assertFalse(this.map1.containsKey("key1")); 73 assertFalse(this.map2.containsKey("key1")); 74 this.map1.put("key1", "value"); 75 assertTrue(this.map1.containsKey("key1")); 76 assertTrue(this.map2.containsKey("key1")); 77 this.map2.put("key2", "value"); 78 assertTrue(this.map1.containsKey("key2")); 79 assertTrue(this.map2.containsKey("key2")); 80 } 81 82 public void testContainsValue() { 83 assertFalse(this.map1.containsValue("value1")); 84 assertFalse(this.map2.containsValue("value1")); 85 this.map1.put("key1", "value1"); 86 assertTrue(this.map1.containsValue("value1")); 87 assertTrue(this.map2.containsValue("value1")); 88 this.map2.put("key2", "value2"); 89 assertTrue(this.map1.containsValue("value2")); 90 assertTrue(this.map2.containsValue("value2")); 91 } 92 93 public void testPutAndGet() { 94 assertNull(this.map1.get("key1")); 95 assertNull(this.map2.get("key1")); 96 this.map1.put("key1", "value1"); 97 assertNotNull(this.map1.get("key1")); 98 assertNotNull(this.map2.get("key1")); 99 this.map2.put("key2", "value2"); 100 assertNotNull(this.map1.get("key2")); 101 assertNotNull(this.map2.get("key2")); 102 } 103 104 public void testRemove() { 105 assertNull(this.map1.get("key1")); 106 assertNull(this.map2.get("key1")); 107 this.map1.put("key1", "value1"); 108 this.map2.put("key2", "value2"); 109 assertNotNull(this.map1.get("key1")); 110 assertNotNull(this.map2.get("key1")); 111 assertNotNull(this.map1.get("key2")); 112 assertNotNull(this.map2.get("key2")); 113 114 this.map1.remove("key1"); 115 assertNull(this.map1.get("key1")); 116 assertNull(this.map2.get("key1")); 117 assertNotNull(this.map1.get("key2")); 118 assertNotNull(this.map2.get("key2")); 119 120 this.map2.remove("key2"); 121 assertNull(this.map1.get("key2")); 122 assertNull(this.map2.get("key2")); 123 } 124 125 public void testPutAll() { 126 Map all1 = new HashMap (); 127 all1.put("key1", "value1"); 128 all1.put("key2", "value2"); 129 Map all2 = new HashMap (); 130 all2.put("key3", "value3"); 131 all2.put("key4", "value4"); 132 133 this.map1.putAll(all1); 134 assertEquals(2, this.map1.size()); 135 assertEquals(2, this.map2.size()); 136 this.map2.putAll(all2); 137 assertEquals(4, this.map1.size()); 138 assertEquals(4, this.map2.size()); 139 140 assertTrue(this.map1.containsKey("key1")); 141 assertTrue(this.map1.containsKey("key2")); 142 assertTrue(this.map1.containsKey("key3")); 143 assertTrue(this.map1.containsKey("key4")); 144 145 assertTrue(this.map2.containsKey("key1")); 146 assertTrue(this.map2.containsKey("key2")); 147 assertTrue(this.map2.containsKey("key3")); 148 assertTrue(this.map2.containsKey("key4")); 149 } 150 151 public void testClear() { 152 assertTrue(this.map1.isEmpty()); 153 assertTrue(this.map2.isEmpty()); 154 155 this.map1.put("key", "value"); 156 assertFalse(this.map1.isEmpty()); 157 assertFalse(this.map2.isEmpty()); 158 159 this.map1.clear(); 160 assertTrue(this.map1.isEmpty()); 161 assertTrue(this.map2.isEmpty()); 162 this.map2.put("key", "value"); 163 assertFalse(this.map1.isEmpty()); 164 assertFalse(this.map2.isEmpty()); 165 166 this.map2.clear(); 167 assertTrue(this.map1.isEmpty()); 168 assertTrue(this.map2.isEmpty()); 169 } 170 171 public void testKeySet() { 172 Map all1 = new HashMap (); 173 all1.put("key1", "value1"); 174 all1.put("key2", "value2"); 175 Map all2 = new HashMap (); 176 all2.put("key3", "value3"); 177 all2.put("key4", "value4"); 178 179 this.map1.putAll(all1); 180 assertEquals(all1.keySet(), this.map1.keySet()); 181 assertEquals(all1.keySet(), this.map2.keySet()); 182 183 this.map2.putAll(all2); 184 all1.putAll(all2); 185 assertEquals(all1.keySet(), this.map1.keySet()); 186 assertEquals(all1.keySet(), this.map2.keySet()); 187 } 188 189 public void testValues() { 190 Map all1 = new HashMap (); 191 all1.put("key1", "value1"); 192 all1.put("key2", "value2"); 193 Map all2 = new HashMap (); 194 all2.put("key3", "value3"); 195 all2.put("key4", "value4"); 196 197 this.map1.putAll(all1); 198 assertTrue(this.map1.values().containsAll(all1.values())); 199 assertTrue(this.map2.values().containsAll(all1.values())); 200 201 this.map2.putAll(all2); 202 all1.putAll(all2); 203 assertTrue(this.map1.values().containsAll(all1.values())); 204 assertTrue(this.map2.values().containsAll(all1.values())); 205 } 206 207 208 209 } 210 | Popular Tags |