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