Changeset 14565 in josm


Ignore:
Timestamp:
2018-12-15T21:00:08+01:00 (3 months ago)
Author:
Don-vip
Message:

see #16073 - detection of json errors

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java

    r14535 r14565  
    5151    static final Pattern SERVICE_EXCEPTION_PATTERN = Pattern.compile("(?s).+<ServiceException[^>]*>(.+)</ServiceException>.+");
    5252    static final Pattern CDATA_PATTERN = Pattern.compile("(?s)\\s*<!\\[CDATA\\[(.+)\\]\\]>\\s*");
     53    static final Pattern JSON_PATTERN = Pattern.compile("\\{\"message\":\"(.+)\"\\}");
    5354    protected final Tile tile;
    5455    private volatile URL url;
     
    321322    @Override
    322323    public String detectErrorMessage(String data) {
    323         Matcher m = SERVICE_EXCEPTION_PATTERN.matcher(data);
    324         return m.matches() ? removeCdata(Utils.strip(m.group(1))) : super.detectErrorMessage(data);
     324        Matcher xml = SERVICE_EXCEPTION_PATTERN.matcher(data);
     325        Matcher json = JSON_PATTERN.matcher(data);
     326        return xml.matches() ? removeCdata(Utils.strip(xml.group(1)))
     327            : json.matches() ? Utils.strip(json.group(1))
     328            : super.detectErrorMessage(data);
    325329    }
    326330
  • trunk/test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java

    r14311 r14565  
    174174    }
    175175
     176    /**
     177     * Tests that {@code TMSCachedTileLoaderJob#JSON_PATTERN} is correct.
     178     */
     179    @Test
     180    public void testJsonPattern() {
     181        testJson("Tile does not exist",
     182                "{\"message\":\"Tile does not exist\"}");
     183    }
     184
    176185    private static void testServiceException(String expected, String xml) {
    177186        test(TMSCachedTileLoaderJob.SERVICE_EXCEPTION_PATTERN, expected, xml);
     
    182191    }
    183192
    184     private static void test(Pattern pattern, String expected, String xml) {
    185         Matcher m = pattern.matcher(xml);
    186         assertTrue(xml, m.matches());
     193    private static void testJson(String expected, String json) {
     194        test(TMSCachedTileLoaderJob.JSON_PATTERN, expected, json);
     195    }
     196
     197    private static void test(Pattern pattern, String expected, String text) {
     198        Matcher m = pattern.matcher(text);
     199        assertTrue(text, m.matches());
    187200        assertEquals(expected, Utils.strip(m.group(1)));
    188201    }
Note: See TracChangeset for help on using the changeset viewer.