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.MapFeature.ALLOWS_NULL_KEYS;
20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
21 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
22 import static org.truth0.Truth.ASSERT;
23 
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.collect.Multimap;
26 import com.google.common.collect.testing.Helpers;
27 import com.google.common.collect.testing.features.MapFeature;
28 
29 import java.util.Collection;
30 
31 /**
32  * Tester for {@link Multimap#putAll(Multimap)}.
33  *
34  * @author Louis Wasserman
35  */
36 @GwtCompatible
37 public class MultimapPutAllMultimapTester<K, V>
38     extends AbstractMultimapTester<K, V, Multimap<K, V>> {
39   @MapFeature.Require(absent = SUPPORTS_PUT)
testPutUnsupported()40   public void testPutUnsupported() {
41     try {
42       multimap().putAll(getSubjectGenerator().create(
43           Helpers.mapEntry(sampleKeys().e3, sampleValues().e3)));
44       fail("Expected UnsupportedOperationException");
45     } catch (UnsupportedOperationException expected) {}
46   }
47 
48   @MapFeature.Require(SUPPORTS_PUT)
testPutAllIntoEmpty()49   public void testPutAllIntoEmpty() {
50     Multimap<K, V> target = getSubjectGenerator().create();
51     assertEquals(!multimap().isEmpty(), target.putAll(multimap()));
52     assertEquals(multimap(), target);
53   }
54 
55   @MapFeature.Require(SUPPORTS_PUT)
testPutAll()56   public void testPutAll() {
57     Multimap<K, V> source = getSubjectGenerator().create(
58         Helpers.mapEntry(sampleKeys().e0, sampleValues().e3),
59         Helpers.mapEntry(sampleKeys().e3, sampleValues().e3));
60     assertTrue(multimap().putAll(source));
61     assertTrue(multimap().containsEntry(sampleKeys().e0, sampleValues().e3));
62     assertTrue(multimap().containsEntry(sampleKeys().e3, sampleValues().e3));
63   }
64 
65   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
testPutAllWithNullValue()66   public void testPutAllWithNullValue() {
67     Multimap<K, V> source = getSubjectGenerator().create(
68         Helpers.mapEntry(sampleKeys().e0, null));
69     assertTrue(multimap().putAll(source));
70     assertTrue(multimap().containsEntry(sampleKeys().e0, null));
71   }
72 
73   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
testPutAllWithNullKey()74   public void testPutAllWithNullKey() {
75     Multimap<K, V> source = getSubjectGenerator().create(
76         Helpers.mapEntry(null, sampleValues().e0));
77     assertTrue(multimap().putAll(source));
78     assertTrue(multimap().containsEntry(null, sampleValues().e0));
79   }
80 
81   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
testPutAllRejectsNullValue()82   public void testPutAllRejectsNullValue() {
83     Multimap<K, V> source = getSubjectGenerator().create(
84         Helpers.mapEntry(sampleKeys().e0, null));
85     try {
86       multimap().putAll(source);
87       fail("Expected NullPointerException");
88     } catch (NullPointerException expected) {}
89     expectUnchanged();
90   }
91 
92   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
testPutAllRejectsNullKey()93   public void testPutAllRejectsNullKey() {
94     Multimap<K, V> source = getSubjectGenerator().create(
95         Helpers.mapEntry(null, sampleValues().e0));
96     try {
97       multimap().putAll(source);
98       fail("Expected NullPointerException");
99     } catch (NullPointerException expected) {}
100     expectUnchanged();
101   }
102 
103   @MapFeature.Require(SUPPORTS_PUT)
testPutAllPropagatesToGet()104   public void testPutAllPropagatesToGet() {
105     Multimap<K, V> source = getSubjectGenerator().create(
106         Helpers.mapEntry(sampleKeys().e0, sampleValues().e3),
107         Helpers.mapEntry(sampleKeys().e3, sampleValues().e3));
108     Collection<V> getCollection = multimap().get(sampleKeys().e0);
109     int getCollectionSize = getCollection.size();
110     assertTrue(multimap().putAll(source));
111     assertEquals(getCollectionSize + 1, getCollection.size());
112     ASSERT.that(getCollection).has().allOf(sampleValues().e3);
113   }
114 }
115