001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.Helpers.getMethod; 020import static com.google.common.collect.testing.features.CollectionSize.ONE; 021import static com.google.common.collect.testing.features.CollectionSize.ZERO; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 024import static com.google.common.collect.testing.features.MapFeature.REJECTS_DUPLICATES_AT_CREATION; 025import static java.util.Arrays.asList; 026import static org.junit.Assert.assertThrows; 027 028import com.google.common.annotations.GwtCompatible; 029import com.google.common.annotations.GwtIncompatible; 030import com.google.common.annotations.J2ktIncompatible; 031import com.google.common.collect.testing.AbstractMapTester; 032import com.google.common.collect.testing.features.CollectionSize; 033import com.google.common.collect.testing.features.MapFeature; 034import java.lang.reflect.Method; 035import java.util.List; 036import java.util.Map.Entry; 037 038/** 039 * A generic JUnit test which tests creation (typically through a constructor or static factory 040 * method) of a map. Can't be invoked directly; please see {@link 041 * com.google.common.collect.testing.MapTestSuiteBuilder}. 042 * 043 * @author Chris Povirk 044 * @author Kevin Bourrillion 045 */ 046@GwtCompatible 047public class MapCreationTester<K, V> extends AbstractMapTester<K, V> { 048 @MapFeature.Require(ALLOWS_NULL_KEYS) 049 @CollectionSize.Require(absent = ZERO) 050 public void testCreateWithNullKeySupported() { 051 initMapWithNullKey(); 052 expectContents(createArrayWithNullKey()); 053 } 054 055 @MapFeature.Require(absent = ALLOWS_NULL_KEYS) 056 @CollectionSize.Require(absent = ZERO) 057 public void testCreateWithNullKeyUnsupported() { 058 assertThrows(NullPointerException.class, () -> initMapWithNullKey()); 059 } 060 061 @MapFeature.Require(ALLOWS_NULL_VALUES) 062 @CollectionSize.Require(absent = ZERO) 063 public void testCreateWithNullValueSupported() { 064 initMapWithNullValue(); 065 expectContents(createArrayWithNullValue()); 066 } 067 068 @MapFeature.Require(absent = ALLOWS_NULL_VALUES) 069 @CollectionSize.Require(absent = ZERO) 070 public void testCreateWithNullValueUnsupported() { 071 assertThrows(NullPointerException.class, () -> initMapWithNullValue()); 072 } 073 074 @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES}) 075 @CollectionSize.Require(absent = ZERO) 076 public void testCreateWithNullKeyAndValueSupported() { 077 Entry<K, V>[] entries = createSamplesArray(); 078 entries[getNullLocation()] = entry(null, null); 079 resetMap(entries); 080 expectContents(entries); 081 } 082 083 @MapFeature.Require(value = ALLOWS_NULL_KEYS, absent = REJECTS_DUPLICATES_AT_CREATION) 084 @CollectionSize.Require(absent = {ZERO, ONE}) 085 public void testCreateWithDuplicates_nullDuplicatesNotRejected() { 086 expectFirstRemoved(getEntriesMultipleNullKeys()); 087 } 088 089 @MapFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION) 090 @CollectionSize.Require(absent = {ZERO, ONE}) 091 public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() { 092 expectFirstRemoved(getEntriesMultipleNonNullKeys()); 093 } 094 095 @MapFeature.Require({ALLOWS_NULL_KEYS, REJECTS_DUPLICATES_AT_CREATION}) 096 @CollectionSize.Require(absent = {ZERO, ONE}) 097 public void testCreateWithDuplicates_nullDuplicatesRejected() { 098 Entry<K, V>[] entries = getEntriesMultipleNullKeys(); 099 assertThrows(IllegalArgumentException.class, () -> resetMap(entries)); 100 } 101 102 @MapFeature.Require(REJECTS_DUPLICATES_AT_CREATION) 103 @CollectionSize.Require(absent = {ZERO, ONE}) 104 public void testCreateWithDuplicates_nonNullDuplicatesRejected() { 105 Entry<K, V>[] entries = getEntriesMultipleNonNullKeys(); 106 assertThrows(IllegalArgumentException.class, () -> resetMap(entries)); 107 } 108 109 private Entry<K, V>[] getEntriesMultipleNullKeys() { 110 Entry<K, V>[] entries = createArrayWithNullKey(); 111 entries[0] = entry(null, entries[0].getValue()); 112 return entries; 113 } 114 115 private Entry<K, V>[] getEntriesMultipleNonNullKeys() { 116 Entry<K, V>[] entries = createSamplesArray(); 117 entries[0] = entry(k1(), v0()); 118 return entries; 119 } 120 121 private void expectFirstRemoved(Entry<K, V>[] entries) { 122 resetMap(entries); 123 124 List<Entry<K, V>> expectedWithDuplicateRemoved = asList(entries).subList(1, getNumElements()); 125 expectContents(expectedWithDuplicateRemoved); 126 } 127 128 /** 129 * Returns the {@link Method} instance for {@link #testCreateWithNullKeyUnsupported()} so that 130 * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 131 * href="https://bugs.openjdk.org/browse/JDK-5045147">JDK-5045147</a> is fixed. 132 */ 133 @J2ktIncompatible 134 @GwtIncompatible // reflection 135 public static Method getCreateWithNullKeyUnsupportedMethod() { 136 return getMethod(MapCreationTester.class, "testCreateWithNullKeyUnsupported"); 137 } 138}