Java

Форк
0
/
ConcurrentMapPutIfAbsentTester.java 
140 строк · 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 com.google.errorprone.annotations.CanIgnoreReturnValue;
29
import java.util.Map.Entry;
30
import java.util.concurrent.ConcurrentMap;
31
import org.junit.Ignore;
32

33
/**
34
 * A generic JUnit test which tests {@code putIfAbsent} operations on a concurrent map. Can't be
35
 * invoked directly; please see {@link
36
 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
37
 *
38
 * @author Louis Wasserman
39
 */
40
@GwtCompatible
41
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
42
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
43
@ElementTypesAreNonnullByDefault
44
public class ConcurrentMapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
45
  @Override
46
  protected ConcurrentMap<K, V> getMap() {
47
    return (ConcurrentMap<K, V>) super.getMap();
48
  }
49

50
  @MapFeature.Require(SUPPORTS_PUT)
51
  public void testPutIfAbsent_supportedAbsent() {
52
    assertNull("putIfAbsent(notPresent, value) should return null", putIfAbsent(e3()));
53
    expectAdded(e3());
54
  }
55

56
  @MapFeature.Require(SUPPORTS_PUT)
57
  @CollectionSize.Require(absent = ZERO)
58
  public void testPutIfAbsent_supportedPresent() {
59
    assertEquals(
60
        "putIfAbsent(present, value) should return existing value",
61
        v0(),
62
        getMap().putIfAbsent(k0(), v3()));
63
    expectUnchanged();
64
  }
65

66
  @MapFeature.Require(absent = SUPPORTS_PUT)
67
  public void testPutIfAbsent_unsupportedAbsent() {
68
    try {
69
      putIfAbsent(e3());
70
      fail("putIfAbsent(notPresent, value) should throw");
71
    } catch (UnsupportedOperationException expected) {
72
    }
73
    expectUnchanged();
74
    expectMissing(e3());
75
  }
76

77
  @MapFeature.Require(absent = SUPPORTS_PUT)
78
  @CollectionSize.Require(absent = ZERO)
79
  public void testPutIfAbsent_unsupportedPresentExistingValue() {
80
    try {
81
      assertEquals(
82
          "putIfAbsent(present, existingValue) should return present or throw",
83
          v0(),
84
          putIfAbsent(e0()));
85
    } catch (UnsupportedOperationException tolerated) {
86
    }
87
    expectUnchanged();
88
  }
89

90
  @MapFeature.Require(absent = SUPPORTS_PUT)
91
  @CollectionSize.Require(absent = ZERO)
92
  public void testPutIfAbsent_unsupportedPresentDifferentValue() {
93
    try {
94
      getMap().putIfAbsent(k0(), v3());
95
    } catch (UnsupportedOperationException tolerated) {
96
    }
97
    expectUnchanged();
98
  }
99

100
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
101
  public void testPutIfAbsent_nullKeyUnsupported() {
102
    try {
103
      getMap().putIfAbsent(null, v3());
104
      fail("putIfAbsent(null, value) should throw");
105
    } catch (NullPointerException expected) {
106
    }
107
    expectUnchanged();
108
    expectNullKeyMissingWhenNullKeysUnsupported(
109
        "Should not contain null key after unsupported putIfAbsent(null, value)");
110
  }
111

112
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
113
  public void testPutIfAbsent_nullValueUnsupported() {
114
    try {
115
      getMap().putIfAbsent(k3(), null);
116
      fail("putIfAbsent(key, null) should throw");
117
    } catch (NullPointerException expected) {
118
    }
119
    expectUnchanged();
120
    expectNullValueMissingWhenNullValuesUnsupported(
121
        "Should not contain null value after unsupported put(key, null)");
122
  }
123

124
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
125
  @CollectionSize.Require(absent = ZERO)
126
  public void testPutIfAbsent_putWithNullValueUnsupported() {
127
    try {
128
      getMap().putIfAbsent(k0(), null);
129
    } catch (NullPointerException tolerated) {
130
    }
131
    expectUnchanged();
132
    expectNullValueMissingWhenNullValuesUnsupported(
133
        "Should not contain null after unsupported putIfAbsent(present, null)");
134
  }
135

136
  @CanIgnoreReturnValue
137
  private V putIfAbsent(Entry<K, V> entry) {
138
    return getMap().putIfAbsent(entry.getKey(), entry.getValue());
139
  }
140
}
141

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

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

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

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