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; 023 024import com.google.common.annotations.GwtCompatible; 025import com.google.common.collect.Multimap; 026import com.google.common.collect.testing.features.CollectionSize; 027import com.google.common.collect.testing.features.MapFeature; 028import java.util.Collection; 029import java.util.Map; 030import java.util.Map.Entry; 031import org.junit.Ignore; 032 033/** 034 * Tests for {@link Multimap#clear()}. 035 * 036 * @author Louis Wasserman 037 */ 038@GwtCompatible 039@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 040@SuppressWarnings("JUnit4ClassUsedInJUnit3") 041public class MultimapClearTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> { 042 @CollectionSize.Require(absent = ZERO) 043 @MapFeature.Require(absent = SUPPORTS_REMOVE) 044 public void testClearUnsupported() { 045 try { 046 multimap().clear(); 047 fail("Expected UnsupportedOperationException"); 048 } catch (UnsupportedOperationException expected) { 049 } 050 } 051 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}