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