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.assertContentsAnyOrder; 020import static com.google.common.collect.testing.Helpers.copyToList; 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; 025import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 026import static java.util.Arrays.asList; 027import static java.util.Collections.emptyList; 028import static java.util.Collections.singletonList; 029import static org.junit.Assert.assertThrows; 030 031import com.google.common.annotations.GwtCompatible; 032import com.google.common.collect.Multimap; 033import com.google.common.collect.testing.features.CollectionSize; 034import com.google.common.collect.testing.features.MapFeature; 035import java.util.ArrayList; 036import java.util.Collection; 037import java.util.List; 038 039/** 040 * Tests for {@link Multimap#replaceValues(Object, Iterable)}. 041 * 042 * @author Louis Wasserman 043 */ 044@GwtCompatible 045public class MultimapReplaceValuesTester<K, V> 046 extends AbstractMultimapTester<K, V, Multimap<K, V>> { 047 048 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) 049 public void testReplaceValuesWithNullValue() { 050 List<V> values = asList(v0(), null, v3()); 051 multimap().replaceValues(k0(), values); 052 assertGet(k0(), values); 053 } 054 055 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS}) 056 public void testReplaceValuesWithNullKey() { 057 List<V> values = asList(v0(), v2(), v3()); 058 multimap().replaceValues(null, values); 059 assertGet(null, values); 060 } 061 062 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 063 public void testReplaceEmptyValues() { 064 int size = multimap().size(); 065 List<V> values = asList(v0(), v2(), v3()); 066 multimap().replaceValues(k3(), values); 067 assertGet(k3(), values); 068 assertEquals(size + values.size(), multimap().size()); 069 } 070 071 @SuppressWarnings("EmptyList") // ImmutableList doesn't support nullable element types 072 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 073 public void testReplaceValuesWithEmpty() { 074 int size = multimap().size(); 075 List<V> oldValues = new ArrayList<>(multimap().get(k0())); 076 List<V> values = emptyList(); 077 assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values))); 078 assertGet(k0()); 079 assertEquals(size - oldValues.size(), multimap().size()); 080 } 081 082 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 083 public void testReplaceValuesWithDuplicates() { 084 int size = multimap().size(); 085 List<V> oldValues = new ArrayList<>(multimap().get(k0())); 086 List<V> values = asList(v0(), v3(), v0()); 087 assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values))); 088 assertEquals(size - oldValues.size() + multimap().get(k0()).size(), multimap().size()); 089 assertTrue(multimap().get(k0()).containsAll(values)); 090 } 091 092 @CollectionSize.Require(absent = ZERO) 093 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 094 public void testReplaceNonEmptyValues() { 095 List<K> keys = copyToList(multimap().keySet()); 096 List<V> values = asList(v0(), v2(), v3()); 097 098 for (K k : keys) { 099 resetContainer(); 100 101 int size = multimap().size(); 102 Collection<V> oldKeyValues = copyToList(multimap().get(k)); 103 multimap().replaceValues(k, values); 104 assertGet(k, values); 105 assertEquals(size + values.size() - oldKeyValues.size(), multimap().size()); 106 } 107 } 108 109 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 110 public void testReplaceValuesPropagatesToGet() { 111 Collection<V> getCollection = multimap().get(k0()); 112 List<V> values = asList(v0(), v2(), v3()); 113 multimap().replaceValues(k0(), values); 114 assertContentsAnyOrder(getCollection, v0(), v2(), v3()); 115 } 116 117 @MapFeature.Require(absent = SUPPORTS_REMOVE) 118 @CollectionSize.Require(absent = ZERO) 119 public void testReplaceValuesRemoveNotSupported() { 120 List<V> values = singletonList(v3()); 121 assertThrows(UnsupportedOperationException.class, () -> multimap().replaceValues(k0(), values)); 122 } 123 124 @MapFeature.Require(absent = SUPPORTS_PUT) 125 public void testReplaceValuesPutNotSupported() { 126 List<V> values = singletonList(v3()); 127 assertThrows(UnsupportedOperationException.class, () -> multimap().replaceValues(k0(), values)); 128 } 129}