Changeset 19697 in josm


Ignore:
Timestamp:
2026-11-21T17:35:19+01:00 (less than one hour ago)
Author:
stoecker
Message:

fix #25045 - adapt loader to properly handle values of country code

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Territories.java

    r19696 r19697  
    202202                        ofNullable(props.getJsonObject("urls")).flatMap(urls ->
    203203                        ofNullable(urls.getString(TAGINFO))).ifPresent(taginfo -> {
    204                             JsonArray iso1 = props.getJsonArray(ISO3166_1_LC);
    205                             JsonArray iso2 = props.getJsonArray(ISO3166_2_LC);
     204                            JsonValue iso1 = props.get(ISO3166_1_LC);
     205                            JsonValue iso2 = props.get(ISO3166_2_LC);
    206206                            if (iso1 != null) {
    207207                                readExternalTaginfo(taginfoGeofabrikCache, taginfo, iso1, source);
    … …  
    219219    }
    220220
    221     private static void readExternalTaginfo(Map<String, TaginfoRegionalInstance> cache, String taginfo, JsonArray jsonCodes, String source) {
    222         Set<String> isoCodes = jsonCodes.getValuesAs(JsonString.class).stream().map(JsonString::getString).collect(Collectors.toSet());
     221    private static void readExternalTaginfo(Map<String, TaginfoRegionalInstance> cache, String taginfo, JsonValue jsonCodes, String source) {
     222        Stream<String> codes;
     223        if (jsonCodes instanceof JsonArray) {
     224            codes = jsonCodes.asJsonArray().stream().filter(JsonString.class::isInstance)
     225                    .map(JsonString.class::cast).map(JsonString::getString);
     226        } else if (jsonCodes instanceof JsonString) {
     227            codes = Stream.of(((JsonString) jsonCodes).getString());
     228        } else {
     229            return;
     230        }
     231        Set<String> isoCodes = codes.flatMap(s -> Arrays.stream(s.split(",")))
     232                .map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toSet());
    223233        isoCodes.forEach(s -> cache.put(s, new TaginfoRegionalInstance(taginfo, isoCodes, source)));
    224234    }
  • trunk/test/unit/org/openstreetmap/josm/tools/TerritoriesTest.java

    r18799 r19697  
    44import static java.util.Collections.singleton;
    55import static org.junit.jupiter.api.Assertions.assertEquals;
     6import static org.junit.jupiter.api.Assertions.assertFalse;
    67import static org.junit.jupiter.api.Assertions.assertNull;
    78import static org.junit.jupiter.api.Assertions.assertTrue;
    … …  
    5152
    5253    /**
    53      * Test of {@link Territories#initializeExternalData} - nominal case
     54     * Test of {@link Territories#initializeExternalData} with ISO codes represented as arrays.
    5455     */
    5556    @Test
    56     void testTaginfoGeofabrik_nominal() {
     57    void testTaginfoGeofabrik_arrays() {
    5758        Territories.initializeExternalData("foo", TestUtils.getTestDataRoot() + "/taginfo/geofabrik-index-v1-nogeom.json");
    5859        Map<String, TaginfoRegionalInstance> cache = Territories.taginfoGeofabrikCache;
    … …  
    6768        assertEquals(1, regionalTaginfo.size());
    6869        checkTaginfoInstance(regionalTaginfo.iterator().next(), singleton("AL"), "https://taginfo.geofabrik.de/europe/albania/");
     70    }
     71
     72    /**
     73     * Test of {@link Territories#initializeExternalData} with ISO codes represented as strings.
     74     */
     75    @Test
     76    void testTaginfoGeofabrik_strings() {
     77        Territories.initializeExternalData("foo", TestUtils.getTestDataRoot() + "/taginfo/geofabrik-index-v1-nogeom-strings.json");
     78        Map<String, TaginfoRegionalInstance> cache = Territories.taginfoGeofabrikCache;
     79        assertEquals(3, cache.size());
     80        assertFalse(cache.containsKey("CA-AB"), "ISO3166-1 must take precedence over ISO3166-2");
     81        assertFalse(cache.containsKey(""), "Empty ISO codes must not be cached");
     82        checkTaginfoInstance(cache.get("AL"), singleton("AL"), "https://taginfo.geofabrik.de/europe/albania/");
     83        Set<String> guernseyAndJersey = new HashSet<>(Arrays.asList("GG", "JE"));
     84        checkTaginfoInstance(cache.get("GG"), guernseyAndJersey, "https://taginfo.geofabrik.de/europe/guernsey-jersey/");
     85        checkTaginfoInstance(cache.get("JE"), guernseyAndJersey, "https://taginfo.geofabrik.de/europe/guernsey-jersey/");
    6986    }
    7087
Note: See TracChangeset for help on using the changeset viewer.