Java

Форк
0
/
MapComputeIfPresentTester.java 
183 строки · 5.9 Кб
1
/*
2
 * Copyright (C) 2016 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 junit.framework.AssertionFailedError;
31
import org.junit.Ignore;
32

33
/**
34
 * A generic JUnit test which tests {@link Map#computeIfPresent}. Can't be invoked directly; please
35
 * see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
36
 *
37
 * @author Louis Wasserman
38
 */
39
@GwtCompatible
40
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
41
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
42
public class MapComputeIfPresentTester<K, V> extends AbstractMapTester<K, V> {
43

44
  @MapFeature.Require(SUPPORTS_PUT)
45
  public void testComputeIfPresent_supportedAbsent() {
46
    assertNull(
47
        "computeIfPresent(notPresent, function) should return null",
48
        getMap()
49
            .computeIfPresent(
50
                k3(),
51
                (k, v) -> {
52
                  throw new AssertionFailedError();
53
                }));
54
    expectUnchanged();
55
  }
56

57
  @MapFeature.Require(SUPPORTS_PUT)
58
  @CollectionSize.Require(absent = ZERO)
59
  public void testComputeIfPresent_supportedPresent() {
60
    assertEquals(
61
        "computeIfPresent(present, function) should return new value",
62
        v3(),
63
        getMap()
64
            .computeIfPresent(
65
                k0(),
66
                (k, v) -> {
67
                  assertEquals(k0(), k);
68
                  assertEquals(v0(), v);
69
                  return v3();
70
                }));
71
    expectReplacement(entry(k0(), v3()));
72
  }
73

74
  @MapFeature.Require(SUPPORTS_PUT)
75
  @CollectionSize.Require(absent = ZERO)
76
  public void testComputeIfPresent_functionReturnsNull() {
77
    assertNull(
78
        "computeIfPresent(present, returnsNull) should return null",
79
        getMap()
80
            .computeIfPresent(
81
                k0(),
82
                (k, v) -> {
83
                  assertEquals(k0(), k);
84
                  assertEquals(v0(), v);
85
                  return null;
86
                }));
87
    expectMissing(e0());
88
  }
89

90
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
91
  @CollectionSize.Require(absent = ZERO)
92
  public void testComputeIfPresent_nullTreatedAsAbsent() {
93
    initMapWithNullValue();
94
    assertNull(
95
        "computeIfPresent(presentAssignedToNull, function) should return null",
96
        getMap()
97
            .computeIfPresent(
98
                getKeyForNullValue(),
99
                (k, v) -> {
100
                  throw new AssertionFailedError();
101
                }));
102
    expectReplacement(entry(getKeyForNullValue(), null));
103
  }
104

105
  static class ExpectedException extends RuntimeException {}
106

107
  @MapFeature.Require(SUPPORTS_PUT)
108
  @CollectionSize.Require(absent = ZERO)
109
  public void testComputeIfPresent_functionThrows() {
110
    try {
111
      getMap()
112
          .computeIfPresent(
113
              k0(),
114
              (k, v) -> {
115
                assertEquals(k0(), k);
116
                assertEquals(v0(), v);
117
                throw new ExpectedException();
118
              });
119
      fail("Expected ExpectedException");
120
    } catch (ExpectedException expected) {
121
    }
122
    expectUnchanged();
123
  }
124

125
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
126
  @CollectionSize.Require(absent = ZERO)
127
  public void testComputeIfPresent_nullKeySupportedPresent() {
128
    initMapWithNullKey();
129
    assertEquals(
130
        "computeIfPresent(null, function) should return new value",
131
        v3(),
132
        getMap()
133
            .computeIfPresent(
134
                null,
135
                (k, v) -> {
136
                  assertNull(k);
137
                  assertEquals(getValueForNullKey(), v);
138
                  return v3();
139
                }));
140

141
    Entry<K, V>[] expected = createArrayWithNullKey();
142
    expected[getNullLocation()] = entry(null, v3());
143
    expectContents(expected);
144
  }
145

146
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
147
  public void testComputeIfPresent_nullKeySupportedAbsent() {
148
    assertNull(
149
        "computeIfPresent(null, function) should return null",
150
        getMap()
151
            .computeIfPresent(
152
                null,
153
                (k, v) -> {
154
                  throw new AssertionFailedError();
155
                }));
156
    expectUnchanged();
157
  }
158

159
  @MapFeature.Require(absent = SUPPORTS_PUT)
160
  public void testComputeIfPresent_unsupportedAbsent() {
161
    try {
162
      getMap()
163
          .computeIfPresent(
164
              k3(),
165
              (k, v) -> {
166
                throw new AssertionFailedError();
167
              });
168
    } catch (UnsupportedOperationException tolerated) {
169
    }
170
    expectUnchanged();
171
  }
172

173
  @MapFeature.Require(absent = SUPPORTS_PUT)
174
  @CollectionSize.Require(absent = ZERO)
175
  public void testComputeIfPresent_unsupportedPresent() {
176
    try {
177
      getMap().computeIfPresent(k0(), (k, v) -> v3());
178
      fail("Expected UnsupportedOperationException");
179
    } catch (UnsupportedOperationException expected) {
180
    }
181
    expectUnchanged();
182
  }
183
}
184

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

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

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

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