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.Helpers.mapEntry;
020import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
023import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.collect.testing.AbstractMapTester;
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")
046public class MapReplaceAllTester<K, V> extends AbstractMapTester<K, V> {
047  private SampleElements<K> keys() {
048    return new SampleElements<>(k0(), k1(), k2(), k3(), k4());
049  }
050
051  private SampleElements<V> values() {
052    return new SampleElements<>(v0(), v1(), v2(), v3(), v4());
053  }
054
055  @MapFeature.Require(SUPPORTS_PUT)
056  public void testReplaceAllRotate() {
057    getMap()
058        .replaceAll(
059            (K k, V v) -> {
060              int index = keys().asList().indexOf(k);
061              return values().asList().get(index + 1);
062            });
063    List<Entry<K, V>> expectedEntries = new ArrayList<>();
064    for (Entry<K, V> entry : getSampleEntries()) {
065      int index = keys().asList().indexOf(entry.getKey());
066      expectedEntries.add(mapEntry(entry.getKey(), values().asList().get(index + 1)));
067    }
068    expectContents(expectedEntries);
069  }
070
071  @MapFeature.Require(SUPPORTS_PUT)
072  @CollectionFeature.Require(KNOWN_ORDER)
073  public void testReplaceAllPreservesOrder() {
074    getMap()
075        .replaceAll(
076            (K k, V v) -> {
077              int index = keys().asList().indexOf(k);
078              return values().asList().get(index + 1);
079            });
080    List<Entry<K, V>> orderedEntries = getOrderedElements();
081    int index = 0;
082    for (K key : getMap().keySet()) {
083      assertEquals(orderedEntries.get(index).getKey(), key);
084      index++;
085    }
086  }
087
088  @MapFeature.Require(absent = SUPPORTS_PUT)
089  @CollectionSize.Require(absent = ZERO)
090  public void testReplaceAll_unsupported() {
091    assertThrows(
092        UnsupportedOperationException.class,
093        () ->
094            getMap()
095                .replaceAll(
096                    (K k, V v) -> {
097                      int index = keys().asList().indexOf(k);
098                      return values().asList().get(index + 1);
099                    }));
100    expectUnchanged();
101  }
102
103  @MapFeature.Require(absent = SUPPORTS_PUT)
104  @CollectionSize.Require(ZERO)
105  public void testReplaceAll_unsupportedByEmptyCollection() {
106    try {
107      getMap()
108          .replaceAll(
109              (K k, V v) -> {
110                int index = keys().asList().indexOf(k);
111                return values().asList().get(index + 1);
112              });
113    } catch (UnsupportedOperationException tolerated) {
114    }
115    expectUnchanged();
116  }
117
118  @MapFeature.Require(absent = SUPPORTS_PUT)
119  public void testReplaceAll_unsupportedNoOpFunction() {
120    try {
121      getMap().replaceAll((K k, V v) -> v);
122    } catch (UnsupportedOperationException tolerated) {
123    }
124    expectUnchanged();
125  }
126}