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;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.Collections;
19 import org.junit.Test;
21 public class SharedSingletonMapTest {
22 private static UnmodifiableMapPhase<String, String> create() {
23 return SharedSingletonMap.orderedOf("k1", "v1");
27 public void testSimpleOperations() {
28 final Map<String, String> m = create();
30 assertFalse(m.isEmpty());
31 assertEquals(1, m.size());
33 assertTrue(m.containsKey("k1"));
34 assertFalse(m.containsKey(null));
35 assertFalse(m.containsKey("v1"));
37 assertTrue(m.containsValue("v1"));
38 assertFalse(m.containsValue(null));
39 assertFalse(m.containsValue("k1"));
41 assertEquals("v1", m.get("k1"));
42 assertNull(m.get(null));
43 assertNull(m.get("v1"));
45 assertFalse(m.equals(null));
46 assertTrue(m.equals(m));
47 assertFalse(m.equals(""));
49 final Map<String, String> same = Collections.singletonMap("k1", "v1");
50 assertEquals(same.toString(), m.toString());
51 assertTrue(same.equals(m));
52 assertTrue(m.equals(same));
53 assertEquals(same.entrySet(), m.entrySet());
54 assertEquals(same.values(), m.values());
56 // Perform twice to exercise the cache
57 assertEquals(same.hashCode(), m.hashCode());
58 assertEquals(same.hashCode(), m.hashCode());
60 assertFalse(m.equals(Collections.singletonMap(null, null)));
61 assertFalse(m.equals(Collections.singletonMap("k1", null)));
62 assertFalse(m.equals(Collections.singletonMap(null, "v1")));
63 assertFalse(m.equals(Collections.singletonMap("k1", "v2")));
64 assertFalse(m.equals(ImmutableMap.of("k1", "v1", "k2", "v2")));
66 final Set<String> set = m.keySet();
67 assertTrue(set instanceof SingletonSet);
68 assertTrue(set.contains("k1"));
72 public void testOrderedCopyOf() {
73 final Map<String, String> t = Collections.singletonMap("k1", "v1");
74 final Map<String, String> m = SharedSingletonMap.orderedCopyOf(t);
75 assertTrue(t.equals(m));
76 assertTrue(m.equals(t));
80 public void testUnorderedCopyOf() {
81 final Map<String, String> t = Collections.singletonMap("k1", "v1");
82 final Map<String, String> m = SharedSingletonMap.unorderedCopyOf(t);
83 assertTrue(t.equals(m));
84 assertTrue(m.equals(t));
87 @Test(expected=IllegalArgumentException.class)
88 public void testEmptyOrderedCopyOf() {
89 SharedSingletonMap.orderedCopyOf(ImmutableMap.of());
92 @Test(expected=IllegalArgumentException.class)
93 public void testEmptyUnorderedCopyOf() {
94 SharedSingletonMap.unorderedCopyOf(ImmutableMap.of());
97 @Test(expected=UnsupportedOperationException.class)
98 public void testClear() {
102 @Test(expected=UnsupportedOperationException.class)
103 public void testPut() {
104 create().put(null, null);
107 @Test(expected=UnsupportedOperationException.class)
108 public void testPutAll() {
109 create().putAll(Collections.singletonMap("", ""));
112 @Test(expected=UnsupportedOperationException.class)
113 public void testRemove() {
114 create().remove(null);