001/* 002 * Copyright (C) 2015 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.features.CollectionSize.ZERO; 020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 023import static org.junit.Assert.assertThrows; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.collect.testing.AbstractMapTester; 027import com.google.common.collect.testing.features.CollectionSize; 028import com.google.common.collect.testing.features.MapFeature; 029import java.util.concurrent.ConcurrentMap; 030import org.jspecify.annotations.NullMarked; 031 032/** 033 * A generic JUnit test which tests {@code replace(K, V, V)} operations on a concurrent map. Can't 034 * be invoked directly; please see {@link 035 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}. 036 * 037 * @author Louis Wasserman 038 */ 039@GwtCompatible 040@NullMarked 041public class ConcurrentMapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> { 042 @Override 043 protected ConcurrentMap<K, V> getMap() { 044 return (ConcurrentMap<K, V>) super.getMap(); 045 } 046 047 @MapFeature.Require(SUPPORTS_PUT) 048 @CollectionSize.Require(absent = ZERO) 049 public void testReplaceEntry_supportedPresent() { 050 assertTrue(getMap().replace(k0(), v0(), v3())); 051 expectReplacement(entry(k0(), v3())); 052 } 053 054 @MapFeature.Require(SUPPORTS_PUT) 055 @CollectionSize.Require(absent = ZERO) 056 public void testReplaceEntry_supportedPresentUnchanged() { 057 assertTrue(getMap().replace(k0(), v0(), v0())); 058 expectUnchanged(); 059 } 060 061 @MapFeature.Require(SUPPORTS_PUT) 062 @CollectionSize.Require(absent = ZERO) 063 public void testReplaceEntry_supportedWrongValue() { 064 assertFalse(getMap().replace(k0(), v3(), v4())); 065 expectUnchanged(); 066 } 067 068 @MapFeature.Require(SUPPORTS_PUT) 069 public void testReplaceEntry_supportedAbsentKey() { 070 assertFalse(getMap().replace(k3(), v3(), v4())); 071 expectUnchanged(); 072 } 073 074 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 075 @CollectionSize.Require(absent = ZERO) 076 public void testReplaceEntry_presentNullValueUnsupported() { 077 assertThrows(NullPointerException.class, () -> getMap().replace(k0(), v0(), null)); 078 expectUnchanged(); 079 } 080 081 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 082 @CollectionSize.Require(absent = ZERO) 083 public void testReplaceEntry_wrongValueNullValueUnsupported() { 084 try { 085 assertFalse(getMap().replace(k0(), v3(), null)); 086 } catch (NullPointerException tolerated) { 087 // the operation would be a no-op, so exceptions are allowed but not required 088 } 089 expectUnchanged(); 090 } 091 092 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 093 public void testReplaceEntry_absentKeyNullValueUnsupported() { 094 try { 095 assertFalse(getMap().replace(k3(), v3(), null)); 096 } catch (NullPointerException tolerated) { 097 // the operation would be a no-op, so exceptions are allowed but not required 098 } 099 expectUnchanged(); 100 } 101 102 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES}) 103 public void testReplaceEntry_nullDifferentFromAbsent() { 104 assertFalse(getMap().replace(k3(), null, v3())); 105 expectUnchanged(); 106 } 107 108 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 109 public void testReplaceEntry_expectNullUnsupported() { 110 try { 111 assertFalse(getMap().replace(k3(), null, v3())); 112 } catch (NullPointerException tolerated) { 113 // the operation would be a no-op, so exceptions are allowed but not required 114 } 115 expectUnchanged(); 116 } 117 118 @MapFeature.Require(absent = SUPPORTS_PUT) 119 @CollectionSize.Require(absent = ZERO) 120 public void testReplaceEntry_unsupportedPresent() { 121 assertThrows(UnsupportedOperationException.class, () -> getMap().replace(k0(), v0(), v3())); 122 expectUnchanged(); 123 } 124 125 @MapFeature.Require(absent = SUPPORTS_PUT) 126 @CollectionSize.Require(absent = ZERO) 127 public void testReplaceEntry_unsupportedWrongValue() { 128 try { 129 getMap().replace(k0(), v3(), v4()); 130 } catch (UnsupportedOperationException tolerated) { 131 // the operation would be a no-op, so exceptions are allowed but not required 132 } 133 expectUnchanged(); 134 } 135 136 @MapFeature.Require(absent = SUPPORTS_PUT) 137 public void testReplaceEntry_unsupportedAbsentKey() { 138 try { 139 getMap().replace(k3(), v3(), v4()); 140 } catch (UnsupportedOperationException tolerated) { 141 // the operation would be a no-op, so exceptions are allowed but not required 142 } 143 expectUnchanged(); 144 } 145}