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