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

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

fix #17240 - Presets: verify that wiki links are no redirect

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