Java

Форк
0
138 строк · 4.9 Кб
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_VALUES;
22
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
23

24
import com.google.common.annotations.GwtCompatible;
25
import com.google.common.collect.testing.AbstractMapTester;
26
import com.google.common.collect.testing.features.CollectionSize;
27
import com.google.common.collect.testing.features.MapFeature;
28
import java.util.Map;
29
import java.util.Map.Entry;
30
import org.junit.Ignore;
31

32
/**
33
 * A generic JUnit test which tests {@link Map#putIfAbsent}. 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 MapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
42

43
  @MapFeature.Require(SUPPORTS_PUT)
44
  public void testPutIfAbsent_supportedAbsent() {
45
    assertNull(
46
        "putIfAbsent(notPresent, value) should return null", getMap().putIfAbsent(k3(), v3()));
47
    expectAdded(e3());
48
  }
49

50
  @MapFeature.Require(SUPPORTS_PUT)
51
  @CollectionSize.Require(absent = ZERO)
52
  public void testPutIfAbsent_supportedPresent() {
53
    assertEquals(
54
        "putIfAbsent(present, value) should return existing value",
55
        v0(),
56
        getMap().putIfAbsent(k0(), v3()));
57
    expectUnchanged();
58
  }
59

60
  @MapFeature.Require(absent = SUPPORTS_PUT)
61
  public void testPutIfAbsent_unsupportedAbsent() {
62
    try {
63
      getMap().putIfAbsent(k3(), v3());
64
      fail("putIfAbsent(notPresent, value) should throw");
65
    } catch (UnsupportedOperationException expected) {
66
    }
67
    expectUnchanged();
68
    expectMissing(e3());
69
  }
70

71
  @MapFeature.Require(absent = SUPPORTS_PUT)
72
  @CollectionSize.Require(absent = ZERO)
73
  public void testPutIfAbsent_unsupportedPresentExistingValue() {
74
    try {
75
      assertEquals(
76
          "putIfAbsent(present, existingValue) should return present or throw",
77
          v0(),
78
          getMap().putIfAbsent(k0(), v0()));
79
    } catch (UnsupportedOperationException tolerated) {
80
    }
81
    expectUnchanged();
82
  }
83

84
  @MapFeature.Require(absent = SUPPORTS_PUT)
85
  @CollectionSize.Require(absent = ZERO)
86
  public void testPutIfAbsent_unsupportedPresentDifferentValue() {
87
    try {
88
      getMap().putIfAbsent(k0(), v3());
89
    } catch (UnsupportedOperationException tolerated) {
90
    }
91
    expectUnchanged();
92
  }
93

94
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
95
  public void testPutIfAbsent_nullKeyUnsupported() {
96
    try {
97
      getMap().putIfAbsent(null, v3());
98
      fail("putIfAbsent(null, value) should throw");
99
    } catch (NullPointerException expected) {
100
    }
101
    expectUnchanged();
102
    expectNullKeyMissingWhenNullKeysUnsupported(
103
        "Should not contain null key after unsupported putIfAbsent(null, value)");
104
  }
105

106
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
107
  public void testPutIfAbsent_nullValueUnsupportedAndKeyAbsent() {
108
    try {
109
      getMap().putIfAbsent(k3(), null);
110
      fail("putIfAbsent(key, null) should throw");
111
    } catch (NullPointerException expected) {
112
    }
113
    expectUnchanged();
114
    expectNullValueMissingWhenNullValuesUnsupported(
115
        "Should not contain null value after unsupported putIfAbsent(key, null)");
116
  }
117

118
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
119
  @CollectionSize.Require(absent = ZERO)
120
  public void testPutIfAbsent_nullValueUnsupportedAndKeyPresent() {
121
    try {
122
      getMap().putIfAbsent(k0(), null);
123
    } catch (NullPointerException tolerated) {
124
    }
125
    expectUnchanged();
126
    expectNullValueMissingWhenNullValuesUnsupported(
127
        "Should not contain null after unsupported putIfAbsent(present, null)");
128
  }
129

130
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
131
  public void testPut_nullValueSupported() {
132
    Entry<K, V> nullValueEntry = entry(k3(), null);
133
    assertNull(
134
        "putIfAbsent(key, null) should return null",
135
        getMap().putIfAbsent(nullValueEntry.getKey(), nullValueEntry.getValue()));
136
    expectAdded(nullValueEntry);
137
  }
138
}
139

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

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

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

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