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.Map; 030 031/** 032 * A generic JUnit test which tests {@link Map#replace(Object, Object, Object)}. Can't be invoked 033 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 034 * 035 * @author Louis Wasserman 036 */ 037@GwtCompatible 038public class MapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> { 039 040 @MapFeature.Require(SUPPORTS_PUT) 041 @CollectionSize.Require(absent = ZERO) 042 public void testReplaceEntry_supportedPresent() { 043 try { 044 assertTrue(getMap().replace(k0(), v0(), v3())); 045 expectReplacement(entry(k0(), v3())); 046 } catch (ClassCastException tolerated) { // for ClassToInstanceMap 047 expectUnchanged(); 048 } 049 } 050 051 @MapFeature.Require(SUPPORTS_PUT) 052 @CollectionSize.Require(absent = ZERO) 053 public void testReplaceEntry_supportedPresentUnchanged() { 054 assertTrue(getMap().replace(k0(), v0(), v0())); 055 expectUnchanged(); 056 } 057 058 @MapFeature.Require(SUPPORTS_PUT) 059 @CollectionSize.Require(absent = ZERO) 060 public void testReplaceEntry_supportedWrongValue() { 061 assertFalse(getMap().replace(k0(), v3(), v4())); 062 expectUnchanged(); 063 } 064 065 @MapFeature.Require(SUPPORTS_PUT) 066 public void testReplaceEntry_supportedAbsentKey() { 067 assertFalse(getMap().replace(k3(), v3(), v4())); 068 expectUnchanged(); 069 } 070 071 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 072 @CollectionSize.Require(absent = ZERO) 073 public void testReplaceEntry_presentNullValueUnsupported() { 074 assertThrows(NullPointerException.class, () -> getMap().replace(k0(), v0(), null)); 075 expectUnchanged(); 076 } 077 078 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 079 @CollectionSize.Require(absent = ZERO) 080 public void testReplaceEntry_wrongValueNullValueUnsupported() { 081 try { 082 assertFalse(getMap().replace(k0(), v3(), null)); 083 } catch (NullPointerException tolerated) { 084 // the operation would be a no-op, so exceptions are allowed but not required 085 } 086 expectUnchanged(); 087 } 088 089 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 090 public void testReplaceEntry_absentKeyNullValueUnsupported() { 091 try { 092 assertFalse(getMap().replace(k3(), v3(), null)); 093 } catch (NullPointerException tolerated) { 094 // the operation would be a no-op, so exceptions are allowed but not required 095 } 096 expectUnchanged(); 097 } 098 099 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES}) 100 public void testReplaceEntry_nullDifferentFromAbsent() { 101 assertFalse(getMap().replace(k3(), null, v3())); 102 expectUnchanged(); 103 } 104 105 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 106 public void testReplaceEntry_expectNullUnsupported() { 107 try { 108 assertFalse(getMap().replace(k3(), null, v3())); 109 } catch (NullPointerException tolerated) { 110 // the operation would be a no-op, so exceptions are allowed but not required 111 } 112 expectUnchanged(); 113 } 114 115 @MapFeature.Require(absent = SUPPORTS_PUT) 116 @CollectionSize.Require(absent = ZERO) 117 public void testReplaceEntry_unsupportedPresent() { 118 assertThrows(UnsupportedOperationException.class, () -> getMap().replace(k0(), v0(), v3())); 119 expectUnchanged(); 120 } 121 122 @MapFeature.Require(absent = SUPPORTS_PUT) 123 @CollectionSize.Require(absent = ZERO) 124 public void testReplaceEntry_unsupportedWrongValue() { 125 try { 126 getMap().replace(k0(), v3(), v4()); 127 } catch (UnsupportedOperationException tolerated) { 128 // the operation would be a no-op, so exceptions are allowed but not required 129 } 130 expectUnchanged(); 131 } 132 133 @MapFeature.Require(absent = SUPPORTS_PUT) 134 public void testReplaceEntry_unsupportedAbsentKey() { 135 try { 136 getMap().replace(k3(), v3(), v4()); 137 } catch (UnsupportedOperationException tolerated) { 138 // the operation would be a no-op, so exceptions are allowed but not required 139 } 140 expectUnchanged(); 141 } 142}