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.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;
18 import com.google.common.collect.ImmutableMap;
19 import java.util.Collections;
21 import org.junit.jupiter.api.Test;
23 class SharedSingletonMapTest {
24 private static UnmodifiableMapPhase<String, String> create() {
25 return SharedSingletonMap.orderedOf("k1", "v1");
29 void testSimpleOperations() {
30 final var m = create();
32 assertFalse(m.isEmpty());
33 assertEquals(1, m.size());
35 assertTrue(m.containsKey("k1"));
36 assertFalse(m.containsKey(null));
37 assertFalse(m.containsKey("v1"));
39 assertTrue(m.containsValue("v1"));
40 assertFalse(m.containsValue(null));
41 assertFalse(m.containsValue("k1"));
43 assertEquals("v1", m.get("k1"));
44 assertNull(m.get(null));
45 assertNull(m.get("v1"));
47 assertNotEquals(null, m);
49 assertNotEquals("", m);
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());
58 // Perform twice to exercise the cache
59 assertEquals(same.hashCode(), m.hashCode());
60 assertEquals(same.hashCode(), m.hashCode());
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"));
68 final var set = m.keySet();
69 assertInstanceOf(SingletonSet.class, set);
70 assertTrue(set.contains("k1"));
74 void testOrderedCopyOf() {
75 final var t = Map.of("k1", "v1");
76 final var m = SharedSingletonMap.orderedCopyOf(t);
82 void testUnorderedCopyOf() {
83 final var t = Map.of("k1", "v1");
84 final var m = SharedSingletonMap.unorderedCopyOf(t);
90 void testEmptyOrderedCopyOf() {
91 assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.orderedCopyOf(ImmutableMap.of()));
95 void testEmptyUnorderedCopyOf() {
96 assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.unorderedCopyOf(ImmutableMap.of()));
101 assertThrows(UnsupportedOperationException.class, () -> create().clear());
106 assertThrows(UnsupportedOperationException.class, () -> create().put(null, null));
111 assertThrows(UnsupportedOperationException.class, () -> create().putAll(Collections.singletonMap("", "")));
116 assertThrows(UnsupportedOperationException.class, () -> create().remove(null));