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