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