1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
24 import junit.framework.TestCase;
25
26 import java.util.ConcurrentModificationException;
27 import java.util.List;
28 import java.util.RandomAccess;
29
30
31
32
33
34
35 @GwtCompatible(emulated = true)
36 public class ArrayListMultimapTest extends TestCase {
37
38 protected ListMultimap<String, Integer> create() {
39 return ArrayListMultimap.create();
40 }
41
42
43
44
45 public void testGetRandomAccess() {
46 Multimap<String, Integer> multimap = create();
47 multimap.put("foo", 1);
48 multimap.put("foo", 3);
49 assertTrue(multimap.get("foo") instanceof RandomAccess);
50 assertTrue(multimap.get("bar") instanceof RandomAccess);
51 }
52
53
54
55
56 public void testRemoveAllRandomAccess() {
57 Multimap<String, Integer> multimap = create();
58 multimap.put("foo", 1);
59 multimap.put("foo", 3);
60 assertTrue(multimap.removeAll("foo") instanceof RandomAccess);
61 assertTrue(multimap.removeAll("bar") instanceof RandomAccess);
62 }
63
64
65
66
67 public void testReplaceValuesRandomAccess() {
68 Multimap<String, Integer> multimap = create();
69 multimap.put("foo", 1);
70 multimap.put("foo", 3);
71 assertTrue(multimap.replaceValues("foo", asList(2, 4))
72 instanceof RandomAccess);
73 assertTrue(multimap.replaceValues("bar", asList(2, 4))
74 instanceof RandomAccess);
75 }
76
77
78
79
80
81 public void testSublistConcurrentModificationException() {
82 ListMultimap<String, Integer> multimap = create();
83 multimap.putAll("foo", asList(1, 2, 3, 4, 5));
84 List<Integer> list = multimap.get("foo");
85 assertThat(multimap.get("foo")).has().exactly(1, 2, 3, 4, 5).inOrder();
86 List<Integer> sublist = list.subList(0, 5);
87 assertThat(sublist).has().exactly(1, 2, 3, 4, 5).inOrder();
88
89 sublist.clear();
90 assertTrue(sublist.isEmpty());
91 multimap.put("foo", 6);
92
93 try {
94 sublist.isEmpty();
95 fail("Expected ConcurrentModificationException");
96 } catch (ConcurrentModificationException expected) {}
97 }
98
99 public void testCreateFromMultimap() {
100 Multimap<String, Integer> multimap = create();
101 multimap.put("foo", 1);
102 multimap.put("foo", 3);
103 multimap.put("bar", 2);
104 ArrayListMultimap<String, Integer> copy
105 = ArrayListMultimap.create(multimap);
106 assertEquals(multimap, copy);
107 }
108
109 public void testCreate() {
110 ArrayListMultimap<String, Integer> multimap
111 = ArrayListMultimap.create();
112 assertEquals(3, multimap.expectedValuesPerKey);
113 }
114
115 public void testCreateFromSizes() {
116 ArrayListMultimap<String, Integer> multimap
117 = ArrayListMultimap.create(15, 20);
118 assertEquals(20, multimap.expectedValuesPerKey);
119 }
120
121 public void testCreateFromIllegalSizes() {
122 try {
123 ArrayListMultimap.create(15, -2);
124 fail();
125 } catch (IllegalArgumentException expected) {}
126
127 try {
128 ArrayListMultimap.create(-15, 2);
129 fail();
130 } catch (IllegalArgumentException expected) {}
131 }
132
133 public void testCreateFromHashMultimap() {
134 Multimap<String, Integer> original = HashMultimap.create();
135 ArrayListMultimap<String, Integer> multimap
136 = ArrayListMultimap.create(original);
137 assertEquals(3, multimap.expectedValuesPerKey);
138 }
139
140 public void testCreateFromArrayListMultimap() {
141 ArrayListMultimap<String, Integer> original
142 = ArrayListMultimap.create(15, 20);
143 ArrayListMultimap<String, Integer> multimap
144 = ArrayListMultimap.create(original);
145 assertEquals(20, multimap.expectedValuesPerKey);
146 }
147
148 public void testTrimToSize() {
149 ArrayListMultimap<String, Integer> multimap
150 = ArrayListMultimap.create();
151 multimap.put("foo", 1);
152 multimap.put("foo", 2);
153 multimap.put("bar", 3);
154 multimap.trimToSize();
155 assertEquals(3, multimap.size());
156 assertThat(multimap.get("foo")).has().exactly(1, 2).inOrder();
157 assertThat(multimap.get("bar")).has().item(3);
158 }
159 }
160