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.CollectionFeature.SUPPORTS_REMOVE;
021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
022import static com.google.common.collect.testing.features.CollectionSize.ZERO;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.testing.AbstractCollectionTester;
026import com.google.common.collect.testing.features.CollectionFeature;
027import com.google.common.collect.testing.features.CollectionSize;
028import java.util.ConcurrentModificationException;
029import java.util.Iterator;
030import org.junit.Ignore;
031
032/**
033 * A generic JUnit test which tests {@code clear()} operations on a collection. Can't be invoked
034 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
035 *
036 * @author George van den Driessche
037 */
038@GwtCompatible
039@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
040@SuppressWarnings("JUnit4ClassUsedInJUnit3")
041public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
042  @CollectionFeature.Require(SUPPORTS_REMOVE)
043  public void testClear() {
044    collection.clear();
045    assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
046    assertEquals(0, collection.size());
047    assertFalse(collection.iterator().hasNext());
048  }
049
050  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
051  @CollectionSize.Require(absent = ZERO)
052  public void testClear_unsupported() {
053    try {
054      collection.clear();
055      fail(
056          "clear() should throw UnsupportedOperation if a collection does "
057              + "not support it and is not empty.");
058    } catch (UnsupportedOperationException expected) {
059    }
060    expectUnchanged();
061  }
062
063  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
064  @CollectionSize.Require(ZERO)
065  public void testClear_unsupportedByEmptyCollection() {
066    try {
067      collection.clear();
068    } catch (UnsupportedOperationException tolerated) {
069    }
070    expectUnchanged();
071  }
072
073  @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
074  @CollectionSize.Require(SEVERAL)
075  public void testClearConcurrentWithIteration() {
076    try {
077      Iterator<E> iterator = collection.iterator();
078      collection.clear();
079      iterator.next();
080      /*
081       * We prefer for iterators to fail immediately on hasNext, but ArrayList
082       * and LinkedList will notably return true on hasNext here!
083       */
084      fail("Expected ConcurrentModificationException");
085    } catch (ConcurrentModificationException expected) {
086      // success
087    }
088  }
089}