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;
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 org.junit.Ignore;
030
031/**
032 * A generic JUnit test which tests {@link Map#putIfAbsent}. Can't be invoked directly; please see
033 * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
034 *
035 * @author Louis Wasserman
036 */
037@GwtCompatible
038@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
039public class MapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
040
041  @MapFeature.Require(SUPPORTS_PUT)
042  public void testPutIfAbsent_supportedAbsent() {
043    assertNull(
044        "putIfAbsent(notPresent, value) should return null", getMap().putIfAbsent(k3(), v3()));
045    expectAdded(e3());
046  }
047
048  @MapFeature.Require(SUPPORTS_PUT)
049  @CollectionSize.Require(absent = ZERO)
050  public void testPutIfAbsent_supportedPresent() {
051    assertEquals(
052        "putIfAbsent(present, value) should return existing value",
053        v0(),
054        getMap().putIfAbsent(k0(), v3()));
055    expectUnchanged();
056  }
057
058  @MapFeature.Require(absent = SUPPORTS_PUT)
059  public void testPutIfAbsent_unsupportedAbsent() {
060    try {
061      getMap().putIfAbsent(k3(), v3());
062      fail("putIfAbsent(notPresent, value) should throw");
063    } catch (UnsupportedOperationException expected) {
064    }
065    expectUnchanged();
066    expectMissing(e3());
067  }
068
069  @MapFeature.Require(absent = SUPPORTS_PUT)
070  @CollectionSize.Require(absent = ZERO)
071  public void testPutIfAbsent_unsupportedPresentExistingValue() {
072    try {
073      assertEquals(
074          "putIfAbsent(present, existingValue) should return present or throw",
075          v0(),
076          getMap().putIfAbsent(k0(), v0()));
077    } catch (UnsupportedOperationException tolerated) {
078    }
079    expectUnchanged();
080  }
081
082  @MapFeature.Require(absent = SUPPORTS_PUT)
083  @CollectionSize.Require(absent = ZERO)
084  public void testPutIfAbsent_unsupportedPresentDifferentValue() {
085    try {
086      getMap().putIfAbsent(k0(), v3());
087    } catch (UnsupportedOperationException tolerated) {
088    }
089    expectUnchanged();
090  }
091
092  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
093  public void testPutIfAbsent_nullKeyUnsupported() {
094    try {
095      getMap().putIfAbsent(null, v3());
096      fail("putIfAbsent(null, value) should throw");
097    } catch (NullPointerException expected) {
098    }
099    expectUnchanged();
100    expectNullKeyMissingWhenNullKeysUnsupported(
101        "Should not contain null key after unsupported putIfAbsent(null, value)");
102  }
103
104  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
105  public void testPutIfAbsent_nullValueUnsupported() {
106    try {
107      getMap().putIfAbsent(k3(), null);
108      fail("putIfAbsent(key, null) should throw");
109    } catch (NullPointerException expected) {
110    }
111    expectUnchanged();
112    expectNullValueMissingWhenNullValuesUnsupported(
113        "Should not contain null value after unsupported put(key, null)");
114  }
115
116  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
117  @CollectionSize.Require(absent = ZERO)
118  public void testPutIfAbsent_putWithNullValueUnsupported() {
119    try {
120      getMap().putIfAbsent(k0(), null);
121    } catch (NullPointerException tolerated) {
122    }
123    expectUnchanged();
124    expectNullValueMissingWhenNullValuesUnsupported(
125        "Should not contain null after unsupported putIfAbsent(present, null)");
126  }
127}