Java
126 строк · 4.3 Кб
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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
23
24import com.google.common.annotations.GwtCompatible;
25import com.google.common.collect.testing.AbstractMapTester;
26import com.google.common.collect.testing.WrongType;
27import com.google.common.collect.testing.features.CollectionSize;
28import com.google.common.collect.testing.features.MapFeature;
29import java.util.Map;
30import 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@SuppressWarnings("JUnit4ClassUsedInJUnit3")
41public class MapGetOrDefaultTester<K, V> extends AbstractMapTester<K, V> {
42@CollectionSize.Require(absent = ZERO)
43public void testGetOrDefault_present() {
44assertEquals(
45"getOrDefault(present, def) should return the associated value",
46v0(),
47getMap().getOrDefault(k0(), v3()));
48}
49
50@CollectionSize.Require(absent = ZERO)
51public void testGetOrDefault_presentNullDefault() {
52assertEquals(
53"getOrDefault(present, null) should return the associated value",
54v0(),
55getMap().getOrDefault(k0(), null));
56}
57
58public void testGetOrDefault_absent() {
59assertEquals(
60"getOrDefault(absent, def) should return the default value",
61v3(),
62getMap().getOrDefault(k3(), v3()));
63}
64
65public void testGetOrDefault_absentNullDefault() {
66assertNull("getOrDefault(absent, null) should return null", getMap().getOrDefault(k3(), null));
67}
68
69@MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
70public void testGetOrDefault_absentNull() {
71assertEquals(
72"getOrDefault(null, def) should return the default value",
73v3(),
74getMap().getOrDefault(null, v3()));
75}
76
77@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
78public void testGetOrDefault_nullAbsentAndUnsupported() {
79try {
80assertEquals(
81"getOrDefault(null, def) should return default or throw",
82v3(),
83getMap().getOrDefault(null, v3()));
84} catch (NullPointerException tolerated) {
85}
86}
87
88@MapFeature.Require(ALLOWS_NULL_KEYS)
89@CollectionSize.Require(absent = ZERO)
90public void testGetOrDefault_nonNullWhenNullContained() {
91initMapWithNullKey();
92assertEquals(
93"getOrDefault(absent, default) should return default",
94v3(),
95getMap().getOrDefault(k3(), v3()));
96}
97
98@MapFeature.Require(ALLOWS_NULL_KEYS)
99@CollectionSize.Require(absent = ZERO)
100public void testGetOrDefault_presentNull() {
101initMapWithNullKey();
102assertEquals(
103"getOrDefault(null, default) should return the associated value",
104getValueForNullKey(),
105getMap().getOrDefault(null, v3()));
106}
107
108@MapFeature.Require(ALLOWS_NULL_VALUES)
109@CollectionSize.Require(absent = ZERO)
110public void testGetOrDefault_presentMappedToNull() {
111initMapWithNullValue();
112assertNull(
113"getOrDefault(mappedToNull, default) should return null",
114getMap().getOrDefault(getKeyForNullValue(), v3()));
115}
116
117public void testGet_wrongType() {
118try {
119assertEquals(
120"getOrDefault(wrongType, default) should return default or throw",
121v3(),
122getMap().getOrDefault(WrongType.VALUE, v3()));
123} catch (ClassCastException tolerated) {
124}
125}
126}
127