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_VALUE_QUERIES; 022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 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.concurrent.ConcurrentMap; 030import org.junit.Ignore; 031 032/** 033 * Tester for {@link ConcurrentMap#remove}. Can't be invoked directly; please see {@link 034 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}. 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") 042@ElementTypesAreNonnullByDefault 043public class ConcurrentMapRemoveTester<K, V> extends AbstractMapTester<K, V> { 044 @Override 045 protected ConcurrentMap<K, V> getMap() { 046 return (ConcurrentMap<K, V>) super.getMap(); 047 } 048 049 @MapFeature.Require(SUPPORTS_REMOVE) 050 @CollectionSize.Require(absent = ZERO) 051 public void testRemove_supportedPresent() { 052 assertTrue(getMap().remove(k0(), v0())); 053 expectMissing(e0()); 054 } 055 056 @MapFeature.Require(SUPPORTS_REMOVE) 057 public void testRemove_supportedPresentKeyWrongValue() { 058 assertFalse(getMap().remove(k0(), v3())); 059 expectUnchanged(); 060 } 061 062 @MapFeature.Require(SUPPORTS_REMOVE) 063 public void testRemove_supportedWrongKeyPresentValue() { 064 assertFalse(getMap().remove(k3(), v0())); 065 expectUnchanged(); 066 } 067 068 @MapFeature.Require(SUPPORTS_REMOVE) 069 public void testRemove_supportedAbsentKeyAbsentValue() { 070 assertFalse(getMap().remove(k3(), v3())); 071 expectUnchanged(); 072 } 073 074 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES) 075 public void testRemove_nullKeyQueriesUnsupported() { 076 try { 077 assertFalse(getMap().remove(null, v3())); 078 } catch (NullPointerException tolerated) { 079 // since the operation would be a no-op, the exception is not required 080 } 081 expectUnchanged(); 082 } 083 084 @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_VALUE_QUERIES) 085 public void testRemove_nullValueQueriesUnsupported() { 086 try { 087 assertFalse(getMap().remove(k3(), null)); 088 } catch (NullPointerException tolerated) { 089 // since the operation would be a no-op, the exception is not required 090 } 091 expectUnchanged(); 092 } 093 094 @MapFeature.Require(absent = SUPPORTS_REMOVE) 095 @CollectionSize.Require(absent = ZERO) 096 public void testRemove_unsupportedPresent() { 097 assertThrows(UnsupportedOperationException.class, () -> getMap().remove(k0(), v0())); 098 expectUnchanged(); 099 } 100 101 @MapFeature.Require(absent = SUPPORTS_REMOVE) 102 public void testRemove_unsupportedAbsent() { 103 try { 104 assertFalse(getMap().remove(k0(), v3())); 105 } catch (UnsupportedOperationException tolerated) { 106 // since the operation would be a no-op, the exception is not required 107 } 108 expectUnchanged(); 109 } 110}