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