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;
023import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.collect.testing.AbstractCollectionTester;
027import com.google.common.collect.testing.features.CollectionFeature;
028import com.google.common.collect.testing.features.CollectionSize;
029import java.util.ConcurrentModificationException;
030import java.util.Iterator;
031import org.junit.Ignore;
032
033/**
034 * A generic JUnit test which tests {@code clear()} operations on a collection. Can't be invoked
035 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
036 *
037 * @author George van den Driessche
038 */
039@GwtCompatible
040@Ignore("test runners must not instantiate and run this directly, only via suites we build")
041// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
042@SuppressWarnings("JUnit4ClassUsedInJUnit3")
043public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
044  @CollectionFeature.Require(SUPPORTS_REMOVE)
045  public void testClear() {
046    collection.clear();
047    assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
048    assertEquals(0, collection.size());
049    assertFalse(collection.iterator().hasNext());
050  }
051
052  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
053  @CollectionSize.Require(absent = ZERO)
054  public void testClear_unsupported() {
055    assertThrows(UnsupportedOperationException.class, () -> collection.clear());
056    expectUnchanged();
057  }
058
059  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
060  @CollectionSize.Require(ZERO)
061  public void testClear_unsupportedByEmptyCollection() {
062    try {
063      collection.clear();
064    } catch (UnsupportedOperationException tolerated) {
065    }
066    expectUnchanged();
067  }
068
069  @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
070  @CollectionSize.Require(SEVERAL)
071  public void testClearConcurrentWithIteration() {
072    assertThrows(
073        ConcurrentModificationException.class,
074        () -> {
075          Iterator<E> iterator = collection.iterator();
076          collection.clear();
077          iterator.next();
078        });
079  }
080}