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