Cleanup use of Guava library
[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 package org.opendaylight.yangtools.util;
9
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;
14
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;
22
23 public class UnmodifiableCollectionTest {
24
25     @Test
26     public void testUnmodifiableCollection() {
27         final List<Integer> immutableTestList = ImmutableList.<Integer>builder()
28                 .add(1)
29                 .add(2)
30                 .add(3)
31                 .add(4)
32                 .add(5).build();
33
34         final Collection<Integer> testUnmodifiableCollection = UnmodifiableCollection.create(immutableTestList);
35         assertNotNull(testUnmodifiableCollection);
36
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);
41
42         final Iterator<Integer> iterator = testUnmodifiableCollection2.iterator();
43         assertNotNull(iterator);
44         assertTrue(iterator instanceof UnmodifiableIterator);
45
46         assertEquals(5, testUnmodifiableCollection2.size());
47
48         assertFalse(testUnmodifiableCollection2.isEmpty());
49
50         assertTrue(testUnmodifiableCollection2.contains(1));
51
52         final Object[] objectArray = testUnmodifiableCollection2.toArray();
53         assertNotNull(objectArray);
54         assertEquals(5, objectArray.length);
55
56         final Integer[] integerArray = testUnmodifiableCollection2.toArray(
57                 new Integer[testUnmodifiableCollection2.size()]);
58         assertNotNull(integerArray);
59         assertEquals(5, integerArray.length);
60
61         assertTrue(testUnmodifiableCollection2.containsAll(testUnmodifiableCollection));
62
63         assertEquals("UnmodifiableCollection{" + testList + "}", testUnmodifiableCollection2.toString());
64     }
65 }