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

Last change on this file since 14485 was 14485, checked in by stoecker, 5 years ago

handle {lang} for EULA, see #16073

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