001/* 002 * Copyright (C) 2012 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.Helpers.mapEntry; 020import static com.google.common.collect.testing.features.CollectionSize.ONE; 021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 024import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 025import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 026import static org.junit.Assert.assertThrows; 027 028import com.google.common.annotations.GwtCompatible; 029import com.google.common.collect.testing.features.CollectionSize; 030import com.google.common.collect.testing.features.MapFeature; 031 032/** Tester for {@code BiMap.put} and {@code BiMap.forcePut}. */ 033@GwtCompatible 034public class BiMapPutTester<K, V> extends AbstractBiMapTester<K, V> { 035 036 @MapFeature.Require(SUPPORTS_PUT) 037 @CollectionSize.Require(ZERO) 038 public void testPutWithSameValueFails() { 039 getMap().put(k0(), v0()); 040 assertThrows(IllegalArgumentException.class, () -> getMap().put(k1(), v0())); 041 // verify that the bimap is unchanged 042 expectAdded(e0()); 043 } 044 045 @MapFeature.Require(SUPPORTS_PUT) 046 @CollectionSize.Require(ZERO) 047 public void testPutPresentKeyDifferentValue() { 048 getMap().put(k0(), v0()); 049 getMap().put(k0(), v1()); 050 // verify that the bimap is changed, and that the old inverse mapping 051 // from v1 -> v0 is deleted 052 expectContents(mapEntry(k0(), v1())); 053 } 054 055 @MapFeature.Require(SUPPORTS_PUT) 056 @CollectionSize.Require(ZERO) 057 public void putDistinctKeysDistinctValues() { 058 getMap().put(k0(), v0()); 059 getMap().put(k1(), v1()); 060 expectAdded(e0(), e1()); 061 } 062 063 @MapFeature.Require(SUPPORTS_PUT) 064 @CollectionSize.Require(ONE) 065 public void testForcePutKeyPresent() { 066 getMap().forcePut(k0(), v1()); 067 expectContents(mapEntry(k0(), v1())); 068 assertFalse(getMap().containsValue(v0())); 069 assertNull(getMap().inverse().get(v0())); 070 assertEquals(1, getMap().size()); 071 assertTrue(getMap().containsKey(k0())); 072 } 073 074 @MapFeature.Require(SUPPORTS_PUT) 075 @CollectionSize.Require(ONE) 076 public void testForcePutValuePresent() { 077 getMap().forcePut(k1(), v0()); 078 expectContents(mapEntry(k1(), v0())); 079 assertEquals(k1(), getMap().inverse().get(v0())); 080 assertEquals(1, getMap().size()); 081 assertFalse(getMap().containsKey(k0())); 082 } 083 084 @MapFeature.Require(SUPPORTS_PUT) 085 @CollectionSize.Require(SEVERAL) 086 public void testForcePutKeyAndValuePresent() { 087 getMap().forcePut(k0(), v1()); 088 expectContents(mapEntry(k0(), v1()), mapEntry(k2(), v2())); 089 assertEquals(2, getMap().size()); 090 assertFalse(getMap().containsKey(k1())); 091 assertFalse(getMap().containsValue(v0())); 092 } 093 094 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) 095 @CollectionSize.Require(ONE) 096 public void testForcePutNullKeyPresent() { 097 initMapWithNullKey(); 098 099 getMap().forcePut(null, v1()); 100 101 expectContents(mapEntry(null, v1())); 102 103 assertFalse(getMap().containsValue(v0())); 104 105 assertTrue(getMap().containsValue(v1())); 106 assertTrue(getMap().inverse().containsKey(v1())); 107 assertNull(getMap().inverse().get(v1())); 108 assertEquals(v1(), getMap().get(null)); 109 assertEquals(1, getMap().size()); 110 } 111 112 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) 113 @CollectionSize.Require(ONE) 114 public void testForcePutNullValuePresent() { 115 initMapWithNullValue(); 116 117 getMap().forcePut(k1(), null); 118 119 expectContents(mapEntry(k1(), null)); 120 121 assertFalse(getMap().containsKey(k0())); 122 123 assertTrue(getMap().containsKey(k1())); 124 assertTrue(getMap().inverse().containsKey(null)); 125 assertNull(getMap().get(k1())); 126 assertEquals(k1(), getMap().inverse().get(null)); 127 assertEquals(1, getMap().size()); 128 } 129 130 // nb: inverse is run through its own entire suite 131 132 @MapFeature.Require(SUPPORTS_PUT) 133 @CollectionSize.Require(ZERO) 134 public void testInversePut() { 135 getMap().put(k0(), v0()); 136 getMap().inverse().put(v1(), k1()); 137 expectAdded(e0(), e1()); 138 } 139}