Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.collect;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static java.util.Arrays.asList;
     21 
     22 import com.google.common.annotations.GwtCompatible;
     23 import com.google.common.annotations.GwtIncompatible;
     24 import com.google.common.collect.testing.features.CollectionFeature;
     25 import com.google.common.collect.testing.features.CollectionSize;
     26 import com.google.common.collect.testing.features.MapFeature;
     27 import com.google.common.collect.testing.google.ListMultimapTestSuiteBuilder;
     28 import com.google.common.collect.testing.google.TestStringListMultimapGenerator;
     29 
     30 import junit.framework.Test;
     31 import junit.framework.TestCase;
     32 import junit.framework.TestSuite;
     33 
     34 import java.util.ConcurrentModificationException;
     35 import java.util.List;
     36 import java.util.Map.Entry;
     37 import java.util.RandomAccess;
     38 
     39 /**
     40  * Unit tests for {@code ArrayListMultimap}.
     41  *
     42  * @author Jared Levy
     43  */
     44 @GwtCompatible(emulated = true)
     45 public class ArrayListMultimapTest extends TestCase {
     46 
     47   @GwtIncompatible("suite")
     48   public static Test suite() {
     49     TestSuite suite = new TestSuite();
     50     suite.addTest(ListMultimapTestSuiteBuilder.using(new TestStringListMultimapGenerator() {
     51         @Override
     52         protected ListMultimap<String, String> create(Entry<String, String>[] entries) {
     53           ListMultimap<String, String> multimap = ArrayListMultimap.create();
     54           for (Entry<String, String> entry : entries) {
     55             multimap.put(entry.getKey(), entry.getValue());
     56           }
     57           return multimap;
     58         }
     59       })
     60       .named("ArrayListMultimap")
     61       .withFeatures(
     62           MapFeature.ALLOWS_NULL_KEYS,
     63           MapFeature.ALLOWS_NULL_VALUES,
     64           MapFeature.ALLOWS_ANY_NULL_QUERIES,
     65           MapFeature.GENERAL_PURPOSE,
     66           MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
     67           CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
     68           CollectionFeature.SERIALIZABLE,
     69           CollectionSize.ANY)
     70       .createTestSuite());
     71     suite.addTestSuite(ArrayListMultimapTest.class);
     72     return suite;
     73   }
     74 
     75   protected ListMultimap<String, Integer> create() {
     76     return ArrayListMultimap.create();
     77   }
     78 
     79   /**
     80    * Confirm that get() returns a List implementing RandomAccess.
     81    */
     82   public void testGetRandomAccess() {
     83     Multimap<String, Integer> multimap = create();
     84     multimap.put("foo", 1);
     85     multimap.put("foo", 3);
     86     assertTrue(multimap.get("foo") instanceof RandomAccess);
     87     assertTrue(multimap.get("bar") instanceof RandomAccess);
     88   }
     89 
     90   /**
     91    * Confirm that removeAll() returns a List implementing RandomAccess.
     92    */
     93   public void testRemoveAllRandomAccess() {
     94     Multimap<String, Integer> multimap = create();
     95     multimap.put("foo", 1);
     96     multimap.put("foo", 3);
     97     assertTrue(multimap.removeAll("foo") instanceof RandomAccess);
     98     assertTrue(multimap.removeAll("bar") instanceof RandomAccess);
     99   }
    100 
    101   /**
    102    * Confirm that replaceValues() returns a List implementing RandomAccess.
    103    */
    104   public void testReplaceValuesRandomAccess() {
    105     Multimap<String, Integer> multimap = create();
    106     multimap.put("foo", 1);
    107     multimap.put("foo", 3);
    108     assertTrue(multimap.replaceValues("foo", asList(2, 4))
    109         instanceof RandomAccess);
    110     assertTrue(multimap.replaceValues("bar", asList(2, 4))
    111         instanceof RandomAccess);
    112   }
    113 
    114   /**
    115    * Test throwing ConcurrentModificationException when a sublist's ancestor's
    116    * delegate changes.
    117    */
    118   public void testSublistConcurrentModificationException() {
    119     ListMultimap<String, Integer> multimap = create();
    120     multimap.putAll("foo", asList(1, 2, 3, 4, 5));
    121     List<Integer> list = multimap.get("foo");
    122     assertThat(multimap.get("foo")).has().exactly(1, 2, 3, 4, 5).inOrder();
    123     List<Integer> sublist = list.subList(0, 5);
    124     assertThat(sublist).has().exactly(1, 2, 3, 4, 5).inOrder();
    125 
    126     sublist.clear();
    127     assertTrue(sublist.isEmpty());
    128     multimap.put("foo", 6);
    129 
    130     try {
    131       sublist.isEmpty();
    132       fail("Expected ConcurrentModificationException");
    133     } catch (ConcurrentModificationException expected) {}
    134   }
    135 
    136   public void testCreateFromMultimap() {
    137     Multimap<String, Integer> multimap = create();
    138     multimap.put("foo", 1);
    139     multimap.put("foo", 3);
    140     multimap.put("bar", 2);
    141     ArrayListMultimap<String, Integer> copy
    142         = ArrayListMultimap.create(multimap);
    143     assertEquals(multimap, copy);
    144   }
    145 
    146   public void testCreate() {
    147     ArrayListMultimap<String, Integer> multimap
    148         = ArrayListMultimap.create();
    149     assertEquals(3, multimap.expectedValuesPerKey);
    150   }
    151 
    152   public void testCreateFromSizes() {
    153     ArrayListMultimap<String, Integer> multimap
    154         = ArrayListMultimap.create(15, 20);
    155     assertEquals(20, multimap.expectedValuesPerKey);
    156   }
    157 
    158   public void testCreateFromIllegalSizes() {
    159     try {
    160       ArrayListMultimap.create(15, -2);
    161       fail();
    162     } catch (IllegalArgumentException expected) {}
    163 
    164     try {
    165       ArrayListMultimap.create(-15, 2);
    166       fail();
    167     } catch (IllegalArgumentException expected) {}
    168   }
    169 
    170   public void testCreateFromHashMultimap() {
    171     Multimap<String, Integer> original = HashMultimap.create();
    172     ArrayListMultimap<String, Integer> multimap
    173         = ArrayListMultimap.create(original);
    174     assertEquals(3, multimap.expectedValuesPerKey);
    175   }
    176 
    177   public void testCreateFromArrayListMultimap() {
    178     ArrayListMultimap<String, Integer> original
    179         = ArrayListMultimap.create(15, 20);
    180     ArrayListMultimap<String, Integer> multimap
    181         = ArrayListMultimap.create(original);
    182     assertEquals(20, multimap.expectedValuesPerKey);
    183   }
    184 
    185   public void testTrimToSize() {
    186     ArrayListMultimap<String, Integer> multimap
    187         = ArrayListMultimap.create();
    188     multimap.put("foo", 1);
    189     multimap.put("foo", 2);
    190     multimap.put("bar", 3);
    191     multimap.trimToSize();
    192     assertEquals(3, multimap.size());
    193     assertThat(multimap.get("foo")).has().exactly(1, 2).inOrder();
    194     assertThat(multimap.get("bar")).has().item(3);
    195   }
    196 }
    197