Convert ConstantArrayCollectionTest to use assertThrows()
[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.assertThrows;
13 import static org.junit.Assert.assertTrue;
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         assertThrows(UnsupportedOperationException.class, () -> c.add(null));
67         assertThrows(UnsupportedOperationException.class, () -> c.remove(null));
68         assertThrows(UnsupportedOperationException.class, () -> c.addAll(null));
69         assertThrows(UnsupportedOperationException.class, () -> c.removeAll(null));
70         assertThrows(UnsupportedOperationException.class, () -> c.retainAll(null));
71         assertThrows(UnsupportedOperationException.class, () -> c.clear());
72     }
73 }