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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
18 import com.google.common.collect.ImmutableMap;
19 import java.util.Collections;
22 import org.junit.Test;
24 public class SharedSingletonMapTest {
25 private static UnmodifiableMapPhase<String, String> create() {
26 return SharedSingletonMap.orderedOf("k1", "v1");
30 public void testSimpleOperations() {
31 final Map<String, String> m = create();
33 assertFalse(m.isEmpty());
34 assertEquals(1, m.size());
36 assertTrue(m.containsKey("k1"));
37 assertFalse(m.containsKey(null));
38 assertFalse(m.containsKey("v1"));
40 assertTrue(m.containsValue("v1"));
41 assertFalse(m.containsValue(null));
42 assertFalse(m.containsValue("k1"));
44 assertEquals("v1", m.get("k1"));
45 assertNull(m.get(null));
46 assertNull(m.get("v1"));
48 assertFalse(m.equals(null));
49 assertTrue(m.equals(m));
50 assertFalse(m.equals(""));
52 final Map<String, String> same = Collections.singletonMap("k1", "v1");
53 assertEquals(same.toString(), m.toString());
54 assertTrue(same.equals(m));
55 assertTrue(m.equals(same));
56 assertEquals(same.entrySet(), m.entrySet());
57 assertEquals(same.values(), m.values());
59 // Perform twice to exercise the cache
60 assertEquals(same.hashCode(), m.hashCode());
61 assertEquals(same.hashCode(), m.hashCode());
63 assertFalse(m.equals(Collections.singletonMap(null, null)));
64 assertFalse(m.equals(Collections.singletonMap("k1", null)));
65 assertFalse(m.equals(Collections.singletonMap(null, "v1")));
66 assertFalse(m.equals(Collections.singletonMap("k1", "v2")));
67 assertFalse(m.equals(ImmutableMap.of("k1", "v1", "k2", "v2")));
69 final Set<String> set = m.keySet();
70 assertThat(set, instanceOf(SingletonSet.class));
71 assertTrue(set.contains("k1"));
75 public void testOrderedCopyOf() {
76 final Map<String, String> t = Collections.singletonMap("k1", "v1");
77 final Map<String, String> m = SharedSingletonMap.orderedCopyOf(t);
78 assertTrue(t.equals(m));
79 assertTrue(m.equals(t));
83 public void testUnorderedCopyOf() {
84 final Map<String, String> t = Collections.singletonMap("k1", "v1");
85 final Map<String, String> m = SharedSingletonMap.unorderedCopyOf(t);
86 assertTrue(t.equals(m));
87 assertTrue(m.equals(t));
91 public void testEmptyOrderedCopyOf() {
92 assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.orderedCopyOf(ImmutableMap.of()));
96 public void testEmptyUnorderedCopyOf() {
97 assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.unorderedCopyOf(ImmutableMap.of()));
101 public void testClear() {
102 assertThrows(UnsupportedOperationException.class, () -> create().clear());
106 public void testPut() {
107 assertThrows(UnsupportedOperationException.class, () -> create().put(null, null));
111 public void testPutAll() {
112 assertThrows(UnsupportedOperationException.class, () -> create().putAll(Collections.singletonMap("", "")));
116 public void testRemove() {
117 assertThrows(UnsupportedOperationException.class, () -> create().remove(null));