Java

Форк
0
146 строк · 6.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

25
import com.google.common.annotations.GwtCompatible;
26
import com.google.common.annotations.GwtIncompatible;
27
import com.google.common.annotations.J2ktIncompatible;
28
import com.google.common.collect.testing.AbstractCollectionTester;
29
import com.google.common.collect.testing.Helpers;
30
import com.google.common.collect.testing.features.CollectionFeature;
31
import com.google.common.collect.testing.features.CollectionSize;
32
import java.lang.reflect.Method;
33
import java.util.ConcurrentModificationException;
34
import java.util.Iterator;
35
import org.junit.Ignore;
36

37
/**
38
 * A generic JUnit test which tests {@code add} operations on a collection. Can't be invoked
39
 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
40
 *
41
 * @author Chris Povirk
42
 * @author Kevin Bourrillion
43
 */
44
@GwtCompatible(emulated = true)
45
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
46
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
47
public class CollectionAddTester<E> extends AbstractCollectionTester<E> {
48
  @CollectionFeature.Require(SUPPORTS_ADD)
49
  public void testAdd_supportedNotPresent() {
50
    assertTrue("add(notPresent) should return true", collection.add(e3()));
51
    expectAdded(e3());
52
  }
53

54
  @CollectionFeature.Require(absent = SUPPORTS_ADD)
55
  public void testAdd_unsupportedNotPresent() {
56
    try {
57
      collection.add(e3());
58
      fail("add(notPresent) should throw");
59
    } catch (UnsupportedOperationException expected) {
60
    }
61
    expectUnchanged();
62
    expectMissing(e3());
63
  }
64

65
  @CollectionFeature.Require(absent = SUPPORTS_ADD)
66
  @CollectionSize.Require(absent = ZERO)
67
  public void testAdd_unsupportedPresent() {
68
    try {
69
      assertFalse("add(present) should return false or throw", collection.add(e0()));
70
    } catch (UnsupportedOperationException tolerated) {
71
    }
72
    expectUnchanged();
73
  }
74

75
  @CollectionFeature.Require(
76
      value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
77
      absent = RESTRICTS_ELEMENTS)
78
  public void testAdd_nullSupported() {
79
    assertTrue("add(null) should return true", collection.add(null));
80
    expectAdded((E) null);
81
  }
82

83
  @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
84
  public void testAdd_nullUnsupported() {
85
    try {
86
      collection.add(null);
87
      fail("add(null) should throw");
88
    } catch (NullPointerException expected) {
89
    }
90
    expectUnchanged();
91
    expectNullMissingWhenNullUnsupported("Should not contain null after unsupported add(null)");
92
  }
93

94
  @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
95
  @CollectionSize.Require(absent = ZERO)
96
  public void testAddConcurrentWithIteration() {
97
    try {
98
      Iterator<E> iterator = collection.iterator();
99
      assertTrue(collection.add(e3()));
100
      iterator.next();
101
      fail("Expected ConcurrentModificationException");
102
    } catch (ConcurrentModificationException expected) {
103
      // success
104
    }
105
  }
106

107
  /**
108
   * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so that tests of
109
   * {@link java.util.Collections#checkedCollection(java.util.Collection, Class)} can suppress it
110
   * with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
111
   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug 6409434</a> is fixed.
112
   * It's unclear whether nulls were to be permitted or forbidden, but presumably the eventual fix
113
   * will be to permit them, as it seems more likely that code would depend on that behavior than on
114
   * the other. Thus, we say the bug is in add(), which fails to support null.
115
   */
116
  @J2ktIncompatible
117
  @GwtIncompatible // reflection
118
  public static Method getAddNullSupportedMethod() {
119
    return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullSupported");
120
  }
121

122
  /**
123
   * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so that tests of
124
   * {@link java.util.TreeSet} can suppress it with {@code
125
   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
126
   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
127
   */
128
  @J2ktIncompatible
129
  @GwtIncompatible // reflection
130
  public static Method getAddNullUnsupportedMethod() {
131
    return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported");
132
  }
133

134
  /**
135
   * Returns the {@link Method} instance for {@link #testAdd_unsupportedNotPresent()} so that tests
136
   * can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we figure out
137
   * what to do with <a
138
   * href="https://github.com/openjdk/jdk/blob/c25c4896ad9ef031e3cddec493aef66ff87c48a7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L4830">{@code
139
   * ConcurrentHashMap} support for {@code entrySet().add()}</a>.
140
   */
141
  @J2ktIncompatible
142
  @GwtIncompatible // reflection
143
  public static Method getAddUnsupportedNotPresentMethod() {
144
    return Helpers.getMethod(CollectionAddTester.class, "testAdd_unsupportedNotPresent");
145
  }
146
}
147

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

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

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

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