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