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 // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041@SuppressWarnings("JUnit4ClassUsedInJUnit3")
042@ElementTypesAreNonnullByDefault
043public class MultimapSizeTester<K extends @Nullable Object, V extends @Nullable Object>
044    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
045
046  public void testSize() {
047    int expectedSize = getNumElements();
048    Multimap<K, V> multimap = multimap();
049    assertEquals(expectedSize, multimap.size());
050
051    int size = 0;
052    for (Entry<K, V> entry : multimap.entries()) {
053      assertTrue(multimap.containsEntry(entry.getKey(), entry.getValue()));
054      size++;
055    }
056    assertEquals(expectedSize, size);
057
058    int size2 = 0;
059    for (Entry<K, Collection<V>> entry2 : multimap.asMap().entrySet()) {
060      size2 += entry2.getValue().size();
061    }
062    assertEquals(expectedSize, size2);
063  }
064
065  @CollectionSize.Require(ZERO)
066  public void testIsEmptyYes() {
067    assertTrue(multimap().isEmpty());
068  }
069
070  @CollectionSize.Require(absent = ZERO)
071  public void testIsEmptyNo() {
072    assertFalse(multimap().isEmpty());
073  }
074
075  @CollectionSize.Require(absent = ZERO)
076  @MapFeature.Require(ALLOWS_NULL_KEYS)
077  public void testSizeNullKey() {
078    initMultimapWithNullKey();
079    assertEquals(getNumElements(), multimap().size());
080    assertFalse(multimap().isEmpty());
081  }
082
083  @CollectionSize.Require(absent = ZERO)
084  @MapFeature.Require(ALLOWS_NULL_VALUES)
085  public void testSizeNullValue() {
086    initMultimapWithNullValue();
087    assertEquals(getNumElements(), multimap().size());
088    assertFalse(multimap().isEmpty());
089  }
090
091  @CollectionSize.Require(absent = ZERO)
092  @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
093  public void testSizeNullKeyAndValue() {
094    initMultimapWithNullKeyAndValue();
095    assertEquals(getNumElements(), multimap().size());
096    assertFalse(multimap().isEmpty());
097  }
098
099  @CollectionSize.Require(SEVERAL)
100  public void testSizeMultipleValues() {
101    resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v2()));
102
103    assertEquals(3, multimap().size());
104    assertEquals(3, multimap().entries().size());
105    assertEquals(3, multimap().keys().size());
106
107    assertEquals(1, multimap().keySet().size());
108    assertEquals(1, multimap().asMap().size());
109  }
110}