aboutsummaryrefslogtreecommitdiff
path: root/guava-testlib/src/com/google/common/collect/testing/google/UnmodifiableCollectionTests.java
blob: 8cbd7ea9a766c9f7829cf3aa4898e91b4177dc41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * Copyright (C) 2007 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect.testing.google;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterators;
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

/**
 * A series of tests that support asserting that collections cannot be modified, either through
 * direct or indirect means.
 *
 * @author Robert Konigsberg
 */
@GwtCompatible
public class UnmodifiableCollectionTests {

  public static void assertMapEntryIsUnmodifiable(Entry<?, ?> entry) {
    try {
      entry.setValue(null);
      fail("setValue on unmodifiable Map.Entry succeeded");
    } catch (UnsupportedOperationException expected) {
    }
  }

  /**
   * Verifies that an Iterator is unmodifiable.
   *
   * <p>This test only works with iterators that iterate over a finite set.
   */
  public static void assertIteratorIsUnmodifiable(Iterator<?> iterator) {
    while (iterator.hasNext()) {
      iterator.next();
      try {
        iterator.remove();
        fail("Remove on unmodifiable iterator succeeded");
      } catch (UnsupportedOperationException expected) {
      }
    }
  }

  /**
   * Asserts that two iterators contain elements in tandem.
   *
   * <p>This test only works with iterators that iterate over a finite set.
   */
  public static void assertIteratorsInOrder(
      Iterator<?> expectedIterator, Iterator<?> actualIterator) {
    int i = 0;
    while (expectedIterator.hasNext()) {
      Object expected = expectedIterator.next();

      assertTrue(
          "index " + i + " expected <" + expected + "., actual is exhausted",
          actualIterator.hasNext());

      Object actual = actualIterator.next();
      assertEquals("index " + i, expected, actual);
      i++;
    }
    if (actualIterator.hasNext()) {
      fail("index " + i + ", expected is exhausted, actual <" + actualIterator.next() + ">");
    }
  }

