Java

Форк
0
/
CollectionAddAllTester.java 
204 строки · 8.0 Кб
1
/*
2
 * Copyright (C) 2007 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.CollectionFeature.ALLOWS_NULL_VALUES;
20
import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21
import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
22
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
23
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24
import static java.util.Collections.singletonList;
25

26
import com.google.common.annotations.GwtCompatible;
27
import com.google.common.annotations.GwtIncompatible;
28
import com.google.common.annotations.J2ktIncompatible;
29
import com.google.common.collect.testing.AbstractCollectionTester;
30
import com.google.common.collect.testing.Helpers;
31
import com.google.common.collect.testing.MinimalCollection;
32
import com.google.common.collect.testing.features.CollectionFeature;
33
import com.google.common.collect.testing.features.CollectionSize;
34
import java.lang.reflect.Method;
35
import java.util.ConcurrentModificationException;
36
import java.util.Iterator;
37
import java.util.List;
38
import org.checkerframework.checker.nullness.qual.Nullable;
39
import org.junit.Ignore;
40

41
/**
42
 * A generic JUnit test which tests addAll operations on a collection. Can't be invoked directly;
43
 * please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
44
 *
45
 * @author Chris Povirk
46
 * @author Kevin Bourrillion
47
 */
48
@GwtCompatible(emulated = true)
49
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
50
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
51
public class CollectionAddAllTester<E extends @Nullable Object>
52
    extends AbstractCollectionTester<E> {
53
  @CollectionFeature.Require(SUPPORTS_ADD)
54
  public void testAddAll_supportedNothing() {
55
    assertFalse("addAll(nothing) should return false", collection.addAll(emptyCollection()));
56
    expectUnchanged();
57
  }
58

59
  @CollectionFeature.Require(absent = SUPPORTS_ADD)
60
  public void testAddAll_unsupportedNothing() {
61
    try {
62
      assertFalse(
63
          "addAll(nothing) should return false or throw", collection.addAll(emptyCollection()));
64
    } catch (UnsupportedOperationException tolerated) {
65
    }
66
    expectUnchanged();
67
  }
68

69
  @CollectionFeature.Require(SUPPORTS_ADD)
70
  public void testAddAll_supportedNonePresent() {
71
    assertTrue(
72
        "addAll(nonePresent) should return true", collection.addAll(createDisjointCollection()));
73
    expectAdded(e3(), e4());
74
  }
75

76
  @CollectionFeature.Require(absent = SUPPORTS_ADD)
77
  public void testAddAll_unsupportedNonePresent() {
78
    try {
79
      collection.addAll(createDisjointCollection());
80
      fail("addAll(nonePresent) should throw");
81
    } catch (UnsupportedOperationException expected) {
82
    }
83
    expectUnchanged();
84
    expectMissing(e3(), e4());
85
  }
86

87
  @CollectionFeature.Require(SUPPORTS_ADD)
88
  @CollectionSize.Require(absent = ZERO)
89
  public void testAddAll_supportedSomePresent() {
90
    assertTrue(
91
        "addAll(somePresent) should return true",
92
        collection.addAll(MinimalCollection.of(e3(), e0())));
93
    assertTrue("should contain " + e3(), collection.contains(e3()));
94
    assertTrue("should contain " + e0(), collection.contains(e0()));
95
  }
96

97
  @CollectionFeature.Require(absent = SUPPORTS_ADD)
98
  @CollectionSize.Require(absent = ZERO)
99
  public void testAddAll_unsupportedSomePresent() {
100
    try {
101
      collection.addAll(MinimalCollection.of(e3(), e0()));
102
      fail("addAll(somePresent) should throw");
103
    } catch (UnsupportedOperationException expected) {
104
    }
105
    expectUnchanged();
106
  }
107

108
  @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
109
  @CollectionSize.Require(absent = ZERO)
110
  public void testAddAllConcurrentWithIteration() {
111
    try {
112
      Iterator<E> iterator = collection.iterator();
113
      assertTrue(collection.addAll(MinimalCollection.of(e3(), e0())));
114
      iterator.next();
115
      fail("Expected ConcurrentModificationException");
116
    } catch (ConcurrentModificationException expected) {
117
      // success
118
    }
119
  }
120

121
  @CollectionFeature.Require(absent = SUPPORTS_ADD)
122
  @CollectionSize.Require(absent = ZERO)
123
  public void testAddAll_unsupportedAllPresent() {
124
    try {
125
      assertFalse(
126
          "addAll(allPresent) should return false or throw",
127
          collection.addAll(MinimalCollection.of(e0())));
128
    } catch (UnsupportedOperationException tolerated) {
129
    }
130
    expectUnchanged();
131
  }
132

133
  @CollectionFeature.Require(
134
      value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
135
      absent = RESTRICTS_ELEMENTS)
136
  public void testAddAll_nullSupported() {
137
    List<E> containsNull = singletonList(null);
138
    assertTrue("addAll(containsNull) should return true", collection.addAll(containsNull));
139
    /*
140
     * We need (E) to force interpretation of null as the single element of a
141
     * varargs array, not the array itself
142
     */
143
    expectAdded((E) null);
144
  }
145

146
  @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
147
  public void testAddAll_nullUnsupported() {
148
    List<E> containsNull = singletonList(null);
149
    try {
150
      collection.addAll(containsNull);
151
      fail("addAll(containsNull) should throw");
152
    } catch (NullPointerException expected) {
153
    }
154
    expectUnchanged();
155
    expectNullMissingWhenNullUnsupported(
156
        "Should not contain null after unsupported addAll(containsNull)");
157
  }
158

159
  @CollectionFeature.Require(SUPPORTS_ADD)
160
  public void testAddAll_nullCollectionReference() {
161
    try {
162
      collection.addAll(null);
163
      fail("addAll(null) should throw NullPointerException");
164
    } catch (NullPointerException expected) {
165
    }
166
  }
167

168
  /**
169
   * Returns the {@link Method} instance for {@link #testAddAll_nullUnsupported()} so that tests can
170
   * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
171
   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
172
   */
173
  @J2ktIncompatible
174
  @GwtIncompatible // reflection
175
  public static Method getAddAllNullUnsupportedMethod() {
176
    return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_nullUnsupported");
177
  }
178

179
  /**
180
   * Returns the {@link Method} instance for {@link #testAddAll_unsupportedNonePresent()} so that
181
   * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
182
   * figure out what to do with <a
183
   * href="https://github.com/openjdk/jdk/blob/c25c4896ad9ef031e3cddec493aef66ff87c48a7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L4830">{@code
184
   * ConcurrentHashMap} support for {@code entrySet().add()}</a>.
185
   */
186
  @J2ktIncompatible
187
  @GwtIncompatible // reflection
188
  public static Method getAddAllUnsupportedNonePresentMethod() {
189
    return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedNonePresent");
190
  }
191

192
  /**
193
   * Returns the {@link Method} instance for {@link #testAddAll_unsupportedSomePresent()} so that
194
   * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
195
   * figure out what to do with <a
196
   * href="https://github.com/openjdk/jdk/blob/c25c4896ad9ef031e3cddec493aef66ff87c48a7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L4830">{@code
197
   * ConcurrentHashMap} support for {@code entrySet().add()}</a>.
198
   */
199
  @J2ktIncompatible
200
  @GwtIncompatible // reflection
201
  public static Method getAddAllUnsupportedSomePresentMethod() {
202
    return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedSomePresent");
203
  }
204
}
205

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

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

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

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