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