2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.UnmodifiableIterator;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.List;
21 import org.junit.Test;
23 public class UnmodifiableCollectionTest {
26 public void testUnmodifiableCollection() {
27 final List<Integer> immutableTestList = ImmutableList.<Integer>builder()
34 final Collection<Integer> testUnmodifiableCollection = UnmodifiableCollection.create(immutableTestList);
35 assertNotNull(testUnmodifiableCollection);
37 // Note: this cannot be ImmutableList, because UnmodifiableCollection does recognize it and returns it as is,
38 // without converting it to an UnmodifiableCollection -- which is not what we want.
39 final List<Integer> testList = Arrays.asList(1, 2, 3, 4, 5);
40 final Collection<Integer> testUnmodifiableCollection2 = UnmodifiableCollection.create(testList);
42 final Iterator<Integer> iterator = testUnmodifiableCollection2.iterator();
43 assertNotNull(iterator);
44 assertTrue(iterator instanceof UnmodifiableIterator);
46 assertEquals(5, testUnmodifiableCollection2.size());
48 assertFalse(testUnmodifiableCollection2.isEmpty());
50 assertTrue(testUnmodifiableCollection2.contains(1));
52 final Object[] objectArray = testUnmodifiableCollection2.toArray();
53 assertNotNull(objectArray);
54 assertEquals(5, objectArray.length);
56 final Integer[] integerArray = testUnmodifiableCollection2.toArray(
57 new Integer[testUnmodifiableCollection2.size()]);
58 assertNotNull(integerArray);
59 assertEquals(5, integerArray.length);
61 assertTrue(testUnmodifiableCollection2.containsAll(testUnmodifiableCollection));
63 assertEquals("UnmodifiableCollection{" + testList + "}", testUnmodifiableCollection2.toString());