source: josm/trunk/test/unit/org/openstreetmap/josm/gui/preferences/AbstractExtendedSourceEntryTestCase.java@ 17536

Last change on this file since 17536 was 17531, checked in by Don-vip, 3 years ago

see #16567 - consistency between extended source entry integration tests

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