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.Assert.assertTrue;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
7 |
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Set;
|
---|
12 | import java.util.TreeMap;
|
---|
13 |
|
---|
14 | import org.junit.Rule;
|
---|
15 | import org.junit.Test;
|
---|
16 | import org.openstreetmap.josm.TestUtils;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
19 |
|
---|
20 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
21 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Unit tests of {@link Territories} class.
|
---|
25 | */
|
---|
26 | public class TerritoriesTest {
|
---|
27 | /**
|
---|
28 | * Test rules.
|
---|
29 | */
|
---|
30 | @Rule
|
---|
31 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
32 | public JOSMTestRules rules = new JOSMTestRules().projection().territories();
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Tests that {@code Territories} satisfies utility class criterias.
|
---|
36 | * @throws ReflectiveOperationException if an error occurs
|
---|
37 | */
|
---|
38 | @Test
|
---|
39 | public void testUtilityClass() throws ReflectiveOperationException {
|
---|
40 | UtilityClassTestUtil.assertUtilityClassWellDefined(Territories.class);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Test of {@link Territories#isIso3166Code} method.
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void testIsIso3166Code() {
|
---|
48 | check("Paris", new LatLon(48.8567, 2.3508), "EU", "FR", "FX");
|
---|
49 | }
|
---|
50 |
|
---|
51 | private static void check(String name, LatLon ll, String... expectedCodes) {
|
---|
52 | for (String e : expectedCodes) {
|
---|
53 | assertTrue(name + " " + e, Territories.isIso3166Code(e, ll));
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Test of {@link Territories#initializeExternalData} - nominal case
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | public void testTaginfoGeofabrik_nominal() {
|
---|
62 | Map<String, TaginfoRegionalInstance> cache = new TreeMap<>();
|
---|
63 | Territories.initializeExternalData(cache, "foo", TestUtils.getTestDataRoot() + "/taginfo/geofabrik-index-v1-nogeom.json");
|
---|
64 | assertEquals(5, cache.size());
|
---|
65 | checkTaginfoInstance(cache.get("AF"), singleton("AF"), "https://taginfo.geofabrik.de/asia/afghanistan/");
|
---|
66 | checkTaginfoInstance(cache.get("AL"), singleton("AL"), "https://taginfo.geofabrik.de/europe/albania/");
|
---|
67 | checkTaginfoInstance(cache.get("CA-AB"), singleton("CA-AB"), "https://taginfo.geofabrik.de/north-america/canada/alberta/");
|
---|
68 | Set<String> israelAndPalestine = new HashSet<>(Arrays.asList("PS", "IL"));
|
---|
69 | checkTaginfoInstance(cache.get("PS"), israelAndPalestine, "https://taginfo.geofabrik.de/asia/israel-and-palestine/");
|
---|
70 | checkTaginfoInstance(cache.get("IL"), israelAndPalestine, "https://taginfo.geofabrik.de/asia/israel-and-palestine/");
|
---|
71 | }
|
---|
72 |
|
---|
73 | private static void checkTaginfoInstance(TaginfoRegionalInstance instance, Set<String> expectedIsoCodes, String expectedUrl) {
|
---|
74 | assertEquals(expectedIsoCodes, instance.getIsoCodes());
|
---|
75 | assertEquals("foo", instance.getSuffix());
|
---|
76 | assertEquals(expectedUrl, instance.getUrl());
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Test of {@link Territories#initializeExternalData} - broken contents
|
---|
81 | */
|
---|
82 | @Test
|
---|
83 | public void testTaginfoGeofabrik_broken() {
|
---|
84 | Map<String, TaginfoRegionalInstance> cache = new TreeMap<>();
|
---|
85 | Logging.clearLastErrorAndWarnings();
|
---|
86 | Territories.initializeExternalData(cache, "foo", TestUtils.getTestDataRoot() + "taginfo/geofabrik-index-v1-nogeom-broken.json");
|
---|
87 | assertTrue(cache.isEmpty());
|
---|
88 | assertEquals("W: Failed to parse external taginfo data at test/data/taginfo/geofabrik-index-v1-nogeom-broken.json: " +
|
---|
89 | "Invalid token=EOF at (line no=3, column no=49, offset=97). Expected tokens are: " +
|
---|
90 | "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]",
|
---|
91 | Logging.getLastErrorAndWarnings().get(0));
|
---|
92 | }
|
---|
93 | }
|
---|