source: josm/trunk/test/unit/org/openstreetmap/josm/tools/TerritoriesTest.java

Last change on this file was 18799, checked in by taylor.smock, 9 months ago

See r18798: Actually use the @Territories annotation

This also fixes some tests that fail with specific options, but were passing due
to run order.

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