guava
144 строки · 5.8 Кб
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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
22import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
23import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24
25import com.google.common.annotations.GwtCompatible;
26import com.google.common.annotations.GwtIncompatible;
27import com.google.common.annotations.J2ktIncompatible;
28import com.google.common.collect.testing.AbstractCollectionTester;
29import com.google.common.collect.testing.Helpers;
30import com.google.common.collect.testing.features.CollectionFeature;
31import com.google.common.collect.testing.features.CollectionSize;
32import java.lang.reflect.Method;
33import java.util.ConcurrentModificationException;
34import java.util.Iterator;
35import 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.
46public class CollectionAddTester<E> extends AbstractCollectionTester<E> {
47@CollectionFeature.Require(SUPPORTS_ADD)
48public void testAdd_supportedNotPresent() {
49assertTrue("add(notPresent) should return true", collection.add(e3()));
50expectAdded(e3());
51}
52
53@CollectionFeature.Require(absent = SUPPORTS_ADD)
54public void testAdd_unsupportedNotPresent() {
55try {
56collection.add(e3());
57fail("add(notPresent) should throw");
58} catch (UnsupportedOperationException expected) {
59}
60expectUnchanged();
61expectMissing(e3());
62}
63
64@CollectionFeature.Require(absent = SUPPORTS_ADD)
65@CollectionSize.Require(absent = ZERO)
66public void testAdd_unsupportedPresent() {
67try {
68assertFalse("add(present) should return false or throw", collection.add(e0()));
69} catch (UnsupportedOperationException tolerated) {
70}
71expectUnchanged();
72}
73
74@CollectionFeature.Require(
75value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
76absent = RESTRICTS_ELEMENTS)
77public void testAdd_nullSupported() {
78assertTrue("add(null) should return true", collection.add(null));
79expectAdded((E) null);
80}
81
82@CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
83public void testAdd_nullUnsupported() {
84try {
85collection.add(null);
86fail("add(null) should throw");
87} catch (NullPointerException expected) {
88}
89expectUnchanged();
90expectNullMissingWhenNullUnsupported("Should not contain null after unsupported add(null)");
91}
92
93@CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
94@CollectionSize.Require(absent = ZERO)
95public void testAddConcurrentWithIteration() {
96try {
97Iterator<E> iterator = collection.iterator();
98assertTrue(collection.add(e3()));
99iterator.next();
100fail("Expected ConcurrentModificationException");
101} catch (ConcurrentModificationException expected) {
102// success
103}
104}
105
106/**
107* Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so that tests of
108* {@link java.util.Collections#checkedCollection(java.util.Collection, Class)} can suppress it
109* with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
110* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug 6409434</a> is fixed.
111* It's unclear whether nulls were to be permitted or forbidden, but presumably the eventual fix
112* will be to permit them, as it seems more likely that code would depend on that behavior than on
113* the other. Thus, we say the bug is in add(), which fails to support null.
114*/
115@J2ktIncompatible
116@GwtIncompatible // reflection
117public static Method getAddNullSupportedMethod() {
118return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullSupported");
119}
120
121/**
122* Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so that tests of
123* {@link java.util.TreeSet} can suppress it with {@code
124* FeatureSpecificTestSuiteBuilder.suppressing()} until <a
125* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
126*/
127@J2ktIncompatible
128@GwtIncompatible // reflection
129public static Method getAddNullUnsupportedMethod() {
130return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported");
131}
132
133/**
134* Returns the {@link Method} instance for {@link #testAdd_unsupportedNotPresent()} so that tests
135* can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we figure out
136* what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for {@code
137* entrySet().add()}</a>.
138*/
139@J2ktIncompatible
140@GwtIncompatible // reflection
141public static Method getAddUnsupportedNotPresentMethod() {
142return Helpers.getMethod(CollectionAddTester.class, "testAdd_unsupportedNotPresent");
143}
144}
145