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.ZERO; 20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 22 import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 23 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 24 import static java.util.Collections.singletonList; 25 26 import com.google.common.annotations.GwtCompatible; 27 import com.google.common.annotations.GwtIncompatible; 28 import com.google.common.collect.testing.AbstractMapTester; 29 import com.google.common.collect.testing.Helpers; 30 import com.google.common.collect.testing.MinimalCollection; 31 import com.google.common.collect.testing.features.CollectionSize; 32 import com.google.common.collect.testing.features.MapFeature; 33 import java.lang.reflect.Method; 34 import java.util.Collections; 35 import java.util.ConcurrentModificationException; 36 import java.util.Iterator; 37 import java.util.LinkedHashMap; 38 import java.util.List; 39 import java.util.Map; 40 import java.util.Map.Entry; 41 import org.junit.Ignore; 42 43 /** 44 * A generic JUnit test which tests {@code putAll} operations on a map. Can't be invoked directly; 45 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 46 * 47 * @author Chris Povirk 48 * @author Kevin Bourrillion 49 */ 50 @SuppressWarnings("unchecked") // too many "unchecked generic array creations" 51 @GwtCompatible(emulated = true) 52 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 53 public class MapPutAllTester<K, V> extends AbstractMapTester<K, V> { 54 private List<Entry<K, V>> containsNullKey; 55 private List<Entry<K, V>> containsNullValue; 56 57 @Override setUp()58 public void setUp() throws Exception { 59 super.setUp(); 60 containsNullKey = singletonList(entry(null, v3())); 61 containsNullValue = singletonList(entry(k3(), null)); 62 } 63 64 @MapFeature.Require(SUPPORTS_PUT) testPutAll_supportedNothing()65 public void testPutAll_supportedNothing() { 66 getMap().putAll(emptyMap()); 67 expectUnchanged(); 68 } 69 70 @MapFeature.Require(absent = SUPPORTS_PUT) testPutAll_unsupportedNothing()71 public void testPutAll_unsupportedNothing() { 72 try { 73 getMap().putAll(emptyMap()); 74 } catch (UnsupportedOperationException tolerated) { 75 } 76 expectUnchanged(); 77 } 78 79 @MapFeature.Require(SUPPORTS_PUT) testPutAll_supportedNonePresent()80 public void testPutAll_supportedNonePresent() { 81 putAll(createDisjointCollection()); 82 expectAdded(e3(), e4()); 83 } 84 85 @MapFeature.Require(absent = SUPPORTS_PUT) testPutAll_unsupportedNonePresent()86 public void testPutAll_unsupportedNonePresent() { 87 try { 88 putAll(createDisjointCollection()); 89 fail("putAll(nonePresent) should throw"); 90 } catch (UnsupportedOperationException expected) { 91 } 92 expectUnchanged(); 93 expectMissing(e3(), e4()); 94 } 95 96 @MapFeature.Require(SUPPORTS_PUT) 97 @CollectionSize.Require(absent = ZERO) testPutAll_supportedSomePresent()98 public void testPutAll_supportedSomePresent() { 99 putAll(MinimalCollection.of(e3(), e0())); 100 expectAdded(e3()); 101 } 102 103 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT}) 104 @CollectionSize.Require(absent = ZERO) testPutAllSomePresentConcurrentWithEntrySetIteration()105 public void testPutAllSomePresentConcurrentWithEntrySetIteration() { 106 try { 107 Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); 108 putAll(MinimalCollection.of(e3(), e0())); 109 iterator.next(); 110 fail("Expected ConcurrentModificationException"); 111 } catch (ConcurrentModificationException expected) { 112 // success 113 } 114 } 115 116 @MapFeature.Require(absent = SUPPORTS_PUT) 117 @CollectionSize.Require(absent = ZERO) testPutAll_unsupportedSomePresent()118 public void testPutAll_unsupportedSomePresent() { 119 try { 120 putAll(MinimalCollection.of(e3(), e0())); 121 fail("putAll(somePresent) should throw"); 122 } catch (UnsupportedOperationException expected) { 123 } 124 expectUnchanged(); 125 } 126 127 @MapFeature.Require(absent = SUPPORTS_PUT) 128 @CollectionSize.Require(absent = ZERO) testPutAll_unsupportedAllPresent()129 public void testPutAll_unsupportedAllPresent() { 130 try { 131 putAll(MinimalCollection.of(e0())); 132 } catch (UnsupportedOperationException tolerated) { 133 } 134 expectUnchanged(); 135 } 136 137 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) testPutAll_nullKeySupported()138 public void testPutAll_nullKeySupported() { 139 putAll(containsNullKey); 140 expectAdded(containsNullKey.get(0)); 141 } 142 143 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS) testPutAll_nullKeyUnsupported()144 public void testPutAll_nullKeyUnsupported() { 145 try { 146 putAll(containsNullKey); 147 fail("putAll(containsNullKey) should throw"); 148 } catch (NullPointerException expected) { 149 } 150 expectUnchanged(); 151 expectNullKeyMissingWhenNullKeysUnsupported( 152 "Should not contain null key after unsupported putAll(containsNullKey)"); 153 } 154 155 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) testPutAll_nullValueSupported()156 public void testPutAll_nullValueSupported() { 157 putAll(containsNullValue); 158 expectAdded(containsNullValue.get(0)); 159 } 160 161 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) testPutAll_nullValueUnsupported()162 public void testPutAll_nullValueUnsupported() { 163 try { 164 putAll(containsNullValue); 165 fail("putAll(containsNullValue) should throw"); 166 } catch (NullPointerException expected) { 167 } 168 expectUnchanged(); 169 expectNullValueMissingWhenNullValuesUnsupported( 170 "Should not contain null value after unsupported putAll(containsNullValue)"); 171 } 172 173 @MapFeature.Require(SUPPORTS_PUT) testPutAll_nullCollectionReference()174 public void testPutAll_nullCollectionReference() { 175 try { 176 getMap().putAll(null); 177 fail("putAll(null) should throw NullPointerException"); 178 } catch (NullPointerException expected) { 179 } 180 } 181 emptyMap()182 private Map<K, V> emptyMap() { 183 return Collections.emptyMap(); 184 } 185 putAll(Iterable<Entry<K, V>> entries)186 private void putAll(Iterable<Entry<K, V>> entries) { 187 Map<K, V> map = new LinkedHashMap<>(); 188 for (Entry<K, V> entry : entries) { 189 map.put(entry.getKey(), entry.getValue()); 190 } 191 getMap().putAll(map); 192 } 193 194 /** 195 * Returns the {@link Method} instance for {@link #testPutAll_nullKeyUnsupported()} so that tests 196 * can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 197 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed. 198 */ 199 @GwtIncompatible // reflection getPutAllNullKeyUnsupportedMethod()200 public static Method getPutAllNullKeyUnsupportedMethod() { 201 return Helpers.getMethod(MapPutAllTester.class, "testPutAll_nullKeyUnsupported"); 202 } 203 } 204