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;
030
031/**
032 * A generic JUnit test which tests {@link Map#getOrDefault}. Can't be invoked directly; please see
033 * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
034 *
035 * @author Louis Wasserman
036 */
037@GwtCompatible
038public class MapGetOrDefaultTester<K, V> extends AbstractMapTester<K, V> {
039  @CollectionSize.Require(absent = ZERO)
040  public void testGetOrDefault_present() {
041    assertEquals(
042        "getOrDefault(present, def) should return the associated value",
043        v0(),
044        getMap().getOrDefault(k0(), v3()));
045  }
046
047  @CollectionSize.Require(absent = ZERO)
048  public void testGetOrDefault_presentNullDefault() {
049    assertEquals(
050        "getOrDefault(present, null) should return the associated value",
051        v0(),
052        getMap().getOrDefault(k0(), null));
053  }
054
055  public void testGetOrDefault_absent() {
056    assertEquals(
057        "getOrDefault(absent, def) should return the default value",
058        v3(),
059        getMap().getOrDefault(k3(), v3()));
060  }
061
062  public void testGetOrDefault_absentNullDefault() {
063    assertNull("getOrDefault(absent, null) should return null", getMap().getOrDefault(k3(), null));
064  }
065
066  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
067  public void testGetOrDefault_absentNull() {
068    assertEquals(
069        "getOrDefault(null, def) should return the default value",
070        v3(),
071        getMap().getOrDefault(null, v3()));
072  }
073
074  @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
075  public void testGetOrDefault_nullAbsentAndUnsupported() {
076    try {
077      assertEquals(
078          "getOrDefault(null, def) should return default or throw",
079          v3(),
080          getMap().getOrDefault(null, v3()));
081    } catch (NullPointerException tolerated) {
082    }
083  }
084
085  @MapFeature.Require(ALLOWS_NULL_KEYS)
086  @CollectionSize.Require(absent = ZERO)
087  public void testGetOrDefault_nonNullWhenNullContained() {
088    initMapWithNullKey();
089    assertEquals(
090        "getOrDefault(absent, default) should return default",
091        v3(),
092        getMap().getOrDefault(k3(), v3()));
093  }
094
095  @MapFeature.Require(ALLOWS_NULL_KEYS)
096  @CollectionSize.Require(absent = ZERO)
097  public void testGetOrDefault_presentNull() {
098    initMapWithNullKey();
099    assertEquals(
100        "getOrDefault(null, default) should return the associated value",
101        getValueForNullKey(),
102        getMap().getOrDefault(null, v3()));
103  }
104
105  @MapFeature.Require(ALLOWS_NULL_VALUES)
106  @CollectionSize.Require(absent = ZERO)
107  public void testGetOrDefault_presentMappedToNull() {
108    initMapWithNullValue();
109    assertNull(
110        "getOrDefault(mappedToNull, default) should return null",
111        getMap().getOrDefault(getKeyForNullValue(), v3()));
112  }
113
114  public void testGet_wrongType() {
115    try {
116      assertEquals(
117          "getOrDefault(wrongType, default) should return default or throw",
118          v3(),
119          getMap().getOrDefault(WrongType.VALUE, v3()));
120    } catch (ClassCastException tolerated) {
121    }
122  }
123}