View Javadoc
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  
24  import junit.framework.TestCase;
25  
26  import java.util.ConcurrentModificationException;
27  import java.util.List;
28  import java.util.RandomAccess;
29  
30  /**
31   * Unit tests for {@code ArrayListMultimap}.
32   *
33   * @author Jared Levy
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     * Confirm that get() returns a List implementing RandomAccess.
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     * Confirm that removeAll() returns a List implementing RandomAccess.
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     * Confirm that replaceValues() returns a List implementing RandomAccess.
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     * Test throwing ConcurrentModificationException when a sublist's ancestor's
79     * delegate changes.
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