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