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