1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.imagery;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.net.MalformedURLException;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Map;
|
---|
15 | import java.util.Set;
|
---|
16 |
|
---|
17 | import org.junit.Rule;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
20 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
21 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
22 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
23 | import org.openstreetmap.josm.tools.HttpClient.Response;
|
---|
24 | import org.openstreetmap.josm.tools.Logging;
|
---|
25 |
|
---|
26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Integration tests of {@link ImageryPreference} class.
|
---|
30 | */
|
---|
31 | public 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 | }
|
---|