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.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
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_REMOVE_WITH_INDEX;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.testing.Helpers;
26  import com.google.common.collect.testing.features.CollectionFeature;
27  import com.google.common.collect.testing.features.CollectionSize;
28  import com.google.common.collect.testing.features.ListFeature;
29  import java.util.ConcurrentModificationException;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * A generic JUnit test which tests {@code remove(int)} operations on a list.
35   * Can't be invoked directly; please see
36   * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
37   *
38   * @author Chris Povirk
39   */
40  @GwtCompatible
41  public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> {
42    @ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX)
43    @CollectionSize.Require(absent = ZERO)
44    public void testRemoveAtIndex_unsupported() {
45      try {
46        getList().remove(0);
47        fail("remove(i) should throw");
48      } catch (UnsupportedOperationException expected) {
49      }
50      expectUnchanged();
51    }
52  
53    @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
54    public void testRemoveAtIndex_negative() {
55      try {
56        getList().remove(-1);
57        fail("remove(-1) should throw");
58      } catch (IndexOutOfBoundsException expected) {
59      }
60      expectUnchanged();
61    }
62  
63    @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
64    public void testRemoveAtIndex_tooLarge() {
65      try {
66        getList().remove(getNumElements());
67        fail("remove(size) should throw");
68      } catch (IndexOutOfBoundsException expected) {
69      }
70      expectUnchanged();
71    }
72  
73    @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
74    @CollectionSize.Require(absent = ZERO)
75    public void testRemoveAtIndex_first() {
76      runRemoveTest(0);
77    }
78  
79    @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
80    @CollectionSize.Require(absent = {ZERO, ONE})
81    public void testRemoveAtIndex_middle() {
82      runRemoveTest(getNumElements() / 2);
83    }
84  
85    @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION)
86    @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
87    @CollectionSize.Require(absent = ZERO)
88    public void testRemoveAtIndexConcurrentWithIteration() {
89      try {
90        Iterator<E> iterator = collection.iterator();
91        getList().remove(getNumElements() / 2);
92        iterator.next();
93        fail("Expected ConcurrentModificationException");
94      } catch (ConcurrentModificationException expected) {
95        // success
96      }
97    }
98  
99    @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
100   @CollectionSize.Require(absent = ZERO)
101   public void testRemoveAtIndex_last() {
102     runRemoveTest(getNumElements() - 1);
103   }
104 
105   private void runRemoveTest(int index) {
106     assertEquals(
107         Platform.format("remove(%d) should return the element at index %d", index, index),
108         getList().get(index),
109         getList().remove(index));
110     List<E> expected = Helpers.copyToList(createSamplesArray());
111     expected.remove(index);
112     expectContents(expected);
113   }
114 }