001/*
002 * Copyright (C) 2016 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.CollectionFeature.KNOWN_ORDER;
020import static com.google.common.collect.testing.features.CollectionSize.ZERO;
021import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
022import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.testing.AbstractMapTester;
026import com.google.common.collect.testing.Helpers;
027import com.google.common.collect.testing.SampleElements;
028import com.google.common.collect.testing.features.CollectionFeature;
029import com.google.common.collect.testing.features.CollectionSize;
030import com.google.common.collect.testing.features.MapFeature;
031import java.util.ArrayList;
032import java.util.List;
033import java.util.Map.Entry;
034import org.junit.Ignore;
035
036/**
037 * A generic JUnit test which tests {@code replaceAll()} operations on a map. Can't be invoked
038 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
039 *
040 * @author Louis Wasserman
041 */
042@GwtCompatible
043@Ignore("test runners must not instantiate and run this directly, only via suites we build")
044// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
045@SuppressWarnings("JUnit4ClassUsedInJUnit3")
046@IgnoreJRERequirement // We opt into library desugaring for our tests.
047public class MapReplaceAllTester<K, V> extends AbstractMapTester<K, V> {
048  private SampleElements<K> keys() {
049    return new SampleElements<>(k0(), k1(), k2(), k3(), k4());
050  }
051
052  private SampleElements<V> values() {
053    return new SampleElements<>(v0(), v1(), v2(), v3(), v4());
054  }
055
056  @MapFeature.Require(SUPPORTS_PUT)
057  public void testReplaceAllRotate() {
058    getMap()
059        .replaceAll(
060            (K k, V v) -> {
061              int index = keys().asList().indexOf(k);
062              return values().asList().get(index + 1);
063            });
064    List<Entry<K, V>> expectedEntries = new ArrayList<>();
065    for (Entry<K, V> entry : getSampleEntries()) {
066      int index = keys().asList().indexOf(entry.getKey());
067      expectedEntries.add(Helpers.mapEntry(entry.getKey(), values().asList().get(index + 1)));
068    }
069    expectContents(expectedEntries);
070  }
071
072  @MapFeature.Require(SUPPORTS_PUT)
073  @CollectionFeature.Require(KNOWN_ORDER)
074  public void testReplaceAllPreservesOrder() {
075    getMap()
076        .replaceAll(
077            (K k, V v) -> {
078              int index = keys().asList().indexOf(k);
079              return values().asList().get(index + 1);
080            });
081    List<Entry<K, V>> orderedEntries = getOrderedElements();
082    int index = 0;
083    for (K key : getMap().keySet()) {
084      assertEquals(orderedEntries.get(index).getKey(), key);
085      index++;
086    }
087  }
088
089  @MapFeature.Require(absent = SUPPORTS_PUT)
090  @CollectionSize.Require(absent = ZERO)
091  public void testReplaceAll_unsupported() {
092    assertThrows(
093        UnsupportedOperationException.class,
094        () ->
095            getMap()
096                .replaceAll(
097                    (K k, V v) -> {
098                      int index = keys().asList().indexOf(k);
099                      return values().asList().get(index + 1);
100                    }));
101    expectUnchanged();
102  }
103
104  @MapFeature.Require(absent = SUPPORTS_PUT)
105  @CollectionSize.Require(ZERO)
106  public void testReplaceAll_unsupportedByEmptyCollection() {
107    try {
108      getMap()
109          .replaceAll(
110              (K k, V v) -> {
111                int index = keys().asList().indexOf(k);
112                return values().asList().get(index + 1);
113              });
114    } catch (UnsupportedOperationException tolerated) {
115    }
116    expectUnchanged();
117  }
118
119  @MapFeature.Require(absent = SUPPORTS_PUT)
120  public void testReplaceAll_unsupportedNoOpFunction() {
121    try {
122      getMap().replaceAll((K k, V v) -> v);
123    } catch (UnsupportedOperationException tolerated) {
124    }
125    expectUnchanged();
126  }
127}