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 java.util.Collections;
17 import org.junit.Test;
19 public class SharedSingletonMapTest {
20 private static UnmodifiableMapPhase<String, String> create() {
21 return SharedSingletonMap.of("k1", "v1");
25 public void testSimpleOperations() {
26 final Map<String, String> m = create();
28 assertFalse(m.isEmpty());
29 assertEquals(1, m.size());
31 assertTrue(m.containsKey("k1"));
32 assertFalse(m.containsKey(null));
33 assertFalse(m.containsKey("v1"));
35 assertTrue(m.containsValue("v1"));
36 assertFalse(m.containsValue(null));
37 assertFalse(m.containsValue("k1"));
39 assertEquals("v1", m.get("k1"));
40 assertNull(m.get(null));
41 assertNull(m.get("v1"));
43 assertFalse(m.equals(null));
44 assertTrue(m.equals(m));
45 assertFalse(m.equals(""));
47 final Map<String, String> same = Collections.singletonMap("k1", "v1");
48 assertEquals(same.toString(), m.toString());
49 assertTrue(same.equals(m));
50 assertTrue(m.equals(same));
51 assertEquals(same.entrySet(), m.entrySet());
52 assertEquals(same.values(), m.values());
54 // Perform twice to exercise the cache
55 assertEquals(same.hashCode(), m.hashCode());
56 assertEquals(same.hashCode(), m.hashCode());
58 assertFalse(m.equals(Collections.singletonMap(null, null)));
59 assertFalse(m.equals(Collections.singletonMap("k1", null)));
60 assertFalse(m.equals(Collections.singletonMap(null, "v1")));
61 assertFalse(m.equals(Collections.singletonMap("k1", "v2")));
63 final Set<String> set = m.keySet();
64 assertTrue(set instanceof SingletonSet);
65 assertTrue(set.contains("k1"));
68 @Test(expected=UnsupportedOperationException.class)
69 public void testClear() {
73 @Test(expected=UnsupportedOperationException.class)
74 public void testPut() {
75 create().put(null, null);
78 @Test(expected=UnsupportedOperationException.class)
79 public void testPutAll() {
80 create().putAll(Collections.singletonMap("", ""));
83 @Test(expected=UnsupportedOperationException.class)
84 public void testRemove() {
85 create().remove(null);