guava

Форк
0
174 строки · 6.3 Кб
1
/*
2
 * Copyright (C) 2013 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.SUPPORTS_ITERATOR_REMOVE;
20
import static com.google.common.collect.testing.features.CollectionSize.ONE;
21
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
23
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
24
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
25
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
26
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
27

28
import com.google.common.annotations.GwtCompatible;
29
import com.google.common.annotations.GwtIncompatible;
30
import com.google.common.annotations.J2ktIncompatible;
31
import com.google.common.collect.testing.AbstractMapTester;
32
import com.google.common.collect.testing.Helpers;
33
import com.google.common.collect.testing.features.CollectionFeature;
34
import com.google.common.collect.testing.features.CollectionSize;
35
import com.google.common.collect.testing.features.MapFeature;
36
import java.lang.reflect.Method;
37
import java.util.Iterator;
38
import java.util.Map.Entry;
39
import java.util.Set;
40
import org.junit.Ignore;
41

42
/**
43
 * Tests {@link java.util.Map#entrySet}.
44
 *
45
 * @author Louis Wasserman
46
 * @param <K> The key type of the map implementation under test.
47
 * @param <V> The value type of the map implementation under test.
48
 */
49
@GwtCompatible
50
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
51
public class MapEntrySetTester<K, V> extends AbstractMapTester<K, V> {
52
  private enum IncomparableType {
53
    INSTANCE;
54
  }
55

56
  @CollectionSize.Require(ONE)
57
  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
58
  public void testEntrySetIteratorRemove() {
59
    Set<Entry<K, V>> entrySet = getMap().entrySet();
60
    Iterator<Entry<K, V>> entryItr = entrySet.iterator();
61
    assertEquals(e0(), entryItr.next());
62
    entryItr.remove();
63
    assertTrue(getMap().isEmpty());
64
    assertFalse(entrySet.contains(e0()));
65
  }
66

67
  public void testContainsEntryWithIncomparableKey() {
68
    try {
69
      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0())));
70
    } catch (ClassCastException acceptable) {
71
      // allowed by the spec
72
    }
73
  }
74

75
  public void testContainsEntryWithIncomparableValue() {
76
    try {
77
      assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), IncomparableType.INSTANCE)));
78
    } catch (ClassCastException acceptable) {
79
      // allowed by the spec
80
    }
81
  }
82

83
  @MapFeature.Require(ALLOWS_NULL_KEY_QUERIES)
84
  public void testContainsEntryWithNullKeyAbsent() {
85
    assertFalse(getMap().entrySet().contains(Helpers.mapEntry(null, v0())));
86
  }
87

88
  @CollectionSize.Require(absent = ZERO)
89
  @MapFeature.Require(ALLOWS_NULL_KEYS)
90
  public void testContainsEntryWithNullKeyPresent() {
91
    initMapWithNullKey();
92
    assertTrue(getMap().entrySet().contains(Helpers.mapEntry(null, getValueForNullKey())));
93
  }
94

95
  @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
96
  public void testContainsEntryWithNullValueAbsent() {
97
    assertFalse(getMap().entrySet().contains(Helpers.mapEntry(k0(), null)));
98
  }
99

100
  @CollectionSize.Require(absent = ZERO)
101
  @MapFeature.Require(ALLOWS_NULL_VALUES)
102
  public void testContainsEntryWithNullValuePresent() {
103
    initMapWithNullValue();
104
    assertTrue(getMap().entrySet().contains(Helpers.mapEntry(getKeyForNullValue(), null)));
105
  }
106

107
  @MapFeature.Require(SUPPORTS_PUT)
108
  @CollectionSize.Require(absent = ZERO)
109
  public void testSetValue() {
110
    for (Entry<K, V> entry : getMap().entrySet()) {
111
      if (entry.getKey().equals(k0())) {
112
        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3()));
113
        break;
114
      }
115
    }
116
    expectReplacement(entry(k0(), v3()));
117
  }
118

119
  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
120
  @CollectionSize.Require(absent = ZERO)
121
  public void testSetValueWithNullValuesPresent() {
122
    for (Entry<K, V> entry : getMap().entrySet()) {
123
      if (entry.getKey().equals(k0())) {
124
        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(null));
125
        break;
126
      }
127
    }
128
    expectReplacement(entry(k0(), (V) null));
129
  }
130

131
  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
132
  @CollectionSize.Require(absent = ZERO)
133
  public void testSetValueWithNullValuesAbsent() {
134
    for (Entry<K, V> entry : getMap().entrySet()) {
135
      try {
136
        entry.setValue(null);
137
        fail("Expected NullPointerException");
138
      } catch (NullPointerException exception) {
139
        break;
140
      }
141
    }
142
    expectUnchanged();
143
  }
144

145
  @J2ktIncompatible
146
  @GwtIncompatible // reflection
147
  public static Method getContainsEntryWithIncomparableKeyMethod() {
148
    return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableKey");
149
  }
150

151
  @J2ktIncompatible
152
  @GwtIncompatible // reflection
153
  public static Method getContainsEntryWithIncomparableValueMethod() {
154
    return Helpers.getMethod(MapEntrySetTester.class, "testContainsEntryWithIncomparableValue");
155
  }
156

157
  @J2ktIncompatible
158
  @GwtIncompatible // reflection
159
  public static Method getSetValueMethod() {
160
    return Helpers.getMethod(MapEntrySetTester.class, "testSetValue");
161
  }
162

163
  @J2ktIncompatible
164
  @GwtIncompatible // reflection
165
  public static Method getSetValueWithNullValuesPresentMethod() {
166
    return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesPresent");
167
  }
168

169
  @J2ktIncompatible
170
  @GwtIncompatible // reflection
171
  public static Method getSetValueWithNullValuesAbsentMethod() {
172
    return Helpers.getMethod(MapEntrySetTester.class, "testSetValueWithNullValuesAbsent");
173
  }
174
}
175

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

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

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

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