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