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