1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static java.util.Collections.singleton;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
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.HashSet;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Set;
|
---|
14 |
|
---|
15 | import org.junit.jupiter.api.Test;
|
---|
16 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
17 | import org.openstreetmap.josm.TestUtils;
|
---|
18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
19 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
20 |
|
---|
21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
22 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Unit tests of {@link Territories} class.
|
---|
26 | */
|
---|
27 | class TerritoriesTest {
|
---|
28 | /**
|
---|
29 | * Test rules.
|
---|
30 | */
|
---|
31 | @RegisterExtension
|
---|
32 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
33 | public JOSMTestRules rules = new JOSMTestRules().projection().territories();
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Tests that {@code Territories} satisfies utility class criteria.
|
---|
37 | *
|
---|
38 | * @throws ReflectiveOperationException if an error occurs
|
---|
39 | */
|
---|
40 | @Test
|
---|
41 | void testUtilityClass() throws ReflectiveOperationException {
|
---|
42 | UtilityClassTestUtil.assertUtilityClassWellDefined(Territories.class);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Test of {@link Territories#isIso3166Code} method.
|
---|
47 | */
|
---|
48 | @Test
|
---|
49 | void testIsIso3166Code() {
|
---|
50 | check("Paris", new LatLon(48.8567, 2.3508), "EU", "FR", "FX");
|
---|
51 | }
|
---|
52 |
|
---|
53 | private static void check(String name, LatLon ll, String... expectedCodes) {
|
---|
54 | for (String e : expectedCodes) {
|
---|
55 | assertTrue(Territories.isIso3166Code(e, ll), name + " " + e);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Test of {@link Territories#initializeExternalData} - nominal case
|
---|
61 | */
|
---|
62 | @Test
|
---|
63 | void testTaginfoGeofabrik_nominal() {
|
---|
64 | Territories.initializeExternalData("foo", TestUtils.getTestDataRoot() + "/taginfo/geofabrik-index-v1-nogeom.json");
|
---|
65 | Map<String, TaginfoRegionalInstance> cache = Territories.taginfoGeofabrikCache;
|
---|
66 | assertEquals(5, cache.size());
|
---|
67 | checkTaginfoInstance(cache.get("AF"), singleton("AF"), "https://taginfo.geofabrik.de/asia/afghanistan/");
|
---|
68 | checkTaginfoInstance(cache.get("AL"), singleton("AL"), "https://taginfo.geofabrik.de/europe/albania/");
|
---|
69 | checkTaginfoInstance(cache.get("CA-AB"), singleton("CA-AB"), "https://taginfo.geofabrik.de/north-america/canada/alberta/");
|
---|
70 | Set<String> israelAndPalestine = new HashSet<>(Arrays.asList("PS", "IL"));
|
---|
71 | checkTaginfoInstance(cache.get("PS"), israelAndPalestine, "https://taginfo.geofabrik.de/asia/israel-and-palestine/");
|
---|
72 | checkTaginfoInstance(cache.get("IL"), israelAndPalestine, "https://taginfo.geofabrik.de/asia/israel-and-palestine/");
|
---|
73 | List<TaginfoRegionalInstance> regionalTaginfo = Territories.getRegionalTaginfoUrls(new LatLon(41.3268733, 19.8187913));
|
---|
74 | assertEquals(1, regionalTaginfo.size());
|
---|
75 | checkTaginfoInstance(regionalTaginfo.iterator().next(), singleton("AL"), "https://taginfo.geofabrik.de/europe/albania/");
|
---|
76 | }
|
---|
77 |
|
---|
78 | private static void checkTaginfoInstance(TaginfoRegionalInstance instance, Set<String> expectedIsoCodes, String expectedUrl) {
|
---|
79 | assertEquals(expectedIsoCodes, instance.getIsoCodes());
|
---|
80 | assertEquals("foo", instance.getSuffix());
|
---|
81 | assertEquals(expectedUrl, instance.getUrl());
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Test of {@link Territories#initializeExternalData} - broken contents
|
---|
86 | */
|
---|
87 | @Test
|
---|
88 | void testTaginfoGeofabrik_broken() {
|
---|
89 | Logging.clearLastErrorAndWarnings();
|
---|
90 | Territories.initializeExternalData("foo", TestUtils.getTestDataRoot() + "taginfo/geofabrik-index-v1-nogeom-broken.json");
|
---|
91 | Map<String, TaginfoRegionalInstance> cache = Territories.taginfoGeofabrikCache;
|
---|
92 | assertTrue(cache.isEmpty());
|
---|
93 | String error = Logging.getLastErrorAndWarnings().get(0);
|
---|
94 | assertTrue(error.contains("W: Failed to parse external taginfo data at "), error);
|
---|
95 | assertTrue(error.contains(": Invalid token=EOF at (line no=3,"), error);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Unit test of {@link Territories#getCustomTags}
|
---|
100 | */
|
---|
101 | @Test
|
---|
102 | void testGetCustomTags() {
|
---|
103 | assertNull(Territories.getCustomTags(null));
|
---|
104 | assertNull(Territories.getCustomTags("foo"));
|
---|
105 | assertEquals("arab", Territories.getCustomTags("BH").get("ldml:nu:ar"));
|
---|
106 | }
|
---|
107 | }
|
---|