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_KEY_QUERIES;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.testing.AbstractMapTester;
026import com.google.common.collect.testing.WrongType;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.common.collect.testing.features.MapFeature;
029import java.util.Map;
030import org.junit.Ignore;
031
032/**
033 * A generic JUnit test which tests {@link Map#getOrDefault}. Can't be invoked directly; please see
034 * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
035 *
036 * @author Louis Wasserman
037 */
038@GwtCompatible
039@Ignore("test runners must not instantiate and run this directly, only via suites we build")
040// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041@SuppressWarnings("JUnit4ClassUsedInJUnit3")
042@IgnoreJRERequirement // We opt into library desugaring for our tests.
043public class MapGetOrDefaultTester<K, V> extends AbstractMapTester<K, V> {
044  @CollectionSize.Require(absent = ZERO)
045  public void testGetOrDefault_present() {
046    assertEquals(
047        "getOrDefault(present, def) should return the associated value",
048        v0(),
049        getMap().getOrDefault(k0(), v3()));
050  }
051
052  @CollectionSize.Require(absent = ZERO)
053  public void testGetOrDefault_presentNullDefault() {
054    assertEquals(
055        "getOrDefault(present, null) should return the associated value",
056        v0(),
057        getMap().getOrDefault(k0(), null));
058  }
059
060  public void testGetOrDefault_absent() {
061    assertEquals(
062        "getOrDefault(absent, def) should return the default value",
063        v3(),
064        getMap().getOrDefault(k3(), v3()));
065  }
066
067  public void testGetOrDefault_absentNullDefault() {
068    assertNull("getOrDefault(absent, null) should return null", getMap().getOrDefault(k3(), null));
069  }
070
071  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
072  public void testGetOrDefault_absentNull() {
073    assertEquals(
074        "getOrDefault(null, def) should return the default value",
075        v3(),
076        getMap().getOrDefault(null, v3()));
077  }
078
079  @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
080  public void testGetOrDefault_nullAbsentAndUnsupported() {
081    try {
082      assertEquals(
083          "getOrDefault(null, def) should return default or throw",
084          v3(),
085          getMap().getOrDefault(null, v3()));
086    } catch (NullPointerException tolerated) {
087    }
088  }
089
090  @MapFeature.Require(ALLOWS_NULL_KEYS)
091  @CollectionSize.Require(absent = ZERO)
092  public void testGetOrDefault_nonNullWhenNullContained() {
093    initMapWithNullKey();
094    assertEquals(
095        "getOrDefault(absent, default) should return default",
096        v3(),
097        getMap().getOrDefault(k3(), v3()));
098  }
099
100  @MapFeature.Require(ALLOWS_NULL_KEYS)
101  @CollectionSize.Require(absent = ZERO)
102  public void testGetOrDefault_presentNull() {
103    initMapWithNullKey();
104    assertEquals(
105        "getOrDefault(null, default) should return the associated value",
106        getValueForNullKey(),
107        getMap().getOrDefault(null, v3()));
108  }
109
110  @MapFeature.Require(ALLOWS_NULL_VALUES)
111  @CollectionSize.Require(absent = ZERO)
112  public void testGetOrDefault_presentMappedToNull() {
113    initMapWithNullValue();
114    assertNull(
115        "getOrDefault(mappedToNull, default) should return null",
116        getMap().getOrDefault(getKeyForNullValue(), v3()));
117  }
118
119  public void testGet_wrongType() {
120    try {
121      assertEquals(
122          "getOrDefault(wrongType, default) should return default or throw",
123          v3(),
124          getMap().getOrDefault(WrongType.VALUE, v3()));
125    } catch (ClassCastException tolerated) {
126    }
127  }
128}