001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.collect.testing.google; 016 017import static com.google.common.collect.testing.Helpers.copyToList; 018import static com.google.common.collect.testing.Helpers.copyToSet; 019import static com.google.common.collect.testing.features.CollectionSize.ZERO; 020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 021import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT; 022 023import com.google.common.annotations.GwtCompatible; 024import com.google.common.collect.SetMultimap; 025import com.google.common.collect.testing.features.CollectionSize; 026import com.google.common.collect.testing.features.MapFeature; 027import java.util.List; 028import java.util.Map.Entry; 029import java.util.Set; 030import org.junit.Ignore; 031 032/** 033 * Tests for {@link SetMultimap#replaceValues}. 034 * 035 * @author Louis Wasserman 036 */ 037@GwtCompatible 038@Ignore("test runners must not instantiate and run this directly, only via suites we build") 039// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 040@SuppressWarnings("JUnit4ClassUsedInJUnit3") 041public class SetMultimapPutTester<K, V> extends AbstractMultimapTester<K, V, SetMultimap<K, V>> { 042 // Tests for non-duplicate values are in MultimapPutTester 043 044 @MapFeature.Require(SUPPORTS_PUT) 045 @CollectionSize.Require(absent = ZERO) 046 public void testPutDuplicateValuePreservesSize() { 047 assertFalse(multimap().put(k0(), v0())); 048 assertEquals(getNumElements(), multimap().size()); 049 } 050 051 @MapFeature.Require(SUPPORTS_PUT) 052 public void testPutDuplicateValue() { 053 List<Entry<K, V>> entries = copyToList(multimap().entries()); 054 055 for (Entry<K, V> entry : entries) { 056 resetContainer(); 057 K k = entry.getKey(); 058 V v = entry.getValue(); 059 060 Set<V> values = multimap().get(k); 061 Set<V> expectedValues = copyToSet(values); 062 063 assertFalse(multimap().put(k, v)); 064 assertEquals(expectedValues, values); 065 assertGet(k, expectedValues); 066 } 067 } 068 069 @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) 070 @CollectionSize.Require(absent = ZERO) 071 public void testPutDuplicateValue_null() { 072 initMultimapWithNullValue(); 073 assertFalse(multimap().put(getKeyForNullValue(), null)); 074 expectContents(createArrayWithNullValue()); 075 } 076}