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 | protected static List<Object[]> getTestParameters(Collection<ExtendedSourceEntry> entries) throws Exception {
|
---|
23 | return entries.stream().map(x -> new Object[] {x.getDisplayName(), cleanUrl(x.url), x}).collect(Collectors.toList());
|
---|
24 | }
|
---|
25 |
|
---|
26 | private static String cleanUrl(String url) {
|
---|
27 | Matcher wiki = JOSM_WIKI_PATTERN.matcher(url);
|
---|
28 | if (wiki.matches()) {
|
---|
29 | return "https://josm.openstreetmap.de/wiki/" + wiki.group(1);
|
---|
30 | }
|
---|
31 | Matcher github = GITHUB_PATTERN.matcher(url);
|
---|
32 | if (github.matches()) {
|
---|
33 | return String.format("https://github.com/%s/%s/blob/%s/%s", github.group(1), github.group(2), github.group(3), github.group(4));
|
---|
34 | }
|
---|
35 | Matcher resource = RESOURCE_PATTERN.matcher(url);
|
---|
36 | if (resource.matches()) {
|
---|
37 | return "https://josm.openstreetmap.de/browser/trunk/" + resource.group(1);
|
---|
38 | }
|
---|
39 | return url;
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected final void handleException(ExtendedSourceEntry source, Exception e, Set<String> errors, List<String> ignoredErrors) {
|
---|
43 | e.printStackTrace();
|
---|
44 | String s = source.url + " => " + e.toString();
|
---|
45 | if (isIgnoredSubstring(source, s)) {
|
---|
46 | ignoredErrors.add(s);
|
---|
47 | } else {
|
---|
48 | errors.add(s);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected boolean isIgnoredSubstring(ExtendedSourceEntry source, String substring) {
|
---|
53 | return errorsToIgnore.parallelStream().anyMatch(x -> substring.contains(x) || source.url.contains(x));
|
---|
54 | }
|
---|
55 | }
|
---|