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