001/* 002 * Copyright (C) 2013 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.assertEmpty; 020import static com.google.common.collect.testing.features.CollectionSize.ZERO; 021import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 022import static com.google.common.collect.testing.google.GoogleHelpers.assertEmpty; 023import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.collect.Multimap; 027import com.google.common.collect.testing.features.CollectionSize; 028import com.google.common.collect.testing.features.MapFeature; 029import java.util.Collection; 030import java.util.Map; 031import java.util.Map.Entry; 032import org.junit.Ignore; 033 034/** 035 * Tests for {@link Multimap#clear()}. 036 * 037 * @author Louis Wasserman 038 */ 039@GwtCompatible 040@Ignore("test runners must not instantiate and run this directly, only via suites we build") 041// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 042@SuppressWarnings("JUnit4ClassUsedInJUnit3") 043public class MultimapClearTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 044 @CollectionSize.Require(absent = ZERO) 045 @MapFeature.Require(absent = SUPPORTS_REMOVE) 046 public void testClearUnsupported() { 047 assertThrows(UnsupportedOperationException.class, () -> multimap().clear()); 048 } 049 050 // Empty multimaps *do* have defined equals semantics. 051 @SuppressWarnings("UndefinedEquals") 052 private void assertCleared() { 053 assertEquals(0, multimap().size()); 054 assertEmpty(multimap()); 055 assertEquals(multimap(), getSubjectGenerator().create()); 056 assertEmpty(multimap().entries()); 057 assertEmpty(multimap().asMap()); 058 assertEmpty(multimap().keySet()); 059 assertEmpty(multimap().keys()); 060 assertEmpty(multimap().values()); 061 for (K key : sampleKeys()) { 062 assertGet(key); 063 } 064 } 065 066 @MapFeature.Require(SUPPORTS_REMOVE) 067 public void testClear() { 068 multimap().clear(); 069 assertCleared(); 070 } 071 072 @MapFeature.Require(SUPPORTS_REMOVE) 073 public void testClearThroughEntries() { 074 multimap().entries().clear(); 075 assertCleared(); 076 } 077 078 @MapFeature.Require(SUPPORTS_REMOVE) 079 public void testClearThroughAsMap() { 080 multimap().asMap().clear(); 081 assertCleared(); 082 } 083 084 @MapFeature.Require(SUPPORTS_REMOVE) 085 public void testClearThroughKeySet() { 086 multimap().keySet().clear(); 087 assertCleared(); 088 } 089 090 @MapFeature.Require(SUPPORTS_REMOVE) 091 public void testClearThroughKeys() { 092 multimap().keys().clear(); 093 assertCleared(); 094 } 095 096 @MapFeature.Require(SUPPORTS_REMOVE) 097 public void testClearThroughValues() { 098 multimap().values().clear(); 099 assertCleared(); 100 } 101 102 @MapFeature.Require(SUPPORTS_REMOVE) 103 @CollectionSize.Require(absent = ZERO) 104 public void testClearPropagatesToGet() { 105 for (K key : sampleKeys()) { 106 resetContainer(); 107 Collection<V> collection = multimap().get(key); 108 multimap().clear(); 109 assertEmpty(collection); 110 } 111 } 112 113 @MapFeature.Require(SUPPORTS_REMOVE) 114 @CollectionSize.Require(absent = ZERO) 115 public void testClearPropagatesToAsMapGet() { 116 for (K key : sampleKeys()) { 117 resetContainer(); 118 Collection<V> collection = multimap().asMap().get(key); 119 if (collection != null) { 120 multimap().clear(); 121 assertEmpty(collection); 122 } 123 } 124 } 125 126 @MapFeature.Require(SUPPORTS_REMOVE) 127 public void testClearPropagatesToAsMap() { 128 Map<K, Collection<V>> asMap = multimap().asMap(); 129 multimap().clear(); 130 assertEmpty(asMap); 131 } 132 133 @MapFeature.Require(SUPPORTS_REMOVE) 134 public void testClearPropagatesToEntries() { 135 Collection<Entry<K, V>> entries = multimap().entries(); 136 multimap().clear(); 137 assertEmpty(entries); 138 } 139}