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;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.collect.testing.AbstractMapTester;
025import com.google.common.collect.testing.Helpers;
026import com.google.common.collect.testing.SampleElements;
027import com.google.common.collect.testing.features.CollectionFeature;
028import com.google.common.collect.testing.features.CollectionSize;
029import com.google.common.collect.testing.features.MapFeature;
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Map.Entry;
033import org.junit.Ignore;
034
035/**
036 * A generic JUnit test which tests {@code replaceAll()} operations on a map. Can't be invoked
037 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
038 *
039 * @author Louis Wasserman
040 */
041@GwtCompatible
042@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
043@SuppressWarnings("JUnit4ClassUsedInJUnit3")
044public class MapReplaceAllTester<K, V> extends AbstractMapTester<K, V> {
045  private SampleElements<K> keys() {
046    return new SampleElements<>(k0(), k1(), k2(), k3(), k4());
047  }
048
049  private SampleElements<V> values() {
050    return new SampleElements<>(v0(), v1(), v2(), v3(), v4());
051  }
052
053  @MapFeature.Require(SUPPORTS_PUT)
054  public void testReplaceAllRotate() {
055    getMap()
056        .replaceAll(
057            (K k, V v) -> {
058              int index = keys().asList().indexOf(k);
059              return values().asList().get(index + 1);
060            });
061    List<Entry<K, V>> expectedEntries = new ArrayList<>();
062    for (Entry<K, V> entry : getSampleEntries()) {
063      int index = keys().asList().indexOf(entry.getKey());
064      expectedEntries.add(Helpers.mapEntry(entry.getKey(), values().asList().get(index + 1)));
065    }
066    expectContents(expectedEntries);
067  }
068
069  @MapFeature.Require(SUPPORTS_PUT)
070  @CollectionFeature.Require(KNOWN_ORDER)
071  public void testReplaceAllPreservesOrder() {
072    getMap()
073        .replaceAll(
074            (K k, V v) -> {
075              int index = keys().asList().indexOf(k);
076              return values().asList().get(index + 1);
077            });
078    List<Entry<K, V>> orderedEntries = getOrderedElements();
079    int index = 0;
080    for (K key : getMap().keySet()) {
081      assertEquals(orderedEntries.get(index).getKey(), key);
082      index++;
083    }
084  }
085
086  @MapFeature.Require(absent = SUPPORTS_PUT)
087  @CollectionSize.Require(absent = ZERO)
088  public void testReplaceAll_unsupported() {
089    try {
090      getMap()
091          .replaceAll(
092              (K k, V v) -> {
093                int index = keys().asList().indexOf(k);
094                return values().asList().get(index + 1);
095              });
096      fail(
097          "replaceAll() should throw UnsupportedOperation if a map does "
098              + "not support it and is not empty.");
099    } catch (UnsupportedOperationException expected) {
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}