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; 023 024import com.google.common.annotations.GwtCompatible; 025import com.google.common.collect.testing.AbstractMapTester; 026import com.google.common.collect.testing.features.CollectionSize; 027import com.google.common.collect.testing.features.MapFeature; 028import java.util.Map; 029import java.util.Map.Entry; 030import junit.framework.AssertionFailedError; 031import org.junit.Ignore; 032 033/** 034 * A generic JUnit test which tests {@link Map#computeIfPresent}. Can't be invoked directly; please 035 * see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 036 * 037 * @author Louis Wasserman 038 */ 039@GwtCompatible 040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 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 static class ExpectedException extends RuntimeException {} 105 106 @MapFeature.Require(SUPPORTS_PUT) 107 @CollectionSize.Require(absent = ZERO) 108 public void testComputeIfPresent_functionThrows() { 109 try { 110 getMap() 111 .computeIfPresent( 112 k0(), 113 (k, v) -> { 114 assertEquals(k0(), k); 115 assertEquals(v0(), v); 116 throw new ExpectedException(); 117 }); 118 fail("Expected ExpectedException"); 119 } catch (ExpectedException expected) { 120 } 121 expectUnchanged(); 122 } 123 124 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) 125 @CollectionSize.Require(absent = ZERO) 126 public void testComputeIfPresent_nullKeySupportedPresent() { 127 initMapWithNullKey(); 128 assertEquals( 129 "computeIfPresent(null, function) should return new value", 130 v3(), 131 getMap() 132 .computeIfPresent( 133 null, 134 (k, v) -> { 135 assertNull(k); 136 assertEquals(getValueForNullKey(), v); 137 return v3(); 138 })); 139 140 Entry<K, V>[] expected = createArrayWithNullKey(); 141 expected[getNullLocation()] = entry(null, v3()); 142 expectContents(expected); 143 } 144 145 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) 146 public void testComputeIfPresent_nullKeySupportedAbsent() { 147 assertNull( 148 "computeIfPresent(null, function) should return null", 149 getMap() 150 .computeIfPresent( 151 null, 152 (k, v) -> { 153 throw new AssertionFailedError(); 154 })); 155 expectUnchanged(); 156 } 157 158 @MapFeature.Require(absent = SUPPORTS_PUT) 159 public void testComputeIfPresent_unsupportedAbsent() { 160 try { 161 getMap() 162 .computeIfPresent( 163 k3(), 164 (k, v) -> { 165 throw new AssertionFailedError(); 166 }); 167 } catch (UnsupportedOperationException tolerated) { 168 } 169 expectUnchanged(); 170 } 171 172 @MapFeature.Require(absent = SUPPORTS_PUT) 173 @CollectionSize.Require(absent = ZERO) 174 public void testComputeIfPresent_unsupportedPresent() { 175 try { 176 getMap().computeIfPresent(k0(), (k, v) -> v3()); 177 fail("Expected UnsupportedOperationException"); 178 } catch (UnsupportedOperationException expected) { 179 } 180 expectUnchanged(); 181 } 182}