Java
127 строк · 4.3 Кб
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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
20import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
22
23import com.google.common.annotations.GwtCompatible;
24import com.google.common.collect.testing.AbstractMapTester;
25import com.google.common.collect.testing.Helpers;
26import com.google.common.collect.testing.SampleElements;
27import com.google.common.collect.testing.features.CollectionFeature;
28import com.google.common.collect.testing.features.CollectionSize;
29import com.google.common.collect.testing.features.MapFeature;
30import java.util.ArrayList;
31import java.util.List;
32import java.util.Map.Entry;
33import 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@SuppressWarnings("JUnit4ClassUsedInJUnit3")
44public class MapReplaceAllTester<K, V> extends AbstractMapTester<K, V> {
45private SampleElements<K> keys() {
46return new SampleElements<>(k0(), k1(), k2(), k3(), k4());
47}
48
49private SampleElements<V> values() {
50return new SampleElements<>(v0(), v1(), v2(), v3(), v4());
51}
52
53@MapFeature.Require(SUPPORTS_PUT)
54public void testReplaceAllRotate() {
55getMap()
56.replaceAll(
57(K k, V v) -> {
58int index = keys().asList().indexOf(k);
59return values().asList().get(index + 1);
60});
61List<Entry<K, V>> expectedEntries = new ArrayList<>();
62for (Entry<K, V> entry : getSampleEntries()) {
63int index = keys().asList().indexOf(entry.getKey());
64expectedEntries.add(Helpers.mapEntry(entry.getKey(), values().asList().get(index + 1)));
65}
66expectContents(expectedEntries);
67}
68
69@MapFeature.Require(SUPPORTS_PUT)
70@CollectionFeature.Require(KNOWN_ORDER)
71public void testReplaceAllPreservesOrder() {
72getMap()
73.replaceAll(
74(K k, V v) -> {
75int index = keys().asList().indexOf(k);
76return values().asList().get(index + 1);
77});
78List<Entry<K, V>> orderedEntries = getOrderedElements();
79int index = 0;
80for (K key : getMap().keySet()) {
81assertEquals(orderedEntries.get(index).getKey(), key);
82index++;
83}
84}
85
86@MapFeature.Require(absent = SUPPORTS_PUT)
87@CollectionSize.Require(absent = ZERO)
88public void testReplaceAll_unsupported() {
89try {
90getMap()
91.replaceAll(
92(K k, V v) -> {
93int index = keys().asList().indexOf(k);
94return values().asList().get(index + 1);
95});
96fail(
97"replaceAll() should throw UnsupportedOperation if a map does "
98+ "not support it and is not empty.");
99} catch (UnsupportedOperationException expected) {
100}
101expectUnchanged();
102}
103
104@MapFeature.Require(absent = SUPPORTS_PUT)
105@CollectionSize.Require(ZERO)
106public void testReplaceAll_unsupportedByEmptyCollection() {
107try {
108getMap()
109.replaceAll(
110(K k, V v) -> {
111int index = keys().asList().indexOf(k);
112return values().asList().get(index + 1);
113});
114} catch (UnsupportedOperationException tolerated) {
115}
116expectUnchanged();
117}
118
119@MapFeature.Require(absent = SUPPORTS_PUT)
120public void testReplaceAll_unsupportedNoOpFunction() {
121try {
122getMap().replaceAll((K k, V v) -> v);
123} catch (UnsupportedOperationException tolerated) {
124}
125expectUnchanged();
126}
127}
128