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  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.annotations.GwtIncompatible;
27  import com.google.common.collect.testing.AbstractMapTester;
28  import com.google.common.collect.testing.Helpers;
29  import com.google.common.collect.testing.features.CollectionSize;
30  import com.google.common.collect.testing.features.MapFeature;
31  import java.lang.reflect.Method;
32  import java.util.Map;
33  import junit.framework.AssertionFailedError;
34  
35  /**
36   * A generic JUnit test which tests {@link Map#merge}. Can't be
37   * invoked directly; please see
38   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
39   *
40   * @author Louis Wasserman
41   */
42  @GwtCompatible(emulated = true)
43  public class MapMergeTester<K, V> extends AbstractMapTester<K, V> {
44    @MapFeature.Require(SUPPORTS_PUT)
45    public void testAbsent() {
46      assertEquals(
47          "Map.merge(absent, value, function) should return value",
48          v3(),
49          getMap()
50              .merge(
51                  k3(),
52                  v3(),
53                  (oldV, newV) -> {
54                    throw new AssertionFailedError(
55                        "Should not call merge function if key was absent");
56                  }));
57      expectAdded(e3());
58    }
59  
60    @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
61    @CollectionSize.Require(absent = ZERO)
62    public void testMappedToNull() {
63      initMapWithNullValue();
64      assertEquals(
65          "Map.merge(keyMappedToNull, value, function) should return value",
66          v3(),
67          getMap()
68              .merge(
69                  getKeyForNullValue(),
70                  v3(),
71                  (oldV, newV) -> {
72                    throw new AssertionFailedError(
73                        "Should not call merge function if key was mapped to null");
74                  }));
75      expectReplacement(entry(getKeyForNullValue(), v3()));
76    }
77  
78    @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
79    public void testMergeAbsentNullKey() {
80      assertEquals(
81          "Map.merge(null, value, function) should return value",
82          v3(),
83          getMap()
84              .merge(
85                  null,
86                  v3(),
87                  (oldV, newV) -> {
88                    throw new AssertionFailedError(
89                        "Should not call merge function if key was absent");
90                  }));
91      expectAdded(entry(null, v3()));
92    }
93  
94    @MapFeature.Require(SUPPORTS_PUT)
95    @CollectionSize.Require(absent = ZERO)
96    public void testMergePresent() {
97      assertEquals(
98          "Map.merge(present, value, function) should return function result",
99          v4(),
100         getMap()
101             .merge(
102                 k0(),
103                 v3(),
104                 (oldV, newV) -> {
105                   assertEquals(v0(), oldV);
106                   assertEquals(v3(), newV);
107                   return v4();
108                 }));
109     expectReplacement(entry(k0(), v4()));
110   }
111 
112   private static class ExpectedException extends RuntimeException {}
113 
114   @MapFeature.Require(SUPPORTS_PUT)
115   @CollectionSize.Require(absent = ZERO)
116   public void testMergeFunctionThrows() {
117     try {
118       getMap()
119           .merge(
120               k0(),
121               v3(),
122               (oldV, newV) -> {
123                 assertEquals(v0(), oldV);
124                 assertEquals(v3(), newV);
125                 throw new ExpectedException();
126               });
127       fail("Expected ExpectedException");
128     } catch (ExpectedException expected) {
129     }
130     expectUnchanged();
131   }
132 
133   @MapFeature.Require(SUPPORTS_REMOVE)
134   @CollectionSize.Require(absent = ZERO)
135   public void testMergePresentToNull() {
136     assertNull(
137         "Map.merge(present, value, functionReturningNull) should return null",
138         getMap()
139             .merge(
140                 k0(),
141                 v3(),
142                 (oldV, newV) -> {
143                   assertEquals(v0(), oldV);
144                   assertEquals(v3(), newV);
145                   return null;
146                 }));
147     expectMissing(e0());
148   }
149 
150   public void testMergeNullValue() {
151     try {
152       getMap()
153           .merge(
154               k0(),
155               null,
156               (oldV, newV) -> {
157                 throw new AssertionFailedError("Should not call merge function if value was null");
158               });
159       fail("Expected NullPointerException or UnsupportedOperationException");
160     } catch (NullPointerException | UnsupportedOperationException expected) {
161     }
162   }
163 
164   public void testMergeNullFunction() {
165     try {
166       getMap().merge(k0(), v3(), null);
167       fail("Expected NullPointerException or UnsupportedOperationException");
168     } catch (NullPointerException | UnsupportedOperationException expected) {
169     }
170   }
171 
172   @MapFeature.Require(absent = SUPPORTS_PUT)
173   public void testMergeUnsupported() {
174     try {
175       getMap()
176           .merge(
177               k3(),
178               v3(),
179               (oldV, newV) -> {
180                 throw new AssertionFailedError();
181               });
182       fail("Expected UnsupportedOperationException");
183     } catch (UnsupportedOperationException expected) {
184     }
185   }
186 
187   /**
188    * Returns the {@link Method} instance for {@link #testMergeNullValue()} so that tests of {@link
189    * Hashtable} can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()}.
190    */
191   @GwtIncompatible // reflection
192   public static Method getMergeNullValueMethod() {
193     return Helpers.getMethod(MapMergeTester.class, "testMergeNullValue");
194   }
195 }