View Javadoc
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  
32  /**
33   * A generic JUnit test which tests {@link Map#computeIfPresent}. Can't be
34   * invoked directly; please see
35   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
36   *
37   * @author Louis Wasserman
38   */
39  @GwtCompatible
40  public class MapComputeIfPresentTester<K, V> extends AbstractMapTester<K, V> {
41  
42    @MapFeature.Require(SUPPORTS_PUT)
43    public void testComputeIfPresent_supportedAbsent() {
44      assertNull(
45          "computeIfPresent(notPresent, function) should return null",
46          getMap()
47              .computeIfPresent(
48                  k3(),
49                  (k, v) -> {
50                    throw new AssertionFailedError();
51                  }));
52      expectUnchanged();
53    }
54  
55    @MapFeature.Require(SUPPORTS_PUT)
56    @CollectionSize.Require(absent = ZERO)
57    public void testComputeIfPresent_supportedPresent() {
58      assertEquals(
59          "computeIfPresent(present, function) should return new value",
60          v3(),
61          getMap()
62              .computeIfPresent(
63                  k0(),
64                  (k, v) -> {
65                    assertEquals(k0(), k);
66                    assertEquals(v0(), v);
67                    return v3();
68                  }));
69      expectReplacement(entry(k0(), v3()));
70    }
71  
72    @MapFeature.Require(SUPPORTS_PUT)
73    @CollectionSize.Require(absent = ZERO)
74    public void testComputeIfPresent_functionReturnsNull() {
75      assertNull(
76          "computeIfPresent(present, returnsNull) should return null",
77          getMap()
78              .computeIfPresent(
79                  k0(),
80                  (k, v) -> {
81                    assertEquals(k0(), k);
82                    assertEquals(v0(), v);
83                    return null;
84                  }));
85      expectMissing(e0());
86    }
87  
88    @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
89    @CollectionSize.Require(absent = ZERO)
90    public void testComputeIfPresent_nullTreatedAsAbsent() {
91      initMapWithNullValue();
92      assertNull(
93          "computeIfPresent(presentAssignedToNull, function) should return null",
94          getMap()
95              .computeIfPresent(
96                  getKeyForNullValue(),
97                  (k, v) -> {
98                    throw new AssertionFailedError();
99                  }));
100     expectReplacement(entry(getKeyForNullValue(), null));
101   }
102 
103   static class ExpectedException extends RuntimeException {}
104 
105   @MapFeature.Require(SUPPORTS_PUT)
106   @CollectionSize.Require(absent = ZERO)
107   public void testComputeIfPresent_functionThrows() {
108     try {
109       getMap()
110           .computeIfPresent(
111               k0(),
112               (k, v) -> {
113                 assertEquals(k0(), k);
114                 assertEquals(v0(), v);
115                 throw new ExpectedException();
116               });
117       fail("Expected ExpectedException");
118     } catch (ExpectedException expected) {
119     }
120     expectUnchanged();
121   }
122 
123   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
124   @CollectionSize.Require(absent = ZERO)
125   public void testComputeIfPresent_nullKeySupportedPresent() {
126     initMapWithNullKey();
127     assertEquals(
128         "computeIfPresent(null, function) should return new value",
129         v3(),
130         getMap()
131             .computeIfPresent(
132                 null,
133                 (k, v) -> {
134                   assertNull(k);
135                   assertEquals(getValueForNullKey(), v);
136                   return v3();
137                 }));
138 
139     Entry<K, V>[] expected = createArrayWithNullKey();
140     expected[getNullLocation()] = entry(null, v3());
141     expectContents(expected);
142   }
143 
144   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
145   public void testComputeIfPresent_nullKeySupportedAbsent() {
146     assertNull(
147         "computeIfPresent(null, function) should return null",
148         getMap()
149             .computeIfPresent(
150                 null,
151                 (k, v) -> {
152                   throw new AssertionFailedError();
153                 }));
154     expectUnchanged();
155   }
156 
157   @MapFeature.Require(absent = SUPPORTS_PUT)
158   public void testComputeIfPresent_unsupportedAbsent() {
159     try {
160       getMap()
161           .computeIfPresent(
162               k3(),
163               (k, v) -> {
164                 throw new AssertionFailedError();
165               });
166     } catch (UnsupportedOperationException tolerated) {
167     }
168     expectUnchanged();
169   }
170 
171   @MapFeature.Require(absent = SUPPORTS_PUT)
172   @CollectionSize.Require(absent = ZERO)
173   public void testComputeIfPresent_unsupportedPresent() {
174     try {
175       getMap().computeIfPresent(k0(), (k, v) -> v3());
176       fail("Expected UnsupportedOperationException");
177     } catch (UnsupportedOperationException expected) {
178     }
179     expectUnchanged();
180   }
181 }