Java

Форк
0
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

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
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
41
public class MapGetOrDefaultTester<K, V> extends AbstractMapTester<K, V> {
42
  @CollectionSize.Require(absent = ZERO)
43
  public void testGetOrDefault_present() {
44
    assertEquals(
45
        "getOrDefault(present, def) should return the associated value",
46
        v0(),
47
        getMap().getOrDefault(k0(), v3()));
48
  }
49

50
  @CollectionSize.Require(absent = ZERO)
51
  public void testGetOrDefault_presentNullDefault() {
52
    assertEquals(
53
        "getOrDefault(present, null) should return the associated value",
54
        v0(),
55
        getMap().getOrDefault(k0(), null));
56
  }
57

58
  public void testGetOrDefault_absent() {
59
    assertEquals(
60
        "getOrDefault(absent, def) should return the default value",
61
        v3(),
62
        getMap().getOrDefault(k3(), v3()));
63
  }
64

65
  public void testGetOrDefault_absentNullDefault() {
66
    assertNull("getOrDefault(absent, null) should return null", getMap().getOrDefault(k3(), null));
67
  }
68

69
  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
70
  public void testGetOrDefault_absentNull() {
71
    assertEquals(
72
        "getOrDefault(null, def) should return the default value",
73
        v3(),
74
        getMap().getOrDefault(null, v3()));
75
  }
76

77
  @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
78
  public void testGetOrDefault_nullAbsentAndUnsupported() {
79
    try {
80
      assertEquals(
81
          "getOrDefault(null, def) should return default or throw",
82
          v3(),
83
          getMap().getOrDefault(null, v3()));
84
    } catch (NullPointerException tolerated) {
85
    }
86
  }
87

88
  @MapFeature.Require(ALLOWS_NULL_KEYS)
89
  @CollectionSize.Require(absent = ZERO)
90
  public void testGetOrDefault_nonNullWhenNullContained() {
91
    initMapWithNullKey();
92
    assertEquals(
93
        "getOrDefault(absent, default) should return default",
94
        v3(),
95
        getMap().getOrDefault(k3(), v3()));
96
  }
97

98
  @MapFeature.Require(ALLOWS_NULL_KEYS)
99
  @CollectionSize.Require(absent = ZERO)
100
  public void testGetOrDefault_presentNull() {
101
    initMapWithNullKey();
102
    assertEquals(
103
        "getOrDefault(null, default) should return the associated value",
104
        getValueForNullKey(),
105
        getMap().getOrDefault(null, v3()));
106
  }
107

108
  @MapFeature.Require(ALLOWS_NULL_VALUES)
109
  @CollectionSize.Require(absent = ZERO)
110
  public void testGetOrDefault_presentMappedToNull() {
111
    initMapWithNullValue();
112
    assertNull(
113
        "getOrDefault(mappedToNull, default) should return null",
114
        getMap().getOrDefault(getKeyForNullValue(), v3()));
115
  }
116

117
  public void testGet_wrongType() {
118
    try {
119
      assertEquals(
120
          "getOrDefault(wrongType, default) should return default or throw",
121
          v3(),
122
          getMap().getOrDefault(WrongType.VALUE, v3()));
123
    } catch (ClassCastException tolerated) {
124
    }
125
  }
126
}
127

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.