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.
040public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
041  @CollectionFeature.Require(SUPPORTS_REMOVE)
042  public void testClear() {
043    collection.clear();
044    assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
045    assertEquals(0, collection.size());
046    assertFalse(collection.iterator().hasNext());
047  }
048
049  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
050  @CollectionSize.Require(absent = ZERO)
051  public void testClear_unsupported() {
052    try {
053      collection.clear();
054      fail(
055          "clear() should throw UnsupportedOperation if a collection does "
056              + "not support it and is not empty.");
057    } catch (UnsupportedOperationException expected) {
058    }
059    expectUnchanged();
060  }
061
062  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
063  @CollectionSize.Require(ZERO)
064  public void testClear_unsupportedByEmptyCollection() {
065    try {
066      collection.clear();
067    } catch (UnsupportedOperationException tolerated) {
068    }
069    expectUnchanged();
070  }
071
072  @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
073  @CollectionSize.Require(SEVERAL)
074  public void testClearConcurrentWithIteration() {
075    try {
076      Iterator<E> iterator = collection.iterator();
077      collection.clear();
078      iterator.next();
079      /*
080       * We prefer for iterators to fail immediately on hasNext, but ArrayList
081       * and LinkedList will notably return true on hasNext here!
082       */
083      fail("Expected ConcurrentModificationException");
084    } catch (ConcurrentModificationException expected) {
085      // success
086    }
087  }
088}