View Javadoc
1   /*
2    * Copyright (C) 2008 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.CollectionSize.ONE;
21  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22  import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX;
23  import static java.util.Collections.singletonList;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.collect.testing.MinimalCollection;
27  import com.google.common.collect.testing.features.CollectionFeature;
28  import com.google.common.collect.testing.features.CollectionSize;
29  import com.google.common.collect.testing.features.ListFeature;
30  import java.util.List;
31  
32  /**
33   * A generic JUnit test which tests {@code addAll(int, Collection)} operations
34   * on a list. Can't be invoked directly; please see
35   * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
36   *
37   * @author Chris Povirk
38   */
39  @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
40  @GwtCompatible
41  public class ListAddAllAtIndexTester<E> extends AbstractListTester<E> {
42    @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
43    @CollectionSize.Require(absent = ZERO)
44    public void testAddAllAtIndex_supportedAllPresent() {
45      assertTrue(
46          "addAll(n, allPresent) should return true",
47          getList().addAll(0, MinimalCollection.of(e0())));
48      expectAdded(0, e0());
49    }
50  
51    @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
52    @CollectionSize.Require(absent = ZERO)
53    public void testAddAllAtIndex_unsupportedAllPresent() {
54      try {
55        getList().addAll(0, MinimalCollection.of(e0()));
56        fail("addAll(n, allPresent) should throw");
57      } catch (UnsupportedOperationException expected) {
58      }
59      expectUnchanged();
60    }
61  
62    @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
63    @CollectionSize.Require(absent = ZERO)
64    public void testAddAllAtIndex_supportedSomePresent() {
65      assertTrue(
66          "addAll(n, allPresent) should return true",
67          getList().addAll(0, MinimalCollection.of(e0(), e3())));
68      expectAdded(0, e0(), e3());
69    }
70  
71    @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
72    @CollectionSize.Require(absent = ZERO)
73    public void testAddAllAtIndex_unsupportedSomePresent() {
74      try {
75        getList().addAll(0, MinimalCollection.of(e0(), e3()));
76        fail("addAll(n, allPresent) should throw");
77      } catch (UnsupportedOperationException expected) {
78      }
79      expectUnchanged();
80      expectMissing(e3());
81    }
82  
83    @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
84    public void testAddAllAtIndex_supportedNothing() {
85      assertFalse("addAll(n, nothing) should return false", getList().addAll(0, emptyCollection()));
86      expectUnchanged();
87    }
88  
89    @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
90    public void testAddAllAtIndex_unsupportedNothing() {
91      try {
92        assertFalse(
93            "addAll(n, nothing) should return false or throw",
94            getList().addAll(0, emptyCollection()));
95      } catch (UnsupportedOperationException tolerated) {
96      }
97      expectUnchanged();
98    }
99  
100   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
101   public void testAddAllAtIndex_withDuplicates() {
102     MinimalCollection<E> elementsToAdd = MinimalCollection.of(e0(), e1(), e0(), e1());
103     assertTrue("addAll(n, hasDuplicates) should return true", getList().addAll(0, elementsToAdd));
104     expectAdded(0, e0(), e1(), e0(), e1());
105   }
106 
107   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
108   @CollectionFeature.Require(ALLOWS_NULL_VALUES)
109   public void testAddAllAtIndex_nullSupported() {
110     List<E> containsNull = singletonList(null);
111     assertTrue("addAll(n, containsNull) should return true", getList().addAll(0, containsNull));
112     /*
113      * We need (E) to force interpretation of null as the single element of a
114      * varargs array, not the array itself
115      */
116     expectAdded(0, (E) null);
117   }
118 
119   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
120   @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
121   public void testAddAllAtIndex_nullUnsupported() {
122     List<E> containsNull = singletonList(null);
123     try {
124       getList().addAll(0, containsNull);
125       fail("addAll(n, containsNull) should throw");
126     } catch (NullPointerException expected) {
127     }
128     expectUnchanged();
129     expectNullMissingWhenNullUnsupported(
130         "Should not contain null after unsupported addAll(n, containsNull)");
131   }
132 
133   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
134   @CollectionSize.Require(absent = {ZERO, ONE})
135   public void testAddAllAtIndex_middle() {
136     assertTrue(
137         "addAll(middle, disjoint) should return true",
138         getList().addAll(getNumElements() / 2, createDisjointCollection()));
139     expectAdded(getNumElements() / 2, createDisjointCollection());
140   }
141 
142   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
143   @CollectionSize.Require(absent = ZERO)
144   public void testAddAllAtIndex_end() {
145     assertTrue(
146         "addAll(end, disjoint) should return true",
147         getList().addAll(getNumElements(), createDisjointCollection()));
148     expectAdded(getNumElements(), createDisjointCollection());
149   }
150 
151   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
152   public void testAddAllAtIndex_nullCollectionReference() {
153     try {
154       getList().addAll(0, null);
155       fail("addAll(n, null) should throw");
156     } catch (NullPointerException expected) {
157     }
158     expectUnchanged();
159   }
160 
161   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
162   public void testAddAllAtIndex_negative() {
163     try {
164       getList().addAll(-1, MinimalCollection.of(e3()));
165       fail("addAll(-1, e) should throw");
166     } catch (IndexOutOfBoundsException expected) {
167     }
168     expectUnchanged();
169     expectMissing(e3());
170   }
171 
172   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
173   public void testAddAllAtIndex_tooLarge() {
174     try {
175       getList().addAll(getNumElements() + 1, MinimalCollection.of(e3()));
176       fail("addAll(size + 1, e) should throw");
177     } catch (IndexOutOfBoundsException expected) {
178     }
179     expectUnchanged();
180     expectMissing(e3());
181   }
182 }