source: josm/trunk/test/unit/org/openstreetmap/josm/tools/MultiMapTest.java@ 17442

Last change on this file since 17442 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNull;
7import static org.junit.jupiter.api.Assertions.assertTrue;
8
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.HashSet;
12import java.util.Map;
13import java.util.Set;
14
15import org.junit.jupiter.api.Test;
16import org.openstreetmap.josm.TestUtils;
17
18import nl.jqno.equalsverifier.EqualsVerifier;
19
20/**
21 * Unit tests of {@link MultiMap} class.
22 */
23class MultiMapTest {
24
25 /**
26 * Unit test of methods {@link MultiMap#equals} and {@link MultiMap#hashCode}.
27 */
28 @Test
29 void testEqualsContract() {
30 TestUtils.assumeWorkingEqualsVerifier();
31 EqualsVerifier.forClass(MultiMap.class).usingGetClass().verify();
32 }
33
34 /**
35 * Various test of {@link MultiMap}.
36 */
37 @Test
38 void testMultiMap() {
39 final MultiMap<String, String> map = new MultiMap<>();
40 assertTrue(map.isEmpty());
41 map.put("foo", "bar");
42 map.put("foo", "baz");
43 map.putVoid("alpha");
44 assertEquals(2, map.size());
45 assertEquals(new HashSet<>(Arrays.asList("foo", "alpha")), map.keySet());
46 assertEquals(new HashSet<>(Arrays.asList("bar", "baz")), map.get("foo"));
47 assertEquals(new HashSet<>(Arrays.asList("bar", "baz")), map.getValues("foo"));
48 assertEquals(new HashSet<>(), map.get("alpha"));
49 assertEquals(Collections.emptySet(), map.getValues("alpha"));
50 assertNull(map.get("beta"));
51 assertEquals(Collections.emptySet(), map.getValues("beta"));
52 assertEquals(new HashSet<>(), map.getValues("alpha"));
53 assertEquals(new HashSet<>(), map.getValues("beta"));
54 map.put("foo", "baz2");
55 map.put("foo", "baz");
56 assertTrue(map.containsKey("foo"));
57 assertTrue(map.contains("foo", "bar"));
58 assertEquals(new HashSet<>(Arrays.asList("bar", "baz", "baz2")), map.get("foo"));
59 assertFalse(map.contains("foo", "xxx"));
60 assertFalse(map.remove("foo", "xxx"));
61 assertTrue(map.remove("foo", "baz"));
62 assertEquals(new HashSet<>(Arrays.asList("bar", "baz2")), map.get("foo"));
63 assertEquals(new HashSet<>(Arrays.asList("bar", "baz2")), map.remove("foo"));
64 assertFalse(map.containsKey("foo"));
65 assertNull(map.get("foo"));
66 assertEquals("(alpha->[])", map.toString());
67 assertEquals(Collections.emptySet(), map.remove("alpha"));
68 assertTrue(map.isEmpty());
69 assertFalse(map.remove("omega", null));
70 assertTrue(map.isEmpty());
71 map.clear();
72 assertTrue(map.isEmpty());
73 map.putAll("foo", Arrays.asList("bar", "baz"));
74 assertEquals(new HashSet<>(Arrays.asList("bar", "baz")), map.get("foo"));
75 assertFalse(map.isEmpty());
76 map.clear();
77 assertTrue(map.isEmpty());
78 assertEquals(0, new MultiMap<String, String>(null).size());
79 final Map<String, Set<String>> asMap = Collections.singletonMap("foo", Collections.singleton("bar"));
80 assertEquals(asMap, new MultiMap<>(asMap).toMap());
81 assertEquals("[foo=[bar]]", new MultiMap<>(asMap).entrySet().toString());
82 assertEquals("[foo]", new MultiMap<>(asMap).keySet().toString());
83 assertEquals("[[bar]]", new MultiMap<>(asMap).values().toString());
84 }
85}
Note: See TracBrowser for help on using the repository browser.