Ignore:
Timestamp:
2018-12-15T19:24:34+01:00 (6 years ago)
Author:
Don-vip
Message:

see #16073 - better detection of bad zoom errors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java

    r14553 r14564  
    1212import java.util.Collections;
    1313import java.util.List;
     14import java.util.Locale;
    1415import java.util.Map;
    1516import java.util.Optional;
     
    137138    }
    138139
    139     private boolean checkTileUrl(ImageryInfo info, AbstractTileSource tileSource, ICoordinate center, int zoom)
     140    private String checkTileUrl(ImageryInfo info, AbstractTileSource tileSource, ICoordinate center, int zoom)
    140141            throws IOException {
    141142        TileXY xy = tileSource.latLonToTileXY(center, zoom);
     
    143144            try {
    144145                String url = tileSource.getTileUrl(zoom, xy.getXIndex(), xy.getYIndex());
    145                 checkUrl(info, url).ifPresent(data -> {
     146                Optional<byte[]> optional = checkUrl(info, url);
     147                String error = "";
     148                if (optional.isPresent()) {
     149                    byte[] data = optional.get();
    146150                    try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
    147151                        if (ImageIO.read(bais) == null) {
    148                             addImageError(info, url, data, zoom, "did not return an image");
     152                            error = addImageError(info, url, data, zoom, "did not return an image");
    149153                        }
    150154                    } catch (IOException e) {
    151                         addImageError(info, url, data, zoom, e.toString());
     155                        error = addImageError(info, url, data, zoom, e.toString());
    152156                        Logging.trace(e);
    153157                    }
    154                 });
    155                 // Determines if this is a success (no error message with current zoom marker)
    156                 return errors.getOrDefault(info.getCountryCode(), Collections.emptyMap())
    157                               .getOrDefault(info, Collections.emptyList())
    158                               .stream().noneMatch(e -> e.contains(zoomMarker(zoom)));
     158                }
     159                return error;
    159160            } catch (IOException e) {
    160161                // Try up to three times max to allow Bing source to initialize itself
     
    171172            }
    172173        }
    173         return false;
     174        return "";
    174175    }
    175176
     
    178179    }
    179180
    180     private void addImageError(ImageryInfo info, String url, byte[] data, int zoom, String defaultMessage) {
     181    private String addImageError(ImageryInfo info, String url, byte[] data, int zoom, String defaultMessage) {
    181182        // Check if we have received an error message
    182183        String error = helper.detectErrorMessage(new String(data, StandardCharsets.UTF_8));
    183         addError(info, url + zoomMarker(zoom) + (error != null ? error.split("\\n")[0] : defaultMessage));
     184        String errorMsg = url + zoomMarker(zoom) + (error != null ? error.split("\\n")[0] : defaultMessage);
     185        addError(info, errorMsg);
     186        return errorMsg;
    184187    }
    185188
     
    258261            int maxZoom = info.getMaxZoom() > 0 ? Math.min(DEFAULT_ZOOM, info.getMaxZoom()) : DEFAULT_ZOOM;
    259262            for (int zoom = info.getMinZoom(); zoom < maxZoom; zoom++) {
    260                 if (checkTileUrl(info, tileSource, center, zoom)) {
     263                if (!isZoomError(checkTileUrl(info, tileSource, center, zoom))) {
    261264                    break;
    262265                }
     
    273276            checkEntry(mirror);
    274277        }
     278    }
     279
     280    private static boolean isZoomError(String error) {
     281        String[] parts = error.split(" -> ");
     282        String lastPart = parts.length > 0 ? parts[parts.length - 1].toLowerCase(Locale.ENGLISH) : "";
     283        return lastPart.contains("bbox")
     284            || lastPart.contains("bounding box");
    275285    }
    276286
Note: See TracChangeset for help on using the changeset viewer.