guava

Форк
0
/
ConcurrentMapReplaceEntryTester.java 
153 строки · 5.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_VALUES;
21
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
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.concurrent.ConcurrentMap;
29
import org.junit.Ignore;
30

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

47
  @MapFeature.Require(SUPPORTS_PUT)
48
  @CollectionSize.Require(absent = ZERO)
49
  public void testReplaceEntry_supportedPresent() {
50
    assertTrue(getMap().replace(k0(), v0(), v3()));
51
    expectReplacement(entry(k0(), v3()));
52
  }
53

54
  @MapFeature.Require(SUPPORTS_PUT)
55
  @CollectionSize.Require(absent = ZERO)
56
  public void testReplaceEntry_supportedPresentUnchanged() {
57
    assertTrue(getMap().replace(k0(), v0(), v0()));
58
    expectUnchanged();
59
  }
60

61
  @MapFeature.Require(SUPPORTS_PUT)
62
  @CollectionSize.Require(absent = ZERO)
63
  public void testReplaceEntry_supportedWrongValue() {
64
    assertFalse(getMap().replace(k0(), v3(), v4()));
65
    expectUnchanged();
66
  }
67

68
  @MapFeature.Require(SUPPORTS_PUT)
69
  public void testReplaceEntry_supportedAbsentKey() {
70
    assertFalse(getMap().replace(k3(), v3(), v4()));
71
    expectUnchanged();
72
  }
73

74
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
75
  @CollectionSize.Require(absent = ZERO)
76
  public void testReplaceEntry_presentNullValueUnsupported() {
77
    try {
78
      getMap().replace(k0(), v0(), null);
79
      fail("Expected NullPointerException");
80
    } catch (NullPointerException expected) {
81
    }
82
    expectUnchanged();
83
  }
84

85
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
86
  @CollectionSize.Require(absent = ZERO)
87
  public void testReplaceEntry_wrongValueNullValueUnsupported() {
88
    try {
89
      assertFalse(getMap().replace(k0(), v3(), null));
90
    } catch (NullPointerException tolerated) {
91
      // the operation would be a no-op, so exceptions are allowed but not required
92
    }
93
    expectUnchanged();
94
  }
95

96
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
97
  public void testReplaceEntry_absentKeyNullValueUnsupported() {
98
    try {
99
      assertFalse(getMap().replace(k3(), v3(), null));
100
    } catch (NullPointerException tolerated) {
101
      // the operation would be a no-op, so exceptions are allowed but not required
102
    }
103
    expectUnchanged();
104
  }
105

106
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES})
107
  public void testReplaceEntry_nullDifferentFromAbsent() {
108
    assertFalse(getMap().replace(k3(), null, v3()));
109
    expectUnchanged();
110
  }
111

112
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
113
  public void testReplaceEntry_expectNullUnsupported() {
114
    try {
115
      assertFalse(getMap().replace(k3(), null, v3()));
116
    } catch (NullPointerException tolerated) {
117
      // the operation would be a no-op, so exceptions are allowed but not required
118
    }
119
    expectUnchanged();
120
  }
121

122
  @MapFeature.Require(absent = SUPPORTS_PUT)
123
  @CollectionSize.Require(absent = ZERO)
124
  public void testReplaceEntry_unsupportedPresent() {
125
    try {
126
      getMap().replace(k0(), v0(), v3());
127
      fail("Expected UnsupportedOperationException");
128
    } catch (UnsupportedOperationException expected) {
129
    }
130
    expectUnchanged();
131
  }
132

133
  @MapFeature.Require(absent = SUPPORTS_PUT)
134
  @CollectionSize.Require(absent = ZERO)
135
  public void testReplaceEntry_unsupportedWrongValue() {
136
    try {
137
      getMap().replace(k0(), v3(), v4());
138
    } catch (UnsupportedOperationException tolerated) {
139
      // the operation would be a no-op, so exceptions are allowed but not required
140
    }
141
    expectUnchanged();
142
  }
143

144
  @MapFeature.Require(absent = SUPPORTS_PUT)
145
  public void testReplaceEntry_unsupportedAbsentKey() {
146
    try {
147
      getMap().replace(k3(), v3(), v4());
148
    } catch (UnsupportedOperationException tolerated) {
149
      // the operation would be a no-op, so exceptions are allowed but not required
150
    }
151
    expectUnchanged();
152
  }
153
}
154

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

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

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

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