source: josm/trunk/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java@ 14497

Last change on this file since 14497 was 14497, checked in by Don-vip, 5 years ago

fix #17060, see #16073 - update integration test

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.imagery;
3
4import static org.junit.Assert.assertTrue;
5
6import java.io.IOException;
7import java.net.URL;
8import java.util.ArrayList;
9import java.util.Collections;
10import java.util.HashSet;
11import java.util.List;
12import java.util.Map;
13import java.util.Set;
14import java.util.TreeMap;
15
16import org.junit.Rule;
17import org.junit.Test;
18import org.openstreetmap.josm.data.imagery.ImageryInfo;
19import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21import org.openstreetmap.josm.tools.HttpClient;
22import org.openstreetmap.josm.tools.HttpClient.Response;
23import org.openstreetmap.josm.tools.Logging;
24
25import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26
27/**
28 * Integration tests of {@link ImageryPreference} class.
29 */
30public class ImageryPreferenceTestIT {
31
32 /**
33 * Setup rule
34 */
35 @Rule
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules test = new JOSMTestRules().https().timeout(10000*60);
38
39 private final Map<String, Map<ImageryInfo, List<String>>> errors = Collections.synchronizedMap(new TreeMap<>());
40 private final Set<String> workingURLs = Collections.synchronizedSet(new HashSet<>());
41
42 private boolean addError(ImageryInfo info, String error) {
43 return errors.computeIfAbsent(info.getCountryCode(), x -> Collections.synchronizedMap(new TreeMap<>()))
44 .computeIfAbsent(info, x -> Collections.synchronizedList(new ArrayList<>()))
45 .add(error);
46 }
47
48 private void checkUrl(ImageryInfo info, String url) {
49 if (url != null && !workingURLs.contains(url)) {
50 try {
51 Response response = HttpClient.create(new URL(url)).connect();
52 if (response.getResponseCode() >= 400) {
53 addError(info, url + " -> HTTP " + response.getResponseCode());
54 } else if (response.getResponseCode() >= 300) {
55 Logging.warn(url + " -> HTTP " + response.getResponseCode());
56 } else {
57 workingURLs.add(url);
58 }
59 response.disconnect();
60 } catch (IOException e) {
61 addError(info, url + " -> " + e);
62 }
63 }
64 }
65
66 private void checkEntry(ImageryInfo info) {
67 Logging.info("Checking "+ info);
68
69 if (info.getAttributionImageRaw() != null && info.getAttributionImage() == null) {
70 addError(info, "Can't fetch attribution image: " + info.getAttributionImageRaw());
71 }
72
73 checkUrl(info, info.getAttributionImageURL());
74 checkUrl(info, info.getAttributionLinkURL());
75 String eula = info.getEulaAcceptanceRequired();
76 if (eula != null) {
77 checkUrl(info, eula.replaceAll("\\{lang\\}", ""));
78 }
79 checkUrl(info, info.getPermissionReferenceURL());
80 checkUrl(info, info.getTermsOfUseURL());
81
82 for (ImageryInfo mirror : info.getMirrors()) {
83 checkEntry(mirror);
84 }
85 }
86
87 /**
88 * Test that available imagery entries are valid.
89 * @throws Exception in case of error
90 */
91 @Test
92 public void testValidityOfAvailableImageryEntries() throws Exception {
93 ImageryLayerInfo.instance.load(false);
94 ImageryLayerInfo.instance.getDefaultLayers().parallelStream().forEach(this::checkEntry);
95 assertTrue(errors.toString().replaceAll("\\}, ", "\n\\}, ").replaceAll(", ImageryInfo\\{", "\n ,ImageryInfo\\{"),
96 errors.isEmpty());
97 }
98}
Note: See TracBrowser for help on using the repository browser.