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 // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 040@SuppressWarnings("JUnit4ClassUsedInJUnit3") 041public class MapGetOrDefaultTester<K, V> extends AbstractMapTester<K, V> { 042 @CollectionSize.Require(absent = ZERO) 043 public void testGetOrDefault_present() { 044 assertEquals( 045 "getOrDefault(present, def) should return the associated value", 046 v0(), 047 getMap().getOrDefault(k0(), v3())); 048 } 049 050 @CollectionSize.Require(absent = ZERO) 051 public void testGetOrDefault_presentNullDefault() { 052 assertEquals( 053 "getOrDefault(present, null) should return the associated value", 054 v0(), 055 getMap().getOrDefault(k0(), null)); 056 } 057 058 public void testGetOrDefault_absent() { 059 assertEquals( 060 "getOrDefault(absent, def) should return the default value", 061 v3(), 062 getMap().getOrDefault(k3(), v3())); 063 } 064 065 public void testGetOrDefault_absentNullDefault() { 066 assertNull("getOrDefault(absent, null) should return null", getMap().getOrDefault(k3(), null)); 067 } 068 069 @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES) 070 public void testGetOrDefault_absentNull() { 071 assertEquals( 072 "getOrDefault(null, def) should return the default value", 073 v3(), 074 getMap().getOrDefault(null, v3())); 075 } 076 077 @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES) 078 public void testGetOrDefault_nullAbsentAndUnsupported() { 079 try { 080 assertEquals( 081 "getOrDefault(null, def) should return default or throw", 082 v3(), 083 getMap().getOrDefault(null, v3())); 084 } catch (NullPointerException tolerated) { 085 } 086 } 087 088 @MapFeature.Require(ALLOWS_NULL_KEYS) 089 @CollectionSize.Require(absent = ZERO) 090 public void testGetOrDefault_nonNullWhenNullContained() { 091 initMapWithNullKey(); 092 assertEquals( 093 "getOrDefault(absent, default) should return default", 094 v3(), 095 getMap().getOrDefault(k3(), v3())); 096 } 097 098 @MapFeature.Require(ALLOWS_NULL_KEYS) 099 @CollectionSize.Require(absent = ZERO) 100 public void testGetOrDefault_presentNull() { 101 initMapWithNullKey(); 102 assertEquals( 103 "getOrDefault(null, default) should return the associated value", 104 getValueForNullKey(), 105 getMap().getOrDefault(null, v3())); 106 } 107 108 @MapFeature.Require(ALLOWS_NULL_VALUES) 109 @CollectionSize.Require(absent = ZERO) 110 public void testGetOrDefault_presentMappedToNull() { 111 initMapWithNullValue(); 112 assertNull( 113 "getOrDefault(mappedToNull, default) should return null", 114 getMap().getOrDefault(getKeyForNullValue(), v3())); 115 } 116 117 public void testGet_wrongType() { 118 try { 119 assertEquals( 120 "getOrDefault(wrongType, default) should return default or throw", 121 v3(), 122 getMap().getOrDefault(WrongType.VALUE, v3())); 123 } catch (ClassCastException tolerated) { 124 } 125 } 126}