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.assertTrue;
16 import java.util.Collections;
17 import java.util.Iterator;
18 import org.junit.Test;
20 public class SingletonSetTest {
21 private static final String ELEMENT = "element";
23 private static SingletonSet<?> nullSet() {
24 return SingletonSet.of(null);
28 public void testNullSingleton() {
29 final SingletonSet<?> s = nullSet();
31 assertFalse(s.isEmpty());
32 assertEquals(1, s.size());
33 assertFalse(s.contains(""));
34 assertTrue(s.contains(null));
35 assertNull(s.getElement());
36 assertEquals(0, s.hashCode());
37 assertTrue(s.equals(Collections.singleton(null)));
38 assertFalse(s.equals(Collections.singleton("")));
39 assertFalse(s.equals(""));
40 assertTrue(s.equals(s));
41 assertFalse(s.equals(null));
42 assertEquals(Collections.singleton(null).toString(), s.toString());
46 public void testRegularSingleton() {
47 final SingletonSet<?> s = SingletonSet.of(ELEMENT);
49 assertFalse(s.isEmpty());
50 assertEquals(1, s.size());
51 assertFalse(s.contains(""));
52 assertFalse(s.contains(null));
53 assertTrue(s.contains(ELEMENT));
55 assertSame(ELEMENT, s.getElement());
56 assertEquals(ELEMENT.hashCode(), s.hashCode());
57 assertTrue(s.equals(Collections.singleton(ELEMENT)));
58 assertFalse(s.equals(Collections.singleton("")));
59 assertFalse(s.equals(Collections.singleton(null)));
60 assertFalse(s.equals(""));
61 assertTrue(s.equals(s));
62 assertFalse(s.equals(null));
63 assertEquals(Collections.singleton(ELEMENT).toString(), s.toString());
67 public void testIterator() {
68 final SingletonSet<?> s = SingletonSet.of(ELEMENT);
69 final Iterator<?> it = s.iterator();
71 assertTrue(it.hasNext());
72 assertSame(ELEMENT, it.next());
73 assertFalse(it.hasNext());
76 @Test(expected=UnsupportedOperationException.class)
77 public void testRejectedAdd() {
78 final SingletonSet<?> s = nullSet();
82 @Test(expected=UnsupportedOperationException.class)
83 public void testRejectedAddAll() {
84 final SingletonSet<?> s = nullSet();
88 @Test(expected=UnsupportedOperationException.class)
89 public void testRejectedClear() {
90 final SingletonSet<?> s = nullSet();
94 @Test(expected=UnsupportedOperationException.class)
95 public void testRejectedRemove() {
96 final SingletonSet<?> s = nullSet();
100 @Test(expected=UnsupportedOperationException.class)
101 public void testRejectedRemoveAll() {
102 final SingletonSet<?> s = nullSet();
106 @Test(expected=UnsupportedOperationException.class)
107 public void testRejectedRetainAll() {
108 final SingletonSet<?> s = nullSet();