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