• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import java.util.ConcurrentModificationException;
30 import java.util.List;
31 import java.util.Map.Entry;
32 import java.util.RandomAccess;
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36 
37 /**
38  * Unit tests for {@code ArrayListMultimap}.
39  *
40  * @author Jared Levy
41  */
42 @GwtCompatible(emulated = true)
43 public class ArrayListMultimapTest extends TestCase {
44 
45   @GwtIncompatible // suite
suite()46   public static Test suite() {
47     TestSuite suite = new TestSuite();
48     suite.addTest(
49         ListMultimapTestSuiteBuilder.using(
50                 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 
create()75   protected ListMultimap<String, Integer> create() {
76     return ArrayListMultimap.create();
77   }
78 
79   /** Confirm that get() returns a List implementing RandomAccess. */
testGetRandomAccess()80   public void testGetRandomAccess() {
81     Multimap<String, Integer> multimap = create();
82     multimap.put("foo", 1);
83     multimap.put("foo", 3);
84     assertTrue(multimap.get("foo") instanceof RandomAccess);
85     assertTrue(multimap.get("bar") instanceof RandomAccess);
86   }
87 
88   /** Confirm that removeAll() returns a List implementing RandomAccess. */
testRemoveAllRandomAccess()89   public void testRemoveAllRandomAccess() {
90     Multimap<String, Integer> multimap = create();
91     multimap.put("foo", 1);
92     multimap.put("foo", 3);
93     assertTrue(multimap.removeAll("foo") instanceof RandomAccess);
94     assertTrue(multimap.removeAll("bar") instanceof RandomAccess);
95   }
96 
97   /** Confirm that replaceValues() returns a List implementing RandomAccess. */
testReplaceValuesRandomAccess()98   public void testReplaceValuesRandomAccess() {
99     Multimap<String, Integer> multimap = create();
100     multimap.put("foo", 1);
101     multimap.put("foo", 3);
102     assertTrue(multimap.replaceValues("foo", asList(2, 4)) instanceof RandomAccess);
103     assertTrue(multimap.replaceValues("bar", asList(2, 4)) instanceof RandomAccess);
104   }
105 
106   /** Test throwing ConcurrentModificationException when a sublist's ancestor's delegate changes. */
testSublistConcurrentModificationException()107   public void testSublistConcurrentModificationException() {
108     ListMultimap<String, Integer> multimap = create();
109     multimap.putAll("foo", asList(1, 2, 3, 4, 5));
110     List<Integer> list = multimap.get("foo");
111     assertThat(multimap.get("foo")).containsExactly(1, 2, 3, 4, 5).inOrder();
112     List<Integer> sublist = list.subList(0, 5);
113     assertThat(sublist).containsExactly(1, 2, 3, 4, 5).inOrder();
114 
115     sublist.clear();
116     assertTrue(sublist.isEmpty());
117     multimap.put("foo", 6);
118 
119     try {
120       sublist.isEmpty();
121       fail("Expected ConcurrentModificationException");
122     } catch (ConcurrentModificationException expected) {
123     }
124   }
125 
testCreateFromMultimap()126   public void testCreateFromMultimap() {
127     Multimap<String, Integer> multimap = create();
128     multimap.put("foo", 1);
129     multimap.put("foo", 3);
130     multimap.put("bar", 2);
131     ArrayListMultimap<String, Integer> copy = ArrayListMultimap.create(multimap);
132     assertEquals(multimap, copy);
133   }
134 
testCreate()135   public void testCreate() {
136     ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
137     assertEquals(3, multimap.expectedValuesPerKey);
138   }
139 
testCreateFromSizes()140   public void testCreateFromSizes() {
141     ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create(15, 20);
142     assertEquals(20, multimap.expectedValuesPerKey);
143   }
144 
testCreateFromIllegalSizes()145   public void testCreateFromIllegalSizes() {
146     try {
147       ArrayListMultimap.create(15, -2);
148       fail();
149     } catch (IllegalArgumentException expected) {
150     }
151 
152     try {
153       ArrayListMultimap.create(-15, 2);
154       fail();
155     } catch (IllegalArgumentException expected) {
156     }
157   }
158 
testCreateFromHashMultimap()159   public void testCreateFromHashMultimap() {
160     Multimap<String, Integer> original = HashMultimap.create();
161     ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create(original);
162     assertEquals(3, multimap.expectedValuesPerKey);
163   }
164 
testCreateFromArrayListMultimap()165   public void testCreateFromArrayListMultimap() {
166     ArrayListMultimap<String, Integer> original = ArrayListMultimap.create(15, 20);
167     ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create(original);
168     assertEquals(20, multimap.expectedValuesPerKey);
169   }
170 
testTrimToSize()171   public void testTrimToSize() {
172     ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
173     multimap.put("foo", 1);
174     multimap.put("foo", 2);
175     multimap.put("bar", 3);
176     multimap.trimToSize();
177     assertEquals(3, multimap.size());
178     assertThat(multimap.get("foo")).containsExactly(1, 2).inOrder();
179     assertThat(multimap.get("bar")).contains(3);
180   }
181 }
182