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.features.CollectionFeature.SUPPORTS_ADD;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.collect.testing.features.CollectionFeature;
023import java.util.Arrays;
024import java.util.Collections;
025import org.junit.Ignore;
026
027/**
028 * Tests for {@code Multiset.add}.
029 *
030 * @author Jared Levy
031 */
032@GwtCompatible
033@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
034public class MultisetAddTester<E> extends AbstractMultisetTester<E> {
035  @CollectionFeature.Require(absent = SUPPORTS_ADD)
036  public void testAddUnsupported() {
037    try {
038      getMultiset().add(e0());
039      fail("Expected UnsupportedOperationException");
040    } catch (UnsupportedOperationException expected) {
041    }
042  }
043
044  @CollectionFeature.Require(SUPPORTS_ADD)
045  public void testAddMeansAddOne() {
046    int originalCount = getMultiset().count(e0());
047    assertTrue(getMultiset().add(e0()));
048    assertEquals(originalCount + 1, getMultiset().count(e0()));
049  }
050
051  @CollectionFeature.Require(SUPPORTS_ADD)
052  public void testAddOccurrencesZero() {
053    int originalCount = getMultiset().count(e0());
054    assertEquals("old count", originalCount, getMultiset().add(e0(), 0));
055    expectUnchanged();
056  }
057
058  @CollectionFeature.Require(SUPPORTS_ADD)
059  public void testAddOccurrences() {
060    int originalCount = getMultiset().count(e0());
061    assertEquals("old count", originalCount, getMultiset().add(e0(), 2));
062    assertEquals("old count", originalCount + 2, getMultiset().count(e0()));
063  }
064
065  @CollectionFeature.Require(SUPPORTS_ADD)
066  public void testAddSeveralTimes() {
067    int originalCount = getMultiset().count(e0());
068    assertEquals(originalCount, getMultiset().add(e0(), 2));
069    assertTrue(getMultiset().add(e0()));
070    assertEquals(originalCount + 3, getMultiset().add(e0(), 1));
071    assertEquals(originalCount + 4, getMultiset().count(e0()));
072  }
073
074  @CollectionFeature.Require(absent = SUPPORTS_ADD)
075  public void testAddOccurrences_unsupported() {
076    try {
077      getMultiset().add(e0(), 2);
078      fail("unsupported multiset.add(E, int) didn't throw exception");
079    } catch (UnsupportedOperationException required) {
080    }
081  }
082
083  @CollectionFeature.Require(SUPPORTS_ADD)
084  public void testAddOccurrencesNegative() {
085    try {
086      getMultiset().add(e0(), -1);
087      fail("multiset.add(E, -1) didn't throw an exception");
088    } catch (IllegalArgumentException required) {
089    }
090  }
091
092  @CollectionFeature.Require(SUPPORTS_ADD)
093  public void testAddTooMany() {
094    getMultiset().add(e3(), Integer.MAX_VALUE);
095    try {
096      getMultiset().add(e3());
097      fail();
098    } catch (IllegalArgumentException expected) {
099    }
100    assertEquals(Integer.MAX_VALUE, getMultiset().count(e3()));
101    assertEquals(Integer.MAX_VALUE, getMultiset().size());
102  }
103
104  @CollectionFeature.Require(SUPPORTS_ADD)
105  public void testAddAll_emptySet() {
106    assertFalse(getMultiset().addAll(Collections.<E>emptySet()));
107    expectUnchanged();
108  }
109
110  @CollectionFeature.Require(SUPPORTS_ADD)
111  public void testAddAll_emptyMultiset() {
112    assertFalse(getMultiset().addAll(getSubjectGenerator().create()));
113    expectUnchanged();
114  }
115
116  @CollectionFeature.Require(SUPPORTS_ADD)
117  public void testAddAll_nonEmptyList() {
118    assertTrue(getMultiset().addAll(Arrays.asList(e3(), e4(), e3())));
119    expectAdded(e3(), e4(), e3());
120  }
121
122  @CollectionFeature.Require(SUPPORTS_ADD)
123  public void testAddAll_nonEmptyMultiset() {
124    assertTrue(getMultiset().addAll(getSubjectGenerator().create(e3(), e4(), e3())));
125    expectAdded(e3(), e4(), e3());
126  }
127}