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