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