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;
15 import java.util.Collections;
16 import java.util.Iterator;
17 import org.junit.Test;
19 public class SingletonSetTest {
20 private static final String ELEMENT = "element";
22 private static SingletonSet<?> nullSet() {
23 return SingletonSet.of(null);
27 public void testNullSingleton() {
28 final SingletonSet<?> s = nullSet();
30 assertFalse(s.isEmpty());
31 assertEquals(1, s.size());
32 assertFalse(s.contains(""));
33 assertTrue(s.contains(null));
34 assertNull(s.getElement());
35 assertEquals(0, s.hashCode());
36 assertTrue(s.equals(Collections.singleton(null)));
37 assertFalse(s.equals(Collections.singleton("")));
38 assertFalse(s.equals(""));
39 assertTrue(s.equals(s));
40 assertFalse(s.equals(null));
41 assertEquals(Collections.singleton(null).toString(), s.toString());
45 public void testRegularSingleton() {
46 final SingletonSet<?> s = SingletonSet.of(ELEMENT);
48 assertFalse(s.isEmpty());
49 assertEquals(1, s.size());
50 assertFalse(s.contains(""));
51 assertFalse(s.contains(null));
52 assertTrue(s.contains(ELEMENT));
54 assertSame(ELEMENT, s.getElement());
55 assertEquals(ELEMENT.hashCode(), s.hashCode());
56 assertTrue(s.equals(Collections.singleton(ELEMENT)));
57 assertFalse(s.equals(Collections.singleton("")));
58 assertFalse(s.equals(Collections.singleton(null)));
59 assertFalse(s.equals(""));
60 assertTrue(s.equals(s));
61 assertFalse(s.equals(null));
62 assertEquals(Collections.singleton(ELEMENT).toString(), s.toString());
66 public void testIterator() {
67 final SingletonSet<?> s = SingletonSet.of(ELEMENT);
68 final Iterator<?> it = s.iterator();
70 assertTrue(it.hasNext());
71 assertSame(ELEMENT, it.next());
72 assertFalse(it.hasNext());
75 @Test(expected=UnsupportedOperationException.class)
76 public void testRejectedAdd() {
77 final SingletonSet<?> s = nullSet();
81 @Test(expected=UnsupportedOperationException.class)
82 public void testRejectedAddAll() {
83 final SingletonSet<?> s = nullSet();
87 @Test(expected=UnsupportedOperationException.class)
88 public void testRejectedClear() {
89 final SingletonSet<?> s = nullSet();
93 @Test(expected=UnsupportedOperationException.class)
94 public void testRejectedRemove() {
95 final SingletonSet<?> s = nullSet();
99 @Test(expected=UnsupportedOperationException.class)
100 public void testRejectedRemoveAll() {
101 final SingletonSet<?> s = nullSet();
105 @Test(expected=UnsupportedOperationException.class)
106 public void testRejectedRetainAll() {
107 final SingletonSet<?> s = nullSet();