| 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_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; |
| 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 com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 29 |
import java.util.Map.Entry; |
| 30 |
import java.util.concurrent.ConcurrentMap; |
| 31 |
import org.junit.Ignore; |
| 32 |
|
| 33 |
/** |
| 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}. |
| 37 |
* |
| 38 |
* @author Louis Wasserman |
| 39 |
*/ |
| 40 |
@GwtCompatible |
| 41 |
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. |
| 42 |
public class ConcurrentMapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> { |
| 43 |
@Override |
| 44 |
protected ConcurrentMap<K, V> getMap() { |
| 45 |
return (ConcurrentMap<K, V>) super.getMap(); |
| 46 |
} |
| 47 |
|
| 48 |
@MapFeature.Require(SUPPORTS_PUT) |
| 49 |
public void testPutIfAbsent_supportedAbsent() { |
| 50 |
assertNull("putIfAbsent(notPresent, value) should return null", putIfAbsent(e3())); |
| 51 |
expectAdded(e3()); |
| 52 |
} |
| 53 |
|
| 54 |
@MapFeature.Require(SUPPORTS_PUT) |
| 55 |
@CollectionSize.Require(absent = ZERO) |
| 56 |
public void testPutIfAbsent_supportedPresent() { |
| 57 |
assertEquals( |
| 58 |
"putIfAbsent(present, value) should return existing value", |
| 59 |
v0(), |
| 60 |
getMap().putIfAbsent(k0(), v3())); |
| 61 |
expectUnchanged(); |
| 62 |
} |
| 63 |
|
| 64 |
@MapFeature.Require(absent = SUPPORTS_PUT) |
| 65 |
public void testPutIfAbsent_unsupportedAbsent() { |
| 66 |
try { |
| 67 |
putIfAbsent(e3()); |
| 68 |
fail("putIfAbsent(notPresent, value) should throw"); |
| 69 |
} catch (UnsupportedOperationException expected) { |
| 70 |
} |
| 71 |
expectUnchanged(); |
| 72 |
expectMissing(e3()); |
| 73 |
} |
| 74 |
|
| 75 |
@MapFeature.Require(absent = SUPPORTS_PUT) |
| 76 |
@CollectionSize.Require(absent = ZERO) |
| 77 |
public void testPutIfAbsent_unsupportedPresentExistingValue() { |
| 78 |
try { |
| 79 |
assertEquals( |
| 80 |
"putIfAbsent(present, existingValue) should return present or throw", |
| 81 |
v0(), |
| 82 |
putIfAbsent(e0())); |
| 83 |
} catch (UnsupportedOperationException tolerated) { |
| 84 |
} |
| 85 |
expectUnchanged(); |
| 86 |
} |
| 87 |
|
| 88 |
@MapFeature.Require(absent = SUPPORTS_PUT) |
| 89 |
@CollectionSize.Require(absent = ZERO) |
| 90 |
public void testPutIfAbsent_unsupportedPresentDifferentValue() { |
| 91 |
try { |
| 92 |
getMap().putIfAbsent(k0(), v3()); |
| 93 |
} catch (UnsupportedOperationException tolerated) { |
| 94 |
} |
| 95 |
expectUnchanged(); |
| 96 |
} |
| 97 |
|
| 98 |
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS) |
| 99 |
public void testPutIfAbsent_nullKeyUnsupported() { |
| 100 |
try { |
| 101 |
getMap().putIfAbsent(null, v3()); |
| 102 |
fail("putIfAbsent(null, value) should throw"); |
| 103 |
} catch (NullPointerException expected) { |
| 104 |
} |
| 105 |
expectUnchanged(); |
| 106 |
expectNullKeyMissingWhenNullKeysUnsupported( |
| 107 |
"Should not contain null key after unsupported putIfAbsent(null, value)"); |
| 108 |
} |
| 109 |
|
| 110 |
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) |
| 111 |
public void testPutIfAbsent_nullValueUnsupported() { |
| 112 |
try { |
| 113 |
getMap().putIfAbsent(k3(), null); |
| 114 |
fail("putIfAbsent(key, null) should throw"); |
| 115 |
} catch (NullPointerException expected) { |
| 116 |
} |
| 117 |
expectUnchanged(); |
| 118 |
expectNullValueMissingWhenNullValuesUnsupported( |
| 119 |
"Should not contain null value after unsupported put(key, null)"); |
| 120 |
} |
| 121 |
|
| 122 |
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) |
| 123 |
@CollectionSize.Require(absent = ZERO) |
| 124 |
public void testPutIfAbsent_putWithNullValueUnsupported() { |
| 125 |
try { |
| 126 |
getMap().putIfAbsent(k0(), null); |
| 127 |
} catch (NullPointerException tolerated) { |
| 128 |
} |
| 129 |
expectUnchanged(); |
| 130 |
expectNullValueMissingWhenNullValuesUnsupported( |
| 131 |
"Should not contain null after unsupported putIfAbsent(present, null)"); |
| 132 |
} |
| 133 |
|
| 134 |
@CanIgnoreReturnValue |
| 135 |
private V putIfAbsent(Entry<K, V> entry) { |
| 136 |
return getMap().putIfAbsent(entry.getKey(), entry.getValue()); |
| 137 |
} |
| 138 |
} |