guava

Форк
0
/
CollectionAddAllTester.java 
201 строка · 7.7 Кб
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
public class CollectionAddAllTester<E extends @Nullable Object>
51
    extends AbstractCollectionTester<E> {
52
  @CollectionFeature.Require(SUPPORTS_ADD)
53
  public void testAddAll_supportedNothing() {
54
    assertFalse("addAll(nothing) should return false", collection.addAll(emptyCollection()));
55
    expectUnchanged();
56
  }
57

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

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

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

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

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

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

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

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

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

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

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

178
  /**
179
   * Returns the {@link Method} instance for {@link #testAddAll_unsupportedNonePresent()} so that
180
   * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
181
   * figure out what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for
182
   * {@code entrySet().add()}</a>.
183
   */
184
  @J2ktIncompatible
185
  @GwtIncompatible // reflection
186
  public static Method getAddAllUnsupportedNonePresentMethod() {
187
    return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedNonePresent");
188
  }
189

190
  /**
191
   * Returns the {@link Method} instance for {@link #testAddAll_unsupportedSomePresent()} so that
192
   * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we
193
   * figure out what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for
194
   * {@code entrySet().add()}</a>.
195
   */
196
  @J2ktIncompatible
197
  @GwtIncompatible // reflection
198
  public static Method getAddAllUnsupportedSomePresentMethod() {
199
    return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedSomePresent");
200
  }
201
}
202

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

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

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

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