  /**
   * Verifies that a collection is immutable.
   *
   * <p>A collection is considered immutable if:
   *
   * <ol>
   *   <li>All its mutation methods result in UnsupportedOperationException, and do not change the
   *       underlying contents.
   *   <li>All methods that return objects that can indirectly mutate the collection throw
   *       UnsupportedOperationException when those mutators are called.
   * </ol>
   *
   * @param collection the presumed-immutable collection
   * @param sampleElement an element of the same type as that contained by {@code collection}.
   *     {@code collection} may or may not have {@code sampleElement} as a member.
   */
  public static <E> void assertCollectionIsUnmodifiable(Collection<E> collection, E sampleElement) {
    Collection<E> siblingCollection = new ArrayList<>();
    siblingCollection.add(sampleElement);

    Collection<E> copy = new ArrayList<>();
    // Avoid copy.addAll(collection), which runs afoul of an Android bug in older versions:
    // http://b.android.com/72073 http://r.android.com/98929
    Iterators.addAll(copy, collection.iterator());

    try {
      collection.add(sampleElement);
      fail("add succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }

    assertCollectionsAreEquivalent(copy, collection);

    try {
      collection.addAll(siblingCollection);
      fail("addAll succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }
    assertCollectionsAreEquivalent(copy, collection);

    try {
      collection.clear();
      fail("clear succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }
    assertCollectionsAreEquivalent(copy, collection);

    assertIteratorIsUnmodifiable(collection.iterator());
    assertCollectionsAreEquivalent(copy, collection);

    try {
      collection.remove(sampleElement);
      fail("remove succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }
    assertCollectionsAreEquivalent(copy, collection);

    try {
      collection.removeAll(siblingCollection);
      fail("removeAll succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }
    assertCollectionsAreEquivalent(copy, collection);

    try {
      collection.retainAll(siblingCollection);
      fail("retainAll succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }
    assertCollectionsAreEquivalent(copy, collection);
  }

  /**
   * Verifies that a set is immutable.
   *
   * <p>A set is considered immutable if:
   *
   * <ol>
   *   <li>All its mutation methods result in UnsupportedOperationException, and do not change the
   *       underlying contents.
   *   <li>All methods that return objects that can indirectly mutate the set throw
   *       UnsupportedOperationException when those mutators are called.
   * </ol>
   *
   * @param set the presumed-immutable set
   * @param sampleElement an element of the same type as that contained by {@code set}. {@code set}
   *     may or may not have {@code sampleElement} as a member.
   */
  public static <E> void assertSetIsUnmodifiable(Set<E> set, E sampleElement) {
    assertCollectionIsUnmodifiable(set, sampleElement);
  }

  /**
   * Verifies that a multiset is immutable.
   *
   * <p>A multiset is considered immutable if:
   *
   * <ol>
   *   <li>All its mutation methods result in UnsupportedOperationException, and do not change the
   *       underlying contents.
   *   <li>All methods that return objects that can indirectly mutate the multiset throw
   *       UnsupportedOperationException when those mutators are called.
   * </ol>
   *
   * @param multiset the presumed-immutable multiset
   * @param sampleElement an element of the same type as that contained by {@code multiset}. {@code
   *     multiset} may or may not have {@code sampleElement} as a member.
   */
  public static <E> void assertMultisetIsUnmodifiable(Multiset<E> multiset, E sampleElement) {
    Multiset<E> copy = LinkedHashMultiset.create(multiset);
    assertCollectionsAreEquivalent(multiset, copy);

    // Multiset is a collection, so we can use all those tests.
    assertCollectionIsUnmodifiable(multiset, sampleElement);

    assertCollectionsAreEquivalent(multiset, copy);

    try {
      multiset.add(sampleElement, 2);
      fail("add(Object, int) succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }
    assertCollectionsAreEquivalent(multiset, copy);

    try {
      multiset.remove(sampleElement, 2);
      fail("remove(Object, int) succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }
    assertCollectionsAreEquivalent(multiset, copy);

    try {
      multiset.removeIf(x -> false);
      fail("removeIf(Predicate) succeeded on unmodifiable collection");
    } catch (UnsupportedOperationException expected) {
    }
    assertCollectionsAreEquivalent(multiset, copy);

    assertSetIsUnmodifiable(multiset.elementSet(), sampleElement);
    assertCollectionsAreEquivalent(multiset, copy);

    assertSetIsUnmodifiable(
        multiset.entrySet(),
        new Multiset.Entry<E>() {
          @Override
          public int getCount() {
            return 1;
          }

          @Override
          public E getElement() {
            return sampleElement;
          }
        });
    assertCollectionsAreEquivalent(multiset, copy);
  }

  /**
   * Verifies that a multimap is immutable.
   *
   * <p>A multimap is considered immutable if:
   *
   * <ol>
   *   <li>All its mutation methods result in UnsupportedOperationException, and do not change the
   *       underlying contents.
   *   <li>All methods that return objects that can indirectly mutate the multimap throw
   *       UnsupportedOperationException when those mutators
   * </ol>
   *
   * @param multimap the presumed-immutable multimap
   * @param sampleKey a key of the same type as that contained by {@code multimap}. {@code multimap}
   *     may or may not have {@code sampleKey} as a key.
   * @param sampleValue a key of the same type as that contained by {@code multimap}. {@code
   *     multimap} may or may not have {@code sampleValue} as a key.
   */
  public static <K, V> void assertMultimapIsUnmodifiable(
      Multimap<K, V> multimap, K sampleKey, V sampleValue) {
    List<Entry<K, V>> originalEntries =
        Collections.unmodifiableList(Lists.newArrayList(multimap.entries()));

    assertMultimapRemainsUnmodified(multimap, originalEntries);

    Collection<V> sampleValueAsCollection = Collections.singleton(sampleValue);

    // Test #clear()
    try {
      multimap.clear();
      fail("clear succeeded on unmodifiable multimap");
    } catch (UnsupportedOperationException expected) {
    }

    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test asMap().entrySet()
    assertSetIsUnmodifiable(
        multimap.asMap().entrySet(), Maps.immutableEntry(sampleKey, sampleValueAsCollection));

    // Test #values()

    assertMultimapRemainsUnmodified(multimap, originalEntries);
    if (!multimap.isEmpty()) {
      Collection<V> values = multimap.asMap().entrySet().iterator().next().getValue();

      assertCollectionIsUnmodifiable(values, sampleValue);
    }

    // Test #entries()
    assertCollectionIsUnmodifiable(multimap.entries(), Maps.immutableEntry(sampleKey, sampleValue));
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Iterate over every element in the entry set
    for (Entry<K, V> entry : multimap.entries()) {
      assertMapEntryIsUnmodifiable(entry);
    }
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #keys()
    assertMultisetIsUnmodifiable(multimap.keys(), sampleKey);
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #keySet()
    assertSetIsUnmodifiable(multimap.keySet(), sampleKey);
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #get()
    if (!multimap.isEmpty()) {
      K key = multimap.keySet().iterator().next();
      assertCollectionIsUnmodifiable(multimap.get(key), sampleValue);
      assertMultimapRemainsUnmodified(multimap, originalEntries);
    }

    // Test #put()
    try {
      multimap.put(sampleKey, sampleValue);
      fail("put succeeded on unmodifiable multimap");
    } catch (UnsupportedOperationException expected) {
    }
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #putAll(K, Collection<V>)
    try {
      multimap.putAll(sampleKey, sampleValueAsCollection);
      fail("putAll(K, Iterable) succeeded on unmodifiable multimap");
    } catch (UnsupportedOperationException expected) {
    }
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #putAll(Multimap<K, V>)
    Multimap<K, V> multimap2 = ArrayListMultimap.create();
    multimap2.put(sampleKey, sampleValue);
    try {
      multimap.putAll(multimap2);
      fail("putAll(Multimap<K, V>) succeeded on unmodifiable multimap");
    } catch (UnsupportedOperationException expected) {
    }
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #remove()
    try {
      multimap.remove(sampleKey, sampleValue);
      fail("remove succeeded on unmodifiable multimap");
    } catch (UnsupportedOperationException expected) {
    }
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #removeAll()
    try {
      multimap.removeAll(sampleKey);
      fail("removeAll succeeded on unmodifiable multimap");
    } catch (UnsupportedOperationException expected) {
    }
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #replaceValues()
    try {
      multimap.replaceValues(sampleKey, sampleValueAsCollection);
      fail("replaceValues succeeded on unmodifiable multimap");
    } catch (UnsupportedOperationException expected) {
    }
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    // Test #asMap()
    try {
      multimap.asMap().remove(sampleKey);
      fail("asMap().remove() succeeded on unmodifiable multimap");
    } catch (UnsupportedOperationException expected) {
    }
    assertMultimapRemainsUnmodified(multimap, originalEntries);

    if (!multimap.isEmpty()) {
      K presentKey = multimap.keySet().iterator().next();
      try {
        multimap.asMap().get(presentKey).remove(sampleValue);
        fail("asMap().get().remove() succeeded on unmodifiable multimap");
      } catch (UnsupportedOperationException expected) {
      }
      assertMultimapRemainsUnmodified(multimap, originalEntries);

      try {
        multimap.asMap().values().iterator().next().remove(sampleValue);
        fail("asMap().values().iterator().next().remove() succeeded on unmodifiable multimap");
      } catch (UnsupportedOperationException expected) {
      }

      try {
        ((Collection<?>) multimap.asMap().values().toArray()[0]).clear();
        fail("asMap().values().toArray()[0].clear() succeeded on unmodifiable multimap");
      } catch (UnsupportedOperationException expected) {
      }
    }

    assertCollectionIsUnmodifiable(multimap.values(), sampleValue);
    assertMultimapRemainsUnmodified(multimap, originalEntries);
  }

  private static <E> void assertCollectionsAreEquivalent(
      Collection<E> expected, Collection<E> actual) {
    assertIteratorsInOrder(expected.iterator(), actual.iterator());
  }

  private static <K, V> void assertMultimapRemainsUnmodified(
      Multimap<K, V> expected, List<Entry<K, V>> actual) {
    assertIteratorsInOrder(expected.entries().iterator(), actual.iterator());
  }
}