001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
020import static com.google.common.collect.testing.features.CollectionSize.ONE;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX;
023import static java.util.Collections.singletonList;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.collect.testing.MinimalCollection;
027import com.google.common.collect.testing.features.CollectionFeature;
028import com.google.common.collect.testing.features.CollectionSize;
029import com.google.common.collect.testing.features.ListFeature;
030import java.util.List;
031import org.junit.Ignore;
032
033/**
034 * A generic JUnit test which tests {@code addAll(int, Collection)} operations on a list. Can't be
035 * invoked directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
036 *
037 * @author Chris Povirk
038 */
039@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
040@GwtCompatible
041@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
042public class ListAddAllAtIndexTester<E> extends AbstractListTester<E> {
043  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
044  @CollectionSize.Require(absent = ZERO)
045  public void testAddAllAtIndex_supportedAllPresent() {
046    assertTrue(
047        "addAll(n, allPresent) should return true",
048        getList().addAll(0, MinimalCollection.of(e0())));
049    expectAdded(0, e0());
050  }
051
052  @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
053  @CollectionSize.Require(absent = ZERO)
054  public void testAddAllAtIndex_unsupportedAllPresent() {
055    try {
056      getList().addAll(0, MinimalCollection.of(e0()));
057      fail("addAll(n, allPresent) should throw");
058    } catch (UnsupportedOperationException expected) {
059    }
060    expectUnchanged();
061  }
062
063  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
064  @CollectionSize.Require(absent = ZERO)
065  public void testAddAllAtIndex_supportedSomePresent() {
066    assertTrue(
067        "addAll(n, allPresent) should return true",
068        getList().addAll(0, MinimalCollection.of(e0(), e3())));
069    expectAdded(0, e0(), e3());
070  }
071
072  @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
073  @CollectionSize.Require(absent = ZERO)
074  public void testAddAllAtIndex_unsupportedSomePresent() {
075    try {
076      getList().addAll(0, MinimalCollection.of(e0(), e3()));
077      fail("addAll(n, allPresent) should throw");
078    } catch (UnsupportedOperationException expected) {
079    }
080    expectUnchanged();
081    expectMissing(e3());
082  }
083
084  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
085  public void testAddAllAtIndex_supportedNothing() {
086    assertFalse("addAll(n, nothing) should return false", getList().addAll(0, emptyCollection()));
087    expectUnchanged();
088  }
089
090  @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
091  public void testAddAllAtIndex_unsupportedNothing() {
092    try {
093      assertFalse(
094          "addAll(n, nothing) should return false or throw",
095          getList().addAll(0, emptyCollection()));
096    } catch (UnsupportedOperationException tolerated) {
097    }
098    expectUnchanged();
099  }
100
101  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
102  public void testAddAllAtIndex_withDuplicates() {
103    MinimalCollection<E> elementsToAdd = MinimalCollection.of(e0(), e1(), e0(), e1());
104    assertTrue("addAll(n, hasDuplicates) should return true", getList().addAll(0, elementsToAdd));
105    expectAdded(0, e0(), e1(), e0(), e1());
106  }
107
108  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
109  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
110  public void testAddAllAtIndex_nullSupported() {
111    List<E> containsNull = singletonList(null);
112    assertTrue("addAll(n, containsNull) should return true", getList().addAll(0, containsNull));
113    /*
114     * We need (E) to force interpretation of null as the single element of a
115     * varargs array, not the array itself
116     */
117    expectAdded(0, (E) null);
118  }
119
120  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
121  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
122  public void testAddAllAtIndex_nullUnsupported() {
123    List<E> containsNull = singletonList(null);
124    try {
125      getList().addAll(0, containsNull);
126      fail("addAll(n, containsNull) should throw");
127    } catch (NullPointerException expected) {
128    }
129    expectUnchanged();
130    expectNullMissingWhenNullUnsupported(
131        "Should not contain null after unsupported addAll(n, containsNull)");
132  }
133
134  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
135  @CollectionSize.Require(absent = {ZERO, ONE})
136  public void testAddAllAtIndex_middle() {
137    assertTrue(
138        "addAll(middle, disjoint) should return true",
139        getList().addAll(getNumElements() / 2, createDisjointCollection()));
140    expectAdded(getNumElements() / 2, createDisjointCollection());
141  }
142
143  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
144  @CollectionSize.Require(absent = ZERO)
145  public void testAddAllAtIndex_end() {
146    assertTrue(
147        "addAll(end, disjoint) should return true",
148        getList().addAll(getNumElements(), createDisjointCollection()));
149    expectAdded(getNumElements(), createDisjointCollection());
150  }
151
152  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
153  public void testAddAllAtIndex_nullCollectionReference() {
154    try {
155      getList().addAll(0, null);
156      fail("addAll(n, null) should throw");
157    } catch (NullPointerException expected) {
158    }
159    expectUnchanged();
160  }
161
162  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
163  public void testAddAllAtIndex_negative() {
164    try {
165      getList().addAll(-1, MinimalCollection.of(e3()));
166      fail("addAll(-1, e) should throw");
167    } catch (IndexOutOfBoundsException expected) {
168    }
169    expectUnchanged();
170    expectMissing(e3());
171  }
172
173  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
174  public void testAddAllAtIndex_tooLarge() {
175    try {
176      getList().addAll(getNumElements() + 1, MinimalCollection.of(e3()));
177      fail("addAll(size + 1, e) should throw");
178    } catch (IndexOutOfBoundsException expected) {
179    }
180    expectUnchanged();
181    expectMissing(e3());
182  }
183}