guava
109 строк · 3.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_KEY_QUERIES;21import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;22import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;23import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;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.concurrent.ConcurrentMap;30import org.junit.Ignore;31
32/**
33* A generic JUnit test which tests {@code replace(K, V)} operations on a concurrent map. Can't be
34* invoked directly; please see {@link
35* com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
36*
37* @author Louis Wasserman
38*/
39@GwtCompatible
40@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.41@ElementTypesAreNonnullByDefault
42public class ConcurrentMapReplaceTester<K, V> extends AbstractMapTester<K, V> {43@Override44protected ConcurrentMap<K, V> getMap() {45return (ConcurrentMap<K, V>) super.getMap();46}47
48@MapFeature.Require(SUPPORTS_PUT)49@CollectionSize.Require(absent = ZERO)50public void testReplace_supportedPresent() {51assertEquals(v0(), getMap().replace(k0(), v3()));52expectReplacement(entry(k0(), v3()));53}54
55@MapFeature.Require(SUPPORTS_PUT)56@CollectionSize.Require(absent = ZERO)57public void testReplace_supportedPresentNoChange() {58assertEquals(v0(), getMap().replace(k0(), v0()));59expectUnchanged();60}61
62@MapFeature.Require(SUPPORTS_PUT)63public void testReplace_supportedAbsent() {64assertNull(getMap().replace(k3(), v3()));65expectUnchanged();66}67
68@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)69@CollectionSize.Require(absent = ZERO)70public void testReplace_presentNullValueUnsupported() {71try {72getMap().replace(k0(), null);73fail("Expected NullPointerException");74} catch (NullPointerException expected) {75}76expectUnchanged();77}78
79@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)80public void testReplace_absentNullValueUnsupported() {81try {82getMap().replace(k3(), null);83} catch (NullPointerException tolerated) {84// permitted not to throw because it would be a no-op85}86expectUnchanged();87}88
89@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)90public void testReplace_absentNullKeyUnsupported() {91try {92getMap().replace(null, v3());93} catch (NullPointerException tolerated) {94// permitted not to throw because it would be a no-op95}96expectUnchanged();97}98
99@MapFeature.Require(absent = SUPPORTS_PUT)100@CollectionSize.Require(absent = ZERO)101public void testReplace_unsupportedPresent() {102try {103getMap().replace(k0(), v3());104fail("Expected UnsupportedOperationException");105} catch (UnsupportedOperationException expected) {106}107expectUnchanged();108}109}
110