View Javadoc
1   /*
2    * Copyright (C) 2015 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.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
23  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.collect.testing.AbstractMapTester;
27  import com.google.common.collect.testing.features.CollectionSize;
28  import com.google.common.collect.testing.features.MapFeature;
29  import java.util.concurrent.ConcurrentMap;
30  
31  /**
32   * A generic JUnit test which tests {@code replace(K, V)} operations on a concurrent map. Can't be
33   * invoked directly; please see
34   * {@link com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
35   *
36   * @author Louis Wasserman
37   */
38  @GwtCompatible
39  public class ConcurrentMapReplaceTester<K, V> extends AbstractMapTester<K, V> {
40    @Override
41    protected ConcurrentMap<K, V> getMap() {
42      return (ConcurrentMap<K, V>) super.getMap();
43    }
44  
45    @MapFeature.Require(SUPPORTS_PUT)
46    @CollectionSize.Require(absent = ZERO)
47    public void testReplace_supportedPresent() {
48      assertEquals(v0(), getMap().replace(k0(), v3()));
49      expectReplacement(entry(k0(), v3()));
50    }
51  
52    @MapFeature.Require(SUPPORTS_PUT)
53    @CollectionSize.Require(absent = ZERO)
54    public void testReplace_supportedPresentNoChange() {
55      assertEquals(v0(), getMap().replace(k0(), v0()));
56      expectUnchanged();
57    }
58  
59    @MapFeature.Require(SUPPORTS_PUT)
60    public void testReplace_supportedAbsent() {
61      assertNull(getMap().replace(k3(), v3()));
62      expectUnchanged();
63    }
64  
65    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
66    @CollectionSize.Require(absent = ZERO)
67    public void testReplace_presentNullValueUnsupported() {
68      try {
69        getMap().replace(k0(), null);
70        fail("Expected NullPointerException");
71      } catch (NullPointerException expected) {
72      }
73      expectUnchanged();
74    }
75  
76    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
77    public void testReplace_absentNullValueUnsupported() {
78      try {
79        getMap().replace(k3(), null);
80      } catch (NullPointerException tolerated) {
81        // permitted not to throw because it would be a no-op
82      }
83      expectUnchanged();
84    }
85  
86    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)
87    public void testReplace_absentNullKeyUnsupported() {
88      try {
89        getMap().replace(null, v3());
90      } catch (NullPointerException tolerated) {
91        // permitted not to throw because it would be a no-op
92      }
93      expectUnchanged();
94    }
95  
96    @MapFeature.Require(absent = SUPPORTS_PUT)
97    @CollectionSize.Require(absent = ZERO)
98    public void testReplace_unsupportedPresent() {
99      try {
100       getMap().replace(k0(), v3());
101       fail("Expected UnsupportedOperationException");
102     } catch (UnsupportedOperationException expected) {
103     }
104     expectUnchanged();
105   }
106 }