1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 |
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Set;
|
---|
14 |
|
---|
15 | import org.junit.jupiter.api.Test;
|
---|
16 | import org.openstreetmap.josm.TestUtils;
|
---|
17 |
|
---|
18 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Unit tests of {@link MultiMap} class.
|
---|
22 | */
|
---|
23 | class 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 | }
|
---|