001/*
002 * Copyright (C) 2017 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.google;
018
019import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
020import static com.google.common.collect.testing.features.CollectionSize.ZERO;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
023import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.collect.testing.features.CollectionSize;
027import com.google.common.collect.testing.features.MapFeature;
028import java.util.Map.Entry;
029import org.junit.Ignore;
030
031/** Tester for {@code BiMap.entrySet} and methods on the entries in the set. */
032@GwtCompatible
033@Ignore("test runners must not instantiate and run this directly, only via suites we build")
034// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
035@SuppressWarnings("JUnit4ClassUsedInJUnit3")
036public class BiMapEntrySetTester<K, V> extends AbstractBiMapTester<K, V> {
037  @MapFeature.Require(SUPPORTS_PUT)
038  @CollectionSize.Require(absent = ZERO)
039  public void testSetValue_valueAbsent() {
040    for (Entry<K, V> entry : getMap().entrySet()) {
041      if (entry.getKey().equals(k0())) {
042        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3()));
043      }
044    }
045    expectReplacement(entry(k0(), v3()));
046  }
047
048  @MapFeature.Require(SUPPORTS_PUT)
049  @CollectionSize.Require(SEVERAL)
050  public void testSetValue_valuePresent() {
051    for (Entry<K, V> entry : getMap().entrySet()) {
052      if (entry.getKey().equals(k0())) {
053        assertThrows(IllegalArgumentException.class, () -> entry.setValue(v1()));
054      }
055    }
056    expectUnchanged();
057  }
058
059  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
060  @CollectionSize.Require(absent = ZERO)
061  public void testSetValueNullUnsupported() {
062    for (Entry<K, V> entry : getMap().entrySet()) {
063      assertThrows(NullPointerException.class, () -> entry.setValue(null));
064      expectUnchanged();
065    }
066  }
067
068  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
069  @CollectionSize.Require(absent = ZERO)
070  public void testSetValueNullSupported() {
071    for (Entry<K, V> entry : getMap().entrySet()) {
072      if (entry.getKey().equals(k0())) {
073        entry.setValue(null);
074      }
075    }
076    expectReplacement(entry(k0(), (V) null));
077  }
078}