Java

Форк
0
/
ConcurrentMapReplaceTester.java 
110 строк · 3.8 Кб
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_KEY_QUERIES;
21
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
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.collect.testing.AbstractMapTester;
27
import com.google.common.collect.testing.features.CollectionSize;
28
import com.google.common.collect.testing.features.MapFeature;
29
import java.util.concurrent.ConcurrentMap;
30
import org.junit.Ignore;
31

32
/**
33
 * A generic JUnit test which tests {@code replace(K, V)} operations on a concurrent map. Can't be
34
 * invoked directly; please see {@link
35
 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
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
@ElementTypesAreNonnullByDefault
43
public class ConcurrentMapReplaceTester<K, V> extends AbstractMapTester<K, V> {
44
  @Override
45
  protected ConcurrentMap<K, V> getMap() {
46
    return (ConcurrentMap<K, V>) super.getMap();
47
  }
48

49
  @MapFeature.Require(SUPPORTS_PUT)
50
  @CollectionSize.Require(absent = ZERO)
51
  public void testReplace_supportedPresent() {
52
    assertEquals(v0(), getMap().replace(k0(), v3()));
53
    expectReplacement(entry(k0(), v3()));
54
  }
55

56
  @MapFeature.Require(SUPPORTS_PUT)
57
  @CollectionSize.Require(absent = ZERO)
58
  public void testReplace_supportedPresentNoChange() {
59
    assertEquals(v0(), getMap().replace(k0(), v0()));
60
    expectUnchanged();
61
  }
62

63
  @MapFeature.Require(SUPPORTS_PUT)
64
  public void testReplace_supportedAbsent() {
65
    assertNull(getMap().replace(k3(), v3()));
66
    expectUnchanged();
67
  }
68

69
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
70
  @CollectionSize.Require(absent = ZERO)
71
  public void testReplace_presentNullValueUnsupported() {
72
    try {
73
      getMap().replace(k0(), null);
74
      fail("Expected NullPointerException");
75
    } catch (NullPointerException expected) {
76
    }
77
    expectUnchanged();
78
  }
79

80
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
81
  public void testReplace_absentNullValueUnsupported() {
82
    try {
83
      getMap().replace(k3(), null);
84
    } catch (NullPointerException tolerated) {
85
      // permitted not to throw because it would be a no-op
86
    }
87
    expectUnchanged();
88
  }
89

90
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)
91
  public void testReplace_absentNullKeyUnsupported() {
92
    try {
93
      getMap().replace(null, v3());
94
    } catch (NullPointerException tolerated) {
95
      // permitted not to throw because it would be a no-op
96
    }
97
    expectUnchanged();
98
  }
99

100
  @MapFeature.Require(absent = SUPPORTS_PUT)
101
  @CollectionSize.Require(absent = ZERO)
102
  public void testReplace_unsupportedPresent() {
103
    try {
104
      getMap().replace(k0(), v3());
105
      fail("Expected UnsupportedOperationException");
106
    } catch (UnsupportedOperationException expected) {
107
    }
108
    expectUnchanged();
109
  }
110
}
111

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

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

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

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