View Javadoc
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  
31  /**
32   * A generic JUnit test which tests {@link Map#getOrDefault}. Can't be
33   * invoked directly; please see
34   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
35   *
36   * @author Louis Wasserman
37   */
38  @GwtCompatible
39  public class MapGetOrDefaultTester<K, V> extends AbstractMapTester<K, V> {
40    @CollectionSize.Require(absent = ZERO)
41    public void testGetOrDefault_present() {
42      assertEquals("getOrDefault(present, def) should return the associated value",
43          v0(), getMap().getOrDefault(k0(), v3()));
44    }
45    
46    @CollectionSize.Require(absent = ZERO)
47    public void testGetOrDefault_presentNullDefault() {
48      assertEquals("getOrDefault(present, null) should return the associated value",
49          v0(), getMap().getOrDefault(k0(), null));
50    }
51  
52    public void testGetOrDefault_absent() {
53      assertEquals("getOrDefault(absent, def) should return the default value",
54          v3(), getMap().getOrDefault(k3(), v3()));
55    }
56  
57    public void testGetOrDefault_absentNullDefault() {
58      assertNull("getOrDefault(absent, null) should return null",
59          getMap().getOrDefault(k3(), null));
60    }
61  
62    @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
63    public void testGetOrDefault_absentNull() {
64      assertEquals("getOrDefault(null, def) should return the default value",
65          v3(), getMap().getOrDefault(null, v3()));
66    }
67  
68    @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
69    public void testGetOrDefault_nullAbsentAndUnsupported() {
70      try {
71        assertEquals("getOrDefault(null, def) should return default or throw",
72            v3(), getMap().getOrDefault(null, v3()));
73      } catch (NullPointerException tolerated) {
74      }
75    }
76  
77    @MapFeature.Require(ALLOWS_NULL_KEYS)
78    @CollectionSize.Require(absent = ZERO)
79    public void testGetOrDefault_nonNullWhenNullContained() {
80      initMapWithNullKey();
81      assertEquals("getOrDefault(absent, default) should return default", 
82          v3(), getMap().getOrDefault(k3(), v3()));
83    }
84  
85    @MapFeature.Require(ALLOWS_NULL_KEYS)
86    @CollectionSize.Require(absent = ZERO)
87    public void testGetOrDefault_presentNull() {
88      initMapWithNullKey();
89      assertEquals("getOrDefault(null, default) should return the associated value",
90          getValueForNullKey(), getMap().getOrDefault(null, v3()));
91    }
92    
93    @MapFeature.Require(ALLOWS_NULL_VALUES)
94    @CollectionSize.Require(absent = ZERO)
95    public void testGetOrDefault_presentMappedToNull() {
96      initMapWithNullValue();
97      assertNull("getOrDefault(mappedToNull, default) should return null",
98          getMap().getOrDefault(getKeyForNullValue(), v3()));
99    }
100 
101   public void testGet_wrongType() {
102     try {
103       assertEquals("getOrDefault(wrongType, default) should return default or throw",
104           v3(), getMap().getOrDefault(WrongType.VALUE, v3()));
105     } catch (ClassCastException tolerated) {
106     }
107   }
108 }