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 17 package com.google.common.collect.testing.testers; 18 19 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 22 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 23 24 import com.google.common.annotations.GwtCompatible; 25 import com.google.common.collect.testing.AbstractMapTester; 26 import com.google.common.collect.testing.features.CollectionSize; 27 import com.google.common.collect.testing.features.MapFeature; 28 import java.util.Map; 29 import 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. 39 @SuppressWarnings("JUnit4ClassUsedInJUnit3") 40 public class MapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> { 41 42 @MapFeature.Require(SUPPORTS_PUT) 43 @CollectionSize.Require(absent = ZERO) testReplaceEntry_supportedPresent()44 public void testReplaceEntry_supportedPresent() { 45 try { 46 assertTrue(getMap().replace(k0(), v0(), v3())); 47 expectReplacement(entry(k0(), v3())); 48 } catch (ClassCastException tolerated) { // for ClassToInstanceMap 49 expectUnchanged(); 50 } 51 } 52 53 @MapFeature.Require(SUPPORTS_PUT) 54 @CollectionSize.Require(absent = ZERO) testReplaceEntry_supportedPresentUnchanged()55 public void testReplaceEntry_supportedPresentUnchanged() { 56 assertTrue(getMap().replace(k0(), v0(), v0())); 57 expectUnchanged(); 58 } 59 60 @MapFeature.Require(SUPPORTS_PUT) 61 @CollectionSize.Require(absent = ZERO) testReplaceEntry_supportedWrongValue()62 public void testReplaceEntry_supportedWrongValue() { 63 assertFalse(getMap().replace(k0(), v3(), v4())); 64 expectUnchanged(); 65 } 66 67 @MapFeature.Require(SUPPORTS_PUT) testReplaceEntry_supportedAbsentKey()68 public void testReplaceEntry_supportedAbsentKey() { 69 assertFalse(getMap().replace(k3(), v3(), v4())); 70 expectUnchanged(); 71 } 72 73 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 74 @CollectionSize.Require(absent = ZERO) testReplaceEntry_presentNullValueUnsupported()75 public void testReplaceEntry_presentNullValueUnsupported() { 76 try { 77 getMap().replace(k0(), v0(), null); 78 fail("Expected NullPointerException"); 79 } catch (NullPointerException expected) { 80 } 81 expectUnchanged(); 82 } 83 84 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 85 @CollectionSize.Require(absent = ZERO) testReplaceEntry_wrongValueNullValueUnsupported()86 public void testReplaceEntry_wrongValueNullValueUnsupported() { 87 try { 88 assertFalse(getMap().replace(k0(), v3(), null)); 89 } catch (NullPointerException tolerated) { 90 // the operation would be a no-op, so exceptions are allowed but not required 91 } 92 expectUnchanged(); 93 } 94 95 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) testReplaceEntry_absentKeyNullValueUnsupported()96 public void testReplaceEntry_absentKeyNullValueUnsupported() { 97 try { 98 assertFalse(getMap().replace(k3(), v3(), null)); 99 } catch (NullPointerException tolerated) { 100 // the operation would be a no-op, so exceptions are allowed but not required 101 } 102 expectUnchanged(); 103 } 104 105 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES}) testReplaceEntry_nullDifferentFromAbsent()106 public void testReplaceEntry_nullDifferentFromAbsent() { 107 assertFalse(getMap().replace(k3(), null, v3())); 108 expectUnchanged(); 109 } 110 111 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) testReplaceEntry_expectNullUnsupported()112 public void testReplaceEntry_expectNullUnsupported() { 113 try { 114 assertFalse(getMap().replace(k3(), null, v3())); 115 } catch (NullPointerException tolerated) { 116 // the operation would be a no-op, so exceptions are allowed but not required 117 } 118 expectUnchanged(); 119 } 120 121 @MapFeature.Require(absent = SUPPORTS_PUT) 122 @CollectionSize.Require(absent = ZERO) testReplaceEntry_unsupportedPresent()123 public void testReplaceEntry_unsupportedPresent() { 124 try { 125 getMap().replace(k0(), v0(), v3()); 126 fail("Expected UnsupportedOperationException"); 127 } catch (UnsupportedOperationException expected) { 128 } 129 expectUnchanged(); 130 } 131 132 @MapFeature.Require(absent = SUPPORTS_PUT) 133 @CollectionSize.Require(absent = ZERO) testReplaceEntry_unsupportedWrongValue()134 public void testReplaceEntry_unsupportedWrongValue() { 135 try { 136 getMap().replace(k0(), 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 143 @MapFeature.Require(absent = SUPPORTS_PUT) testReplaceEntry_unsupportedAbsentKey()144 public void testReplaceEntry_unsupportedAbsentKey() { 145 try { 146 getMap().replace(k3(), v3(), v4()); 147 } catch (UnsupportedOperationException tolerated) { 148 // the operation would be a no-op, so exceptions are allowed but not required 149 } 150 expectUnchanged(); 151 } 152 } 153