View Javadoc
1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.Helpers.copyToList;
18  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
19  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.collect.ListMultimap;
23  import com.google.common.collect.testing.Helpers;
24  import com.google.common.collect.testing.features.CollectionSize;
25  import com.google.common.collect.testing.features.MapFeature;
26  import java.util.List;
27  import java.util.Map.Entry;
28  
29  /**
30   * Testers for {@link ListMultimap#put(Object, Object)}.
31   *
32   * @author Louis Wasserman
33   */
34  @GwtCompatible
35  public class ListMultimapPutTester<K, V> extends AbstractListMultimapTester<K, V> {
36    // MultimapPutTester tests non-duplicate values, but ignores ordering
37  
38    @MapFeature.Require(SUPPORTS_PUT)
39    public void testPutAddsValueAtEnd() {
40      for (K key : sampleKeys()) {
41        for (V value : sampleValues()) {
42          resetContainer();
43  
44          List<V> values = multimap().get(key);
45          List<V> expectedValues = Helpers.copyToList(values);
46  
47          assertTrue(multimap().put(key, value));
48          expectedValues.add(value);
49  
50          assertGet(key, expectedValues);
51          assertEquals(value, values.get(values.size() - 1));
52        }
53      }
54    }
55  
56    @MapFeature.Require(SUPPORTS_PUT)
57    @CollectionSize.Require(absent = ZERO)
58    public void testPutDuplicateValue() {
59      List<Entry<K, V>> entries = copyToList(multimap().entries());
60  
61      for (Entry<K, V> entry : entries) {
62        resetContainer();
63  
64        K k = entry.getKey();
65        V v = entry.getValue();
66  
67        List<V> values = multimap().get(k);
68        List<V> expectedValues = copyToList(values);
69  
70        assertTrue(multimap().put(k, v));
71        expectedValues.add(v);
72        assertGet(k, expectedValues);
73        assertEquals(v, values.get(values.size() - 1));
74      }
75    }
76  }