Java
157 строк · 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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionSize.ONE;
20import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
23import static com.google.common.collect.testing.features.MapFeature.REJECTS_DUPLICATES_AT_CREATION;
24
25import com.google.common.annotations.GwtCompatible;
26import com.google.common.annotations.GwtIncompatible;
27import com.google.common.annotations.J2ktIncompatible;
28import com.google.common.collect.testing.AbstractMapTester;
29import com.google.common.collect.testing.Helpers;
30import com.google.common.collect.testing.features.CollectionSize;
31import com.google.common.collect.testing.features.MapFeature;
32import java.lang.reflect.Method;
33import java.util.Arrays;
34import java.util.List;
35import java.util.Map.Entry;
36import 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@SuppressWarnings("JUnit4ClassUsedInJUnit3")
49public class MapCreationTester<K, V> extends AbstractMapTester<K, V> {
50@MapFeature.Require(ALLOWS_NULL_KEYS)
51@CollectionSize.Require(absent = ZERO)
52public void testCreateWithNullKeySupported() {
53initMapWithNullKey();
54expectContents(createArrayWithNullKey());
55}
56
57@MapFeature.Require(absent = ALLOWS_NULL_KEYS)
58@CollectionSize.Require(absent = ZERO)
59public void testCreateWithNullKeyUnsupported() {
60try {
61initMapWithNullKey();
62fail("Creating a map containing a null key should fail");
63} catch (NullPointerException expected) {
64}
65}
66
67@MapFeature.Require(ALLOWS_NULL_VALUES)
68@CollectionSize.Require(absent = ZERO)
69public void testCreateWithNullValueSupported() {
70initMapWithNullValue();
71expectContents(createArrayWithNullValue());
72}
73
74@MapFeature.Require(absent = ALLOWS_NULL_VALUES)
75@CollectionSize.Require(absent = ZERO)
76public void testCreateWithNullValueUnsupported() {
77try {
78initMapWithNullValue();
79fail("Creating a map containing a null value should fail");
80} catch (NullPointerException expected) {
81}
82}
83
84@MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
85@CollectionSize.Require(absent = ZERO)
86public void testCreateWithNullKeyAndValueSupported() {
87Entry<K, V>[] entries = createSamplesArray();
88entries[getNullLocation()] = entry(null, null);
89resetMap(entries);
90expectContents(entries);
91}
92
93@MapFeature.Require(value = ALLOWS_NULL_KEYS, absent = REJECTS_DUPLICATES_AT_CREATION)
94@CollectionSize.Require(absent = {ZERO, ONE})
95public void testCreateWithDuplicates_nullDuplicatesNotRejected() {
96expectFirstRemoved(getEntriesMultipleNullKeys());
97}
98
99@MapFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION)
100@CollectionSize.Require(absent = {ZERO, ONE})
101public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() {
102expectFirstRemoved(getEntriesMultipleNonNullKeys());
103}
104
105@MapFeature.Require({ALLOWS_NULL_KEYS, REJECTS_DUPLICATES_AT_CREATION})
106@CollectionSize.Require(absent = {ZERO, ONE})
107public void testCreateWithDuplicates_nullDuplicatesRejected() {
108Entry<K, V>[] entries = getEntriesMultipleNullKeys();
109try {
110resetMap(entries);
111fail("Should reject duplicate null elements at creation");
112} catch (IllegalArgumentException expected) {
113}
114}
115
116@MapFeature.Require(REJECTS_DUPLICATES_AT_CREATION)
117@CollectionSize.Require(absent = {ZERO, ONE})
118public void testCreateWithDuplicates_nonNullDuplicatesRejected() {
119Entry<K, V>[] entries = getEntriesMultipleNonNullKeys();
120try {
121resetMap(entries);
122fail("Should reject duplicate non-null elements at creation");
123} catch (IllegalArgumentException expected) {
124}
125}
126
127private Entry<K, V>[] getEntriesMultipleNullKeys() {
128Entry<K, V>[] entries = createArrayWithNullKey();
129entries[0] = entry(null, entries[0].getValue());
130return entries;
131}
132
133private Entry<K, V>[] getEntriesMultipleNonNullKeys() {
134Entry<K, V>[] entries = createSamplesArray();
135entries[0] = entry(k1(), v0());
136return entries;
137}
138
139private void expectFirstRemoved(Entry<K, V>[] entries) {
140resetMap(entries);
141
142List<Entry<K, V>> expectedWithDuplicateRemoved =
143Arrays.asList(entries).subList(1, getNumElements());
144expectContents(expectedWithDuplicateRemoved);
145}
146
147/**
148* Returns the {@link Method} instance for {@link #testCreateWithNullKeyUnsupported()} so that
149* tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
150* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
151*/
152@J2ktIncompatible
153@GwtIncompatible // reflection
154public static Method getCreateWithNullKeyUnsupportedMethod() {
155return Helpers.getMethod(MapCreationTester.class, "testCreateWithNullKeyUnsupported");
156}
157}
158