View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.collect.testing.features.CollectionFeature;
23  import java.util.Arrays;
24  import java.util.Collections;
25  
26  /**
27   * Tests for {@code Multiset.add}.
28   *
29   * @author Jared Levy
30   */
31  @GwtCompatible
32  public class MultisetAddTester<E> extends AbstractMultisetTester<E> {
33    @CollectionFeature.Require(absent = SUPPORTS_ADD)
34    public void testAddUnsupported() {
35      try {
36        getMultiset().add(e0());
37        fail("Expected UnsupportedOperationException");
38      } catch (UnsupportedOperationException expected) {
39      }
40    }
41  
42    @CollectionFeature.Require(SUPPORTS_ADD)
43    public void testAddMeansAddOne() {
44      int originalCount = getMultiset().count(e0());
45      assertTrue(getMultiset().add(e0()));
46      assertEquals(originalCount + 1, getMultiset().count(e0()));
47    }
48  
49    @CollectionFeature.Require(SUPPORTS_ADD)
50    public void testAddOccurrencesZero() {
51      int originalCount = getMultiset().count(e0());
52      assertEquals("old count", originalCount, getMultiset().add(e0(), 0));
53      expectUnchanged();
54    }
55  
56    @CollectionFeature.Require(SUPPORTS_ADD)
57    public void testAddOccurrences() {
58      int originalCount = getMultiset().count(e0());
59      assertEquals("old count", originalCount, getMultiset().add(e0(), 2));
60      assertEquals("old count", originalCount + 2, getMultiset().count(e0()));
61    }
62  
63    @CollectionFeature.Require(SUPPORTS_ADD)
64    public void testAddSeveralTimes() {
65      int originalCount = getMultiset().count(e0());
66      assertEquals(originalCount, getMultiset().add(e0(), 2));
67      assertTrue(getMultiset().add(e0()));
68      assertEquals(originalCount + 3, getMultiset().add(e0(), 1));
69      assertEquals(originalCount + 4, getMultiset().count(e0()));
70    }
71  
72    @CollectionFeature.Require(absent = SUPPORTS_ADD)
73    public void testAddOccurrences_unsupported() {
74      try {
75        getMultiset().add(e0(), 2);
76        fail("unsupported multiset.add(E, int) didn't throw exception");
77      } catch (UnsupportedOperationException required) {
78      }
79    }
80  
81    @CollectionFeature.Require(SUPPORTS_ADD)
82    public void testAddOccurrencesNegative() {
83      try {
84        getMultiset().add(e0(), -1);
85        fail("multiset.add(E, -1) didn't throw an exception");
86      } catch (IllegalArgumentException required) {
87      }
88    }
89  
90    @CollectionFeature.Require(SUPPORTS_ADD)
91    public void testAddTooMany() {
92      getMultiset().add(e3(), Integer.MAX_VALUE);
93      try {
94        getMultiset().add(e3());
95        fail();
96      } catch (IllegalArgumentException expected) {
97      }
98      assertEquals(Integer.MAX_VALUE, getMultiset().count(e3()));
99      assertEquals(Integer.MAX_VALUE, getMultiset().size());
100   }
101 
102   @CollectionFeature.Require(SUPPORTS_ADD)
103   public void testAddAll_emptySet() {
104     assertFalse(getMultiset().addAll(Collections.<E>emptySet()));
105     expectUnchanged();
106   }
107 
108   @CollectionFeature.Require(SUPPORTS_ADD)
109   public void testAddAll_emptyMultiset() {
110     assertFalse(getMultiset().addAll(getSubjectGenerator().create()));
111     expectUnchanged();
112   }
113 
114   @CollectionFeature.Require(SUPPORTS_ADD)
115   public void testAddAll_nonEmptyList() {
116     assertTrue(getMultiset().addAll(Arrays.asList(e3(), e4(), e3())));
117     expectAdded(e3(), e4(), e3());
118   }
119 
120   @CollectionFeature.Require(SUPPORTS_ADD)
121   public void testAddAll_nonEmptyMultiset() {
122     assertTrue(getMultiset().addAll(getSubjectGenerator().create(e3(), e4(), e3())));
123     expectAdded(e3(), e4(), e3());
124   }
125 }