Use assertThrows() in common-util
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / SingletonSetTest.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.assertNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.assertTrue;
16
17 import java.util.Collections;
18 import java.util.Iterator;
19 import org.junit.Test;
20
21 public class SingletonSetTest {
22     private static final String ELEMENT = "element";
23
24     private static SingletonSet<?> nullSet() {
25         return SingletonSet.of(null);
26     }
27
28     @Test
29     public void testNullSingleton() {
30         final SingletonSet<?> s = nullSet();
31
32         assertFalse(s.isEmpty());
33         assertEquals(1, s.size());
34         assertFalse(s.contains(""));
35         assertTrue(s.contains(null));
36         assertNull(s.getElement());
37         assertEquals(0, s.hashCode());
38         assertTrue(s.equals(Collections.singleton(null)));
39         assertFalse(s.equals(Collections.singleton("")));
40         assertFalse(s.equals(""));
41         assertTrue(s.equals(s));
42         assertFalse(s.equals(null));
43         assertEquals(Collections.singleton(null).toString(), s.toString());
44     }
45
46     @Test
47     public void testRegularSingleton() {
48         final SingletonSet<?> s = SingletonSet.of(ELEMENT);
49
50         assertFalse(s.isEmpty());
51         assertEquals(1, s.size());
52         assertFalse(s.contains(""));
53         assertFalse(s.contains(null));
54         assertTrue(s.contains(ELEMENT));
55
56         assertSame(ELEMENT, s.getElement());
57         assertEquals(ELEMENT.hashCode(), s.hashCode());
58         assertTrue(s.equals(Collections.singleton(ELEMENT)));
59         assertFalse(s.equals(Collections.singleton("")));
60         assertFalse(s.equals(Collections.singleton(null)));
61         assertFalse(s.equals(""));
62         assertTrue(s.equals(s));
63         assertFalse(s.equals(null));
64         assertEquals(Collections.singleton(ELEMENT).toString(), s.toString());
65     }
66
67     @Test
68     public void testIterator() {
69         final SingletonSet<?> s = SingletonSet.of(ELEMENT);
70         final Iterator<?> it = s.iterator();
71
72         assertTrue(it.hasNext());
73         assertSame(ELEMENT, it.next());
74         assertFalse(it.hasNext());
75     }
76
77     @Test
78     public void testRejectedAdd() {
79         final SingletonSet<?> s = nullSet();
80         assertThrows(UnsupportedOperationException.class, () -> s.add(null));
81     }
82
83     @Test
84     public void testRejectedAddAll() {
85         final SingletonSet<?> s = nullSet();
86         assertThrows(UnsupportedOperationException.class, () -> s.addAll(null));
87     }
88
89     @Test
90     public void testRejectedClear() {
91         final SingletonSet<?> s = nullSet();
92         assertThrows(UnsupportedOperationException.class, () -> s.clear());
93     }
94
95     @Test
96     public void testRejectedRemove() {
97         final SingletonSet<?> s = nullSet();
98         assertThrows(UnsupportedOperationException.class, () -> s.remove(null));
99     }
100
101     @Test
102     public void testRejectedRemoveAll() {
103         final SingletonSet<?> s = nullSet();
104         assertThrows(UnsupportedOperationException.class, () -> s.removeAll(null));
105     }
106
107     @Test
108     public void testRejectedRetainAll() {
109         final SingletonSet<?> s = nullSet();
110         assertThrows(UnsupportedOperationException.class, () -> s.retainAll(null));
111     }
112 }