1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.map;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Set;
|
---|
8 | import java.util.regex.Matcher;
|
---|
9 | import java.util.regex.Pattern;
|
---|
10 | import java.util.stream.Collectors;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
|
---|
13 |
|
---|
14 | abstract class AbstractExtendedSourceEntryTestCase {
|
---|
15 |
|
---|
16 | private static final Pattern RESOURCE_PATTERN = Pattern.compile("resource://(.+)");
|
---|
17 | private static final Pattern JOSM_WIKI_PATTERN = Pattern.compile("https://josm.openstreetmap.de/josmfile\\?page=(.+)&zip=1");
|
---|
18 | private static final Pattern GITHUB_PATTERN = Pattern.compile("https://raw.githubusercontent.com/([^/]+)/([^/]+)/([^/]+)/(.+)");
|
---|
19 |
|
---|
20 | protected static final List<String> errorsToIgnore = new ArrayList<>();
|
---|
21 |
|
---|
22 | /** Entry to test */
|
---|
23 | protected final ExtendedSourceEntry source;
|
---|
24 | protected final List<String> ignoredErrors = new ArrayList<>();
|
---|
25 |
|
---|
26 | protected AbstractExtendedSourceEntryTestCase(ExtendedSourceEntry source) {
|
---|
27 | this.source = source;
|
---|
28 | }
|
---|
29 |
|
---|
30 | protected static List<Object[]> getTestParameters(Collection<ExtendedSourceEntry> entries) throws Exception {
|
---|
31 | return entries.stream().map(x -> new Object[] {x.getDisplayName(), cleanUrl(x.url), x}).collect(Collectors.toList());
|
---|
32 | }
|
---|
33 |
|
---|
34 | private static String cleanUrl(String url) {
|
---|
35 | Matcher wiki = JOSM_WIKI_PATTERN.matcher(url);
|
---|
36 | if (wiki.matches()) {
|
---|
37 | return "https://josm.openstreetmap.de/wiki/" + wiki.group(1);
|
---|
38 | }
|
---|
39 | Matcher github = GITHUB_PATTERN.matcher(url);
|
---|
40 | if (github.matches()) {
|
---|
41 | return String.format("https://github.com/%s/%s/blob/%s/%s", github.group(1), github.group(2), github.group(3), github.group(4));
|
---|
42 | }
|
---|
43 | Matcher resource = RESOURCE_PATTERN.matcher(url);
|
---|
44 | if (resource.matches()) {
|
---|
45 | return "https://josm.openstreetmap.de/browser/trunk/" + resource.group(1);
|
---|
46 | }
|
---|
47 | return url;
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected final void handleException(Exception e, Set<String> errors) {
|
---|
51 | e.printStackTrace();
|
---|
52 | String s = source.url + " => " + e.toString();
|
---|
53 | if (isIgnoredSubstring(s)) {
|
---|
54 | ignoredErrors.add(s);
|
---|
55 | } else {
|
---|
56 | errors.add(s);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected boolean isIgnoredSubstring(String substring) {
|
---|
61 | return errorsToIgnore.parallelStream().anyMatch(x -> substring.contains(x) || source.url.contains(x));
|
---|
62 | }
|
---|
63 | }
|
---|