guava

Форк
0
156 строк · 5.8 Кб
1
/*
2
 * Copyright (C) 2007 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.ONE;
20
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
22
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
23
import static com.google.common.collect.testing.features.MapFeature.REJECTS_DUPLICATES_AT_CREATION;
24

25
import com.google.common.annotations.GwtCompatible;
26
import com.google.common.annotations.GwtIncompatible;
27
import com.google.common.annotations.J2ktIncompatible;
28
import com.google.common.collect.testing.AbstractMapTester;
29
import com.google.common.collect.testing.Helpers;
30
import com.google.common.collect.testing.features.CollectionSize;
31
import com.google.common.collect.testing.features.MapFeature;
32
import java.lang.reflect.Method;
33
import java.util.Arrays;
34
import java.util.List;
35
import java.util.Map.Entry;
36
import org.junit.Ignore;
37

38
/**
39
 * A generic JUnit test which tests creation (typically through a constructor or static factory
40
 * method) of a map. Can't be invoked directly; please see {@link
41
 * com.google.common.collect.testing.MapTestSuiteBuilder}.
42
 *
43
 * @author Chris Povirk
44
 * @author Kevin Bourrillion
45
 */
46
@GwtCompatible(emulated = true)
47
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
48
public class MapCreationTester<K, V> extends AbstractMapTester<K, V> {
49
  @MapFeature.Require(ALLOWS_NULL_KEYS)
50
  @CollectionSize.Require(absent = ZERO)
51
  public void testCreateWithNullKeySupported() {
52
    initMapWithNullKey();
53
    expectContents(createArrayWithNullKey());
54
  }
55

56
  @MapFeature.Require(absent = ALLOWS_NULL_KEYS)
57
  @CollectionSize.Require(absent = ZERO)
58
  public void testCreateWithNullKeyUnsupported() {
59
    try {
60
      initMapWithNullKey();
61
      fail("Creating a map containing a null key should fail");
62
    } catch (NullPointerException expected) {
63
    }
64
  }
65

66
  @MapFeature.Require(ALLOWS_NULL_VALUES)
67
  @CollectionSize.Require(absent = ZERO)
68
  public void testCreateWithNullValueSupported() {
69
    initMapWithNullValue();
70
    expectContents(createArrayWithNullValue());
71
  }
72

73
  @MapFeature.Require(absent = ALLOWS_NULL_VALUES)
74
  @CollectionSize.Require(absent = ZERO)
75
  public void testCreateWithNullValueUnsupported() {
76
    try {
77
      initMapWithNullValue();
78
      fail("Creating a map containing a null value should fail");
79
    } catch (NullPointerException expected) {
80
    }
81
  }
82

83
  @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
84
  @CollectionSize.Require(absent = ZERO)
85
  public void testCreateWithNullKeyAndValueSupported() {
86
    Entry<K, V>[] entries = createSamplesArray();
87
    entries[getNullLocation()] = entry(null, null);
88
    resetMap(entries);
89
    expectContents(entries);
90
  }
91

92
  @MapFeature.Require(value = ALLOWS_NULL_KEYS, absent = REJECTS_DUPLICATES_AT_CREATION)
93
  @CollectionSize.Require(absent = {ZERO, ONE})
94
  public void testCreateWithDuplicates_nullDuplicatesNotRejected() {
95
    expectFirstRemoved(getEntriesMultipleNullKeys());
96
  }
97

98
  @MapFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION)
99
  @CollectionSize.Require(absent = {ZERO, ONE})
100
  public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() {
101
    expectFirstRemoved(getEntriesMultipleNonNullKeys());
102
  }
103

104
  @MapFeature.Require({ALLOWS_NULL_KEYS, REJECTS_DUPLICATES_AT_CREATION})
105
  @CollectionSize.Require(absent = {ZERO, ONE})
106
  public void testCreateWithDuplicates_nullDuplicatesRejected() {
107
    Entry<K, V>[] entries = getEntriesMultipleNullKeys();
108
    try {
109
      resetMap(entries);
110
      fail("Should reject duplicate null elements at creation");
111
    } catch (IllegalArgumentException expected) {
112
    }
113
  }
114

115
  @MapFeature.Require(REJECTS_DUPLICATES_AT_CREATION)
116
  @CollectionSize.Require(absent = {ZERO, ONE})
117
  public void testCreateWithDuplicates_nonNullDuplicatesRejected() {
118
    Entry<K, V>[] entries = getEntriesMultipleNonNullKeys();
119
    try {
120
      resetMap(entries);
121
      fail("Should reject duplicate non-null elements at creation");
122
    } catch (IllegalArgumentException expected) {
123
    }
124
  }
125

126
  private Entry<K, V>[] getEntriesMultipleNullKeys() {
127
    Entry<K, V>[] entries = createArrayWithNullKey();
128
    entries[0] = entry(null, entries[0].getValue());
129
    return entries;
130
  }
131

132
  private Entry<K, V>[] getEntriesMultipleNonNullKeys() {
133
    Entry<K, V>[] entries = createSamplesArray();
134
    entries[0] = entry(k1(), v0());
135
    return entries;
136
  }
137

138
  private void expectFirstRemoved(Entry<K, V>[] entries) {
139
    resetMap(entries);
140

141
    List<Entry<K, V>> expectedWithDuplicateRemoved =
142
        Arrays.asList(entries).subList(1, getNumElements());
143
    expectContents(expectedWithDuplicateRemoved);
144
  }
145

146
  /**
147
   * Returns the {@link Method} instance for {@link #testCreateWithNullKeyUnsupported()} so that
148
   * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
149
   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
150
   */
151
  @J2ktIncompatible
152
  @GwtIncompatible // reflection
153
  public static Method getCreateWithNullKeyUnsupportedMethod() {
154
    return Helpers.getMethod(MapCreationTester.class, "testCreateWithNullKeyUnsupported");
155
  }
156
}
157

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

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

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

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