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

Last change on this file since 15098 was 15098, checked in by Don-vip, 5 years ago

nicer test names

  • 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.map;
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
14abstract 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 static boolean isIgnoredSubstring(String substring) {
61 return errorsToIgnore.parallelStream().anyMatch(x -> substring.contains(x));
62 }
63}
Note: See TracBrowser for help on using the repository browser.