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