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