001/*
002 * Copyright (C) 2015 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 com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.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 junit.framework.AssertionFailedError;
032import org.junit.Ignore;
033
034/**
035 * A generic JUnit test which tests {@link Map#computeIfAbsent}. Can't be invoked directly; please
036 * see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
037 *
038 * @author Louis Wasserman
039 */
040@GwtCompatible
041@Ignore("test runners must not instantiate and run this directly, only via suites we build")
042// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
043@SuppressWarnings("JUnit4ClassUsedInJUnit3")
044public class MapComputeIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
045
046  @MapFeature.Require(SUPPORTS_PUT)
047  public void testComputeIfAbsent_supportedAbsent() {
048    assertEquals(
049        "computeIfAbsent(notPresent, function) should return new value",
050        v3(),
051        getMap()
052            .computeIfAbsent(
053                k3(),
054                k -> {
055                  assertEquals(k3(), k);
056                  return v3();
057                }));
058    expectAdded(e3());
059  }
060
061  @MapFeature.Require(SUPPORTS_PUT)
062  @CollectionSize.Require(absent = ZERO)
063  public void testComputeIfAbsent_supportedPresent() {
064    assertEquals(
065        "computeIfAbsent(present, function) should return existing value",
066        v0(),
067        getMap()
068            .computeIfAbsent(
069                k0(),
070                k -> {
071                  throw new AssertionFailedError();
072                }));
073    expectUnchanged();
074  }
075
076  @MapFeature.Require(SUPPORTS_PUT)
077  public void testComputeIfAbsent_functionReturnsNullNotInserted() {
078    assertNull(
079        "computeIfAbsent(absent, returnsNull) should return null",
080        getMap()
081            .computeIfAbsent(
082                k3(),
083                k -> {
084                  assertEquals(k3(), k);
085                  return null;
086                }));
087    expectUnchanged();
088  }
089
090  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
091  @CollectionSize.Require(absent = ZERO)
092  public void testComputeIfAbsent_nullTreatedAsAbsent() {
093    initMapWithNullValue();
094    assertEquals(
095        "computeIfAbsent(presentAssignedToNull, function) should return newValue",
096        getValueForNullKey(),
097        getMap()
098            .computeIfAbsent(
099                getKeyForNullValue(),
100                k -> {
101                  assertEquals(getKeyForNullValue(), k);
102                  return getValueForNullKey();
103                }));
104    expectReplacement(entry(getKeyForNullValue(), getValueForNullKey()));
105  }
106
107  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
108  public void testComputeIfAbsent_nullKeySupported() {
109    getMap()
110        .computeIfAbsent(
111            null,
112            k -> {
113              assertNull(k);
114              return v3();
115            });
116    expectAdded(entry(null, v3()));
117  }
118
119  @MapFeature.Require(SUPPORTS_PUT)
120  public void testComputeIfAbsent_functionThrows() {
121    assertThrows(
122        SomeUncheckedException.class,
123        () ->
124            getMap()
125                .computeIfAbsent(
126                    k3(),
127                    k -> {
128                      assertEquals(k3(), k);
129                      throw new SomeUncheckedException();
130                    }));
131    expectUnchanged();
132  }
133
134  @MapFeature.Require(absent = SUPPORTS_PUT)
135  public void testComputeIfAbsent_unsupportedAbsent() {
136    assertThrows(
137        UnsupportedOperationException.class,
138        () ->
139            getMap()
140                .computeIfAbsent(
141                    k3(),
142                    k -> {
143                      // allowed to be called
144                      assertEquals(k3(), k);
145                      return v3();
146                    }));
147    expectUnchanged();
148  }
149
150  @MapFeature.Require(absent = SUPPORTS_PUT)
151  @CollectionSize.Require(absent = ZERO)
152  public void testComputeIfAbsent_unsupportedPresentExistingValue() {
153    try {
154      assertEquals(
155          "computeIfAbsent(present, returnsCurrentValue) should return present or throw",
156          v0(),
157          getMap()
158              .computeIfAbsent(
159                  k0(),
160                  k -> {
161                    assertEquals(k0(), k);
162                    return v0();
163                  }));
164    } catch (UnsupportedOperationException tolerated) {
165    }
166    expectUnchanged();
167  }
168
169  @MapFeature.Require(absent = SUPPORTS_PUT)
170  @CollectionSize.Require(absent = ZERO)
171  public void testComputeIfAbsent_unsupportedPresentDifferentValue() {
172    try {
173      assertEquals(
174          "computeIfAbsent(present, returnsDifferentValue) should return present or throw",
175          v0(),
176          getMap()
177              .computeIfAbsent(
178                  k0(),
179                  k -> {
180                    assertEquals(k0(), k);
181                    return v3();
182                  }));
183    } catch (UnsupportedOperationException tolerated) {
184    }
185    expectUnchanged();
186  }
187
188  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
189  public void testComputeIfAbsent_nullKeyUnsupported() {
190    assertThrows(
191        NullPointerException.class,
192        () ->
193            getMap()
194                .computeIfAbsent(
195                    null,
196                    k -> {
197                      assertNull(k);
198                      return v3();
199                    }));
200    expectUnchanged();
201    expectNullKeyMissingWhenNullKeysUnsupported(
202        "Should not contain null key after unsupported computeIfAbsent(null, function)");
203  }
204}