1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.map;
|
---|
3 |
|
---|
4 | import static org.junit.Assume.assumeFalse;
|
---|
5 | import static org.junit.Assume.assumeTrue;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Locale;
|
---|
16 | import java.util.Set;
|
---|
17 |
|
---|
18 | import org.junit.jupiter.api.BeforeAll;
|
---|
19 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
20 | import org.junit.jupiter.params.ParameterizedTest;
|
---|
21 | import org.junit.jupiter.params.provider.MethodSource;
|
---|
22 | import org.openstreetmap.josm.TestUtils;
|
---|
23 | import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
25 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
|
---|
26 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetsTest;
|
---|
27 | import org.openstreetmap.josm.gui.tagging.presets.items.Link;
|
---|
28 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
29 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
30 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
31 | import org.openstreetmap.josm.tools.HttpClient.Response;
|
---|
32 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
33 | import org.openstreetmap.josm.tools.Logging;
|
---|
34 | import org.xml.sax.SAXException;
|
---|
35 |
|
---|
36 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Integration tests of {@link TaggingPresetPreference} class.
|
---|
40 | */
|
---|
41 | class TaggingPresetPreferenceTestIT extends AbstractExtendedSourceEntryTestCase {
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Setup rule
|
---|
45 | */
|
---|
46 | @RegisterExtension
|
---|
47 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
48 | public static JOSMTestRules test = new JOSMTestRules().https().timeout(10000*120).parameters();
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Setup test
|
---|
52 | * @throws IOException in case of I/O error
|
---|
53 | */
|
---|
54 | @BeforeAll
|
---|
55 | public static void beforeClass() throws IOException {
|
---|
56 | errorsToIgnore.addAll(TestUtils.getIgnoredErrorMessages(TaggingPresetPreferenceTestIT.class));
|
---|
57 | // Double traditional timeouts to avoid random problems
|
---|
58 | Config.getPref().putInt("socket.timeout.connect", 30);
|
---|
59 | Config.getPref().putInt("socket.timeout.read", 60);
|
---|
60 | // Make sure error messages are in english
|
---|
61 | Locale.setDefault(Locale.ENGLISH);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Returns list of tagging presets to test.
|
---|
66 | * @return list of tagging presets to test
|
---|
67 | * @throws Exception if an error occurs
|
---|
68 | */
|
---|
69 | public static List<Object[]> data() throws Exception {
|
---|
70 | ImageProvider.clearCache();
|
---|
71 | return getTestParameters(new TaggingPresetPreference.TaggingPresetSourceEditor().loadAndGetAvailableSources());
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Test that tagging presets are valid.
|
---|
76 | * @param displayName displayed name
|
---|
77 | * @param url URL
|
---|
78 | * @param source source entry to test
|
---|
79 | * @throws Exception in case of error
|
---|
80 | */
|
---|
81 | @ParameterizedTest(name = "{0} - {1}")
|
---|
82 | @MethodSource("data")
|
---|
83 | void testPresetsValidity(String displayName, String url, ExtendedSourceEntry source) throws Exception {
|
---|
84 | assumeFalse(isIgnoredSubstring(source, source.url));
|
---|
85 | List<String> ignoredErrors = new ArrayList<>();
|
---|
86 | Set<String> errors = new HashSet<>();
|
---|
87 | try {
|
---|
88 | testPresets(errors, source, ignoredErrors);
|
---|
89 | } catch (IOException e) {
|
---|
90 | try {
|
---|
91 | Logging.warn(e);
|
---|
92 | // try again in case of temporary network error
|
---|
93 | testPresets(errors, source, ignoredErrors);
|
---|
94 | } catch (SAXException | IOException e1) {
|
---|
95 | handleException(source, e1, errors, ignoredErrors);
|
---|
96 | }
|
---|
97 | } catch (SAXException | IllegalArgumentException e) {
|
---|
98 | handleException(source, e, errors, ignoredErrors);
|
---|
99 | }
|
---|
100 | // #16567 - Shouldn't be necessary to print displayName if Ant worked properly
|
---|
101 | // See https://josm.openstreetmap.de/ticket/16567#comment:53
|
---|
102 | // See https://bz.apache.org/bugzilla/show_bug.cgi?id=64564
|
---|
103 | // See https://github.com/apache/ant/pull/121
|
---|
104 | assertTrue(errors.isEmpty(), displayName + " => " + errors);
|
---|
105 | assumeTrue(ignoredErrors.toString(), ignoredErrors.isEmpty());
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void testPresets(Set<String> messages, ExtendedSourceEntry source, List<String> ignoredErrors)
|
---|
109 | throws SAXException, IOException {
|
---|
110 | Collection<TaggingPreset> presets = TaggingPresetReader.readAll(source.url, true);
|
---|
111 | assertFalse(presets.isEmpty());
|
---|
112 | TaggingPresetsTest.waitForIconLoading(presets);
|
---|
113 | // check that links are correct and not redirections
|
---|
114 | presets.parallelStream().flatMap(x -> x.data.stream().filter(i -> i instanceof Link).map(i -> ((Link) i).getUrl())).forEach(u -> {
|
---|
115 | try {
|
---|
116 | Response cr = HttpClient.create(new URL(u)).setMaxRedirects(-1).connect();
|
---|
117 | final int code = cr.getResponseCode();
|
---|
118 | if (HttpClient.isRedirect(code)) {
|
---|
119 | addOrIgnoreError(source, messages,
|
---|
120 | "Found HTTP redirection for " + u + " -> " + code + " -> " + cr.getHeaderField("Location"), ignoredErrors);
|
---|
121 | } else if (code >= 400) {
|
---|
122 | addOrIgnoreError(source, messages, "Found HTTP error for " + u + " -> " + code, ignoredErrors);
|
---|
123 | }
|
---|
124 | } catch (IOException e) {
|
---|
125 | Logging.error(e);
|
---|
126 | }
|
---|
127 | });
|
---|
128 | Collection<String> errorsAndWarnings = Logging.getLastErrorAndWarnings();
|
---|
129 | boolean error = false;
|
---|
130 | for (String message : errorsAndWarnings) {
|
---|
131 | if (message.contains(TaggingPreset.PRESET_ICON_ERROR_MSG_PREFIX)) {
|
---|
132 | error = true;
|
---|
133 | addOrIgnoreError(source, messages, message, ignoredErrors);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | if (error) {
|
---|
137 | Logging.clearLastErrorAndWarnings();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | void addOrIgnoreError(ExtendedSourceEntry source, Set<String> messages, String message, List<String> ignoredErrors) {
|
---|
142 | if (isIgnoredSubstring(source, message)) {
|
---|
143 | ignoredErrors.add(message);
|
---|
144 | } else {
|
---|
145 | messages.add(message);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|