BUG-4803: introduce unordered offset maps
[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 com.google.common.collect.ImmutableMap;
15 import java.util.Collections;
16 import java.util.Map;
17 import java.util.Set;
18 import org.junit.Test;
19
20 public class SharedSingletonMapTest {
21     private static UnmodifiableMapPhase<String, String> create() {
22         return SharedSingletonMap.orderedOf("k1", "v1");
23     }
24
25     @Test
26     public void testSimpleOperations() {
27         final Map<String, String> m = create();
28
29         assertFalse(m.isEmpty());
30         assertEquals(1, m.size());
31
32         assertTrue(m.containsKey("k1"));
33         assertFalse(m.containsKey(null));
34         assertFalse(m.containsKey("v1"));
35
36         assertTrue(m.containsValue("v1"));
37         assertFalse(m.containsValue(null));
38         assertFalse(m.containsValue("k1"));
39
40         assertEquals("v1", m.get("k1"));
41         assertNull(m.get(null));
42         assertNull(m.get("v1"));
43
44         assertFalse(m.equals(null));
45         assertTrue(m.equals(m));
46         assertFalse(m.equals(""));
47
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());
54
55         // Perform twice to exercise the cache
56         assertEquals(same.hashCode(), m.hashCode());
57         assertEquals(same.hashCode(), m.hashCode());
58
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")));
64
65         final Set<String> set = m.keySet();
66         assertTrue(set instanceof SingletonSet);
67         assertTrue(set.contains("k1"));
68     }
69
70     @Test
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));
76     }
77
78     @Test
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));
84     }
85
86     @Test(expected=IllegalArgumentException.class)
87     public void testEmptyOrderedCopyOf() {
88         SharedSingletonMap.orderedCopyOf(ImmutableMap.of());
89     }
90
91     @Test(expected=IllegalArgumentException.class)
92     public void testEmptyUnorderedCopyOf() {
93         SharedSingletonMap.unorderedCopyOf(ImmutableMap.of());
94     }
95
96     @Test(expected=UnsupportedOperationException.class)
97     public void testClear() {
98         create().clear();
99     }
100
101     @Test(expected=UnsupportedOperationException.class)
102     public void testPut() {
103         create().put(null, null);
104     }
105
106     @Test(expected=UnsupportedOperationException.class)
107     public void testPutAll() {
108         create().putAll(Collections.singletonMap("", ""));
109     }
110
111     @Test(expected=UnsupportedOperationException.class)
112     public void testRemove() {
113         create().remove(null);
114     }
115 }