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.assertTrue;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Collections;
18 import org.junit.Test;
20 public class SharedSingletonMapTest {
21 private static UnmodifiableMapPhase<String, String> create() {
22 return SharedSingletonMap.orderedOf("k1", "v1");
26 public void testSimpleOperations() {
27 final Map<String, String> m = create();
29 assertFalse(m.isEmpty());
30 assertEquals(1, m.size());
32 assertTrue(m.containsKey("k1"));
33 assertFalse(m.containsKey(null));
34 assertFalse(m.containsKey("v1"));
36 assertTrue(m.containsValue("v1"));
37 assertFalse(m.containsValue(null));
38 assertFalse(m.containsValue("k1"));
40 assertEquals("v1", m.get("k1"));
41 assertNull(m.get(null));
42 assertNull(m.get("v1"));
44 assertFalse(m.equals(null));
45 assertTrue(m.equals(m));
46 assertFalse(m.equals(""));
48 final Map<String, String> same = Collections.singletonMap("k1", "v1");
49 assertEquals(same.toString(), m.toString());
50 assertTrue(same.equals(m));
51 assertTrue(m.equals(same));
52 assertEquals(same.entrySet(), m.entrySet());
53 assertEquals(same.values(), m.values());
55 // Perform twice to exercise the cache
56 assertEquals(same.hashCode(), m.hashCode());
57 assertEquals(same.hashCode(), m.hashCode());
59 assertFalse(m.equals(Collections.singletonMap(null, null)));
60 assertFalse(m.equals(Collections.singletonMap("k1", null)));
61 assertFalse(m.equals(Collections.singletonMap(null, "v1")));
62 assertFalse(m.equals(Collections.singletonMap("k1", "v2")));
63 assertFalse(m.equals(ImmutableMap.of("k1", "v1", "k2", "v2")));
65 final Set<String> set = m.keySet();
66 assertTrue(set instanceof SingletonSet);
67 assertTrue(set.contains("k1"));
71 public void testOrderedCopyOf() {
72 final Map<String, String> t = Collections.singletonMap("k1", "v1");
73 final Map<String, String> m = SharedSingletonMap.orderedCopyOf(t);
74 assertTrue(t.equals(m));
75 assertTrue(m.equals(t));
79 public void testUnorderedCopyOf() {
80 final Map<String, String> t = Collections.singletonMap("k1", "v1");
81 final Map<String, String> m = SharedSingletonMap.unorderedCopyOf(t);
82 assertTrue(t.equals(m));
83 assertTrue(m.equals(t));
86 @Test(expected=IllegalArgumentException.class)
87 public void testEmptyOrderedCopyOf() {
88 SharedSingletonMap.orderedCopyOf(ImmutableMap.of());
91 @Test(expected=IllegalArgumentException.class)
92 public void testEmptyUnorderedCopyOf() {
93 SharedSingletonMap.unorderedCopyOf(ImmutableMap.of());
96 @Test(expected=UnsupportedOperationException.class)
97 public void testClear() {
101 @Test(expected=UnsupportedOperationException.class)
102 public void testPut() {
103 create().put(null, null);
106 @Test(expected=UnsupportedOperationException.class)
107 public void testPutAll() {
108 create().putAll(Collections.singletonMap("", ""));
111 @Test(expected=UnsupportedOperationException.class)
112 public void testRemove() {
113 create().remove(null);