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.URL;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Set;
|
---|
14 | import java.util.TreeMap;
|
---|
15 |
|
---|
16 | import org.junit.Rule;
|
---|
17 | import org.junit.Test;
|
---|
18 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
19 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
22 | import org.openstreetmap.josm.tools.HttpClient.Response;
|
---|
23 | import org.openstreetmap.josm.tools.Logging;
|
---|
24 |
|
---|
25 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Integration tests of {@link ImageryPreference} class.
|
---|
29 | */
|
---|
30 | public 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 | }
|
---|