Use assertThrows() in common-util
[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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
17
18 import com.google.common.collect.ImmutableMap;
19 import java.util.Collections;
20 import java.util.Map;
21 import java.util.Set;
22 import org.junit.Test;
23
24 public class SharedSingletonMapTest {
25     private static UnmodifiableMapPhase<String, String> create() {
26         return SharedSingletonMap.orderedOf("k1", "v1");
27     }
28
29     @Test
30     public void testSimpleOperations() {
31         final Map<String, String> m = create();
32
33         assertFalse(m.isEmpty());
34         assertEquals(1, m.size());
35
36         assertTrue(m.containsKey("k1"));
37         assertFalse(m.containsKey(null));
38         assertFalse(m.containsKey("v1"));
39
40         assertTrue(m.containsValue("v1"));
41         assertFalse(m.containsValue(null));
42         assertFalse(m.containsValue("k1"));
43
44         assertEquals("v1", m.get("k1"));
45         assertNull(m.get(null));
46         assertNull(m.get("v1"));
47
48         assertFalse(m.equals(null));
49         assertTrue(m.equals(m));
50         assertFalse(m.equals(""));
51
52         final Map<String, String> same = Collections.singletonMap("k1", "v1");
53         assertEquals(same.toString(), m.toString());
54         assertTrue(same.equals(m));
55         assertTrue(m.equals(same));
56         assertEquals(same.entrySet(), m.entrySet());
57         assertEquals(same.values(), m.values());
58
59         // Perform twice to exercise the cache
60         assertEquals(same.hashCode(), m.hashCode());
61         assertEquals(same.hashCode(), m.hashCode());
62
63         assertFalse(m.equals(Collections.singletonMap(null, null)));
64         assertFalse(m.equals(Collections.singletonMap("k1", null)));
65         assertFalse(m.equals(Collections.singletonMap(null, "v1")));
66         assertFalse(m.equals(Collections.singletonMap("k1", "v2")));
67         assertFalse(m.equals(ImmutableMap.of("k1", "v1", "k2", "v2")));
68
69         final Set<String> set = m.keySet();
70         assertThat(set, instanceOf(SingletonSet.class));
71         assertTrue(set.contains("k1"));
72     }
73
74     @Test
75     public void testOrderedCopyOf() {
76         final Map<String, String> t = Collections.singletonMap("k1", "v1");
77         final Map<String, String> m = SharedSingletonMap.orderedCopyOf(t);
78         assertTrue(t.equals(m));
79         assertTrue(m.equals(t));
80     }
81
82     @Test
83     public void testUnorderedCopyOf() {
84         final Map<String, String> t = Collections.singletonMap("k1", "v1");
85         final Map<String, String> m = SharedSingletonMap.unorderedCopyOf(t);
86         assertTrue(t.equals(m));
87         assertTrue(m.equals(t));
88     }
89
90     @Test
91     public void testEmptyOrderedCopyOf() {
92         assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.orderedCopyOf(ImmutableMap.of()));
93     }
94
95     @Test
96     public void testEmptyUnorderedCopyOf() {
97         assertThrows(IllegalArgumentException.class, () -> SharedSingletonMap.unorderedCopyOf(ImmutableMap.of()));
98     }
99
100     @Test
101     public void testClear() {
102         assertThrows(UnsupportedOperationException.class, () -> create().clear());
103     }
104
105     @Test
106     public void testPut() {
107         assertThrows(UnsupportedOperationException.class, () -> create().put(null, null));
108     }
109
110     @Test
111     public void testPutAll() {
112         assertThrows(UnsupportedOperationException.class, () -> create().putAll(Collections.singletonMap("", "")));
113     }
114
115     @Test
116     public void testRemove() {
117         assertThrows(UnsupportedOperationException.class, () -> create().remove(null));
118     }
119 }