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