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.features.CollectionSize.ZERO; 021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 023import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 024import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 025 026import com.google.common.annotations.GwtCompatible; 027import com.google.common.collect.Multimap; 028import com.google.common.collect.testing.Helpers; 029import com.google.common.collect.testing.features.CollectionSize; 030import com.google.common.collect.testing.features.MapFeature; 031import java.util.ArrayList; 032import java.util.Arrays; 033import java.util.Collection; 034import java.util.Collections; 035import java.util.List; 036import org.junit.Ignore; 037 038/** 039 * Tests for {@link Multimap#replaceValues(Object, Iterable)}. 040 * 041 * @author Louis Wasserman 042 */ 043@GwtCompatible 044@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 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 = Arrays.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 = Arrays.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 = Arrays.asList(v0(), v2(), v3()); 066 multimap().replaceValues(k3(), values); 067 assertGet(k3(), values); 068 assertEquals(size + values.size(), multimap().size()); 069 } 070 071 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 072 public void testReplaceValuesWithEmpty() { 073 int size = multimap().size(); 074 List<V> oldValues = new ArrayList<>(multimap().get(k0())); 075 List<V> values = Collections.emptyList(); 076 assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values))); 077 assertGet(k0()); 078 assertEquals(size - oldValues.size(), multimap().size()); 079 } 080 081 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 082 public void testReplaceValuesWithDuplicates() { 083 int size = multimap().size(); 084 List<V> oldValues = new ArrayList<>(multimap().get(k0())); 085 List<V> values = Arrays.asList(v0(), v3(), v0()); 086 assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values))); 087 assertEquals(size - oldValues.size() + multimap().get(k0()).size(), multimap().size()); 088 assertTrue(multimap().get(k0()).containsAll(values)); 089 } 090 091 @CollectionSize.Require(absent = ZERO) 092 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 093 public void testReplaceNonEmptyValues() { 094 List<K> keys = Helpers.copyToList(multimap().keySet()); 095 List<V> values = Arrays.asList(v0(), v2(), v3()); 096 097 for (K k : keys) { 098 resetContainer(); 099 100 int size = multimap().size(); 101 Collection<V> oldKeyValues = Helpers.copyToList(multimap().get(k)); 102 multimap().replaceValues(k, values); 103 assertGet(k, values); 104 assertEquals(size + values.size() - oldKeyValues.size(), multimap().size()); 105 } 106 } 107 108 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 109 public void testReplaceValuesPropagatesToGet() { 110 Collection<V> getCollection = multimap().get(k0()); 111 List<V> values = Arrays.asList(v0(), v2(), v3()); 112 multimap().replaceValues(k0(), values); 113 assertContentsAnyOrder(getCollection, v0(), v2(), v3()); 114 } 115 116 @MapFeature.Require(absent = SUPPORTS_REMOVE) 117 @CollectionSize.Require(absent = ZERO) 118 public void testReplaceValuesRemoveNotSupported() { 119 List<V> values = Collections.singletonList(v3()); 120 try { 121 multimap().replaceValues(k0(), values); 122 fail("Expected UnsupportedOperationException"); 123 } catch (UnsupportedOperationException expected) { 124 // success 125 } 126 } 127 128 @MapFeature.Require(absent = SUPPORTS_PUT) 129 public void testReplaceValuesPutNotSupported() { 130 List<V> values = Collections.singletonList(v3()); 131 try { 132 multimap().replaceValues(k0(), values); 133 fail("Expected UnsupportedOperationException"); 134 } catch (UnsupportedOperationException expected) { 135 // success 136 } 137 } 138}