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