2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
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;
18 import java.util.Collections;
20 import org.junit.jupiter.api.Test;
22 class SingletonSetTest {
23 private static final String ELEMENT = "element";
25 private static SingletonSet<?> nullSet() {
26 return SingletonSet.of(null);
30 void testNullSingleton() {
31 final var s = nullSet();
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);
43 assertNotEquals(null, s);
44 assertEquals(Collections.singleton(null).toString(), s.toString());
48 void testRegularSingleton() {
49 final var s = SingletonSet.of(ELEMENT);
51 assertFalse(s.isEmpty());
52 assertEquals(1, s.size());
53 assertFalse(s.contains(""));
54 assertFalse(s.contains(null));
55 assertTrue(s.contains(ELEMENT));
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);
64 assertNotEquals(null, s);
65 assertEquals(Collections.singleton(ELEMENT).toString(), s.toString());
70 final var s = SingletonSet.of(ELEMENT);
71 final var it = s.iterator();
73 assertTrue(it.hasNext());
74 assertSame(ELEMENT, it.next());
75 assertFalse(it.hasNext());
79 void testRejectedAdd() {
80 final var s = nullSet();
81 assertThrows(UnsupportedOperationException.class, () -> s.add(null));
85 void testRejectedAddAll() {
86 final var s = nullSet();
87 assertThrows(UnsupportedOperationException.class, () -> s.addAll(null));
91 void testRejectedClear() {
92 final var s = nullSet();
93 assertThrows(UnsupportedOperationException.class, () -> s.clear());
97 void testRejectedRemove() {
98 final var s = nullSet();
99 assertThrows(UnsupportedOperationException.class, () -> s.remove(null));
103 void testRejectedRemoveAll() {
104 final var s = nullSet();
105 assertThrows(UnsupportedOperationException.class, () -> s.removeAll(null));
109 void testRejectedRetainAll() {
110 final var s = nullSet();
111 assertThrows(UnsupportedOperationException.class, () -> s.retainAll(null));