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.Helpers.assertEmpty;
20  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
21  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
22  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
23  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.collect.testing.features.CollectionFeature;
27  import com.google.common.collect.testing.features.CollectionSize;
28  import java.util.Collections;
29  import java.util.Set;
30  
31  /**
32   * Tests for {@code Multiset.elementSet()} not covered by the derived {@code SetTestSuiteBuilder}.
33   *
34   * @author Louis Wasserman
35   */
36  @GwtCompatible
37  public class MultisetElementSetTester<E> extends AbstractMultisetTester<E> {
38    @CollectionFeature.Require(SUPPORTS_ADD)
39    public void testElementSetReflectsAddAbsent() {
40      Set<E> elementSet = getMultiset().elementSet();
41      assertFalse(elementSet.contains(e3()));
42      getMultiset().add(e3(), 4);
43      assertTrue(elementSet.contains(e3()));
44    }
45  
46    @CollectionSize.Require(absent = ZERO)
47    @CollectionFeature.Require(SUPPORTS_REMOVE)
48    public void testElementSetReflectsRemove() {
49      Set<E> elementSet = getMultiset().elementSet();
50      assertTrue(elementSet.contains(e0()));
51      getMultiset().removeAll(Collections.singleton(e0()));
52      assertFalse(elementSet.contains(e0()));
53    }
54  
55    @CollectionSize.Require(absent = ZERO)
56    @CollectionFeature.Require(SUPPORTS_REMOVE)
57    public void testElementSetRemovePropagatesToMultiset() {
58      Set<E> elementSet = getMultiset().elementSet();
59      assertTrue(elementSet.remove(e0()));
60      assertFalse(getMultiset().contains(e0()));
61    }
62  
63    @CollectionSize.Require(SEVERAL)
64    @CollectionFeature.Require(SUPPORTS_REMOVE)
65    public void testElementSetRemoveDuplicatePropagatesToMultiset() {
66      initThreeCopies();
67      Set<E> elementSet = getMultiset().elementSet();
68      assertTrue(elementSet.remove(e0()));
69      assertEmpty(getMultiset());
70    }
71  
72    @CollectionFeature.Require(SUPPORTS_REMOVE)
73    public void testElementSetRemoveAbsent() {
74      Set<E> elementSet = getMultiset().elementSet();
75      assertFalse(elementSet.remove(e3()));
76      expectUnchanged();
77    }
78  
79    @CollectionFeature.Require(SUPPORTS_REMOVE)
80    public void testElementSetClear() {
81      getMultiset().elementSet().clear();
82      assertEmpty(getMultiset());
83    }
84  }