3e0bb753dfbead2d027e3808f964faa0695d1628
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / SharedSingletonMapTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.util;
9
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;
15 import java.util.Map;
16 import java.util.Set;
17 import org.junit.Test;
18
19 public class SharedSingletonMapTest {
20     private static UnmodifiableMapPhase<String, String> create() {
21         return SharedSingletonMap.of("k1", "v1");
22     }
23
24     @Test
25     public void testSimpleOperations() {
26         final Map<String, String> m = create();
27
28         assertFalse(m.isEmpty());
29         assertEquals(1, m.size());
30
31         assertTrue(m.containsKey("k1"));
32         assertFalse(m.containsKey(null));
33         assertFalse(m.containsKey("v1"));
34
35         assertTrue(m.containsValue("v1"));
36         assertFalse(m.containsValue(null));
37         assertFalse(m.containsValue("k1"));
38
39         assertEquals("v1", m.get("k1"));
40         assertNull(m.get(null));
41         assertNull(m.get("v1"));
42
43         assertFalse(m.equals(null));
44         assertTrue(m.equals(m));
45         assertFalse(m.equals(""));
46
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());
53
54         // Perform twice to exercise the cache
55         assertEquals(same.hashCode(), m.hashCode());
56         assertEquals(same.hashCode(), m.hashCode());
57
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")));
62
63         final Set<String> set = m.keySet();
64         assertTrue(set instanceof SingletonSet);
65         assertTrue(set.contains("k1"));
66     }
67
68     @Test(expected=UnsupportedOperationException.class)
69     public void testClear() {
70         create().clear();
71     }
72
73     @Test(expected=UnsupportedOperationException.class)
74     public void testPut() {
75         create().put(null, null);
76     }
77
78     @Test(expected=UnsupportedOperationException.class)
79     public void testPutAll() {
80         create().putAll(Collections.singletonMap("", ""));
81     }
82
83     @Test(expected=UnsupportedOperationException.class)
84     public void testRemove() {
85         create().remove(null);
86     }
87 }