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)
testContainsEntryYes()39   public void testContainsEntryYes() {
40     assertTrue(multimap().containsEntry(sampleKeys().e0, sampleValues().e0));
41   }
42 
testContainsEntryNo()43   public void testContainsEntryNo() {
44     assertFalse(multimap().containsEntry(sampleKeys().e3, sampleValues().e3));
45   }
46 
testContainsEntryAgreesWithGet()47   public void testContainsEntryAgreesWithGet() {
48     for (K k : sampleKeys()) {
49       for (V v : sampleValues()) {
50         assertEquals(multimap().get(k).contains(v),
51             multimap().containsEntry(k, v));
52       }
53     }
54   }
55 
56   @CollectionSize.Require(absent = ZERO)
57   @MapFeature.Require({ ALLOWS_NULL_KEYS, ALLOWS_NULL_VALUES })
testContainsEntryNullYes()58   public void testContainsEntryNullYes() {
59     initMultimapWithNullKeyAndValue();
60     assertTrue(multimap().containsEntry(null, null));
61   }
62 
63   @MapFeature.Require({ ALLOWS_NULL_KEY_QUERIES, ALLOWS_NULL_VALUE_QUERIES })
testContainsEntryNullNo()64   public void testContainsEntryNullNo() {
65     assertFalse(multimap().containsEntry(null, null));
66   }
67 
68   @MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
testContainsEntryNullDisallowedBecauseKeyQueriesDisallowed()69   public void testContainsEntryNullDisallowedBecauseKeyQueriesDisallowed() {
70     try {
71       multimap().containsEntry(null, sampleValues().e3);
72       fail("Expected NullPointerException");
73     } catch (NullPointerException expected) {
74       // success
75     }
76   }
77 
78   /**
79    * Copy of the {@link #testContainsEntryNullDisallowed} test. Needed because
80    * "optional" feature requirements are not supported.
81    */
82   @MapFeature.Require(absent = ALLOWS_NULL_VALUE_QUERIES)
testContainsEntryNullDisallowedBecauseValueQueriesDisallowed()83   public void testContainsEntryNullDisallowedBecauseValueQueriesDisallowed() {
84     try {
85       multimap().containsEntry(sampleKeys().e3, null);
86       fail("Expected NullPointerException");
87     } catch (NullPointerException expected) {
88       // success
89     }
90   }
91 }
92