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.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
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_REMOVE_WITH_INDEX;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.testing.Helpers;
026import com.google.common.collect.testing.features.CollectionFeature;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.common.collect.testing.features.ListFeature;
029import java.util.ConcurrentModificationException;
030import java.util.Iterator;
031import java.util.List;
032import org.junit.Ignore;
033
034/**
035 * A generic JUnit test which tests {@code remove(int)} operations on a list. Can't be invoked
036 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
037 *
038 * @author Chris Povirk
039 */
040@GwtCompatible
041@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
042@SuppressWarnings("JUnit4ClassUsedInJUnit3")
043public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> {
044  @ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX)
045  @CollectionSize.Require(absent = ZERO)
046  public void testRemoveAtIndex_unsupported() {
047    try {
048      getList().remove(0);
049      fail("remove(i) should throw");
050    } catch (UnsupportedOperationException expected) {
051    }
052    expectUnchanged();
053  }
054
055  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
056  public void testRemoveAtIndex_negative() {
057    try {
058      getList().remove(-1);
059      fail("remove(-1) should throw");
060    } catch (IndexOutOfBoundsException expected) {
061    }
062    expectUnchanged();
063  }
064
065  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
066  public void testRemoveAtIndex_tooLarge() {
067    try {
068      getList().remove(getNumElements());
069      fail("remove(size) should throw");
070    } catch (IndexOutOfBoundsException expected) {
071    }
072    expectUnchanged();
073  }
074
075  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
076  @CollectionSize.Require(absent = ZERO)
077  public void testRemoveAtIndex_first() {
078    runRemoveTest(0);
079  }
080
081  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
082  @CollectionSize.Require(absent = {ZERO, ONE})
083  public void testRemoveAtIndex_middle() {
084    runRemoveTest(getNumElements() / 2);
085  }
086
087  @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION)
088  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
089  @CollectionSize.Require(absent = ZERO)
090  public void testRemoveAtIndexConcurrentWithIteration() {
091    try {
092      Iterator<E> iterator = collection.iterator();
093      getList().remove(getNumElements() / 2);
094      iterator.next();
095      fail("Expected ConcurrentModificationException");
096    } catch (ConcurrentModificationException expected) {
097      // success
098    }
099  }
100
101  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
102  @CollectionSize.Require(absent = ZERO)
103  public void testRemoveAtIndex_last() {
104    runRemoveTest(getNumElements() - 1);
105  }
106
107  private void runRemoveTest(int index) {
108    assertEquals(
109        Platform.format("remove(%d) should return the element at index %d", index, index),
110        getList().get(index),
111        getList().remove(index));
112    List<E> expected = Helpers.copyToList(createSamplesArray());
113    expected.remove(index);
114    expectContents(expected);
115  }
116}