View Javadoc
1   /*
2    * Copyright (C) 2012 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.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
23  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.collect.Multimap;
27  import com.google.common.collect.testing.features.CollectionSize;
28  import com.google.common.collect.testing.features.MapFeature;
29  
30  /**
31   * Tester for {@link Multimap#containsEntry}.
32   *
33   * @author Louis Wasserman
34   */
35  @GwtCompatible
36  public class MultimapContainsEntryTester<K, V>
37      extends AbstractMultimapTester<K, V, Multimap<K, V>> {
38    @CollectionSize.Require(absent = ZERO)
39    public void testContainsEntryYes() {
40      assertTrue(multimap().containsEntry(k0(), v0()));
41    }
42  
43    public void testContainsEntryNo() {
44      assertFalse(multimap().containsEntry(k3(), v3()));
45    }
46  
47    public void testContainsEntryAgreesWithGet() {
48      for (K k : sampleKeys()) {
49        for (V v : sampleValues()) {
50          assertEquals(multimap().get(k).contains(v), multimap().containsEntry(k, v));
51        }
52      }
53    }
54  
55    @CollectionSize.Require(absent = ZERO)
56    @MapFeature.Require({ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES})
57    public void testContainsEntryNullYes() {
58      initMultimapWithNullKeyAndValue();
59      assertTrue(multimap().containsEntry(null, null));
60    }
61  
62    @MapFeature.Require({ALLOWS_NULL_KEY_QUERIES, ALLOWS_NULL_VALUE_QUERIES})
63    public void testContainsEntryNullNo() {
64      assertFalse(multimap().containsEntry(null, null));
65    }
66  
67    @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
68    public void testContainsEntryNullDisallowedBecauseKeyQueriesDisallowed() {
69      try {
70        multimap().containsEntry(null, v3());
71        fail("Expected NullPointerException");
72      } catch (NullPointerException expected) {
73        // success
74      }
75    }
76  
77    /**
78     * Copy of the {@link #testContainsEntryNullDisallowed} test. Needed because
79     * "optional" feature requirements are not supported.
80     */
81    @MapFeature.Require(absent = ALLOWS_NULL_VALUE_QUERIES)
82    public void testContainsEntryNullDisallowedBecauseValueQueriesDisallowed() {
83      try {
84        multimap().containsEntry(k3(), null);
85        fail("Expected NullPointerException");
86      } catch (NullPointerException expected) {
87        // success
88      }
89    }
90  }