1 /* 2 * Copyright (C) 2008 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import junit.framework.TestCase; 20 21 import java.util.concurrent.ConcurrentHashMap; 22 import java.util.concurrent.ConcurrentMap; 23 24 /** 25 * Tests for {@link ForwardingConcurrentMap}. 26 * 27 * @author Jared Levy 28 */ 29 public class ForwardingConcurrentMapTest extends TestCase { 30 31 private static class TestMap 32 extends ForwardingConcurrentMap<String, Integer> { 33 final ConcurrentMap<String, Integer> delegate 34 = new ConcurrentHashMap<String, Integer>(); 35 @Override protected ConcurrentMap<String, Integer> delegate() { 36 return delegate; 37 } 38 } 39 40 public void testPutIfAbsent() { 41 TestMap map = new TestMap(); 42 map.put("foo", 1); 43 assertEquals(Integer.valueOf(1), map.putIfAbsent("foo", 2)); 44 assertEquals(Integer.valueOf(1), map.get("foo")); 45 assertNull(map.putIfAbsent("bar", 3)); 46 assertEquals(Integer.valueOf(3), map.get("bar")); 47 } 48 49 public void testRemove() { 50 TestMap map = new TestMap(); 51 map.put("foo", 1); 52 assertFalse(map.remove("foo", 2)); 53 assertFalse(map.remove("bar", 1)); 54 assertEquals(Integer.valueOf(1), map.get("foo")); 55 assertTrue(map.remove("foo", 1)); 56 assertTrue(map.isEmpty()); 57 } 58 59 public void testReplace() { 60 TestMap map = new TestMap(); 61 map.put("foo", 1); 62 assertEquals(Integer.valueOf(1), map.replace("foo", 2)); 63 assertNull(map.replace("bar", 3)); 64 assertEquals(Integer.valueOf(2), map.get("foo")); 65 assertFalse(map.containsKey("bar")); 66 } 67 68 public void testReplaceConditional() { 69 TestMap map = new TestMap(); 70 map.put("foo", 1); 71 assertFalse(map.replace("foo", 2, 3)); 72 assertFalse(map.replace("bar", 1, 2)); 73 assertEquals(Integer.valueOf(1), map.get("foo")); 74 assertFalse(map.containsKey("bar")); 75 assertTrue(map.replace("foo", 1, 4)); 76 assertEquals(Integer.valueOf(4), map.get("foo")); 77 } 78 } 79