Java

Форк
0
266 строк · 9.4 Кб
1
/*
2
 * Copyright (C) 2007 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.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
23
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
24

25
import com.google.common.annotations.GwtCompatible;
26
import com.google.common.annotations.GwtIncompatible;
27
import com.google.common.annotations.J2ktIncompatible;
28
import com.google.common.collect.testing.AbstractMapTester;
29
import com.google.common.collect.testing.Helpers;
30
import com.google.common.collect.testing.features.CollectionSize;
31
import com.google.common.collect.testing.features.MapFeature;
32
import com.google.errorprone.annotations.CanIgnoreReturnValue;
33
import java.lang.reflect.Method;
34
import java.util.ConcurrentModificationException;
35
import java.util.Iterator;
36
import java.util.Map.Entry;
37
import org.junit.Ignore;
38

39
/**
40
 * A generic JUnit test which tests {@code put} operations on a map. Can't be invoked directly;
41
 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
42
 *
43
 * @author Chris Povirk
44
 * @author Kevin Bourrillion
45
 */
46
@GwtCompatible(emulated = true)
47
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
48
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
49
public class MapPutTester<K, V> extends AbstractMapTester<K, V> {
50
  private Entry<K, V> nullKeyEntry;
51
  private Entry<K, V> nullValueEntry;
52
  private Entry<K, V> nullKeyValueEntry;
53
  private Entry<K, V> presentKeyNullValueEntry;
54

55
  @Override
56
  public void setUp() throws Exception {
57
    super.setUp();
58
    nullKeyEntry = entry(null, v3());
59
    nullValueEntry = entry(k3(), null);
60
    nullKeyValueEntry = entry(null, null);
61
    presentKeyNullValueEntry = entry(k0(), null);
62
  }
63

64
  @MapFeature.Require(SUPPORTS_PUT)
65
  @CollectionSize.Require(absent = ZERO)
66
  public void testPut_supportedPresent() {
67
    assertEquals("put(present, value) should return the old value", v0(), getMap().put(k0(), v3()));
68
    expectReplacement(entry(k0(), v3()));
69
  }
70

71
  @MapFeature.Require(SUPPORTS_PUT)
72
  public void testPut_supportedNotPresent() {
73
    assertNull("put(notPresent, value) should return null", put(e3()));
74
    expectAdded(e3());
75
  }
76

77
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
78
  @CollectionSize.Require(absent = ZERO)
79
  public void testPutAbsentConcurrentWithEntrySetIteration() {
80
    try {
81
      Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
82
      put(e3());
83
      iterator.next();
84
      fail("Expected ConcurrentModificationException");
85
    } catch (ConcurrentModificationException expected) {
86
      // success
87
    }
88
  }
89

90
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
91
  @CollectionSize.Require(absent = ZERO)
92
  public void testPutAbsentConcurrentWithKeySetIteration() {
93
    try {
94
      Iterator<K> iterator = getMap().keySet().iterator();
95
      put(e3());
96
      iterator.next();
97
      fail("Expected ConcurrentModificationException");
98
    } catch (ConcurrentModificationException expected) {
99
      // success
100
    }
101
  }
102

103
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
104
  @CollectionSize.Require(absent = ZERO)
105
  public void testPutAbsentConcurrentWithValueIteration() {
106
    try {
107
      Iterator<V> iterator = getMap().values().iterator();
108
      put(e3());
109
      iterator.next();
110
      fail("Expected ConcurrentModificationException");
111
    } catch (ConcurrentModificationException expected) {
112
      // success
113
    }
114
  }
115

116
  @MapFeature.Require(absent = SUPPORTS_PUT)
117
  public void testPut_unsupportedNotPresent() {
118
    try {
119
      put(e3());
120
      fail("put(notPresent, value) should throw");
121
    } catch (UnsupportedOperationException expected) {
122
    }
123
    expectUnchanged();
124
    expectMissing(e3());
125
  }
126

127
  @MapFeature.Require(absent = SUPPORTS_PUT)
128
  @CollectionSize.Require(absent = ZERO)
129
  public void testPut_unsupportedPresentExistingValue() {
130
    try {
131
      assertEquals("put(present, existingValue) should return present or throw", v0(), put(e0()));
132
    } catch (UnsupportedOperationException tolerated) {
133
    }
134
    expectUnchanged();
135
  }
136

137
  @MapFeature.Require(absent = SUPPORTS_PUT)
138
  @CollectionSize.Require(absent = ZERO)
139
  public void testPut_unsupportedPresentDifferentValue() {
140
    try {
141
      getMap().put(k0(), v3());
142
      fail("put(present, differentValue) should throw");
143
    } catch (UnsupportedOperationException expected) {
144
    }
145
    expectUnchanged();
146
  }
147

148
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
149
  public void testPut_nullKeySupportedNotPresent() {
150
    assertNull("put(null, value) should return null", put(nullKeyEntry));
151
    expectAdded(nullKeyEntry);
152
  }
153

154
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
155
  @CollectionSize.Require(absent = ZERO)
156
  public void testPut_nullKeySupportedPresent() {
157
    Entry<K, V> newEntry = entry(null, v3());
158
    initMapWithNullKey();
159
    assertEquals(
160
        "put(present, value) should return the associated value",
161
        getValueForNullKey(),
162
        put(newEntry));
163

164
    Entry<K, V>[] expected = createArrayWithNullKey();
165
    expected[getNullLocation()] = newEntry;
166
    expectContents(expected);
167
  }
168

169
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
170
  public void testPut_nullKeyUnsupported() {
171
    try {
172
      put(nullKeyEntry);
173
      fail("put(null, value) should throw");
174
    } catch (NullPointerException expected) {
175
    }
176
    expectUnchanged();
177
    expectNullKeyMissingWhenNullKeysUnsupported(
178
        "Should not contain null key after unsupported put(null, value)");
179
  }
180

181
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
182
  public void testPut_nullValueSupported() {
183
    assertNull("put(key, null) should return null", put(nullValueEntry));
184
    expectAdded(nullValueEntry);
185
  }
186

187
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
188
  public void testPut_nullValueUnsupported() {
189
    try {
190
      put(nullValueEntry);
191
      fail("put(key, null) should throw");
192
    } catch (NullPointerException expected) {
193
    }
194
    expectUnchanged();
195
    expectNullValueMissingWhenNullValuesUnsupported(
196
        "Should not contain null value after unsupported put(key, null)");
197
  }
198

199
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
200
  @CollectionSize.Require(absent = ZERO)
201
  public void testPut_replaceWithNullValueSupported() {
202
    assertEquals(
203
        "put(present, null) should return the associated value",
204
        v0(),
205
        put(presentKeyNullValueEntry));
206
    expectReplacement(presentKeyNullValueEntry);
207
  }
208

209
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
210
  @CollectionSize.Require(absent = ZERO)
211
  public void testPut_replaceWithNullValueUnsupported() {
212
    try {
213
      put(presentKeyNullValueEntry);
214
      fail("put(present, null) should throw");
215
    } catch (NullPointerException expected) {
216
    }
217
    expectUnchanged();
218
    expectNullValueMissingWhenNullValuesUnsupported(
219
        "Should not contain null after unsupported put(present, null)");
220
  }
221

222
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
223
  @CollectionSize.Require(absent = ZERO)
224
  public void testPut_replaceNullValueWithNullSupported() {
225
    initMapWithNullValue();
226
    assertNull(
227
        "put(present, null) should return the associated value (null)",
228
        getMap().put(getKeyForNullValue(), null));
229
    expectContents(createArrayWithNullValue());
230
  }
231

232
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
233
  @CollectionSize.Require(absent = ZERO)
234
  public void testPut_replaceNullValueWithNonNullSupported() {
235
    Entry<K, V> newEntry = entry(getKeyForNullValue(), v3());
236
    initMapWithNullValue();
237
    assertNull("put(present, value) should return the associated value (null)", put(newEntry));
238

239
    Entry<K, V>[] expected = createArrayWithNullValue();
240
    expected[getNullLocation()] = newEntry;
241
    expectContents(expected);
242
  }
243

244
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
245
  public void testPut_nullKeyAndValueSupported() {
246
    assertNull("put(null, null) should return null", put(nullKeyValueEntry));
247
    expectAdded(nullKeyValueEntry);
248
  }
249

250
  @CanIgnoreReturnValue
251
  private V put(Entry<K, V> entry) {
252
    return getMap().put(entry.getKey(), entry.getValue());
253
  }
254

255
  /**
256
   * Returns the {@link Method} instance for {@link #testPut_nullKeyUnsupported()} so that tests of
257
   * {@link java.util.TreeMap} can suppress it with {@code
258
   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
259
   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
260
   */
261
  @J2ktIncompatible
262
  @GwtIncompatible // reflection
263
  public static Method getPutNullKeyUnsupportedMethod() {
264
    return Helpers.getMethod(MapPutTester.class, "testPut_nullKeyUnsupported");
265
  }
266
}
267

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

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

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

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