guava
204 строки · 6.8 Кб
1/*
2* Copyright (C) 2015 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.CollectionSize.ZERO;20import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;22import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;24
25import com.google.common.annotations.GwtCompatible;26import com.google.common.collect.testing.AbstractMapTester;27import com.google.common.collect.testing.features.CollectionSize;28import com.google.common.collect.testing.features.MapFeature;29import java.util.Map;30import org.junit.Ignore;31
32/**
33* A generic JUnit test which tests {@link Map#compute}. Can't be invoked directly; please see
34* {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
35*
36* @author Louis Wasserman
37*/
38@GwtCompatible
39@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.40public class MapComputeTester<K, V> extends AbstractMapTester<K, V> {41@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})42public void testCompute_absentToPresent() {43assertEquals(44"Map.compute(absent, functionReturningValue) should return value",45v3(),46getMap()47.compute(48k3(),49(k, v) -> {50assertEquals(k3(), k);51assertNull(v);52return v3();53}));54expectAdded(e3());55assertEquals(getNumElements() + 1, getMap().size());56}57
58@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})59public void testCompute_absentToAbsent() {60assertNull(61"Map.compute(absent, functionReturningNull) should return null",62getMap()63.compute(64k3(),65(k, v) -> {66assertEquals(k3(), k);67assertNull(v);68return null;69}));70expectUnchanged();71assertEquals(getNumElements(), getMap().size());72}73
74@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})75@CollectionSize.Require(absent = ZERO)76public void testCompute_presentToPresent() {77assertEquals(78"Map.compute(present, functionReturningValue) should return new value",79v3(),80getMap()81.compute(82k0(),83(k, v) -> {84assertEquals(k0(), k);85assertEquals(v0(), v);86return v3();87}));88expectReplacement(entry(k0(), v3()));89assertEquals(getNumElements(), getMap().size());90}91
92@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})93@CollectionSize.Require(absent = ZERO)94public void testCompute_presentToAbsent() {95assertNull(96"Map.compute(present, functionReturningNull) should return null",97getMap()98.compute(99k0(),100(k, v) -> {101assertEquals(k0(), k);102assertEquals(v0(), v);103return null;104}));105expectMissing(e0());106expectMissingKeys(k0());107assertEquals(getNumElements() - 1, getMap().size());108}109
110@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})111@CollectionSize.Require(absent = ZERO)112public void testCompute_presentNullToPresentNonnull() {113initMapWithNullValue();114V value = getValueForNullKey();115assertEquals(116"Map.compute(presentMappedToNull, functionReturningValue) should return new value",117value,118getMap()119.compute(120getKeyForNullValue(),121(k, v) -> {122assertEquals(getKeyForNullValue(), k);123assertNull(v);124return value;125}));126expectReplacement(entry(getKeyForNullValue(), value));127assertEquals(getNumElements(), getMap().size());128}129
130@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})131@CollectionSize.Require(absent = ZERO)132public void testCompute_presentNullToNull() {133// The spec is somewhat ambiguous about this case, but the actual default implementation134// in Map will remove a present null.135initMapWithNullValue();136assertNull(137"Map.compute(presentMappedToNull, functionReturningNull) should return null",138getMap()139.compute(140getKeyForNullValue(),141(k, v) -> {142assertEquals(getKeyForNullValue(), k);143assertNull(v);144return null;145}));146expectMissingKeys(getKeyForNullValue());147assertEquals(getNumElements() - 1, getMap().size());148}149
150@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})151@CollectionSize.Require(absent = ZERO)152public void testCompute_nullKeyPresentToPresent() {153initMapWithNullKey();154assertEquals(155"Map.compute(present, functionReturningValue) should return new value",156v3(),157getMap()158.compute(159null,160(k, v) -> {161assertNull(k);162assertEquals(getValueForNullKey(), v);163return v3();164}));165assertEquals(getNumElements(), getMap().size());166}167
168static class ExpectedException extends RuntimeException {}169
170@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})171@CollectionSize.Require(absent = ZERO)172public void testCompute_presentFunctionThrows() {173try {174getMap()175.compute(176k0(),177(k, v) -> {178assertEquals(k0(), k);179assertEquals(v0(), v);180throw new ExpectedException();181});182fail("Expected ExpectedException");183} catch (ExpectedException expected) {184}185expectUnchanged();186}187
188@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})189public void testCompute_absentFunctionThrows() {190try {191getMap()192.compute(193k3(),194(k, v) -> {195assertEquals(k3(), k);196assertNull(v);197throw new ExpectedException();198});199fail("Expected ExpectedException");200} catch (ExpectedException expected) {201}202expectUnchanged();203}204}
205