Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/testers/MapReplaceAllTester.java

raw
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 import org.junit.Ignore;
34
35 /**
36 * A generic JUnit test which tests {@code replaceAll()} operations on a map. Can't be invoked
37 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
38 *
39 * @author Louis Wasserman
40 */
41 @GwtCompatible
42 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
43 public class MapReplaceAllTester<K, V> extends AbstractMapTester<K, V> {
44 private SampleElements<K> keys() {
45 return new SampleElements<>(k0(), k1(), k2(), k3(), k4());
46 }
47
48 private SampleElements<V> values() {
49 return new SampleElements<>(v0(), v1(), v2(), v3(), v4());
50 }
51
52 @MapFeature.Require(SUPPORTS_PUT)
53 public void testReplaceAllRotate() {
54 getMap()
55 .replaceAll(
56 (K k, V v) -> {
57 int index = keys().asList().indexOf(k);
58 return values().asList().get(index + 1);
59 });
60 List<Entry<K, V>> expectedEntries = new ArrayList<>();
61 for (Entry<K, V> entry : getSampleEntries()) {
62 int index = keys().asList().indexOf(entry.getKey());
63 expectedEntries.add(Helpers.mapEntry(entry.getKey(), values().asList().get(index + 1)));
64 }
65 expectContents(expectedEntries);
66 }
67
68 @MapFeature.Require(SUPPORTS_PUT)
69 @CollectionFeature.Require(KNOWN_ORDER)
70 public void testReplaceAllPreservesOrder() {
71 getMap()
72 .replaceAll(
73 (K k, V v) -> {
74 int index = keys().asList().indexOf(k);
75 return values().asList().get(index + 1);
76 });
77 List<Entry<K, V>> orderedEntries = getOrderedElements();
78 int index = 0;
79 for (K key : getMap().keySet()) {
80 assertEquals(orderedEntries.get(index).getKey(), key);
81 index++;
82 }
83 }
84
85 @MapFeature.Require(absent = SUPPORTS_PUT)
86 @CollectionSize.Require(absent = ZERO)
87 public void testReplaceAll_unsupported() {
88 try {
89 getMap()
90 .replaceAll(
91 (K k, V v) -> {
92 int index = keys().asList().indexOf(k);
93 return values().asList().get(index + 1);
94 });
95 fail(
96 "replaceAll() should throw UnsupportedOperation if a map does "
97 + "not support it and is not empty.");
98 } catch (UnsupportedOperationException expected) {
99 }
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 }