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_KEY_QUERIES; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES; 023import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 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)}. Can't be invoked directly; 034 * please see {@link com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}. 035 * 036 * @author Louis Wasserman 037 */ 038@GwtCompatible 039@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 040public class MapReplaceTester<K, V> extends AbstractMapTester<K, V> { 041 042 @MapFeature.Require(SUPPORTS_PUT) 043 @CollectionSize.Require(absent = ZERO) 044 public void testReplace_supportedPresent() { 045 try { 046 assertEquals(v0(), getMap().replace(k0(), v3())); 047 expectReplacement(entry(k0(), v3())); 048 } catch (ClassCastException tolerated) { // for ClassToInstanceMap 049 expectUnchanged(); 050 } 051 } 052 053 @MapFeature.Require(SUPPORTS_PUT) 054 @CollectionSize.Require(absent = ZERO) 055 public void testReplace_supportedPresentNoChange() { 056 assertEquals(v0(), getMap().replace(k0(), v0())); 057 expectUnchanged(); 058 } 059 060 @MapFeature.Require(SUPPORTS_PUT) 061 public void testReplace_supportedAbsent() { 062 assertNull(getMap().replace(k3(), v3())); 063 expectUnchanged(); 064 } 065 066 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) 067 @CollectionSize.Require(absent = ZERO) 068 public void testReplace_presentNullValueUnsupported() { 069 try { 070 getMap().replace(k0(), null); 071 fail("Expected NullPointerException"); 072 } catch (NullPointerException expected) { 073 } 074 expectUnchanged(); 075 } 076 077 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 078 public void testReplace_absentNullValueUnsupported() { 079 try { 080 getMap().replace(k3(), null); 081 } catch (NullPointerException tolerated) { 082 // permitted not to throw because it would be a no-op 083 } 084 expectUnchanged(); 085 } 086 087 @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES) 088 public void testReplace_absentNullKeyUnsupported() { 089 try { 090 getMap().replace(null, v3()); 091 } catch (NullPointerException tolerated) { 092 // permitted not to throw because it would be a no-op 093 } 094 expectUnchanged(); 095 } 096 097 @MapFeature.Require(absent = SUPPORTS_PUT) 098 @CollectionSize.Require(absent = ZERO) 099 public void testReplace_unsupportedPresent() { 100 try { 101 getMap().replace(k0(), v3()); 102 fail("Expected UnsupportedOperationException"); 103 } catch (UnsupportedOperationException expected) { 104 } catch (ClassCastException tolerated) { 105 // for ClassToInstanceMap 106 } 107 108 expectUnchanged(); 109 } 110}