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