001/* 002 * Copyright (C) 2012 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.mapEntry; 020import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 021import static com.google.common.collect.testing.features.CollectionSize.ZERO; 022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS; 023import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; 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.Entry; 031import org.checkerframework.checker.nullness.qual.Nullable; 032import org.junit.Ignore; 033 034/** 035 * Tester for the {@code size} methods of {@code Multimap} and its views. 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") 043@ElementTypesAreNonnullByDefault 044public class MultimapSizeTester<K extends @Nullable Object, V extends @Nullable Object> 045 extends AbstractMultimapTester<K, V, Multimap<K, V>> { 046 047 public void testSize() { 048 int expectedSize = getNumElements(); 049 Multimap<K, V> multimap = multimap(); 050 assertEquals(expectedSize, multimap.size()); 051 052 int size = 0; 053 for (Entry<K, V> entry : multimap.entries()) { 054 assertTrue(multimap.containsEntry(entry.getKey(), entry.getValue())); 055 size++; 056 } 057 assertEquals(expectedSize, size); 058 059 int size2 = 0; 060 for (Entry<K, Collection<V>> entry2 : multimap.asMap().entrySet()) { 061 size2 += entry2.getValue().size(); 062 } 063 assertEquals(expectedSize, size2); 064 } 065 066 @CollectionSize.Require(ZERO) 067 public void testIsEmptyYes() { 068 assertTrue(multimap().isEmpty()); 069 } 070 071 @CollectionSize.Require(absent = ZERO) 072 public void testIsEmptyNo() { 073 assertFalse(multimap().isEmpty()); 074 } 075 076 @CollectionSize.Require(absent = ZERO) 077 @MapFeature.Require(ALLOWS_NULL_KEYS) 078 public void testSizeNullKey() { 079 initMultimapWithNullKey(); 080 assertEquals(getNumElements(), multimap().size()); 081 assertFalse(multimap().isEmpty()); 082 } 083 084 @CollectionSize.Require(absent = ZERO) 085 @MapFeature.Require(ALLOWS_NULL_VALUES) 086 public void testSizeNullValue() { 087 initMultimapWithNullValue(); 088 assertEquals(getNumElements(), multimap().size()); 089 assertFalse(multimap().isEmpty()); 090 } 091 092 @CollectionSize.Require(absent = ZERO) 093 @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES}) 094 public void testSizeNullKeyAndValue() { 095 initMultimapWithNullKeyAndValue(); 096 assertEquals(getNumElements(), multimap().size()); 097 assertFalse(multimap().isEmpty()); 098 } 099 100 @CollectionSize.Require(SEVERAL) 101 public void testSizeMultipleValues() { 102 resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v2())); 103 104 assertEquals(3, multimap().size()); 105 assertEquals(3, multimap().entries().size()); 106 assertEquals(3, multimap().keys().size()); 107 108 assertEquals(1, multimap().keySet().size()); 109 assertEquals(1, multimap().asMap().size()); 110 } 111}