af7d65beb9e349ea22b38354a0885b5dfbb05ecc
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / UnmodifiableCollectionTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.util;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.Lists;
18 import com.google.common.collect.UnmodifiableIterator;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22 import org.junit.Test;
23
24 public class UnmodifiableCollectionTest {
25
26     @Test
27     public void testUnmodifiableCollection() {
28         final List<Integer> immutableTestList = ImmutableList.<Integer>builder()
29                 .add(1)
30                 .add(2)
31                 .add(3)
32                 .add(4)
33                 .add(5).build();
34
35         final Collection<Integer> testUnmodifiableCollection = UnmodifiableCollection.create(immutableTestList);
36         assertNotNull(testUnmodifiableCollection);
37
38         final List<Integer> testList = Lists.newArrayList();
39         testList.add(1);
40         testList.add(2);
41         testList.add(3);
42         testList.add(4);
43         testList.add(5);
44
45         final Collection<Integer> testUnmodifiableCollection2 = UnmodifiableCollection.create(testList);
46
47         final Iterator<Integer> iterator = testUnmodifiableCollection2.iterator();
48         assertNotNull(iterator);
49         assertTrue(iterator instanceof UnmodifiableIterator);
50
51         assertEquals(5, testUnmodifiableCollection2.size());
52
53         assertFalse(testUnmodifiableCollection2.isEmpty());
54
55         assertTrue(testUnmodifiableCollection2.contains(1));
56
57         final Object[] objectArray = testUnmodifiableCollection2.toArray();
58         assertNotNull(objectArray);
59         assertEquals(5, objectArray.length);
60
61         final Integer[] integerArray = testUnmodifiableCollection2.toArray(
62                 new Integer[testUnmodifiableCollection2.size()]);
63         assertNotNull(integerArray);
64         assertEquals(5, integerArray.length);
65
66         assertTrue(testUnmodifiableCollection2.containsAll(testUnmodifiableCollection));
67
68         assertEquals("UnmodifiableCollection{" + testList + "}", testUnmodifiableCollection2.toString());
69     }
70 }