Java

Форк
0
132 строки · 4.9 Кб
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.MapFeature.ALLOWS_NULL_KEYS;
20
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
21

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

33
/**
34
 * Tests {@link java.util.Map#equals}.
35
 *
36
 * @author George van den Driessche
37
 * @author Chris Povirk
38
 */
39
@GwtCompatible
40
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
41
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
42
public class MapEqualsTester<K, V> extends AbstractMapTester<K, V> {
43
  public void testEquals_otherMapWithSameEntries() {
44
    assertTrue(
45
        "A Map should equal any other Map containing the same entries.",
46
        getMap().equals(newHashMap(getSampleEntries())));
47
  }
48

49
  @CollectionSize.Require(absent = CollectionSize.ZERO)
50
  public void testEquals_otherMapWithDifferentEntries() {
51
    Map<K, V> other = newHashMap(getSampleEntries(getNumEntries() - 1));
52
    other.put(k3(), v3());
53
    assertFalse(
54
        "A Map should not equal another Map containing different entries.", getMap().equals(other));
55
  }
56

57
  @CollectionSize.Require(absent = CollectionSize.ZERO)
58
  @MapFeature.Require(ALLOWS_NULL_KEYS)
59
  public void testEquals_containingNullKey() {
60
    Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
61
    entries.add(entry(null, v3()));
62

63
    resetContainer(getSubjectGenerator().create(entries.toArray()));
64
    assertTrue(
65
        "A Map should equal any other Map containing the same entries,"
66
            + " even if some keys are null.",
67
        getMap().equals(newHashMap(entries)));
68
  }
69

70
  @CollectionSize.Require(absent = CollectionSize.ZERO)
71
  public void testEquals_otherContainsNullKey() {
72
    Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
73
    entries.add(entry(null, v3()));
74
    Map<K, V> other = newHashMap(entries);
75

76
    assertFalse(
77
        "Two Maps should not be equal if exactly one of them contains a null key.",
78
        getMap().equals(other));
79
  }
80

81
  @CollectionSize.Require(absent = CollectionSize.ZERO)
82
  @MapFeature.Require(ALLOWS_NULL_VALUES)
83
  public void testEquals_containingNullValue() {
84
    Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
85
    entries.add(entry(k3(), null));
86

87
    resetContainer(getSubjectGenerator().create(entries.toArray()));
88
    assertTrue(
89
        "A Map should equal any other Map containing the same entries,"
90
            + " even if some values are null.",
91
        getMap().equals(newHashMap(entries)));
92
  }
93

94
  @CollectionSize.Require(absent = CollectionSize.ZERO)
95
  public void testEquals_otherContainsNullValue() {
96
    Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
97
    entries.add(entry(k3(), null));
98
    Map<K, V> other = newHashMap(entries);
99

100
    assertFalse(
101
        "Two Maps should not be equal if exactly one of them contains a null value.",
102
        getMap().equals(other));
103
  }
104

105
  @CollectionSize.Require(absent = CollectionSize.ZERO)
106
  public void testEquals_smallerMap() {
107
    Collection<Entry<K, V>> fewerEntries = getSampleEntries(getNumEntries() - 1);
108
    assertFalse(
109
        "Maps of different sizes should not be equal.", getMap().equals(newHashMap(fewerEntries)));
110
  }
111

112
  public void testEquals_largerMap() {
113
    Collection<Entry<K, V>> moreEntries = getSampleEntries(getNumEntries() + 1);
114
    assertFalse(
115
        "Maps of different sizes should not be equal.", getMap().equals(newHashMap(moreEntries)));
116
  }
117

118
  public void testEquals_list() {
119
    assertFalse(
120
        "A List should never equal a Map.",
121
        getMap().equals(Helpers.copyToList(getMap().entrySet())));
122
  }
123

124
  private static <K, V> Map<K, V> newHashMap(
125
      Collection<? extends Entry<? extends K, ? extends V>> entries) {
126
    HashMap<K, V> map = new HashMap<>();
127
    for (Entry<? extends K, ? extends V> entry : entries) {
128
      map.put(entry.getKey(), entry.getValue());
129
    }
130
    return map;
131
  }
132
}
133

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

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

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

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