1 /*
2  * Copyright (C) 2015 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect.testing.testers;
18 
19 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
22 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
23 
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.collect.testing.AbstractMapTester;
26 import com.google.common.collect.testing.WrongType;
27 import com.google.common.collect.testing.features.CollectionSize;
28 import com.google.common.collect.testing.features.MapFeature;
29 import java.util.Map;
30 import org.junit.Ignore;
31 
32 /**
33  * A generic JUnit test which tests {@link Map#getOrDefault}. Can't be invoked directly; please see
34  * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
35  *
36  * @author Louis Wasserman
37  */
38 @GwtCompatible
39 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
40 public class MapGetOrDefaultTester<K, V> extends AbstractMapTester<K, V> {
41   @CollectionSize.Require(absent = ZERO)
testGetOrDefault_present()42   public void testGetOrDefault_present() {
43     assertEquals(
44         "getOrDefault(present, def) should return the associated value",
45         v0(),
46         getMap().getOrDefault(k0(), v3()));
47   }
48 
49   @CollectionSize.Require(absent = ZERO)
testGetOrDefault_presentNullDefault()50   public void testGetOrDefault_presentNullDefault() {
51     assertEquals(
52         "getOrDefault(present, null) should return the associated value",
53         v0(),
54         getMap().getOrDefault(k0(), null));
55   }
56 
testGetOrDefault_absent()57   public void testGetOrDefault_absent() {
58     assertEquals(
59         "getOrDefault(absent, def) should return the default value",
60         v3(),
61         getMap().getOrDefault(k3(), v3()));
62   }
63 
testGetOrDefault_absentNullDefault()64   public void testGetOrDefault_absentNullDefault() {
65     assertNull("getOrDefault(absent, null) should return null", getMap().getOrDefault(k3(), null));
66   }
67 
68   @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
testGetOrDefault_absentNull()69   public void testGetOrDefault_absentNull() {
70     assertEquals(
71         "getOrDefault(null, def) should return the default value",
72         v3(),
73         getMap().getOrDefault(null, v3()));
74   }
75 
76   @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
testGetOrDefault_nullAbsentAndUnsupported()77   public void testGetOrDefault_nullAbsentAndUnsupported() {
78     try {
79       assertEquals(
80           "getOrDefault(null, def) should return default or throw",
81           v3(),
82           getMap().getOrDefault(null, v3()));
83     } catch (NullPointerException tolerated) {
84     }
85   }
86 
87   @MapFeature.Require(ALLOWS_NULL_KEYS)
88   @CollectionSize.Require(absent = ZERO)
testGetOrDefault_nonNullWhenNullContained()89   public void testGetOrDefault_nonNullWhenNullContained() {
90     initMapWithNullKey();
91     assertEquals(
92         "getOrDefault(absent, default) should return default",
93         v3(),
94         getMap().getOrDefault(k3(), v3()));
95   }
96 
97   @MapFeature.Require(ALLOWS_NULL_KEYS)
98   @CollectionSize.Require(absent = ZERO)
testGetOrDefault_presentNull()99   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)
testGetOrDefault_presentMappedToNull()109   public void testGetOrDefault_presentMappedToNull() {
110     initMapWithNullValue();
111     assertNull(
112         "getOrDefault(mappedToNull, default) should return null",
113         getMap().getOrDefault(getKeyForNullValue(), v3()));
114   }
115 
testGet_wrongType()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 }
126