guava
205 строк · 6.4 Кб
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;23
24import com.google.common.annotations.GwtCompatible;25import com.google.common.collect.testing.AbstractMapTester;26import com.google.common.collect.testing.features.CollectionSize;27import com.google.common.collect.testing.features.MapFeature;28import java.util.Map;29import junit.framework.AssertionFailedError;30import org.junit.Ignore;31
32/**
33* A generic JUnit test which tests {@link Map#computeIfAbsent}. Can't be invoked directly; please
34* see {@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 MapComputeIfAbsentTester<K, V> extends AbstractMapTester<K, V> {41
42@MapFeature.Require(SUPPORTS_PUT)43public void testComputeIfAbsent_supportedAbsent() {44assertEquals(45"computeIfAbsent(notPresent, function) should return new value",46v3(),47getMap()48.computeIfAbsent(49k3(),50k -> {51assertEquals(k3(), k);52return v3();53}));54expectAdded(e3());55}56
57@MapFeature.Require(SUPPORTS_PUT)58@CollectionSize.Require(absent = ZERO)59public void testComputeIfAbsent_supportedPresent() {60assertEquals(61"computeIfAbsent(present, function) should return existing value",62v0(),63getMap()64.computeIfAbsent(65k0(),66k -> {67throw new AssertionFailedError();68}));69expectUnchanged();70}71
72@MapFeature.Require(SUPPORTS_PUT)73public void testComputeIfAbsent_functionReturnsNullNotInserted() {74assertNull(75"computeIfAbsent(absent, returnsNull) should return null",76getMap()77.computeIfAbsent(78k3(),79k -> {80assertEquals(k3(), k);81return null;82}));83expectUnchanged();84}85
86@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})87@CollectionSize.Require(absent = ZERO)88public void testComputeIfAbsent_nullTreatedAsAbsent() {89initMapWithNullValue();90assertEquals(91"computeIfAbsent(presentAssignedToNull, function) should return newValue",92getValueForNullKey(),93getMap()94.computeIfAbsent(95getKeyForNullValue(),96k -> {97assertEquals(getKeyForNullValue(), k);98return getValueForNullKey();99}));100expectReplacement(entry(getKeyForNullValue(), getValueForNullKey()));101}102
103@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})104public void testComputeIfAbsent_nullKeySupported() {105getMap()106.computeIfAbsent(107null,108k -> {109assertNull(k);110return v3();111});112expectAdded(entry(null, v3()));113}114
115static class ExpectedException extends RuntimeException {}116
117@MapFeature.Require(SUPPORTS_PUT)118public void testComputeIfAbsent_functionThrows() {119try {120getMap()121.computeIfAbsent(122k3(),123k -> {124assertEquals(k3(), k);125throw new ExpectedException();126});127fail("Expected ExpectedException");128} catch (ExpectedException expected) {129}130expectUnchanged();131}132
133@MapFeature.Require(absent = SUPPORTS_PUT)134public void testComputeIfAbsent_unsupportedAbsent() {135try {136getMap()137.computeIfAbsent(138k3(),139k -> {140// allowed to be called141assertEquals(k3(), k);142return v3();143});144fail("computeIfAbsent(notPresent, function) should throw");145} catch (UnsupportedOperationException expected) {146}147expectUnchanged();148}149
150@MapFeature.Require(absent = SUPPORTS_PUT)151@CollectionSize.Require(absent = ZERO)152public void testComputeIfAbsent_unsupportedPresentExistingValue() {153try {154assertEquals(155"computeIfAbsent(present, returnsCurrentValue) should return present or throw",156v0(),157getMap()158.computeIfAbsent(159k0(),160k -> {161assertEquals(k0(), k);162return v0();163}));164} catch (UnsupportedOperationException tolerated) {165}166expectUnchanged();167}168
169@MapFeature.Require(absent = SUPPORTS_PUT)170@CollectionSize.Require(absent = ZERO)171public void testComputeIfAbsent_unsupportedPresentDifferentValue() {172try {173assertEquals(174"computeIfAbsent(present, returnsDifferentValue) should return present or throw",175v0(),176getMap()177.computeIfAbsent(178k0(),179k -> {180assertEquals(k0(), k);181return v3();182}));183} catch (UnsupportedOperationException tolerated) {184}185expectUnchanged();186}187
188@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)189public void testComputeIfAbsent_nullKeyUnsupported() {190try {191getMap()192.computeIfAbsent(193null,194k -> {195assertNull(k);196return v3();197});198fail("computeIfAbsent(null, function) should throw");199} catch (NullPointerException expected) {200}201expectUnchanged();202expectNullKeyMissingWhenNullKeysUnsupported(203"Should not contain null key after unsupported computeIfAbsent(null, function)");204}205}
206