2
* Copyright (C) 2015 The Guava Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.google.common.collect.testing.testers;
19
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
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 com.google.errorprone.annotations.CanIgnoreReturnValue;
29
import java.util.Map.Entry;
30
import java.util.concurrent.ConcurrentMap;
31
import org.junit.Ignore;
34
* A generic JUnit test which tests {@code putIfAbsent} operations on a concurrent map. Can't be
35
* invoked directly; please see {@link
36
* com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
38
* @author Louis Wasserman
41
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
42
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
43
@ElementTypesAreNonnullByDefault
44
public class ConcurrentMapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
46
protected ConcurrentMap<K, V> getMap() {
47
return (ConcurrentMap<K, V>) super.getMap();
50
@MapFeature.Require(SUPPORTS_PUT)
51
public void testPutIfAbsent_supportedAbsent() {
52
assertNull("putIfAbsent(notPresent, value) should return null", putIfAbsent(e3()));
56
@MapFeature.Require(SUPPORTS_PUT)
57
@CollectionSize.Require(absent = ZERO)
58
public void testPutIfAbsent_supportedPresent() {
60
"putIfAbsent(present, value) should return existing value",
62
getMap().putIfAbsent(k0(), v3()));
66
@MapFeature.Require(absent = SUPPORTS_PUT)
67
public void testPutIfAbsent_unsupportedAbsent() {
70
fail("putIfAbsent(notPresent, value) should throw");
71
} catch (UnsupportedOperationException expected) {
77
@MapFeature.Require(absent = SUPPORTS_PUT)
78
@CollectionSize.Require(absent = ZERO)
79
public void testPutIfAbsent_unsupportedPresentExistingValue() {
82
"putIfAbsent(present, existingValue) should return present or throw",
85
} catch (UnsupportedOperationException tolerated) {
90
@MapFeature.Require(absent = SUPPORTS_PUT)
91
@CollectionSize.Require(absent = ZERO)
92
public void testPutIfAbsent_unsupportedPresentDifferentValue() {
94
getMap().putIfAbsent(k0(), v3());
95
} catch (UnsupportedOperationException tolerated) {
100
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
101
public void testPutIfAbsent_nullKeyUnsupported() {
103
getMap().putIfAbsent(null, v3());
104
fail("putIfAbsent(null, value) should throw");
105
} catch (NullPointerException expected) {
108
expectNullKeyMissingWhenNullKeysUnsupported(
109
"Should not contain null key after unsupported putIfAbsent(null, value)");
112
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
113
public void testPutIfAbsent_nullValueUnsupported() {
115
getMap().putIfAbsent(k3(), null);
116
fail("putIfAbsent(key, null) should throw");
117
} catch (NullPointerException expected) {
120
expectNullValueMissingWhenNullValuesUnsupported(
121
"Should not contain null value after unsupported put(key, null)");
124
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
125
@CollectionSize.Require(absent = ZERO)
126
public void testPutIfAbsent_putWithNullValueUnsupported() {
128
getMap().putIfAbsent(k0(), null);
129
} catch (NullPointerException tolerated) {
132
expectNullValueMissingWhenNullValuesUnsupported(
133
"Should not contain null after unsupported putIfAbsent(present, null)");
136
@CanIgnoreReturnValue
137
private V putIfAbsent(Entry<K, V> entry) {
138
return getMap().putIfAbsent(entry.getKey(), entry.getValue());