Java

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

24
import com.google.common.annotations.GwtCompatible;
25
import com.google.common.collect.testing.AbstractMapTester;
26
import com.google.common.collect.testing.features.CollectionSize;
27
import com.google.common.collect.testing.features.MapFeature;
28
import java.util.ConcurrentModificationException;
29
import java.util.Iterator;
30
import java.util.Map.Entry;
31
import org.junit.Ignore;
32

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

52
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
53
  @CollectionSize.Require(SEVERAL)
54
  public void testClearConcurrentWithEntrySetIteration() {
55
    try {
56
      Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
57
      getMap().clear();
58
      iterator.next();
59
      fail("Expected ConcurrentModificationException");
60
    } catch (ConcurrentModificationException expected) {
61
      // success
62
    }
63
  }
64

65
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
66
  @CollectionSize.Require(SEVERAL)
67
  public void testClearConcurrentWithKeySetIteration() {
68
    try {
69
      Iterator<K> iterator = getMap().keySet().iterator();
70
      getMap().clear();
71
      iterator.next();
72
      fail("Expected ConcurrentModificationException");
73
    } catch (ConcurrentModificationException expected) {
74
      // success
75
    }
76
  }
77

78
  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
79
  @CollectionSize.Require(SEVERAL)
80
  public void testClearConcurrentWithValuesIteration() {
81
    try {
82
      Iterator<V> iterator = getMap().values().iterator();
83
      getMap().clear();
84
      iterator.next();
85
      fail("Expected ConcurrentModificationException");
86
    } catch (ConcurrentModificationException expected) {
87
      // success
88
    }
89
  }
90

91
  @MapFeature.Require(absent = SUPPORTS_REMOVE)
92
  @CollectionSize.Require(absent = ZERO)
93
  public void testClear_unsupported() {
94
    try {
95
      getMap().clear();
96
      fail(
97
          "clear() should throw UnsupportedOperation if a map does "
98
              + "not support it and is not empty.");
99
    } catch (UnsupportedOperationException expected) {
100
    }
101
    expectUnchanged();
102
  }
103

104
  @MapFeature.Require(absent = SUPPORTS_REMOVE)
105
  @CollectionSize.Require(ZERO)
106
  public void testClear_unsupportedByEmptyCollection() {
107
    try {
108
      getMap().clear();
109
    } catch (UnsupportedOperationException tolerated) {
110
    }
111
    expectUnchanged();
112
  }
113
}
114

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

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

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

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