• 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 java.util.Arrays.asList;
20 import static org.junit.contrib.truth.Truth.ASSERT;
21 
22 import com.google.common.annotations.GwtCompatible;
23 
24 import java.util.ConcurrentModificationException;
25 import java.util.List;
26 import java.util.RandomAccess;
27 
28 /**
29  * Unit tests for {@code ArrayListMultimap}.
30  *
31  * @author Jared Levy
32  */
33 @GwtCompatible
34 public class ArrayListMultimapTest extends AbstractListMultimapTest {
35 
create()36   @Override protected ListMultimap<String, Integer> create() {
37     return ArrayListMultimap.create();
38   }
39 
40   /**
41    * Confirm that get() returns a List implementing RandomAccess.
42    */
testGetRandomAccess()43   public void testGetRandomAccess() {
44     Multimap<String, Integer> multimap = create();
45     multimap.put("foo", 1);
46     multimap.put("foo", 3);
47     assertTrue(multimap.get("foo") instanceof RandomAccess);
48     assertTrue(multimap.get("bar") instanceof RandomAccess);
49   }
50 
51   /**
52    * Confirm that removeAll() returns a List implementing RandomAccess.
53    */
testRemoveAllRandomAccess()54   public void testRemoveAllRandomAccess() {
55     Multimap<String, Integer> multimap = create();
56     multimap.put("foo", 1);
57     multimap.put("foo", 3);
58     assertTrue(multimap.removeAll("foo") instanceof RandomAccess);
59     assertTrue(multimap.removeAll("bar") instanceof RandomAccess);
60   }
61 
62   /**
63    * Confirm that replaceValues() returns a List implementing RandomAccess.
64    */
testReplaceValuesRandomAccess()65   public void testReplaceValuesRandomAccess() {
66     Multimap<String, Integer> multimap = create();
67     multimap.put("foo", 1);
68     multimap.put("foo", 3);
69     assertTrue(multimap.replaceValues("foo", asList(2, 4))
70         instanceof RandomAccess);
71     assertTrue(multimap.replaceValues("bar", asList(2, 4))
72         instanceof RandomAccess);
73   }
74 
75   /**
76    * Test throwing ConcurrentModificationException when a sublist's ancestor's
77    * delegate changes.
78    */
testSublistConcurrentModificationException()79   public void testSublistConcurrentModificationException() {
80     ListMultimap<String, Integer> multimap = create();
81     multimap.putAll("foo", asList(1, 2, 3, 4, 5));
82     List<Integer> list = multimap.get("foo");
83     ASSERT.that(multimap.get("foo")).hasContentsInOrder(1, 2, 3, 4, 5);
84     List<Integer> sublist = list.subList(0, 5);
85     ASSERT.that(sublist).hasContentsInOrder(1, 2, 3, 4, 5);
86 
87     sublist.clear();
88     assertTrue(sublist.isEmpty());
89     multimap.put("foo", 6);
90 
91     try {
92       sublist.isEmpty();
93       fail("Expected ConcurrentModificationException");
94     } catch (ConcurrentModificationException expected) {}
95   }
96 
testCreateFromMultimap()97   public void testCreateFromMultimap() {
98     Multimap<String, Integer> multimap = createSample();
99     ArrayListMultimap<String, Integer> copy
100         = ArrayListMultimap.create(multimap);
101     assertEquals(multimap, copy);
102   }
103 
testCreate()104   public void testCreate() {
105     ArrayListMultimap<String, Integer> multimap
106         = ArrayListMultimap.create();
107     assertEquals(10, multimap.expectedValuesPerKey);
108   }
109 
testCreateFromSizes()110   public void testCreateFromSizes() {
111     ArrayListMultimap<String, Integer> multimap
112         = ArrayListMultimap.create(15, 20);
113     assertEquals(20, multimap.expectedValuesPerKey);
114   }
115 
testCreateFromIllegalSizes()116   public void testCreateFromIllegalSizes() {
117     try {
118       ArrayListMultimap.create(15, -2);
119       fail();
120     } catch (IllegalArgumentException expected) {}
121 
122     try {
123       ArrayListMultimap.create(-15, 2);
124       fail();
125     } catch (IllegalArgumentException expected) {}
126   }
127 
testCreateFromHashMultimap()128   public void testCreateFromHashMultimap() {
129     Multimap<String, Integer> original = HashMultimap.create();
130     ArrayListMultimap<String, Integer> multimap
131         = ArrayListMultimap.create(original);
132     assertEquals(10, multimap.expectedValuesPerKey);
133   }
134 
testCreateFromArrayListMultimap()135   public void testCreateFromArrayListMultimap() {
136     ArrayListMultimap<String, Integer> original
137         = ArrayListMultimap.create(15, 20);
138     ArrayListMultimap<String, Integer> multimap
139         = ArrayListMultimap.create(original);
140     assertEquals(20, multimap.expectedValuesPerKey);
141   }
142 
testTrimToSize()143   public void testTrimToSize() {
144     ArrayListMultimap<String, Integer> multimap
145         = ArrayListMultimap.create();
146     multimap.put("foo", 1);
147     multimap.put("foo", 2);
148     multimap.put("bar", 3);
149     multimap.trimToSize();
150     assertEquals(3, multimap.size());
151     ASSERT.that(multimap.get("foo")).hasContentsInOrder(1, 2);
152     ASSERT.that(multimap.get("bar")).hasContentsInOrder(3);
153   }
154 }
155