Java

Форк
0
89 строк · 3.3 Кб
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.CollectionFeature.SUPPORTS_REMOVE;
21
import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23

24
import com.google.common.annotations.GwtCompatible;
25
import com.google.common.collect.testing.AbstractCollectionTester;
26
import com.google.common.collect.testing.features.CollectionFeature;
27
import com.google.common.collect.testing.features.CollectionSize;
28
import java.util.ConcurrentModificationException;
29
import java.util.Iterator;
30
import org.junit.Ignore;
31

32
/**
33
 * A generic JUnit test which tests {@code clear()} operations on a collection. Can't be invoked
34
 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
35
 *
36
 * @author George van den Driessche
37
 */
38
@GwtCompatible
39
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
40
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
41
public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
42
  @CollectionFeature.Require(SUPPORTS_REMOVE)
43
  public void testClear() {
44
    collection.clear();
45
    assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
46
    assertEquals(0, collection.size());
47
    assertFalse(collection.iterator().hasNext());
48
  }
49

50
  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
51
  @CollectionSize.Require(absent = ZERO)
52
  public void testClear_unsupported() {
53
    try {
54
      collection.clear();
55
      fail(
56
          "clear() should throw UnsupportedOperation if a collection does "
57
              + "not support it and is not empty.");
58
    } catch (UnsupportedOperationException expected) {
59
    }
60
    expectUnchanged();
61
  }
62

63
  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
64
  @CollectionSize.Require(ZERO)
65
  public void testClear_unsupportedByEmptyCollection() {
66
    try {
67
      collection.clear();
68
    } catch (UnsupportedOperationException tolerated) {
69
    }
70
    expectUnchanged();
71
  }
72

73
  @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
74
  @CollectionSize.Require(SEVERAL)
75
  public void testClearConcurrentWithIteration() {
76
    try {
77
      Iterator<E> iterator = collection.iterator();
78
      collection.clear();
79
      iterator.next();
80
      /*
81
       * We prefer for iterators to fail immediately on hasNext, but ArrayList
82
       * and LinkedList will notably return true on hasNext here!
83
       */
84
      fail("Expected ConcurrentModificationException");
85
    } catch (ConcurrentModificationException expected) {
86
      // success
87
    }
88
  }
89
}
90

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.