9cd7f35332df9160af44b4b4ff100f17e9b0deb8
[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.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;
18
19 public class SingletonSetTest {
20     private static final String ELEMENT = "element";
21
22     private static SingletonSet<?> nullSet() {
23         return SingletonSet.of(null);
24     }
25
26     @Test
27     public void testNullSingleton() {
28         final SingletonSet<?> s = nullSet();
29
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());
42     }
43
44     @Test
45     public void testRegularSingleton() {
46         final SingletonSet<?> s = SingletonSet.of(ELEMENT);
47
48         assertFalse(s.isEmpty());
49         assertEquals(1, s.size());
50         assertFalse(s.contains(""));
51         assertFalse(s.contains(null));
52         assertTrue(s.contains(ELEMENT));
53
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());
63     }
64
65     @Test
66     public void testIterator() {
67         final SingletonSet<?> s = SingletonSet.of(ELEMENT);
68         final Iterator<?> it = s.iterator();
69
70         assertTrue(it.hasNext());
71         assertSame(ELEMENT, it.next());
72         assertFalse(it.hasNext());
73     }
74
75     @Test(expected=UnsupportedOperationException.class)
76     public void testRejectedAdd() {
77         final SingletonSet<?> s = nullSet();
78         s.add(null);
79     }
80
81     @Test(expected=UnsupportedOperationException.class)
82     public void testRejectedAddAll() {
83         final SingletonSet<?> s = nullSet();
84         s.addAll(null);
85     }
86
87     @Test(expected=UnsupportedOperationException.class)
88     public void testRejectedClear() {
89         final SingletonSet<?> s = nullSet();
90         s.clear();
91     }
92
93     @Test(expected=UnsupportedOperationException.class)
94     public void testRejectedRemove() {
95         final SingletonSet<?> s = nullSet();
96         s.remove(null);
97     }
98
99     @Test(expected=UnsupportedOperationException.class)
100     public void testRejectedRemoveAll() {
101         final SingletonSet<?> s = nullSet();
102         s.removeAll(null);
103     }
104
105     @Test(expected=UnsupportedOperationException.class)
106     public void testRejectedRetainAll() {
107         final SingletonSet<?> s = nullSet();
108         s.retainAll(null);
109     }
110 }