CollectionAddTester.java revision 3c77433663281544363151bf284b0240dfd22a42
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.collect.testing.AbstractCollectionTester;
28import com.google.common.collect.testing.Helpers;
29import com.google.common.collect.testing.features.CollectionFeature;
30import com.google.common.collect.testing.features.CollectionSize;
31
32import java.lang.reflect.Method;
33import java.util.ConcurrentModificationException;
34import java.util.Iterator;
35
36/**
37 * A generic JUnit test which tests {@code add} operations on a collection.
38 * Can't be invoked directly; please see
39 * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
40 *
41 * <p>This class is GWT compatible.
42 *
43 * @author Chris Povirk
44 * @author Kevin Bourrillion
45 */
46@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
47@GwtCompatible(emulated = true)
48public class CollectionAddTester<E> extends AbstractCollectionTester<E> {
49  @CollectionFeature.Require(SUPPORTS_ADD)
50  public void testAdd_supportedNotPresent() {
51    assertTrue("add(notPresent) should return true",
52        collection.add(samples.e3));
53    expectAdded(samples.e3);
54  }
55
56  @CollectionFeature.Require(absent = SUPPORTS_ADD)
57  public void testAdd_unsupportedNotPresent() {
58    try {
59      collection.add(samples.e3);
60      fail("add(notPresent) should throw");
61    } catch (UnsupportedOperationException expected) {
62    }
63    expectUnchanged();
64    expectMissing(samples.e3);
65  }
66
67  @CollectionFeature.Require(absent = SUPPORTS_ADD)
68  @CollectionSize.Require(absent = ZERO)
69  public void testAdd_unsupportedPresent() {
70    try {
71      assertFalse("add(present) should return false or throw",
72          collection.add(samples.e0));
73    } catch (UnsupportedOperationException tolerated) {
74    }
75    expectUnchanged();
76  }
77
78  @CollectionFeature.Require(
79      value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
80      absent = RESTRICTS_ELEMENTS)
81  public void testAdd_nullSupported() {
82    assertTrue("add(null) should return true", collection.add(null));
83    expectAdded((E) null);
84  }
85
86  @CollectionFeature.Require(value = SUPPORTS_ADD,
87      absent = ALLOWS_NULL_VALUES)
88  public void testAdd_nullUnsupported() {
89    try {
90      collection.add(null);
91      fail("add(null) should throw");
92    } catch (NullPointerException expected) {
93    }
94    expectUnchanged();
95    expectNullMissingWhenNullUnsupported(
96        "Should not contain null after unsupported add(null)");
97  }
98
99  @CollectionFeature.Require({SUPPORTS_ADD,
100      FAILS_FAST_ON_CONCURRENT_MODIFICATION})
101  @CollectionSize.Require(absent = ZERO)
102  public void testAddConcurrentWithIteration() {
103    try {
104      Iterator<E> iterator = collection.iterator();
105      assertTrue(collection.add(samples.e3));
106      iterator.next();
107      fail("Expected ConcurrentModificationException");
108    } catch (ConcurrentModificationException expected) {
109      // success
110    }
111  }
112
113  /**
114   * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so
115   * that tests of {@link
116   * java.util.Collections#checkedCollection(java.util.Collection, Class)} can
117   * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()}
118   * until <a
119   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug
120   * 6409434</a> is fixed. It's unclear whether nulls were to be permitted or
121   * forbidden, but presumably the eventual fix will be to permit them, as it
122   * seems more likely that code would depend on that behavior than on the
123   * other. Thus, we say the bug is in add(), which fails to support null.
124   */
125  @GwtIncompatible("reflection")
126  public static Method getAddNullSupportedMethod() {
127    return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullSupported");
128  }
129
130  /**
131   * Returns the {@link Method} instance for {@link #testAdd_nullSupported()}
132   * so that tests of {@link java.util.TreeSet} can suppress it with {@code
133   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
134   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug
135   * 5045147</a> is fixed.
136   */
137  @GwtIncompatible("reflection")
138  public static Method getAddNullUnsupportedMethod() {
139    return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported");
140  }
141}
142