ListAddAtIndexTester.java revision 3c77433663281544363151bf284b0240dfd22a42
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
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.CollectionSize.ONE;
22import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX;
24
25import com.google.common.annotations.GwtCompatible;
26import com.google.common.annotations.GwtIncompatible;
27import com.google.common.collect.testing.Helpers;
28import com.google.common.collect.testing.features.CollectionFeature;
29import com.google.common.collect.testing.features.CollectionSize;
30import com.google.common.collect.testing.features.ListFeature;
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(int, Object)} operations on a
38 * list. Can't be invoked directly; please see
39 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
40 *
41 * <p>This class is GWT compatible.
42 *
43 * @author Chris Povirk
44 */
45@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
46@GwtCompatible(emulated = true)
47public class ListAddAtIndexTester<E> extends AbstractListTester<E> {
48  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
49  @CollectionSize.Require(absent = ZERO)
50  public void testAddAtIndex_supportedPresent() {
51    getList().add(0, samples.e0);
52    expectAdded(0, samples.e0);
53  }
54
55  @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
56  @CollectionSize.Require(absent = ZERO)
57  /*
58   * absent = ZERO isn't required, since unmodList.add() must
59   * throw regardless, but it keeps the method name accurate.
60   */
61  public void testAddAtIndex_unsupportedPresent() {
62    try {
63      getList().add(0, samples.e0);
64      fail("add(n, present) should throw");
65    } catch (UnsupportedOperationException expected) {
66    }
67    expectUnchanged();
68  }
69
70  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
71  public void testAddAtIndex_supportedNotPresent() {
72    getList().add(0, samples.e3);
73    expectAdded(0, samples.e3);
74  }
75
76  @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION)
77  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
78  public void testAddAtIndexConcurrentWithIteration() {
79    try {
80      Iterator<E> iterator = collection.iterator();
81      getList().add(0, samples.e3);
82      iterator.next();
83      fail("Expected ConcurrentModificationException");
84    } catch (ConcurrentModificationException expected) {
85      // success
86    }
87  }
88
89  @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
90  public void testAddAtIndex_unsupportedNotPresent() {
91    try {
92      getList().add(0, samples.e3);
93      fail("add(n, notPresent) should throw");
94    } catch (UnsupportedOperationException expected) {
95    }
96    expectUnchanged();
97    expectMissing(samples.e3);
98  }
99
100  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
101  @CollectionSize.Require(absent = {ZERO, ONE})
102  public void testAddAtIndex_middle() {
103    getList().add(getNumElements() / 2, samples.e3);
104    expectAdded(getNumElements() / 2, samples.e3);
105  }
106
107  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
108  @CollectionSize.Require(absent = ZERO)
109  public void testAddAtIndex_end() {
110    getList().add(getNumElements(), samples.e3);
111    expectAdded(getNumElements(), samples.e3);
112  }
113
114  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
115  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
116  public void testAddAtIndex_nullSupported() {
117    getList().add(0, null);
118    expectAdded(0, (E) null);
119  }
120
121  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
122  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
123  public void testAddAtIndex_nullUnsupported() {
124    try {
125      getList().add(0, null);
126      fail("add(n, null) should throw");
127    } catch (NullPointerException expected) {
128    }
129    expectUnchanged();
130    expectNullMissingWhenNullUnsupported(
131        "Should not contain null after unsupported add(n, null)");
132  }
133
134  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
135  public void testAddAtIndex_negative() {
136    try {
137      getList().add(-1, samples.e3);
138      fail("add(-1, e) should throw");
139    } catch (IndexOutOfBoundsException expected) {
140    }
141    expectUnchanged();
142    expectMissing(samples.e3);
143  }
144
145  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
146  public void testAddAtIndex_tooLarge() {
147    try {
148      getList().add(getNumElements() + 1, samples.e3);
149      fail("add(size + 1, e) should throw");
150    } catch (IndexOutOfBoundsException expected) {
151    }
152    expectUnchanged();
153    expectMissing(samples.e3);
154  }
155
156  /**
157   * Returns the {@link Method} instance for
158   * {@link #testAddAtIndex_nullSupported()} so that tests can suppress it. See
159   * {@link CollectionAddTester#getAddNullSupportedMethod()} for details.
160   */
161  @GwtIncompatible("reflection")
162  public static Method getAddNullSupportedMethod() {
163    return Helpers.getMethod(
164        ListAddAtIndexTester.class, "testAddAtIndex_nullSupported");
165  }
166}
167