guava

Форк
0
164 строки · 5.9 Кб
1
/*
2
 * Copyright (C) 2008 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.SEVERAL;
20
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
22
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
23
import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
24
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
25

26
import com.google.common.annotations.GwtCompatible;
27
import com.google.common.collect.testing.AbstractMapTester;
28
import com.google.common.collect.testing.WrongType;
29
import com.google.common.collect.testing.features.CollectionSize;
30
import com.google.common.collect.testing.features.MapFeature;
31
import java.util.ConcurrentModificationException;
32
import java.util.Iterator;
33
import java.util.Map.Entry;
34
import org.junit.Ignore;
35

36
/**
37
 * A generic JUnit test which tests {@code remove} operations on a map. Can't be invoked directly;
38
 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
39
 *
40
 * @author George van den Driessche
41
 * @author Chris Povirk
42
 */
43
@GwtCompatible
44
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
45
public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> {
46
  @MapFeature.Require(SUPPORTS_REMOVE)
47
  @CollectionSize.Require(absent = ZERO)
48
  public void testRemove_present() {
49
    int initialSize = getMap().size();
50
    assertEquals("remove(present) should return the associated value", v0(), getMap().remove(k0()));
51
    assertEquals(
52
        "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
53
    expectMissing(e0());
54
  }
55

56
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
57
  @CollectionSize.Require(SEVERAL)
58
  public void testRemovePresentConcurrentWithEntrySetIteration() {
59
    try {
60
      Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
61
      getMap().remove(k0());
62
      iterator.next();
63
      fail("Expected ConcurrentModificationException");
64
    } catch (ConcurrentModificationException expected) {
65
      // success
66
    }
67
  }
68

69
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
70
  @CollectionSize.Require(SEVERAL)
71
  public void testRemovePresentConcurrentWithKeySetIteration() {
72
    try {
73
      Iterator<K> iterator = getMap().keySet().iterator();
74
      getMap().remove(k0());
75
      iterator.next();
76
      fail("Expected ConcurrentModificationException");
77
    } catch (ConcurrentModificationException expected) {
78
      // success
79
    }
80
  }
81

82
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
83
  @CollectionSize.Require(SEVERAL)
84
  public void testRemovePresentConcurrentWithValuesIteration() {
85
    try {
86
      Iterator<V> iterator = getMap().values().iterator();
87
      getMap().remove(k0());
88
      iterator.next();
89
      fail("Expected ConcurrentModificationException");
90
    } catch (ConcurrentModificationException expected) {
91
      // success
92
    }
93
  }
94

95
  @MapFeature.Require(SUPPORTS_REMOVE)
96
  public void testRemove_notPresent() {
97
    assertNull("remove(notPresent) should return null", getMap().remove(k3()));
98
    expectUnchanged();
99
  }
100

101
  @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
102
  @CollectionSize.Require(absent = ZERO)
103
  public void testRemove_nullPresent() {
104
    initMapWithNullKey();
105

106
    int initialSize = getMap().size();
107
    assertEquals(
108
        "remove(null) should return the associated value",
109
        getValueForNullKey(),
110
        getMap().remove(null));
111
    assertEquals(
112
        "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
113
    expectMissing(entry(null, getValueForNullKey()));
114
  }
115

116
  @MapFeature.Require(absent = SUPPORTS_REMOVE)
117
  @CollectionSize.Require(absent = ZERO)
118
  public void testRemove_unsupported() {
119
    try {
120
      getMap().remove(k0());
121
      fail("remove(present) should throw UnsupportedOperationException");
122
    } catch (UnsupportedOperationException expected) {
123
    }
124
    expectUnchanged();
125
    assertEquals("remove(present) should not remove the element", v0(), get(k0()));
126
  }
127

128
  @MapFeature.Require(absent = SUPPORTS_REMOVE)
129
  public void testRemove_unsupportedNotPresent() {
130
    try {
131
      assertNull(
132
          "remove(notPresent) should return null or throw UnsupportedOperationException",
133
          getMap().remove(k3()));
134
    } catch (UnsupportedOperationException tolerated) {
135
    }
136
    expectUnchanged();
137
    expectMissing(e3());
138
  }
139

140
  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
141
  public void testRemove_nullQueriesNotSupported() {
142
    try {
143
      assertNull(
144
          "remove(null) should return null or throw NullPointerException", getMap().remove(null));
145
    } catch (NullPointerException tolerated) {
146
    }
147
    expectUnchanged();
148
  }
149

150
  @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES})
151
  public void testRemove_nullSupportedMissing() {
152
    assertNull("remove(null) should return null", getMap().remove(null));
153
    expectUnchanged();
154
  }
155

156
  @MapFeature.Require(SUPPORTS_REMOVE)
157
  public void testRemove_wrongType() {
158
    try {
159
      assertNull(getMap().remove(WrongType.VALUE));
160
    } catch (ClassCastException tolerated) {
161
    }
162
    expectUnchanged();
163
  }
164
}
165

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

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

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

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