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