1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.map;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 | import static org.junit.Assume.assumeFalse;
|
---|
7 | import static org.junit.Assume.assumeTrue;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Locale;
|
---|
15 | import java.util.Set;
|
---|
16 |
|
---|
17 | import org.junit.BeforeClass;
|
---|
18 | import org.junit.ClassRule;
|
---|
19 | import org.junit.Test;
|
---|
20 | import org.junit.runner.RunWith;
|
---|
21 | import org.junit.runners.Parameterized;
|
---|
22 | import org.junit.runners.Parameterized.Parameters;
|
---|
23 | import org.openstreetmap.josm.TestUtils;
|
---|
24 | import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
|
---|
25 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
26 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
|
---|
27 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetsTest;
|
---|
28 | import org.openstreetmap.josm.gui.tagging.presets.items.Link;
|
---|
29 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
30 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
31 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
32 | import org.openstreetmap.josm.tools.HttpClient.Response;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
34 | import org.openstreetmap.josm.tools.Logging;
|
---|
35 | import org.xml.sax.SAXException;
|
---|
36 |
|
---|
37 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Integration tests of {@link TaggingPresetPreference} class.
|
---|
41 | */
|
---|
42 | @RunWith(Parameterized.class)
|
---|
43 | public class 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 | @BeforeClass
|
---|
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 | public 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 | public 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.toString(), errors.isEmpty());
|
---|
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 | }
|
---|