ListMultimapPutTester.java revision 3c77433663281544363151bf284b0240dfd22a42
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
15package com.google.common.collect.testing.google;
16
17import static com.google.common.collect.testing.Helpers.copyToList;
18import static com.google.common.collect.testing.features.CollectionSize.ZERO;
19import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
20
21import com.google.common.annotations.GwtCompatible;
22import com.google.common.collect.ListMultimap;
23import com.google.common.collect.testing.Helpers;
24import com.google.common.collect.testing.features.CollectionSize;
25import com.google.common.collect.testing.features.MapFeature;
26
27import java.util.List;
28import java.util.Map.Entry;
29
30/**
31 * Testers for {@link ListMultimap#put(Object, Object)}.
32 *
33 * @author Louis Wasserman
34 */
35@GwtCompatible
36public class ListMultimapPutTester<K, V> extends AbstractListMultimapTester<K, V> {
37  // MultimapPutTester tests non-duplicate values, but ignores ordering
38
39  @MapFeature.Require(SUPPORTS_PUT)
40  public void testPutAddsValueAtEnd() {
41    for (K key : sampleKeys()) {
42      for (V value : sampleValues()) {
43        resetContainer();
44
45        List<V> values = multimap().get(key);
46        List<V> expectedValues = Helpers.copyToList(values);
47
48        multimap().put(key, value);
49        expectedValues.add(value);
50
51        assertGet(key, expectedValues);
52        assertEquals(value, values.get(values.size() - 1));
53      }
54    }
55  }
56
57  @MapFeature.Require(SUPPORTS_PUT)
58  @CollectionSize.Require(absent = ZERO)
59  public void testPutDuplicateValue() {
60    List<Entry<K, V>> entries = copyToList(multimap().entries());
61
62    for (Entry<K, V> entry : entries) {
63      resetContainer();
64
65      K k = entry.getKey();
66      V v = entry.getValue();
67
68      List<V> values = multimap().get(k);
69      List<V> expectedValues = copyToList(values);
70
71      assertTrue(multimap().put(k, v));
72      expectedValues.add(v);
73      assertGet(k, expectedValues);
74      assertEquals(v, values.get(values.size() - 1));
75    }
76  }
77}
78