View Javadoc
1   /*
2    * Copyright (C) 2016 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.testing.AbstractMapTester;
25  import com.google.common.collect.testing.Helpers;
26  import com.google.common.collect.testing.SampleElements;
27  import com.google.common.collect.testing.features.CollectionFeature;
28  import com.google.common.collect.testing.features.CollectionSize;
29  import com.google.common.collect.testing.features.MapFeature;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Map.Entry;
33  
34  /**
35   * A generic JUnit test which tests {@code replaceAll()} operations on a map.
36   * Can't be invoked directly; please see
37   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
38   *
39   * @author Louis Wasserman
40   */
41  @GwtCompatible
42  public class MapReplaceAllTester<K, V> extends AbstractMapTester<K, V> {
43    private SampleElements<K> keys() {
44      return new SampleElements<K>(k0(), k1(), k2(), k3(), k4());
45    }
46  
47    private SampleElements<V> values() {
48      return new SampleElements<V>(v0(), v1(), v2(), v3(), v4());
49    }
50  
51    @MapFeature.Require(SUPPORTS_PUT)
52    public void testReplaceAllRotate() {
53      getMap()
54          .replaceAll(
55              (K k, V v) -> {
56                int index = keys().asList().indexOf(k);
57                return values().asList().get(index + 1);
58              });
59      List<Entry<K, V>> expectedEntries = new ArrayList<>();
60      for (Entry<K, V> entry : getSampleEntries()) {
61        int index = keys().asList().indexOf(entry.getKey());
62        expectedEntries.add(Helpers.mapEntry(entry.getKey(), values().asList().get(index + 1)));
63      }
64      expectContents(expectedEntries);
65    }
66  
67    @MapFeature.Require(SUPPORTS_PUT)
68    @CollectionFeature.Require(KNOWN_ORDER)
69    public void testReplaceAllPreservesOrder() {
70      getMap()
71          .replaceAll(
72              (K k, V v) -> {
73                int index = keys().asList().indexOf(k);
74                return values().asList().get(index + 1);
75              });
76      List<Entry<K, V>> orderedEntries = getOrderedElements();
77      int index = 0;
78      for (K key : getMap().keySet()) {
79        assertEquals(orderedEntries.get(index).getKey(), key);
80        index++;
81      }
82    }
83  
84    @MapFeature.Require(absent = SUPPORTS_PUT)
85    @CollectionSize.Require(absent = ZERO)
86    public void testReplaceAll_unsupported() {
87      try {
88        getMap()
89            .replaceAll(
90                (K k, V v) -> {
91                  int index = keys().asList().indexOf(k);
92                  return values().asList().get(index + 1);
93                });
94        fail(
95            "replaceAll() should throw UnsupportedOperation if a map does "
96                + "not support it and is not empty.");
97      } catch (UnsupportedOperationException expected) {
98      }
99      expectUnchanged();
100   }
101 
102   @MapFeature.Require(absent = SUPPORTS_PUT)
103   @CollectionSize.Require(ZERO)
104   public void testReplaceAll_unsupportedByEmptyCollection() {
105     try {
106       getMap()
107           .replaceAll(
108               (K k, V v) -> {
109                 int index = keys().asList().indexOf(k);
110                 return values().asList().get(index + 1);
111               });
112     } catch (UnsupportedOperationException tolerated) {
113     }
114     expectUnchanged();
115   }
116 
117   @MapFeature.Require(absent = SUPPORTS_PUT)
118   public void testReplaceAll_unsupportedNoOpFunction() {
119     try {
120       getMap().replaceAll((K k, V v) -> v);
121     } catch (UnsupportedOperationException tolerated) {
122     }
123     expectUnchanged();
124   }
125 }