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