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.
043public class MapReplaceAllTester<K, V> extends AbstractMapTester<K, V> {
044  private SampleElements<K> keys() {
045    return new SampleElements<K>(k0(), k1(), k2(), k3(), k4());
046  }
047
048  private SampleElements<V> values() {
049    return new SampleElements<V>(v0(), v1(), v2(), v3(), v4());
050  }
051
052  @MapFeature.Require(SUPPORTS_PUT)
053  public void testReplaceAllRotate() {
054    getMap()
055        .replaceAll(
056            (K k, V v) -> {
057              int index = keys().asList().indexOf(k);
058              return values().asList().get(index + 1);
059            });
060    List<Entry<K, V>> expectedEntries = new ArrayList<>();
061    for (Entry<K, V> entry : getSampleEntries()) {
062      int index = keys().asList().indexOf(entry.getKey());
063      expectedEntries.add(Helpers.mapEntry(entry.getKey(), values().asList().get(index + 1)));
064    }
065    expectContents(expectedEntries);
066  }
067
068  @MapFeature.Require(SUPPORTS_PUT)
069  @CollectionFeature.Require(KNOWN_ORDER)
070  public void testReplaceAllPreservesOrder() {
071    getMap()
072        .replaceAll(
073            (K k, V v) -> {
074              int index = keys().asList().indexOf(k);
075              return values().asList().get(index + 1);
076            });
077    List<Entry<K, V>> orderedEntries = getOrderedElements();
078    int index = 0;
079    for (K key : getMap().keySet()) {
080      assertEquals(orderedEntries.get(index).getKey(), key);
081      index++;
082    }
083  }
084
085  @MapFeature.Require(absent = SUPPORTS_PUT)
086  @CollectionSize.Require(absent = ZERO)
087  public void testReplaceAll_unsupported() {
088    try {
089      getMap()
090          .replaceAll(
091              (K k, V v) -> {
092                int index = keys().asList().indexOf(k);
093                return values().asList().get(index + 1);
094              });
095      fail(
096          "replaceAll() should throw UnsupportedOperation if a map does "
097              + "not support it and is not empty.");
098    } catch (UnsupportedOperationException expected) {
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}