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 @SuppressWarnings("unchecked") 051 List<V> values = Arrays.asList(v0(), null, v3()); 052 multimap().replaceValues(k0(), values); 053 assertGet(k0(), values); 054 } 055 056 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS}) 057 public void testReplaceValuesWithNullKey() { 058 @SuppressWarnings("unchecked") 059 List<V> values = Arrays.asList(v0(), v2(), v3()); 060 multimap().replaceValues(null, values); 061 assertGet(null, values); 062 } 063 064 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 065 public void testReplaceEmptyValues() { 066 int size = multimap().size(); 067 @SuppressWarnings("unchecked") 068 List<V> values = Arrays.asList(v0(), v2(), v3()); 069 multimap().replaceValues(k3(), values); 070 assertGet(k3(), values); 071 assertEquals(size + values.size(), multimap().size()); 072 } 073 074 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 075 public void testReplaceValuesWithEmpty() { 076 int size = multimap().size(); 077 List<V> oldValues = new ArrayList<>(multimap().get(k0())); 078 @SuppressWarnings("unchecked") 079 List<V> values = Collections.emptyList(); 080 assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values))); 081 assertGet(k0()); 082 assertEquals(size - oldValues.size(), multimap().size()); 083 } 084 085 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 086 public void testReplaceValuesWithDuplicates() { 087 int size = multimap().size(); 088 List<V> oldValues = new ArrayList<>(multimap().get(k0())); 089 List<V> values = Arrays.asList(v0(), v3(), v0()); 090 assertEquals(oldValues, new ArrayList<V>(multimap().replaceValues(k0(), values))); 091 assertEquals(size - oldValues.size() + multimap().get(k0()).size(), multimap().size()); 092 assertTrue(multimap().get(k0()).containsAll(values)); 093 } 094 095 @CollectionSize.Require(absent = ZERO) 096 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 097 public void testReplaceNonEmptyValues() { 098 List<K> keys = Helpers.copyToList(multimap().keySet()); 099 @SuppressWarnings("unchecked") 100 List<V> values = Arrays.asList(v0(), v2(), v3()); 101 102 for (K k : keys) { 103 resetContainer(); 104 105 int size = multimap().size(); 106 Collection<V> oldKeyValues = Helpers.copyToList(multimap().get(k)); 107 multimap().replaceValues(k, values); 108 assertGet(k, values); 109 assertEquals(size + values.size() - oldKeyValues.size(), multimap().size()); 110 } 111 } 112 113 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 114 public void testReplaceValuesPropagatesToGet() { 115 Collection<V> getCollection = multimap().get(k0()); 116 @SuppressWarnings("unchecked") 117 List<V> values = Arrays.asList(v0(), v2(), v3()); 118 multimap().replaceValues(k0(), values); 119 assertContentsAnyOrder(getCollection, v0(), v2(), v3()); 120 } 121 122 @MapFeature.Require(absent = SUPPORTS_REMOVE) 123 @CollectionSize.Require(absent = ZERO) 124 public void testReplaceValuesRemoveNotSupported() { 125 List<V> values = Collections.singletonList(v3()); 126 try { 127 multimap().replaceValues(k0(), values); 128 fail("Expected UnsupportedOperationException"); 129 } catch (UnsupportedOperationException expected) { 130 // success 131 } 132 } 133 134 @MapFeature.Require(absent = SUPPORTS_PUT) 135 public void testReplaceValuesPutNotSupported() { 136 List<V> values = Collections.singletonList(v3()); 137 try { 138 multimap().replaceValues(k0(), values); 139 fail("Expected UnsupportedOperationException"); 140 } catch (UnsupportedOperationException expected) { 141 // success 142 } 143 } 144}