@Test public void testRemoveWithKeyAndValue() throws Exception { final SharedHashMap<CharSequence, CharSequence> map = new SharedHashMapBuilder() .minSegments(2) .create(getPersistenceFile(), CharSequence.class, CharSequence.class); map.put("key1", "one"); map.put("key2", "two"); assertEquals("one", map.get("key1")); assertEquals("two", map.get("key2")); // a false remove final boolean wasRemoved1 = map.remove("key1", "three"); assertFalse(wasRemoved1); assertEquals(null, map.get("key1"), "one"); assertEquals("two", map.get("key2"), "two"); map.put("key1", "one"); final boolean wasRemoved2 = map.remove("key1", "three"); assertFalse(wasRemoved2); // lets add one more item for luck ! map.put("key3", "three"); assertEquals("three", map.get("key3")); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); assertEquals("overwritten", map.get("key3")); }
@Test public void testRemoveWithKeyAndRemoveReturnsNull() throws Exception { final SharedHashMap<CharSequence, CharSequence> map = new SharedHashMapBuilder() .minSegments(2) .removeReturnsNull(true) .create(getPersistenceFile(), CharSequence.class, CharSequence.class); assertFalse(map.containsKey("key3")); map.put("key1", "one"); map.put("key2", "two"); assertEquals(2, map.size()); assertTrue(map.containsKey("key1")); assertTrue(map.containsKey("key2")); assertFalse(map.containsKey("key3")); assertEquals("one", map.get("key1")); assertEquals("two", map.get("key2")); final CharSequence result = map.remove("key1"); assertEquals(null, result); assertEquals(1, map.size()); assertFalse(map.containsKey("key1")); assertEquals(null, map.get("key1")); assertEquals("two", map.get("key2")); assertFalse(map.containsKey("key3")); // lets add one more item for luck ! map.put("key3", "three"); assertEquals("three", map.get("key3")); assertTrue(map.containsKey("key3")); assertEquals(2, map.size()); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); assertEquals("overwritten", map.get("key3")); assertTrue(map.containsKey("key3")); assertEquals(2, map.size()); }
@Test public void testReplaceWithKeyAnd2Params() throws Exception { final SharedHashMap<CharSequence, CharSequence> map = new SharedHashMapBuilder() .minSegments(2) .create(getPersistenceFile(), CharSequence.class, CharSequence.class); map.put("key1", "one"); map.put("key2", "two"); assertEquals("one", map.get("key1")); assertEquals("two", map.get("key2")); final boolean result = map.replace("key1", "one", "newValue"); assertEquals(true, result); assertEquals("newValue", map.get("key1")); assertEquals("two", map.get("key2")); // let and one more item for luck ! map.put("key3", "three"); assertEquals("three", map.get("key3")); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); assertEquals("overwritten", map.get("key3")); final boolean result2 = map.replace("key2", "two", "newValue2"); assertEquals(true, result2); assertEquals("newValue2", map.get("key2")); final boolean result3 = map.replace("newKey", "", "newValue"); assertEquals(false, result3); final boolean result4 = map.replace("key2", "newValue2", "newValue2"); assertEquals(true, result4); }