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