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.features.CollectionSize.ONE; 020import static com.google.common.collect.testing.features.CollectionSize.ZERO; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 023import static com.google.common.collect.testing.features.MapFeature.REJECTS_DUPLICATES_AT_CREATION; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.annotations.GwtIncompatible; 027import com.google.common.annotations.J2ktIncompatible; 028import com.google.common.collect.testing.AbstractMapTester; 029import com.google.common.collect.testing.Helpers; 030import com.google.common.collect.testing.features.CollectionSize; 031import com.google.common.collect.testing.features.MapFeature; 032import java.lang.reflect.Method; 033import java.util.Arrays; 034import java.util.List; 035import java.util.Map.Entry; 036import org.junit.Ignore; 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(emulated = true) 047@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 048@SuppressWarnings("JUnit4ClassUsedInJUnit3") 049public class MapCreationTester<K, V> extends AbstractMapTester<K, V> { 050 @MapFeature.Require(ALLOWS_NULL_KEYS) 051 @CollectionSize.Require(absent = ZERO) 052 public void testCreateWithNullKeySupported() { 053 initMapWithNullKey(); 054 expectContents(createArrayWithNullKey()); 055 } 056 057 @MapFeature.Require(absent = ALLOWS_NULL_KEYS) 058 @CollectionSize.Require(absent = ZERO) 059 public void testCreateWithNullKeyUnsupported() { 060 try { 061 initMapWithNullKey(); 062 fail("Creating a map containing a null key should fail"); 063 } catch (NullPointerException expected) { 064 } 065 } 066 067 @MapFeature.Require(ALLOWS_NULL_VALUES) 068 @CollectionSize.Require(absent = ZERO) 069 public void testCreateWithNullValueSupported() { 070 initMapWithNullValue(); 071 expectContents(createArrayWithNullValue()); 072 } 073 074 @MapFeature.Require(absent = ALLOWS_NULL_VALUES) 075 @CollectionSize.Require(absent = ZERO) 076 public void testCreateWithNullValueUnsupported() { 077 try { 078 initMapWithNullValue(); 079 fail("Creating a map containing a null value should fail"); 080 } catch (NullPointerException expected) { 081 } 082 } 083 084 @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES}) 085 @CollectionSize.Require(absent = ZERO) 086 public void testCreateWithNullKeyAndValueSupported() { 087 Entry<K, V>[] entries = createSamplesArray(); 088 entries[getNullLocation()] = entry(null, null); 089 resetMap(entries); 090 expectContents(entries); 091 } 092 093 @MapFeature.Require(value = ALLOWS_NULL_KEYS, absent = REJECTS_DUPLICATES_AT_CREATION) 094 @CollectionSize.Require(absent = {ZERO, ONE}) 095 public void testCreateWithDuplicates_nullDuplicatesNotRejected() { 096 expectFirstRemoved(getEntriesMultipleNullKeys()); 097 } 098 099 @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(k1(), v0()); 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 #testCreateWithNullKeyUnsupported()} so that 149 * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 150 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed. 151 */ 152 @J2ktIncompatible 153 @GwtIncompatible // reflection 154 public static Method getCreateWithNullKeyUnsupportedMethod() { 155 return Helpers.getMethod(MapCreationTester.class, "testCreateWithNullKeyUnsupported"); 156 } 157}