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_VALUES;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
22  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.testing.AbstractMapTester;
26  import com.google.common.collect.testing.features.CollectionSize;
27  import com.google.common.collect.testing.features.MapFeature;
28  import java.util.concurrent.ConcurrentMap;
29  
30  /**
31   * A generic JUnit test which tests {@code replace(K, V, V)} operations on a concurrent map. Can't
32   * be invoked directly; please see
33   * {@link com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
34   *
35   * @author Louis Wasserman
36   */
37  @GwtCompatible
38  public class ConcurrentMapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {
39    @Override
40    protected ConcurrentMap<K, V> getMap() {
41      return (ConcurrentMap<K, V>) super.getMap();
42    }
43  
44    @MapFeature.Require(SUPPORTS_PUT)
45    @CollectionSize.Require(absent = ZERO)
46    public void testReplaceEntry_supportedPresent() {
47      assertTrue(getMap().replace(k0(), v0(), v3()));
48      expectReplacement(entry(k0(), v3()));
49    }
50  
51    @MapFeature.Require(SUPPORTS_PUT)
52    @CollectionSize.Require(absent = ZERO)
53    public void testReplaceEntry_supportedPresentUnchanged() {
54      assertTrue(getMap().replace(k0(), v0(), v0()));
55      expectUnchanged();
56    }
57  
58    @MapFeature.Require(SUPPORTS_PUT)
59    @CollectionSize.Require(absent = ZERO)
60    public void testReplaceEntry_supportedWrongValue() {
61      assertFalse(getMap().replace(k0(), v3(), v4()));
62      expectUnchanged();
63    }
64  
65    @MapFeature.Require(SUPPORTS_PUT)
66    public void testReplaceEntry_supportedAbsentKey() {
67      assertFalse(getMap().replace(k3(), v3(), v4()));
68      expectUnchanged();
69    }
70  
71    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
72    @CollectionSize.Require(absent = ZERO)
73    public void testReplaceEntry_presentNullValueUnsupported() {
74      try {
75        getMap().replace(k0(), v0(), null);
76        fail("Expected NullPointerException");
77      } catch (NullPointerException expected) {
78      }
79      expectUnchanged();
80    }
81  
82    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
83    @CollectionSize.Require(absent = ZERO)
84    public void testReplaceEntry_wrongValueNullValueUnsupported() {
85      try {
86        assertFalse(getMap().replace(k0(), v3(), null));
87      } catch (NullPointerException tolerated) {
88        // the operation would be a no-op, so exceptions are allowed but not required
89      }
90      expectUnchanged();
91    }
92  
93    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
94    public void testReplaceEntry_absentKeyNullValueUnsupported() {
95      try {
96        assertFalse(getMap().replace(k3(), v3(), null));
97      } catch (NullPointerException tolerated) {
98        // the operation would be a no-op, so exceptions are allowed but not required
99      }
100     expectUnchanged();
101   }
102 
103   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES})
104   public void testReplaceEntry_nullDifferentFromAbsent() {
105     assertFalse(getMap().replace(k3(), null, v3()));
106     expectUnchanged();
107   }
108 
109   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
110   public void testReplaceEntry_expectNullUnsupported() {
111     try {
112       assertFalse(getMap().replace(k3(), null, v3()));
113     } catch (NullPointerException tolerated) {
114       // the operation would be a no-op, so exceptions are allowed but not required
115     }
116     expectUnchanged();
117   }
118 
119   @MapFeature.Require(absent = SUPPORTS_PUT)
120   @CollectionSize.Require(absent = ZERO)
121   public void testReplaceEntry_unsupportedPresent() {
122     try {
123       getMap().replace(k0(), v0(), v3());
124       fail("Expected UnsupportedOperationException");
125     } catch (UnsupportedOperationException expected) {
126     }
127     expectUnchanged();
128   }
129 
130   @MapFeature.Require(absent = SUPPORTS_PUT)
131   @CollectionSize.Require(absent = ZERO)
132   public void testReplaceEntry_unsupportedWrongValue() {
133     try {
134       getMap().replace(k0(), v3(), v4());
135     } catch (UnsupportedOperationException tolerated) {
136       // the operation would be a no-op, so exceptions are allowed but not required
137     }
138     expectUnchanged();
139   }
140 
141   @MapFeature.Require(absent = SUPPORTS_PUT)
142   public void testReplaceEntry_unsupportedAbsentKey() {
143     try {
144       getMap().replace(k3(), v3(), v4());
145     } catch (UnsupportedOperationException tolerated) {
146       // the operation would be a no-op, so exceptions are allowed but not required
147     }
148     expectUnchanged();
149   }
150 }