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