001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.collect.testing.google;
016
017import static com.google.common.collect.testing.Helpers.copyToList;
018import static com.google.common.collect.testing.features.CollectionSize.ZERO;
019import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.collect.ListMultimap;
023import com.google.common.collect.testing.features.CollectionSize;
024import com.google.common.collect.testing.features.MapFeature;
025import java.util.List;
026import java.util.Map.Entry;
027
028/**
029 * Testers for {@link ListMultimap#put(Object, Object)}.
030 *
031 * @author Louis Wasserman
032 */
033@GwtCompatible
034public class ListMultimapPutTester<K, V> extends AbstractListMultimapTester<K, V> {
035  // MultimapPutTester tests non-duplicate values, but ignores ordering
036
037  @MapFeature.Require(SUPPORTS_PUT)
038  public void testPutAddsValueAtEnd() {
039    for (K key : sampleKeys()) {
040      for (V value : sampleValues()) {
041        resetContainer();
042
043        List<V> values = multimap().get(key);
044        List<V> expectedValues = copyToList(values);
045
046        assertTrue(multimap().put(key, value));
047        expectedValues.add(value);
048
049        assertGet(key, expectedValues);
050        assertEquals(value, values.get(values.size() - 1));
051      }
052    }
053  }
054
055  @MapFeature.Require(SUPPORTS_PUT)
056  @CollectionSize.Require(absent = ZERO)
057  public void testPutDuplicateValue() {
058    List<Entry<K, V>> entries = copyToList(multimap().entries());
059
060    for (Entry<K, V> entry : entries) {
061      resetContainer();
062
063      K k = entry.getKey();
064      V v = entry.getValue();
065
066      List<V> values = multimap().get(k);
067      List<V> expectedValues = copyToList(values);
068
069      assertTrue(multimap().put(k, v));
070      expectedValues.add(v);
071      assertGet(k, expectedValues);
072      assertEquals(v, values.get(values.size() - 1));
073    }
074  }
075}