• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 junit.framework.AssertionFailedError;
31 import org.junit.Ignore;
32 
33 /**
34  * A generic JUnit test which tests {@link Map#computeIfPresent}. Can't be invoked directly; please
35  * see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
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 MapComputeIfPresentTester<K, V> extends AbstractMapTester<K, V> {
42 
43   @MapFeature.Require(SUPPORTS_PUT)
testComputeIfPresent_supportedAbsent()44   public void testComputeIfPresent_supportedAbsent() {
45     assertNull(
46         "computeIfPresent(notPresent, function) should return null",
47         getMap()
48             .computeIfPresent(
49                 k3(),
50                 (k, v) -> {
51                   throw new AssertionFailedError();
52                 }));
53     expectUnchanged();
54   }
55 
56   @MapFeature.Require(SUPPORTS_PUT)
57   @CollectionSize.Require(absent = ZERO)
testComputeIfPresent_supportedPresent()58   public void testComputeIfPresent_supportedPresent() {
59     assertEquals(
60         "computeIfPresent(present, function) should return new value",
61         v3(),
62         getMap()
63             .computeIfPresent(
64                 k0(),
65                 (k, v) -> {
66                   assertEquals(k0(), k);
67                   assertEquals(v0(), v);
68                   return v3();
69                 }));
70     expectReplacement(entry(k0(), v3()));
71   }
72 
73   @MapFeature.Require(SUPPORTS_PUT)
74   @CollectionSize.Require(absent = ZERO)
testComputeIfPresent_functionReturnsNull()75   public void testComputeIfPresent_functionReturnsNull() {
76     assertNull(
77         "computeIfPresent(present, returnsNull) should return null",
78         getMap()
79             .computeIfPresent(
80                 k0(),
81                 (k, v) -> {
82                   assertEquals(k0(), k);
83                   assertEquals(v0(), v);
84                   return null;
85                 }));
86     expectMissing(e0());
87   }
88 
89   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
90   @CollectionSize.Require(absent = ZERO)
testComputeIfPresent_nullTreatedAsAbsent()91   public void testComputeIfPresent_nullTreatedAsAbsent() {
92     initMapWithNullValue();
93     assertNull(
94         "computeIfPresent(presentAssignedToNull, function) should return null",
95         getMap()
96             .computeIfPresent(
97                 getKeyForNullValue(),
98                 (k, v) -> {
99                   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)
testComputeIfPresent_functionThrows()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)
testComputeIfPresent_nullKeySupportedPresent()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})
testComputeIfPresent_nullKeySupportedAbsent()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)
testComputeIfPresent_unsupportedAbsent()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)
testComputeIfPresent_unsupportedPresent()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 }
183