MultimapReplaceValuesTester.java revision 7dd252788645e940eada959bdde927426e2531c9
1/* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.collect.testing.google; 18 19import static com.google.common.collect.testing.features.CollectionSize.ZERO; 20import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 21import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 22 23import com.google.common.annotations.GwtCompatible; 24import com.google.common.collect.Multimap; 25import com.google.common.collect.testing.Helpers; 26import com.google.common.collect.testing.features.CollectionSize; 27import com.google.common.collect.testing.features.MapFeature; 28 29import java.util.Arrays; 30import java.util.Collection; 31import java.util.Collections; 32import java.util.List; 33 34/** 35 * Tests for {@link Multimap#replaceValues(Object, Iterable)}. 36 * 37 * @author Louis Wasserman 38 */ 39@GwtCompatible 40public class MultimapReplaceValuesTester<K, V> 41 extends AbstractMultimapTester<K, V, Multimap<K, V>> { 42 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 43 public void testReplaceEmptyValues() { 44 int size = multimap().size(); 45 K key = sampleKeys().e3; 46 @SuppressWarnings("unchecked") 47 List<V> values = Arrays.asList(sampleValues().e0, sampleValues().e2, sampleValues().e3); 48 multimap().replaceValues(key, values); 49 assertGet(key, values); 50 assertEquals(size + values.size(), multimap().size()); 51 } 52 53 @CollectionSize.Require(absent = ZERO) 54 @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) 55 public void testReplaceNonEmptyValues() { 56 List<K> keys = Helpers.copyToList(multimap().keySet()); 57 @SuppressWarnings("unchecked") 58 List<V> values = Arrays.asList(sampleValues().e0, sampleValues().e2, sampleValues().e3); 59 60 for (K k : keys) { 61 resetContainer(); 62 63 int size = multimap().size(); 64 Collection<V> oldKeyValues = Helpers.copyToList(multimap().get(k)); 65 multimap().replaceValues(k, values); 66 assertGet(k, values); 67 assertEquals(size + values.size() - oldKeyValues.size(), multimap().size()); 68 } 69 } 70 71 @MapFeature.Require(absent = SUPPORTS_REMOVE) 72 @CollectionSize.Require(absent = ZERO) 73 public void testReplaceValuesRemoveNotSupported() { 74 List<V> values = Collections.singletonList(sampleValues().e3); 75 try { 76 multimap().replaceValues(sampleKeys().e0, values); 77 fail("Expected UnsupportedOperationException"); 78 } catch (UnsupportedOperationException expected) { 79 // success 80 } 81 } 82 83 @MapFeature.Require(absent = SUPPORTS_PUT) 84 public void testReplaceValuesPutNotSupported() { 85 List<V> values = Collections.singletonList(sampleValues().e3); 86 try { 87 multimap().replaceValues(sampleKeys().e0, values); 88 fail("Expected UnsupportedOperationException"); 89 } catch (UnsupportedOperationException expected) { 90 // success 91 } 92 } 93} 94