001/* 002 * Copyright (C) 2016 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.features.CollectionSize.ZERO; 020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 023import static org.junit.Assert.assertThrows; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.collect.testing.AbstractMapTester; 027import com.google.common.collect.testing.features.CollectionSize; 028import com.google.common.collect.testing.features.MapFeature; 029import com.google.common.collect.testing.testers.TestExceptions.SomeUncheckedException; 030import java.util.Map; 031import java.util.Map.Entry; 032import junit.framework.AssertionFailedError; 033 034/** 035 * A generic JUnit test which tests {@link Map#computeIfPresent}. Can't be invoked directly; please 036 * see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 037 * 038 * @author Louis Wasserman 039 */ 040@GwtCompatible 041public class MapComputeIfPresentTester<K, V> extends AbstractMapTester<K, V> { 042 043 @MapFeature.Require(SUPPORTS_PUT) 044 public void testComputeIfPresent_supportedAbsent() { 045 assertNull( 046 "computeIfPresent(notPresent, function) should return null", 047 getMap() 048 .computeIfPresent( 049 k3(), 050 (k, v) -> { 051 throw new AssertionFailedError(); 052 })); 053 expectUnchanged(); 054 } 055 056 @MapFeature.Require(SUPPORTS_PUT) 057 @CollectionSize.Require(absent = ZERO) 058 public void testComputeIfPresent_supportedPresent() { 059 assertEquals( 060 "computeIfPresent(present, function) should return new value", 061 v3(), 062 getMap() 063 .computeIfPresent( 064 k0(), 065 (k, v) -> { 066 assertEquals(k0(), k); 067 assertEquals(v0(), v); 068 return v3(); 069 })); 070 expectReplacement(entry(k0(), v3())); 071 } 072 073 @MapFeature.Require(SUPPORTS_PUT) 074 @CollectionSize.Require(absent = ZERO) 075 public void testComputeIfPresent_functionReturnsNull() { 076 assertNull( 077 "computeIfPresent(present, returnsNull) should return null", 078 getMap() 079 .computeIfPresent( 080 k0(), 081 (k, v) -> { 082 assertEquals(k0(), k); 083 assertEquals(v0(), v); 084 return null; 085 })); 086 expectMissing(e0()); 087 } 088 089 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) 090 @CollectionSize.Require(absent = ZERO) 091 public void testComputeIfPresent_nullTreatedAsAbsent() { 092 initMapWithNullValue(); 093 assertNull( 094 "computeIfPresent(presentAssignedToNull, function) should return null", 095 getMap() 096 .computeIfPresent( 097 getKeyForNullValue(), 098 (k, v) -> { 099 throw new AssertionFailedError(); 100 })); 101 expectReplacement(entry(getKeyForNullValue(), null)); 102 } 103 104 @MapFeature.Require(SUPPORTS_PUT) 105 @CollectionSize.Require(absent = ZERO) 106 public void testComputeIfPresent_functionThrows() { 107 assertThrows( 108 SomeUncheckedException.class, 109 () -> 110 getMap() 111 .computeIfPresent( 112 k0(), 113 (k, v) -> { 114 assertEquals(k0(), k); 115 assertEquals(v0(), v); 116 throw new SomeUncheckedException(); 117 })); 118 expectUnchanged(); 119 } 120 121 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) 122 @CollectionSize.Require(absent = ZERO) 123 public void testComputeIfPresent_nullKeySupportedPresent() { 124 initMapWithNullKey(); 125 assertEquals( 126 "computeIfPresent(null, function) should return new value", 127 v3(), 128 getMap() 129 .computeIfPresent( 130 null, 131 (k, v) -> { 132 assertNull(k); 133 assertEquals(getValueForNullKey(), v); 134 return v3(); 135 })); 136 137 Entry<K, V>[] expected = createArrayWithNullKey(); 138 expected[getNullLocation()] = entry(null, v3()); 139 expectContents(expected); 140 } 141 142 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) 143 public void testComputeIfPresent_nullKeySupportedAbsent() { 144 assertNull( 145 "computeIfPresent(null, function) should return null", 146 getMap() 147 .computeIfPresent( 148 null, 149 (k, v) -> { 150 throw new AssertionFailedError(); 151 })); 152 expectUnchanged(); 153 } 154 155 @MapFeature.Require(absent = SUPPORTS_PUT) 156 public void testComputeIfPresent_unsupportedAbsent() { 157 try { 158 getMap() 159 .computeIfPresent( 160 k3(), 161 (k, v) -> { 162 throw new AssertionFailedError(); 163 }); 164 } catch (UnsupportedOperationException tolerated) { 165 } 166 expectUnchanged(); 167 } 168 169 @MapFeature.Require(absent = SUPPORTS_PUT) 170 @CollectionSize.Require(absent = ZERO) 171 public void testComputeIfPresent_unsupportedPresent() { 172 assertThrows( 173 UnsupportedOperationException.class, () -> getMap().computeIfPresent(k0(), (k, v) -> v3())); 174 expectUnchanged(); 175 } 176}