Migrate common/util to JUnit5
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / SharedSingletonMapTest.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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotEquals;
14 import static org.junit.jupiter.api.Assertions.assertNull;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16 import static org.junit.jupiter.api.Assertions.assertTrue;
17
18 import com.google.common.collect.ImmutableMap;
19 import java.util.Collections;
20 import java.util.Map;
21 import org.junit.jupiter.api.Test;
22
23 class SharedSingletonMapTest {
24     private static UnmodifiableMapPhase<String, String> create() {
25         return SharedSingletonMap.orderedOf("k1", "v1");
26     }
27
28     @Test
29     void testSimpleOperations() {
30         final var m = create();
31
32         assertFalse(m.isEmpty());
33         assertEquals(1, m.size());
34
35         assertTrue(m.containsKey("k1"));
36         assertFalse(m.containsKey(null));
37         assertFalse(m.containsKey("v1"));
38
39         assertTrue(m.containsValue("v1"));
40         assertFalse(m.containsValue(null));
41         assertFalse(m.containsValue("k1"));
42
43         assertEquals("v1", m.get("k1"));
44         assertNull(m.get(null));
45         assertNull(m.get("v1"));
46
47         assertNotEquals(null, m);
48         assertEquals(m, m);
49         assertNotEquals("", m);
50
51         final var same = Collections.singletonMap("k1", "v1");
52         assertEquals(same.toString(), m.toString());
53         assertEquals(same, m);
54         assertEquals(m, same);
55         assertEquals(same.entrySet(), m.entrySet());
56         assertEquals(same.values(), m.values());
57
58         // Perform twice to exercise the cache
59         assertEquals(same.hashCode(), m.hashCode());
60         assertEquals(same.hashCode(), m.hashCode());
61
62         assertNotEquals(m, Collections.singletonMap(null, null));
63         assertNotEquals(m, Collections.singletonMap("k1", null));
64         assertNotEquals(m, Collections.singletonMap(null, "v1"));
65         assertNotEquals(m, Map.of("k1", "v2"));
66         assertNotEquals(m, ImmutableMap.of("k1", "v1", "k2", "v2"));
67
68         final var set = m.keySet();
69         assertInstanceOf(SingletonSet.class, set);
70         assertTrue(set.contains("k1"));
71     }
72
73     @Test
74     void testOrderedCopyOf() {
75         final var t = Map.of("k1", "v1");
76         final var m = SharedSingletonMap.orderedCopyOf(t);
77         assertEquals(t, m);
78         assertEquals(m, t);
79     }
80
81     @Test
82     void testUnorderedCopyOf() {
83         final var t = Map.of("k1", "v1");
84         final var m = SharedSingletonMap.unorderedCopyOf(t);
85         assertEquals(t, m);
86         assertEquals(m, t);
87     }
88
89     @Test
90     void testEmptyOrderedCopyOf() {
91         assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.orderedCopyOf(ImmutableMap.of()));
92     }
93
94     @Test
95     void testEmptyUnorderedCopyOf() {
96         assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.unorderedCopyOf(ImmutableMap.of()));
97     }
98
99     @Test
100     void testClear() {
101         assertThrows(UnsupportedOperationException.class, () -> create().clear());
102     }
103
104     @Test
105     void testPut() {
106         assertThrows(UnsupportedOperationException.class, () -> create().put(null, null));
107     }
108
109     @Test
110     void testPutAll() {
111         assertThrows(UnsupportedOperationException.class, () -> create().putAll(Collections.singletonMap("", "")));
112     }
113
114     @Test
115     void testRemove() {
116         assertThrows(UnsupportedOperationException.class, () -> create().remove(null));
117     }
118 }