• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.collect.testing.AbstractMapTester;
28 import com.google.common.collect.testing.Helpers;
29 import com.google.common.collect.testing.features.CollectionSize;
30 import com.google.common.collect.testing.features.MapFeature;
31 import java.lang.reflect.Method;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Map.Entry;
35 import org.junit.Ignore;
36 
37 /**
38  * A generic JUnit test which tests creation (typically through a constructor or static factory
39  * method) of a map. Can't be invoked directly; please see {@link
40  * com.google.common.collect.testing.MapTestSuiteBuilder}.
41  *
42  * @author Chris Povirk
43  * @author Kevin Bourrillion
44  */
45 @GwtCompatible(emulated = true)
46 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
47 public class MapCreationTester<K, V> extends AbstractMapTester<K, V> {
48   @MapFeature.Require(ALLOWS_NULL_KEYS)
49   @CollectionSize.Require(absent = ZERO)
testCreateWithNullKeySupported()50   public void testCreateWithNullKeySupported() {
51     initMapWithNullKey();
52     expectContents(createArrayWithNullKey());
53   }
54 
55   @MapFeature.Require(absent = ALLOWS_NULL_KEYS)
56   @CollectionSize.Require(absent = ZERO)
testCreateWithNullKeyUnsupported()57   public void testCreateWithNullKeyUnsupported() {
58     try {
59       initMapWithNullKey();
60       fail("Creating a map containing a null key should fail");
61     } catch (NullPointerException expected) {
62     }
63   }
64 
65   @MapFeature.Require(ALLOWS_NULL_VALUES)
66   @CollectionSize.Require(absent = ZERO)
testCreateWithNullValueSupported()67   public void testCreateWithNullValueSupported() {
68     initMapWithNullValue();
69     expectContents(createArrayWithNullValue());
70   }
71 
72   @MapFeature.Require(absent = ALLOWS_NULL_VALUES)
73   @CollectionSize.Require(absent = ZERO)
testCreateWithNullValueUnsupported()74   public void testCreateWithNullValueUnsupported() {
75     try {
76       initMapWithNullValue();
77       fail("Creating a map containing a null value should fail");
78     } catch (NullPointerException expected) {
79     }
80   }
81 
82   @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
83   @CollectionSize.Require(absent = ZERO)
testCreateWithNullKeyAndValueSupported()84   public void testCreateWithNullKeyAndValueSupported() {
85     Entry<K, V>[] entries = createSamplesArray();
86     entries[getNullLocation()] = entry(null, null);
87     resetMap(entries);
88     expectContents(entries);
89   }
90 
91   @MapFeature.Require(value = ALLOWS_NULL_KEYS, absent = REJECTS_DUPLICATES_AT_CREATION)
92   @CollectionSize.Require(absent = {ZERO, ONE})
testCreateWithDuplicates_nullDuplicatesNotRejected()93   public void testCreateWithDuplicates_nullDuplicatesNotRejected() {
94     expectFirstRemoved(getEntriesMultipleNullKeys());
95   }
96 
97   @MapFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION)
98   @CollectionSize.Require(absent = {ZERO, ONE})
testCreateWithDuplicates_nonNullDuplicatesNotRejected()99   public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() {
100     expectFirstRemoved(getEntriesMultipleNonNullKeys());
101   }
102 
103   @MapFeature.Require({ALLOWS_NULL_KEYS, REJECTS_DUPLICATES_AT_CREATION})
104   @CollectionSize.Require(absent = {ZERO, ONE})
testCreateWithDuplicates_nullDuplicatesRejected()105   public void testCreateWithDuplicates_nullDuplicatesRejected() {
106     Entry<K, V>[] entries = getEntriesMultipleNullKeys();
107     try {
108       resetMap(entries);
109       fail("Should reject duplicate null elements at creation");
110     } catch (IllegalArgumentException expected) {
111     }
112   }
113 
114   @MapFeature.Require(REJECTS_DUPLICATES_AT_CREATION)
115   @CollectionSize.Require(absent = {ZERO, ONE})
testCreateWithDuplicates_nonNullDuplicatesRejected()116   public void testCreateWithDuplicates_nonNullDuplicatesRejected() {
117     Entry<K, V>[] entries = getEntriesMultipleNonNullKeys();
118     try {
119       resetMap(entries);
120       fail("Should reject duplicate non-null elements at creation");
121     } catch (IllegalArgumentException expected) {
122     }
123   }
124 
getEntriesMultipleNullKeys()125   private Entry<K, V>[] getEntriesMultipleNullKeys() {
126     Entry<K, V>[] entries = createArrayWithNullKey();
127     entries[0] = entry(null, entries[0].getValue());
128     return entries;
129   }
130 
getEntriesMultipleNonNullKeys()131   private Entry<K, V>[] getEntriesMultipleNonNullKeys() {
132     Entry<K, V>[] entries = createSamplesArray();
133     entries[0] = entry(k1(), v0());
134     return entries;
135   }
136 
expectFirstRemoved(Entry<K, V>[] entries)137   private void expectFirstRemoved(Entry<K, V>[] entries) {
138     resetMap(entries);
139 
140     List<Entry<K, V>> expectedWithDuplicateRemoved =
141         Arrays.asList(entries).subList(1, getNumElements());
142     expectContents(expectedWithDuplicateRemoved);
143   }
144 
145   /**
146    * Returns the {@link Method} instance for {@link #testCreateWithNullKeyUnsupported()} so that
147    * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
148    * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
149    */
150   @GwtIncompatible // reflection
getCreateWithNullKeyUnsupportedMethod()151   public static Method getCreateWithNullKeyUnsupportedMethod() {
152     return Helpers.getMethod(MapCreationTester.class, "testCreateWithNullKeyUnsupported");
153   }
154 }
155