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.Helpers;
024import com.google.common.collect.testing.features.CollectionSize;
025import com.google.common.collect.testing.features.MapFeature;
026import java.util.List;
027import java.util.Map.Entry;
028
029/**
030 * Testers for {@link ListMultimap#put(Object, Object)}.
031 *
032 * @author Louis Wasserman
033 */
034@GwtCompatible
035public class ListMultimapPutTester<K, V> extends AbstractListMultimapTester<K, V> {
036  // MultimapPutTester tests non-duplicate values, but ignores ordering
037
038  @MapFeature.Require(SUPPORTS_PUT)
039  public void testPutAddsValueAtEnd() {
040    for (K key : sampleKeys()) {
041      for (V value : sampleValues()) {
042        resetContainer();
043
044        List<V> values = multimap().get(key);
045        List<V> expectedValues = Helpers.copyToList(values);
046
047        assertTrue(multimap().put(key, value));
048        expectedValues.add(value);
049
050        assertGet(key, expectedValues);
051        assertEquals(value, values.get(values.size() - 1));
052      }
053    }
054  }
055
056  @MapFeature.Require(SUPPORTS_PUT)
057  @CollectionSize.Require(absent = ZERO)
058  public void testPutDuplicateValue() {
059    List<Entry<K, V>> entries = copyToList(multimap().entries());
060
061    for (Entry<K, V> entry : entries) {
062      resetContainer();
063
064      K k = entry.getKey();
065      V v = entry.getValue();
066
067      List<V> values = multimap().get(k);
068      List<V> expectedValues = copyToList(values);
069
070      assertTrue(multimap().put(k, v));
071      expectedValues.add(v);
072      assertGet(k, expectedValues);
073      assertEquals(v, values.get(values.size() - 1));
074    }
075  }
076}