Changeset 17474 in josm
- Timestamp:
- 2021-01-21T23:33:21+01:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java
r17398 r17474 2 2 package org.openstreetmap.josm.gui.preferences.imagery; 3 3 4 import static java.util.Collections.singletonList; 5 import static java.util.Collections.synchronizedList; 6 import static java.util.Collections.synchronizedMap; 7 import static java.util.stream.Collectors.toList; 4 8 import static org.junit.jupiter.api.Assertions.assertFalse; 5 9 import static org.junit.jupiter.api.Assertions.assertTrue; … … 11 15 import java.nio.charset.StandardCharsets; 12 16 import java.util.ArrayList; 13 import java.util.Collections;14 17 import java.util.List; 15 18 import java.util.Locale; 16 19 import java.util.Map; 20 import java.util.Objects; 17 21 import java.util.Optional; 18 22 import java.util.TreeMap; 19 23 import java.util.concurrent.TimeUnit; 20 import java.util.stream.Collectors;21 24 import java.util.stream.Stream; 22 25 … … 86 89 87 90 /** Entry to test */ 88 private final Map<String, Map<ImageryInfo, List<String>>> errors = Collections.synchronizedMap(new TreeMap<>());89 private final Map<String, Map<ImageryInfo, List<String>>> ignoredErrors = Collections.synchronizedMap(new TreeMap<>());90 private static final Map<String, byte[]> workingURLs = Collections.synchronizedMap(new TreeMap<>());91 private final Map<String, Map<ImageryInfo, List<String>>> errors = synchronizedMap(new TreeMap<>()); 92 private final Map<String, Map<ImageryInfo, List<String>>> ignoredErrors = synchronizedMap(new TreeMap<>()); 93 private static final Map<String, byte[]> workingURLs = synchronizedMap(new TreeMap<>()); 91 94 92 95 private static TMSCachedTileLoaderJob helper; … … 124 127 return ImageryLayerInfo.instance.getDefaultLayers() 125 128 .stream() 129 //.filter(i -> "OGDLidarZH-DOM-2017".equals(i.getId())) // enable to test one specific entry 126 130 .map(i -> Arguments.of(i.getCountryCode().isEmpty() ? i.getId() : i.getCountryCode() + '-' + i.getId(), i)) 127 .collect( Collectors.toList());131 .collect(toList()); 128 132 } 129 133 … … 144 148 145 149 private static boolean addError(Map<String, Map<ImageryInfo, List<String>>> map, ImageryInfo info, String errorMsg) { 146 return map.computeIfAbsent(info.getCountryCode(), x -> Collections.synchronizedMap(new TreeMap<>()))147 .computeIfAbsent(info, x -> Collections.synchronizedList(new ArrayList<>()))150 return map.computeIfAbsent(info.getCountryCode(), x -> synchronizedMap(new TreeMap<>())) 151 .computeIfAbsent(info, x -> synchronizedList(new ArrayList<>())) 148 152 .add(errorMsg); 149 153 } … … 187 191 private void checkLinkUrl(ImageryInfo info, String url) { 188 192 checkUrl(info, url).filter(x -> x.length == 0).ifPresent(x -> addError(info, url + " -> returned empty contents")); 193 } 194 195 private List<String> checkTileUrls(ImageryInfo info, List<AbstractTileSource> tileSources, ICoordinate center, int zoom) 196 throws IOException { 197 List<String> errors = new ArrayList<>(); 198 for (AbstractTileSource tileSource : tileSources) { 199 String error = checkTileUrl(info, tileSource, center, zoom); 200 if (!error.isEmpty()) { 201 errors.add(error); 202 } 203 } 204 return errors; 189 205 } 190 206 … … 307 323 // Some imagery sources do not define tiles at (0,0). So pickup Greenwich Royal Observatory for global sources 308 324 ICoordinate center = CoordinateConversion.llToCoor(bounds != null ? getCenter(bounds) : GREENWICH); 309 AbstractTileSource tileSource = getTileSource(info); 325 List<AbstractTileSource> tileSources = getTileSources(info); 310 326 // test min zoom and try to detect the correct value in case of error 311 327 int maxZoom = info.getMaxZoom() > 0 ? Math.min(DEFAULT_ZOOM, info.getMaxZoom()) : DEFAULT_ZOOM; 312 328 for (int zoom = info.getMinZoom(); zoom < maxZoom; zoom++) { 313 if (!isZoomError(checkTileUrl(info, tileSource, center, zoom))) { 329 if (!isZoomError(checkTileUrls(info, tileSources, center, zoom))) { 314 330 break; 315 331 } … … 317 333 // checking max zoom for real is complex, see https://josm.openstreetmap.de/ticket/16073#comment:27 318 334 if (info.getMaxZoom() > 0 && info.getImageryType() != ImageryType.SCANEX) { 319 checkTileUrl(info, tileSource, center, Utils.clamp(DEFAULT_ZOOM, info.getMinZoom() + 1, info.getMaxZoom())); 320 } 321 } catch (IOException | RuntimeException | WMSGetCapabilitiesException | WMTSGetCapabilitiesExceptione) {335 checkTileUrls(info, tileSources, center, Utils.clamp(DEFAULT_ZOOM, info.getMinZoom() + 1, info.getMaxZoom())); 336 } 337 } catch (IOException | RuntimeException | WMSGetCapabilitiesException e) { 322 338 addError(info, info.getUrl() + ERROR_SEP + e.toString()); 323 339 } … … 328 344 } 329 345 330 private static boolean isZoomError(String error) { 331 String[] parts = error.split(ERROR_SEP, -1); 332 String lastPart = parts.length > 0 ? parts[parts.length - 1].toLowerCase(Locale.ENGLISH) : ""; 333 return lastPart.contains("bbox") 334 || lastPart.contains("bounding box"); 335 } 336 337 private static Projection getProjection(ImageryInfo info) { 338 for (String code : info.getServerProjections()) { 339 Projection proj = Projections.getProjectionByCode(code); 340 if (proj != null) { 341 return proj; 342 } 343 } 344 return ProjectionRegistry.getProjection(); 345 } 346 347 @SuppressWarnings("fallthrough") 348 private static AbstractTileSource getTileSource(ImageryInfo info) 349 throws IOException, WMTSGetCapabilitiesException, WMSGetCapabilitiesException { 346 private static boolean isZoomError(List<String> errors) { 347 return errors.stream().anyMatch(error -> { 348 String[] parts = error.split(ERROR_SEP, -1); 349 String lastPart = parts.length > 0 ? parts[parts.length - 1].toLowerCase(Locale.ENGLISH) : ""; 350 return lastPart.contains("bbox") 351 || lastPart.contains("bounding box"); 352 }); 353 } 354 355 private static List<Projection> getProjections(ImageryInfo info) { 356 List<Projection> projs = info.getServerProjections().stream() 357 .map(Projections::getProjectionByCode).filter(Objects::nonNull).collect(toList()); 358 return projs.isEmpty() ? singletonList(ProjectionRegistry.getProjection()) : projs; 359 } 360 361 private List<AbstractTileSource> getTileSources(ImageryInfo info) 362 throws IOException, WMSGetCapabilitiesException { 350 363 switch (info.getImageryType()) { 351 364 case BING: 352 return new BingAerialTileSource(info); 365 return singletonList(new BingAerialTileSource(info)); 353 366 case SCANEX: 354 return new ScanexTileSource(info); 367 return singletonList(new ScanexTileSource(info)); 355 368 case TMS: 356 return new JosmTemplatedTMSTileSource(info); 369 return singletonList(new JosmTemplatedTMSTileSource(info)); 357 370 case WMS_ENDPOINT: 358 info =convertWmsEndpointToWms(info); // fall-through371 return getWmsTileSources(convertWmsEndpointToWms(info)); 359 372 case WMS: 360 return new TemplatedWMSTileSource(info, getProjection(info));373 return getWmsTileSources(info); 361 374 case WMTS: 362 return new WMTSTileSource(info, getProjection(info));375 return getWmtsTileSources(info); 363 376 default: 364 377 throw new UnsupportedOperationException(info.toString()); 365 378 } 379 } 380 381 private static List<AbstractTileSource> getWmsTileSources(ImageryInfo info) { 382 return getProjections(info).stream().map(proj -> new TemplatedWMSTileSource(info, proj)).collect(toList()); 383 } 384 385 private List<AbstractTileSource> getWmtsTileSources(ImageryInfo info) { 386 return getProjections(info).stream().map(proj -> { 387 try { 388 return new WMTSTileSource(info, proj); 389 } catch (IOException | WMTSGetCapabilitiesException e) { 390 addError(info, info.getUrl() + ERROR_SEP + e.toString()); 391 return null; 392 } 393 }).filter(Objects::nonNull).collect(toList()); 366 394 } 367 395 … … 376 404 boolean hasNoChildren = layer.getChildren().isEmpty(); 377 405 if (hasNoChildren && layer.getName() != null) { 378 return Collections.singletonList(layer);406 return singletonList(layer); 379 407 } else if (!hasNoChildren) { 380 408 return firstLeafLayer(layer.getChildren());
Note:
See TracChangeset
for help on using the changeset viewer.