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

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

nicer test names

  • Property svn:eol-style set to native
File size: 4.9 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.assumeTrue;
7
8import java.io.IOException;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.List;
12import java.util.Locale;
13import java.util.Objects;
14import java.util.Set;
15import java.util.concurrent.ExecutionException;
16import java.util.concurrent.TimeUnit;
17import java.util.concurrent.TimeoutException;
18
19import org.junit.BeforeClass;
20import org.junit.ClassRule;
21import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.junit.runners.Parameterized;
24import org.junit.runners.Parameterized.Parameters;
25import org.openstreetmap.josm.TestUtils;
26import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
27import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
28import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
29import org.openstreetmap.josm.spi.preferences.Config;
30import org.openstreetmap.josm.testutils.JOSMTestRules;
31import org.openstreetmap.josm.tools.ImageProvider;
32import org.openstreetmap.josm.tools.Logging;
33import org.xml.sax.SAXException;
34
35import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
36
37/**
38 * Integration tests of {@link TaggingPresetPreference} class.
39 */
40@RunWith(Parameterized.class)
41public 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}
Note: See TracBrowser for help on using the repository browser.