• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21 import junit.framework.TestCase;
22 
23 /**
24  * Tests for {@link ForwardingConcurrentMap}.
25  *
26  * @author Jared Levy
27  */
28 public class ForwardingConcurrentMapTest extends TestCase {
29 
30   private static class TestMap extends ForwardingConcurrentMap<String, Integer> {
31     final ConcurrentMap<String, Integer> delegate = new ConcurrentHashMap<>();
32 
33     @Override
delegate()34     protected ConcurrentMap<String, Integer> delegate() {
35       return delegate;
36     }
37   }
38 
testPutIfAbsent()39   public void testPutIfAbsent() {
40     TestMap map = new TestMap();
41     map.put("foo", 1);
42     assertEquals(Integer.valueOf(1), map.putIfAbsent("foo", 2));
43     assertEquals(Integer.valueOf(1), map.get("foo"));
44     assertNull(map.putIfAbsent("bar", 3));
45     assertEquals(Integer.valueOf(3), map.get("bar"));
46   }
47 
testRemove()48   public void testRemove() {
49     TestMap map = new TestMap();
50     map.put("foo", 1);
51     assertFalse(map.remove("foo", 2));
52     assertFalse(map.remove("bar", 1));
53     assertEquals(Integer.valueOf(1), map.get("foo"));
54     assertTrue(map.remove("foo", 1));
55     assertTrue(map.isEmpty());
56   }
57 
testReplace()58   public void testReplace() {
59     TestMap map = new TestMap();
60     map.put("foo", 1);
61     assertEquals(Integer.valueOf(1), map.replace("foo", 2));
62     assertNull(map.replace("bar", 3));
63     assertEquals(Integer.valueOf(2), map.get("foo"));
64     assertFalse(map.containsKey("bar"));
65   }
66 
testReplaceConditional()67   public void testReplaceConditional() {
68     TestMap map = new TestMap();
69     map.put("foo", 1);
70     assertFalse(map.replace("foo", 2, 3));
71     assertFalse(map.replace("bar", 1, 2));
72     assertEquals(Integer.valueOf(1), map.get("foo"));
73     assertFalse(map.containsKey("bar"));
74     assertTrue(map.replace("foo", 1, 4));
75     assertEquals(Integer.valueOf(4), map.get("foo"));
76   }
77 }
78