MapCreationTester.java revision 3c77433663281544363151bf284b0240dfd22a42
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.collect.testing.AbstractMapTester; 28import com.google.common.collect.testing.Helpers; 29import com.google.common.collect.testing.features.CollectionSize; 30import com.google.common.collect.testing.features.MapFeature; 31 32import java.lang.reflect.Method; 33import java.util.Arrays; 34import java.util.List; 35import java.util.Map.Entry; 36 37/** 38 * A generic JUnit test which tests creation (typically through a constructor or 39 * static factory method) of a map. Can't be invoked directly; please see 40 * {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 41 * 42 * <p>This class is GWT compatible. 43 * 44 * @author Chris Povirk 45 * @author Kevin Bourrillion 46 */ 47@GwtCompatible(emulated = true) 48public 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, 93 absent = REJECTS_DUPLICATES_AT_CREATION) 94 @CollectionSize.Require(absent = {ZERO, ONE}) 95 public void testCreateWithDuplicates_nullDuplicatesNotRejected() { 96 expectFirstRemoved(getEntriesMultipleNullKeys()); 97 } 98 99 @MapFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION) 100 @CollectionSize.Require(absent = {ZERO, ONE}) 101 public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() { 102 expectFirstRemoved(getEntriesMultipleNonNullKeys()); 103 } 104 105 @MapFeature.Require({ALLOWS_NULL_KEYS, REJECTS_DUPLICATES_AT_CREATION}) 106 @CollectionSize.Require(absent = {ZERO, ONE}) 107 public void testCreateWithDuplicates_nullDuplicatesRejected() { 108 Entry<K, V>[] entries = getEntriesMultipleNullKeys(); 109 try { 110 resetMap(entries); 111 fail("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}) 118 public void testCreateWithDuplicates_nonNullDuplicatesRejected() { 119 Entry<K, V>[] entries = getEntriesMultipleNonNullKeys(); 120 try { 121 resetMap(entries); 122 fail("Should reject duplicate non-null elements at creation"); 123 } catch (IllegalArgumentException expected) { 124 } 125 } 126 127 private Entry<K, V>[] getEntriesMultipleNullKeys() { 128 Entry<K, V>[] entries = createArrayWithNullKey(); 129 entries[0] = entry(null, entries[0].getValue()); 130 return entries; 131 } 132 133 private Entry<K, V>[] getEntriesMultipleNonNullKeys() { 134 Entry<K, V>[] entries = createSamplesArray(); 135 entries[0] = entry(samples.e1.getKey(), samples.e0.getValue()); 136 return entries; 137 } 138 139 private void expectFirstRemoved(Entry<K, V>[] entries) { 140 resetMap(entries); 141 142 List<Entry<K, V>> expectedWithDuplicateRemoved = 143 Arrays.asList(entries).subList(1, getNumElements()); 144 expectContents(expectedWithDuplicateRemoved); 145 } 146 147 /** 148 * Returns the {@link Method} instance for {@link 149 * #testCreateWithNullKeyUnsupported()} so that tests can suppress it 150 * with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 151 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun 152 * bug 5045147</a> is fixed. 153 */ 154 @GwtIncompatible("reflection") 155 public static Method getCreateWithNullKeyUnsupportedMethod() { 156 return Helpers.getMethod(MapCreationTester.class, "testCreateWithNullKeyUnsupported"); 157 } 158} 159