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 org.junit.Assert.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;
029
030/** Tester for {@code BiMap.entrySet} and methods on the entries in the set. */
031@GwtCompatible
032public class BiMapEntrySetTester<K, V> extends AbstractBiMapTester<K, V> {
033  @MapFeature.Require(SUPPORTS_PUT)
034  @CollectionSize.Require(absent = ZERO)
035  public void testSetValue_valueAbsent() {
036    for (Entry<K, V> entry : getMap().entrySet()) {
037      if (entry.getKey().equals(k0())) {
038        assertEquals("entry.setValue() should return the old value", v0(), entry.setValue(v3()));
039      }
040    }
041    expectReplacement(entry(k0(), v3()));
042  }
043
044  @MapFeature.Require(SUPPORTS_PUT)
045  @CollectionSize.Require(SEVERAL)
046  public void testSetValue_valuePresent() {
047    for (Entry<K, V> entry : getMap().entrySet()) {
048      if (entry.getKey().equals(k0())) {
049        assertThrows(IllegalArgumentException.class, () -> entry.setValue(v1()));
050      }
051    }
052    expectUnchanged();
053  }
054
055  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
056  @CollectionSize.Require(absent = ZERO)
057  public void testSetValueNullUnsupported() {
058    for (Entry<K, V> entry : getMap().entrySet()) {
059      assertThrows(NullPointerException.class, () -> entry.setValue(null));
060      expectUnchanged();
061    }
062  }
063
064  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
065  @CollectionSize.Require(absent = ZERO)
066  public void testSetValueNullSupported() {
067    for (Entry<K, V> entry : getMap().entrySet()) {
068      if (entry.getKey().equals(k0())) {
069        entry.setValue(null);
070      }
071    }
072    expectReplacement(entry(k0(), null));
073  }
074}