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.ZERO;
020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
022import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
023import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
024import static java.util.Collections.singletonList;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.annotations.GwtIncompatible;
028import com.google.common.annotations.J2ktIncompatible;
029import com.google.common.collect.testing.AbstractMapTester;
030import com.google.common.collect.testing.Helpers;
031import com.google.common.collect.testing.MinimalCollection;
032import com.google.common.collect.testing.features.CollectionSize;
033import com.google.common.collect.testing.features.MapFeature;
034import java.lang.reflect.Method;
035import java.util.Collections;
036import java.util.ConcurrentModificationException;
037import java.util.Iterator;
038import java.util.LinkedHashMap;
039import java.util.List;
040import java.util.Map;
041import java.util.Map.Entry;
042import org.checkerframework.checker.nullness.qual.Nullable;
043import org.junit.Ignore;
044
045/**
046 * A generic JUnit test which tests {@code putAll} operations on a map. Can't be invoked directly;
047 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
048 *
049 * @author Chris Povirk
050 * @author Kevin Bourrillion
051 */
052@GwtCompatible(emulated = true)
053@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
054@SuppressWarnings("JUnit4ClassUsedInJUnit3")
055@ElementTypesAreNonnullByDefault
056public class MapPutAllTester<K extends @Nullable Object, V extends @Nullable Object>
057    extends AbstractMapTester<K, V> {
058  private List<Entry<K, V>> containsNullKey;
059  private List<Entry<K, V>> containsNullValue;
060
061  @Override
062  public void setUp() throws Exception {
063    super.setUp();
064    containsNullKey = singletonList(entry(null, v3()));
065    containsNullValue = singletonList(entry(k3(), null));
066  }
067
068  @MapFeature.Require(SUPPORTS_PUT)
069  public void testPutAll_supportedNothing() {
070    getMap().putAll(emptyMap());
071    expectUnchanged();
072  }
073
074  @MapFeature.Require(absent = SUPPORTS_PUT)
075  public void testPutAll_unsupportedNothing() {
076    try {
077      getMap().putAll(emptyMap());
078    } catch (UnsupportedOperationException tolerated) {
079    }
080    expectUnchanged();
081  }
082
083  @MapFeature.Require(SUPPORTS_PUT)
084  public void testPutAll_supportedNonePresent() {
085    putAll(createDisjointCollection());
086    expectAdded(e3(), e4());
087  }
088
089  @MapFeature.Require(absent = SUPPORTS_PUT)
090  public void testPutAll_unsupportedNonePresent() {
091    try {
092      putAll(createDisjointCollection());
093      fail("putAll(nonePresent) should throw");
094    } catch (UnsupportedOperationException expected) {
095    }
096    expectUnchanged();
097    expectMissing(e3(), e4());
098  }
099
100  @MapFeature.Require(SUPPORTS_PUT)
101  @CollectionSize.Require(absent = ZERO)
102  public void testPutAll_supportedSomePresent() {
103    putAll(MinimalCollection.of(e3(), e0()));
104    expectAdded(e3());
105  }
106
107  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT})
108  @CollectionSize.Require(absent = ZERO)
109  public void testPutAllSomePresentConcurrentWithEntrySetIteration() {
110    try {
111      Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
112      putAll(MinimalCollection.of(e3(), e0()));
113      iterator.next();
114      fail("Expected ConcurrentModificationException");
115    } catch (ConcurrentModificationException expected) {
116      // success
117    }
118  }
119
120  @MapFeature.Require(absent = SUPPORTS_PUT)
121  @CollectionSize.Require(absent = ZERO)
122  public void testPutAll_unsupportedSomePresent() {
123    try {
124      putAll(MinimalCollection.of(e3(), e0()));
125      fail("putAll(somePresent) should throw");
126    } catch (UnsupportedOperationException expected) {
127    }
128    expectUnchanged();
129  }
130
131  @MapFeature.Require(absent = SUPPORTS_PUT)
132  @CollectionSize.Require(absent = ZERO)
133  public void testPutAll_unsupportedAllPresent() {
134    try {
135      putAll(MinimalCollection.of(e0()));
136    } catch (UnsupportedOperationException tolerated) {
137    }
138    expectUnchanged();
139  }
140
141  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
142  public void testPutAll_nullKeySupported() {
143    putAll(containsNullKey);
144    expectAdded(containsNullKey.get(0));
145  }
146
147  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
148  public void testPutAll_nullKeyUnsupported() {
149    try {
150      putAll(containsNullKey);
151      fail("putAll(containsNullKey) should throw");
152    } catch (NullPointerException expected) {
153    }
154    expectUnchanged();
155    expectNullKeyMissingWhenNullKeysUnsupported(
156        "Should not contain null key after unsupported putAll(containsNullKey)");
157  }
158
159  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
160  public void testPutAll_nullValueSupported() {
161    putAll(containsNullValue);
162    expectAdded(containsNullValue.get(0));
163  }
164
165  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
166  public void testPutAll_nullValueUnsupported() {
167    try {
168      putAll(containsNullValue);
169      fail("putAll(containsNullValue) should throw");
170    } catch (NullPointerException expected) {
171    }
172    expectUnchanged();
173    expectNullValueMissingWhenNullValuesUnsupported(
174        "Should not contain null value after unsupported putAll(containsNullValue)");
175  }
176
177  @MapFeature.Require(SUPPORTS_PUT)
178  public void testPutAll_nullCollectionReference() {
179    try {
180      getMap().putAll(null);
181      fail("putAll(null) should throw NullPointerException");
182    } catch (NullPointerException expected) {
183    }
184  }
185
186  private Map<K, V> emptyMap() {
187    return Collections.emptyMap();
188  }
189
190  private void putAll(Iterable<Entry<K, V>> entries) {
191    Map<K, V> map = new LinkedHashMap<>();
192    for (Entry<K, V> entry : entries) {
193      map.put(entry.getKey(), entry.getValue());
194    }
195    getMap().putAll(map);
196  }
197
198  /**
199   * Returns the {@link Method} instance for {@link #testPutAll_nullKeyUnsupported()} so that tests
200   * can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
201   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
202   */
203  @J2ktIncompatible
204  @GwtIncompatible // reflection
205  public static Method getPutAllNullKeyUnsupportedMethod() {
206    return Helpers.getMethod(MapPutAllTester.class, "testPutAll_nullKeyUnsupported");
207  }
208}