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 com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 026import static java.util.Arrays.asList; 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; 037import org.junit.Ignore; 038 039/** 040 * A generic JUnit test which tests creation (typically through a constructor or static factory 041 * method) of a map. Can't be invoked directly; please see {@link 042 * com.google.common.collect.testing.MapTestSuiteBuilder}. 043 * 044 * @author Chris Povirk 045 * @author Kevin Bourrillion 046 */ 047@GwtCompatible(emulated = true) 048@Ignore("test runners must not instantiate and run this directly, only via suites we build") 049// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 050@SuppressWarnings("JUnit4ClassUsedInJUnit3") 051public class MapCreationTester<K, V> extends AbstractMapTester<K, V> { 052 @MapFeature.Require(ALLOWS_NULL_KEYS) 053 @CollectionSize.Require(absent = ZERO) 054 public void testCreateWithNullKeySupported() { 055 initMapWithNullKey(); 056 expectContents(createArrayWithNullKey()); 057 } 058 059 @MapFeature.Require(absent = ALLOWS_NULL_KEYS) 060 @CollectionSize.Require(absent = ZERO) 061 public void testCreateWithNullKeyUnsupported() { 062 assertThrows(NullPointerException.class, () -> initMapWithNullKey()); 063 } 064 065 @MapFeature.Require(ALLOWS_NULL_VALUES) 066 @CollectionSize.Require(absent = ZERO) 067 public void testCreateWithNullValueSupported() { 068 initMapWithNullValue(); 069 expectContents(createArrayWithNullValue()); 070 } 071 072 @MapFeature.Require(absent = ALLOWS_NULL_VALUES) 073 @CollectionSize.Require(absent = ZERO) 074 public void testCreateWithNullValueUnsupported() { 075 assertThrows(NullPointerException.class, () -> initMapWithNullValue()); 076 } 077 078 @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES}) 079 @CollectionSize.Require(absent = ZERO) 080 public void testCreateWithNullKeyAndValueSupported() { 081 Entry<K, V>[] entries = createSamplesArray(); 082 entries[getNullLocation()] = entry(null, null); 083 resetMap(entries); 084 expectContents(entries); 085 } 086 087 @MapFeature.Require(value = ALLOWS_NULL_KEYS, absent = REJECTS_DUPLICATES_AT_CREATION) 088 @CollectionSize.Require(absent = {ZERO, ONE}) 089 public void testCreateWithDuplicates_nullDuplicatesNotRejected() { 090 expectFirstRemoved(getEntriesMultipleNullKeys()); 091 } 092 093 @MapFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION) 094 @CollectionSize.Require(absent = {ZERO, ONE}) 095 public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() { 096 expectFirstRemoved(getEntriesMultipleNonNullKeys()); 097 } 098 099 @MapFeature.Require({ALLOWS_NULL_KEYS, REJECTS_DUPLICATES_AT_CREATION}) 100 @CollectionSize.Require(absent = {ZERO, ONE}) 101 public void testCreateWithDuplicates_nullDuplicatesRejected() { 102 Entry<K, V>[] entries = getEntriesMultipleNullKeys(); 103 assertThrows(IllegalArgumentException.class, () -> resetMap(entries)); 104 } 105 106 @MapFeature.Require(REJECTS_DUPLICATES_AT_CREATION) 107 @CollectionSize.Require(absent = {ZERO, ONE}) 108 public void testCreateWithDuplicates_nonNullDuplicatesRejected() { 109 Entry<K, V>[] entries = getEntriesMultipleNonNullKeys(); 110 assertThrows(IllegalArgumentException.class, () -> resetMap(entries)); 111 } 112 113 private Entry<K, V>[] getEntriesMultipleNullKeys() { 114 Entry<K, V>[] entries = createArrayWithNullKey(); 115 entries[0] = entry(null, entries[0].getValue()); 116 return entries; 117 } 118 119 private Entry<K, V>[] getEntriesMultipleNonNullKeys() { 120 Entry<K, V>[] entries = createSamplesArray(); 121 entries[0] = entry(k1(), v0()); 122 return entries; 123 } 124 125 private void expectFirstRemoved(Entry<K, V>[] entries) { 126 resetMap(entries); 127 128 List<Entry<K, V>> expectedWithDuplicateRemoved = asList(entries).subList(1, getNumElements()); 129 expectContents(expectedWithDuplicateRemoved); 130 } 131 132 /** 133 * Returns the {@link Method} instance for {@link #testCreateWithNullKeyUnsupported()} so that 134 * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 135 * href="https://bugs.openjdk.org/browse/JDK-5045147">JDK-5045147</a> is fixed. 136 */ 137 @J2ktIncompatible 138 @GwtIncompatible // reflection 139 public static Method getCreateWithNullKeyUnsupportedMethod() { 140 return getMethod(MapCreationTester.class, "testCreateWithNullKeyUnsupported"); 141 } 142}