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