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.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;
17 import java.util.Collections;
18 import java.util.Iterator;
19 import org.junit.Test;
21 public class SingletonSetTest {
22 private static final String ELEMENT = "element";
24 private static SingletonSet<?> nullSet() {
25 return SingletonSet.of(null);
29 public void testNullSingleton() {
30 final SingletonSet<?> s = nullSet();
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());
47 public void testRegularSingleton() {
48 final SingletonSet<?> s = SingletonSet.of(ELEMENT);
50 assertFalse(s.isEmpty());
51 assertEquals(1, s.size());
52 assertFalse(s.contains(""));
53 assertFalse(s.contains(null));
54 assertTrue(s.contains(ELEMENT));
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());
68 public void testIterator() {
69 final SingletonSet<?> s = SingletonSet.of(ELEMENT);
70 final Iterator<?> it = s.iterator();
72 assertTrue(it.hasNext());
73 assertSame(ELEMENT, it.next());
74 assertFalse(it.hasNext());
78 public void testRejectedAdd() {
79 final SingletonSet<?> s = nullSet();
80 assertThrows(UnsupportedOperationException.class, () -> s.add(null));
84 public void testRejectedAddAll() {
85 final SingletonSet<?> s = nullSet();
86 assertThrows(UnsupportedOperationException.class, () -> s.addAll(null));
90 public void testRejectedClear() {
91 final SingletonSet<?> s = nullSet();
92 assertThrows(UnsupportedOperationException.class, () -> s.clear());
96 public void testRejectedRemove() {
97 final SingletonSet<?> s = nullSet();
98 assertThrows(UnsupportedOperationException.class, () -> s.remove(null));
102 public void testRejectedRemoveAll() {
103 final SingletonSet<?> s = nullSet();
104 assertThrows(UnsupportedOperationException.class, () -> s.removeAll(null));
108 public void testRejectedRetainAll() {
109 final SingletonSet<?> s = nullSet();
110 assertThrows(UnsupportedOperationException.class, () -> s.retainAll(null));