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

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

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 5.7 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.Collection;
12import java.util.HashSet;
13import java.util.List;
14import java.util.Locale;
15import java.util.Set;
16
17import org.junit.ClassRule;
18import org.junit.jupiter.api.BeforeAll;
19import org.junit.jupiter.api.Test;
20import org.junit.runner.RunWith;
21import org.junit.runners.Parameterized;
22import org.junit.runners.Parameterized.Parameters;
23import org.openstreetmap.josm.TestUtils;
24import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
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 */
42@RunWith(Parameterized.class)
43class TaggingPresetPreferenceTestIT extends AbstractExtendedSourceEntryTestCase {
44
45 /**
46 * Setup rule
47 */
48 @ClassRule
49 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
50 public static JOSMTestRules test = new JOSMTestRules().https().timeout(10000*120).parameters();
51
52 /**
53 * Setup test
54 * @throws IOException in case of I/O error
55 */
56 @BeforeAll
57 public static void beforeClass() throws IOException {
58 errorsToIgnore.addAll(TestUtils.getIgnoredErrorMessages(TaggingPresetPreferenceTestIT.class));
59 // Double traditional timeouts to avoid random problems
60 Config.getPref().putInt("socket.timeout.connect", 30);
61 Config.getPref().putInt("socket.timeout.read", 60);
62 // Make sure error messages are in english
63 Locale.setDefault(Locale.ENGLISH);
64 }
65
66 /**
67 * Returns list of tagging presets to test.
68 * @return list of tagging presets to test
69 * @throws Exception if an error occurs
70 */
71 @Parameters(name = "{0} - {1}")
72 public static List<Object[]> data() throws Exception {
73 ImageProvider.clearCache();
74 return getTestParameters(new TaggingPresetPreference.TaggingPresetSourceEditor().loadAndGetAvailableSources());
75 }
76
77 /**
78 * Constructs a new {@code TaggingPresetPreferenceTestIT}
79 * @param displayName displayed name
80 * @param url URL
81 * @param source source entry to test
82 */
83 TaggingPresetPreferenceTestIT(String displayName, String url, ExtendedSourceEntry source) {
84 super(source);
85 }
86
87 /**
88 * Test that tagging presets are valid.
89 * @throws Exception in case of error
90 */
91 @Test
92 void testPresetsValidity() throws Exception {
93 assumeFalse(isIgnoredSubstring(source.url));
94 Set<String> errors = new HashSet<>();
95 try {
96 testPresets(errors, source);
97 } catch (IOException e) {
98 try {
99 Logging.warn(e);
100 // try again in case of temporary network error
101 testPresets(errors, source);
102 } catch (SAXException | IOException e1) {
103 handleException(e1, errors);
104 }
105 } catch (SAXException | IllegalArgumentException e) {
106 handleException(e, errors);
107 }
108 assertTrue(errors.isEmpty(), errors::toString);
109 assumeTrue(ignoredErrors.toString(), ignoredErrors.isEmpty());
110 }
111
112 private void testPresets(Set<String> messages, ExtendedSourceEntry source) throws SAXException, IOException {
113 Collection<TaggingPreset> presets = TaggingPresetReader.readAll(source.url, true);
114 assertFalse(presets.isEmpty());
115 TaggingPresetsTest.waitForIconLoading(presets);
116 // check that links are correct and not redirections
117 presets.parallelStream().flatMap(x -> x.data.stream().filter(i -> i instanceof Link).map(i -> ((Link) i).getUrl())).forEach(u -> {
118 try {
119 Response cr = HttpClient.create(new URL(u)).setMaxRedirects(-1).connect();
120 final int code = cr.getResponseCode();
121 if (HttpClient.isRedirect(code)) {
122 addOrIgnoreError(messages, "Found HTTP redirection for " + u + " -> " + code + " -> " + cr.getHeaderField("Location"));
123 } else if (code >= 400) {
124 addOrIgnoreError(messages, "Found HTTP error for " + u + " -> " + code);
125 }
126 } catch (IOException e) {
127 Logging.error(e);
128 }
129 });
130 Collection<String> errorsAndWarnings = Logging.getLastErrorAndWarnings();
131 boolean error = false;
132 for (String message : errorsAndWarnings) {
133 if (message.contains(TaggingPreset.PRESET_ICON_ERROR_MSG_PREFIX)) {
134 error = true;
135 addOrIgnoreError(messages, message);
136 }
137 }
138 if (error) {
139 Logging.clearLastErrorAndWarnings();
140 }
141 }
142
143 void addOrIgnoreError(Set<String> messages, String message) {
144 if (isIgnoredSubstring(message)) {
145 ignoredErrors.add(message);
146 } else {
147 messages.add(message);
148 }
149 }
150}
Note: See TracBrowser for help on using the repository browser.