guava

Форк
0
/
ConcurrentMapPutIfAbsentTester.java 
139 строк · 4.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_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
@ElementTypesAreNonnullByDefault
43
public class ConcurrentMapPutIfAbsentTester<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
  public void testPutIfAbsent_supportedAbsent() {
51
    assertNull("putIfAbsent(notPresent, value) should return null", putIfAbsent(e3()));
52
    expectAdded(e3());
53
  }
54

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

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

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

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

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

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

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

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

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

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

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

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