guava
151 строка · 5.3 Кб
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_VALUES;21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;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 org.junit.Ignore;30
31/**
32* A generic JUnit test which tests {@link Map#replace(Object, Object, Object)}. Can't be invoked
33* directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
34*
35* @author Louis Wasserman
36*/
37@GwtCompatible
38@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.39public class MapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {40
41@MapFeature.Require(SUPPORTS_PUT)42@CollectionSize.Require(absent = ZERO)43public void testReplaceEntry_supportedPresent() {44try {45assertTrue(getMap().replace(k0(), v0(), v3()));46expectReplacement(entry(k0(), v3()));47} catch (ClassCastException tolerated) { // for ClassToInstanceMap48expectUnchanged();49}50}51
52@MapFeature.Require(SUPPORTS_PUT)53@CollectionSize.Require(absent = ZERO)54public void testReplaceEntry_supportedPresentUnchanged() {55assertTrue(getMap().replace(k0(), v0(), v0()));56expectUnchanged();57}58
59@MapFeature.Require(SUPPORTS_PUT)60@CollectionSize.Require(absent = ZERO)61public void testReplaceEntry_supportedWrongValue() {62assertFalse(getMap().replace(k0(), v3(), v4()));63expectUnchanged();64}65
66@MapFeature.Require(SUPPORTS_PUT)67public void testReplaceEntry_supportedAbsentKey() {68assertFalse(getMap().replace(k3(), v3(), v4()));69expectUnchanged();70}71
72@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)73@CollectionSize.Require(absent = ZERO)74public void testReplaceEntry_presentNullValueUnsupported() {75try {76getMap().replace(k0(), v0(), null);77fail("Expected NullPointerException");78} catch (NullPointerException expected) {79}80expectUnchanged();81}82
83@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)84@CollectionSize.Require(absent = ZERO)85public void testReplaceEntry_wrongValueNullValueUnsupported() {86try {87assertFalse(getMap().replace(k0(), v3(), null));88} catch (NullPointerException tolerated) {89// the operation would be a no-op, so exceptions are allowed but not required90}91expectUnchanged();92}93
94@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)95public void testReplaceEntry_absentKeyNullValueUnsupported() {96try {97assertFalse(getMap().replace(k3(), v3(), null));98} catch (NullPointerException tolerated) {99// the operation would be a no-op, so exceptions are allowed but not required100}101expectUnchanged();102}103
104@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES})105public void testReplaceEntry_nullDifferentFromAbsent() {106assertFalse(getMap().replace(k3(), null, v3()));107expectUnchanged();108}109
110@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)111public void testReplaceEntry_expectNullUnsupported() {112try {113assertFalse(getMap().replace(k3(), null, v3()));114} catch (NullPointerException tolerated) {115// the operation would be a no-op, so exceptions are allowed but not required116}117expectUnchanged();118}119
120@MapFeature.Require(absent = SUPPORTS_PUT)121@CollectionSize.Require(absent = ZERO)122public void testReplaceEntry_unsupportedPresent() {123try {124getMap().replace(k0(), v0(), v3());125fail("Expected UnsupportedOperationException");126} catch (UnsupportedOperationException expected) {127}128expectUnchanged();129}130
131@MapFeature.Require(absent = SUPPORTS_PUT)132@CollectionSize.Require(absent = ZERO)133public void testReplaceEntry_unsupportedWrongValue() {134try {135getMap().replace(k0(), v3(), v4());136} catch (UnsupportedOperationException tolerated) {137// the operation would be a no-op, so exceptions are allowed but not required138}139expectUnchanged();140}141
142@MapFeature.Require(absent = SUPPORTS_PUT)143public void testReplaceEntry_unsupportedAbsentKey() {144try {145getMap().replace(k3(), v3(), v4());146} catch (UnsupportedOperationException tolerated) {147// the operation would be a no-op, so exceptions are allowed but not required148}149expectUnchanged();150}151}
152