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 private void assertCleared() { 051 assertEquals(0, multimap().size()); 052 assertEmpty(multimap()); 053 assertEquals(multimap(), getSubjectGenerator().create()); 054 assertEmpty(multimap().entries()); 055 assertEmpty(multimap().asMap()); 056 assertEmpty(multimap().keySet()); 057 assertEmpty(multimap().keys()); 058 assertEmpty(multimap().values()); 059 for (K key : sampleKeys()) { 060 assertGet(key); 061 } 062 } 063 064 @MapFeature.Require(SUPPORTS_REMOVE) 065 public void testClear() { 066 multimap().clear(); 067 assertCleared(); 068 } 069 070 @MapFeature.Require(SUPPORTS_REMOVE) 071 public void testClearThroughEntries() { 072 multimap().entries().clear(); 073 assertCleared(); 074 } 075 076 @MapFeature.Require(SUPPORTS_REMOVE) 077 public void testClearThroughAsMap() { 078 multimap().asMap().clear(); 079 assertCleared(); 080 } 081 082 @MapFeature.Require(SUPPORTS_REMOVE) 083 public void testClearThroughKeySet() { 084 multimap().keySet().clear(); 085 assertCleared(); 086 } 087 088 @MapFeature.Require(SUPPORTS_REMOVE) 089 public void testClearThroughKeys() { 090 multimap().keys().clear(); 091 assertCleared(); 092 } 093 094 @MapFeature.Require(SUPPORTS_REMOVE) 095 public void testClearThroughValues() { 096 multimap().values().clear(); 097 assertCleared(); 098 } 099 100 @MapFeature.Require(SUPPORTS_REMOVE) 101 @CollectionSize.Require(absent = ZERO) 102 public void testClearPropagatesToGet() { 103 for (K key : sampleKeys()) { 104 resetContainer(); 105 Collection<V> collection = multimap().get(key); 106 multimap().clear(); 107 assertEmpty(collection); 108 } 109 } 110 111 @MapFeature.Require(SUPPORTS_REMOVE) 112 @CollectionSize.Require(absent = ZERO) 113 public void testClearPropagatesToAsMapGet() { 114 for (K key : sampleKeys()) { 115 resetContainer(); 116 Collection<V> collection = multimap().asMap().get(key); 117 if (collection != null) { 118 multimap().clear(); 119 assertEmpty(collection); 120 } 121 } 122 } 123 124 @MapFeature.Require(SUPPORTS_REMOVE) 125 public void testClearPropagatesToAsMap() { 126 Map<K, Collection<V>> asMap = multimap().asMap(); 127 multimap().clear(); 128 assertEmpty(asMap); 129 } 130 131 @MapFeature.Require(SUPPORTS_REMOVE) 132 public void testClearPropagatesToEntries() { 133 Collection<Entry<K, V>> entries = multimap().entries(); 134 multimap().clear(); 135 assertEmpty(entries); 136 } 137}