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