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.
034@SuppressWarnings("JUnit4ClassUsedInJUnit3")
035public class MultisetAddTester<E> extends AbstractMultisetTester<E> {
036  @CollectionFeature.Require(absent = SUPPORTS_ADD)
037  public void testAddUnsupported() {
038    try {
039      getMultiset().add(e0());
040      fail("Expected UnsupportedOperationException");
041    } catch (UnsupportedOperationException expected) {
042    }
043  }
044
045  @CollectionFeature.Require(SUPPORTS_ADD)
046  public void testAddMeansAddOne() {
047    int originalCount = getMultiset().count(e0());
048    assertTrue(getMultiset().add(e0()));
049    assertEquals(originalCount + 1, getMultiset().count(e0()));
050  }
051
052  @CollectionFeature.Require(SUPPORTS_ADD)
053  public void testAddOccurrencesZero() {
054    int originalCount = getMultiset().count(e0());
055    assertEquals("old count", originalCount, getMultiset().add(e0(), 0));
056    expectUnchanged();
057  }
058
059  @CollectionFeature.Require(SUPPORTS_ADD)
060  public void testAddOccurrences() {
061    int originalCount = getMultiset().count(e0());
062    assertEquals("old count", originalCount, getMultiset().add(e0(), 2));
063    assertEquals("old count", originalCount + 2, getMultiset().count(e0()));
064  }
065
066  @CollectionFeature.Require(SUPPORTS_ADD)
067  public void testAddSeveralTimes() {
068    int originalCount = getMultiset().count(e0());
069    assertEquals(originalCount, getMultiset().add(e0(), 2));
070    assertTrue(getMultiset().add(e0()));
071    assertEquals(originalCount + 3, getMultiset().add(e0(), 1));
072    assertEquals(originalCount + 4, getMultiset().count(e0()));
073  }
074
075  @CollectionFeature.Require(absent = SUPPORTS_ADD)
076  public void testAddOccurrences_unsupported() {
077    try {
078      getMultiset().add(e0(), 2);
079      fail("unsupported multiset.add(E, int) didn't throw exception");
080    } catch (UnsupportedOperationException required) {
081    }
082  }
083
084  @CollectionFeature.Require(SUPPORTS_ADD)
085  public void testAddOccurrencesNegative() {
086    try {
087      getMultiset().add(e0(), -1);
088      fail("multiset.add(E, -1) didn't throw an exception");
089    } catch (IllegalArgumentException required) {
090    }
091  }
092
093  @CollectionFeature.Require(SUPPORTS_ADD)
094  public void testAddTooMany() {
095    getMultiset().add(e3(), Integer.MAX_VALUE);
096    try {
097      getMultiset().add(e3());
098      fail();
099    } catch (IllegalArgumentException expected) {
100    }
101    assertEquals(Integer.MAX_VALUE, getMultiset().count(e3()));
102    assertEquals(Integer.MAX_VALUE, getMultiset().size());
103  }
104
105  @CollectionFeature.Require(SUPPORTS_ADD)
106  public void testAddAll_emptySet() {
107    assertFalse(getMultiset().addAll(Collections.<E>emptySet()));
108    expectUnchanged();
109  }
110
111  @CollectionFeature.Require(SUPPORTS_ADD)
112  public void testAddAll_emptyMultiset() {
113    assertFalse(getMultiset().addAll(getSubjectGenerator().create()));
114    expectUnchanged();
115  }
116
117  @CollectionFeature.Require(SUPPORTS_ADD)
118  public void testAddAll_nonEmptyList() {
119    assertTrue(getMultiset().addAll(Arrays.asList(e3(), e4(), e3())));
120    expectAdded(e3(), e4(), e3());
121  }
122
123  @CollectionFeature.Require(SUPPORTS_ADD)
124  public void testAddAll_nonEmptyMultiset() {
125    assertTrue(getMultiset().addAll(getSubjectGenerator().create(e3(), e4(), e3())));
126    expectAdded(e3(), e4(), e3());
127  }
128}