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.assumeTrue;
|
---|
7 |
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Locale;
|
---|
13 | import java.util.Objects;
|
---|
14 | import java.util.Set;
|
---|
15 | import java.util.concurrent.ExecutionException;
|
---|
16 | import java.util.concurrent.TimeUnit;
|
---|
17 | import java.util.concurrent.TimeoutException;
|
---|
18 |
|
---|
19 | import org.junit.BeforeClass;
|
---|
20 | import org.junit.ClassRule;
|
---|
21 | import org.junit.Test;
|
---|
22 | import org.junit.runner.RunWith;
|
---|
23 | import org.junit.runners.Parameterized;
|
---|
24 | import org.junit.runners.Parameterized.Parameters;
|
---|
25 | import org.openstreetmap.josm.TestUtils;
|
---|
26 | import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
|
---|
27 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
28 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
|
---|
29 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
30 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
32 | import org.openstreetmap.josm.tools.Logging;
|
---|
33 | import org.xml.sax.SAXException;
|
---|
34 |
|
---|
35 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Integration tests of {@link TaggingPresetPreference} class.
|
---|
39 | */
|
---|
40 | @RunWith(Parameterized.class)
|
---|
41 | public class TaggingPresetPreferenceTestIT extends AbstractExtendedSourceEntryTestCase {
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Setup rule
|
---|
45 | */
|
---|
46 | @ClassRule
|
---|
47 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
48 | public static JOSMTestRules test = new JOSMTestRules().https().timeout(10000*60).parameters();
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Setup test
|
---|
52 | * @throws IOException in case of I/O error
|
---|
53 | */
|
---|
54 | @BeforeClass
|
---|
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 | @Parameters(name = "{0} - {1}")
|
---|
70 | public static List<Object[]> data() throws Exception {
|
---|
71 | ImageProvider.clearCache();
|
---|
72 | return getTestParameters(new TaggingPresetPreference.TaggingPresetSourceEditor().loadAndGetAvailableSources());
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Constructs a new {@code TaggingPresetPreferenceTestIT}
|
---|
77 | * @param displayName displayed name
|
---|
78 | * @param url URL
|
---|
79 | * @param source source entry to test
|
---|
80 | */
|
---|
81 | public TaggingPresetPreferenceTestIT(String displayName, String url, ExtendedSourceEntry source) {
|
---|
82 | super(source);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Test that tagging presets are valid.
|
---|
87 | * @throws Exception in case of error
|
---|
88 | */
|
---|
89 | @Test
|
---|
90 | public void testPresetsValidity() throws Exception {
|
---|
91 | Set<String> errors = new HashSet<>();
|
---|
92 | try {
|
---|
93 | testPresets(errors, source);
|
---|
94 | } catch (IOException e) {
|
---|
95 | try {
|
---|
96 | Logging.warn(e);
|
---|
97 | // try again in case of temporary network error
|
---|
98 | testPresets(errors, source);
|
---|
99 | } catch (SAXException | IOException e1) {
|
---|
100 | handleException(e1, errors);
|
---|
101 | }
|
---|
102 | } catch (SAXException | IllegalArgumentException e) {
|
---|
103 | handleException(e, errors);
|
---|
104 | }
|
---|
105 | assertTrue(errors.toString(), errors.isEmpty());
|
---|
106 | assumeTrue(ignoredErrors.toString(), ignoredErrors.isEmpty());
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void testPresets(Set<String> messages, ExtendedSourceEntry source) throws SAXException, IOException {
|
---|
110 | Collection<TaggingPreset> presets = TaggingPresetReader.readAll(source.url, true);
|
---|
111 | assertFalse(presets.isEmpty());
|
---|
112 | // wait for asynchronous icon loading
|
---|
113 | presets.stream().map(TaggingPreset::getIconLoadingTask).filter(Objects::nonNull).forEach(t -> {
|
---|
114 | try {
|
---|
115 | t.get(30, TimeUnit.SECONDS);
|
---|
116 | } catch (InterruptedException | ExecutionException | TimeoutException e) {
|
---|
117 | Logging.error(e);
|
---|
118 | }
|
---|
119 | });
|
---|
120 | Collection<String> errorsAndWarnings = Logging.getLastErrorAndWarnings();
|
---|
121 | boolean error = false;
|
---|
122 | for (String message : errorsAndWarnings) {
|
---|
123 | if (message.contains(TaggingPreset.PRESET_ICON_ERROR_MSG_PREFIX)) {
|
---|
124 | error = true;
|
---|
125 | if (isIgnoredSubstring(message)) {
|
---|
126 | ignoredErrors.add(message);
|
---|
127 | } else {
|
---|
128 | messages.add(message);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | if (error) {
|
---|
133 | Logging.clearLastErrorAndWarnings();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|