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

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

see #16567 - revert parallel execution of TaggingPresetPreferenceTestIT - huge instability

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