b67d751b20a74d2692db937a8fdbefc1d1e96626
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / ConstantArrayCollectionTest.java
1 /*
2  * Copyright (c) 2015 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.assertTrue;
13 import static org.junit.Assert.fail;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.Iterables;
16 import java.util.Collection;
17 import java.util.Collections;
18 import org.junit.Test;
19
20 public class ConstantArrayCollectionTest {
21     private static final String[] ARRAY = new String[] { "a", "bb", "ccc" };
22     private static final Collection<String> REF = ImmutableList.copyOf(ARRAY);
23
24     private static Collection<String> create() {
25         return new ConstantArrayCollection<>(ARRAY.clone());
26     }
27
28     @Test
29     public void testToString() {
30         // Empty
31         assertEquals(Collections.emptySet().toString(), new ConstantArrayCollection<>(new Object[0]).toString());
32
33         // Normal
34         assertEquals(REF.toString(), create().toString());
35     }
36
37     @Test
38     public void testEquals() {
39         final Collection<?> c = create();
40
41         assertTrue(c.containsAll(REF));
42         assertTrue(REF.containsAll(c));
43         assertTrue(Iterables.elementsEqual(REF, c));
44     }
45
46     @Test
47     public void testSimpleOperations() {
48         final Collection<?> c = create();
49
50         assertEquals(ARRAY.length, c.size());
51         assertFalse(c.isEmpty());
52         assertTrue(c.contains("ccc"));
53         assertFalse(c.contains(""));
54         assertFalse(c.contains(1));
55
56         assertTrue(c.containsAll(Collections.<String>emptyList()));
57         assertFalse(c.containsAll(Collections.singleton("")));
58         assertFalse(c.containsAll(Collections.singleton(1)));
59     }
60
61     @Test
62     public void testProtection() {
63         final Collection<?> c = create();
64
65         try {
66             c.add(null);
67             fail();
68         } catch (UnsupportedOperationException e) {
69         }
70
71         try {
72             c.remove(null);
73             fail();
74         } catch (UnsupportedOperationException e) {
75         }
76
77         try {
78             c.addAll(null);
79             fail();
80         } catch (UnsupportedOperationException e) {
81         }
82
83         try {
84             c.removeAll(null);
85             fail();
86         } catch (UnsupportedOperationException e) {
87         }
88
89         try {
90             c.retainAll(null);
91             fail();
92         } catch (UnsupportedOperationException e) {
93         }
94
95         try {
96             c.clear();
97             fail();
98         } catch (UnsupportedOperationException e) {
99         }
100     }
101 }