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

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

parameterize integration tests for map paint styles and tagging presets

  • 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.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6import static org.junit.Assume.assumeTrue;
7
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.HashSet;
12import java.util.List;
13import java.util.Locale;
14import java.util.Objects;
15import java.util.Set;
16import java.util.concurrent.ExecutionException;
17import java.util.concurrent.TimeUnit;
18import java.util.concurrent.TimeoutException;
19import java.util.stream.Collectors;
20
21import org.junit.BeforeClass;
22import org.junit.ClassRule;
23import org.junit.Test;
24import org.junit.runner.RunWith;
25import org.junit.runners.Parameterized.Parameters;
26import org.openstreetmap.josm.TestUtils;
27import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
28import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
29import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
30import org.openstreetmap.josm.spi.preferences.Config;
31import org.openstreetmap.josm.testutils.JOSMTestRules;
32import org.openstreetmap.josm.testutils.ParallelParameterized;
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(ParallelParameterized.class)
43public class TaggingPresetPreferenceTestIT {
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*60).parameters();
51
52 /** Entry to test */
53 private final ExtendedSourceEntry source;
54 private final List<String> ignoredErrors = new ArrayList<>();
55 private static final List<String> errorsToIgnore = new ArrayList<>();
56
57 /**
58 * Setup test
59 * @throws IOException in case of I/O error
60 */
61 @BeforeClass
62 public static void beforeClass() throws IOException {
63 errorsToIgnore.addAll(TestUtils.getIgnoredErrorMessages(TaggingPresetPreferenceTestIT.class));
64 // Double traditional timeouts to avoid random problems
65 Config.getPref().putInt("socket.timeout.connect", 30);
66 Config.getPref().putInt("socket.timeout.read", 60);
67 // Make sure error messages are in english
68 Locale.setDefault(Locale.ENGLISH);
69 }
70
71 /**
72 * Returns list of tagging presets to test.
73 * @return list of tagging presets to test
74 * @throws Exception if an error occurs
75 */
76 @Parameters(name = "{0} - {1}")
77 public static List<Object[]> data() throws Exception {
78 ImageProvider.clearCache();
79 return new TaggingPresetPreference.TaggingPresetSourceEditor().loadAndGetAvailableSources().stream()
80 .map(x -> new Object[] {x.getDisplayName(), x.url, x}).collect(Collectors.toList());
81 }
82
83 /**
84 * Constructs a new {@code TaggingPresetPreferenceTestIT}
85 * @param displayName displayed name
86 * @param url URL
87 * @param source source entry to test
88 */
89 public TaggingPresetPreferenceTestIT(String displayName, String url, ExtendedSourceEntry source) {
90 this.source = source;
91 }
92
93 /**
94 * Test that tagging presets are valid.
95 * @throws Exception in case of error
96 */
97 @Test
98 public void testPresetsValidity() throws Exception {
99 Set<String> errors = new HashSet<>();
100 try {
101 testPresets(errors, source);
102 } catch (IOException e) {
103 try {
104 Logging.warn(e);
105 // try again in case of temporary network error
106 testPresets(errors, source);
107 } catch (SAXException | IOException e1) {
108 handleException(e1, errors);
109 }
110 } catch (SAXException | IllegalArgumentException e) {
111 handleException(e, errors);
112 }
113 assertTrue(errors.toString(), errors.isEmpty());
114 assumeTrue(ignoredErrors.toString(), ignoredErrors.isEmpty());
115 }
116
117 private void handleException(Exception e, Set<String> errors) {
118 e.printStackTrace();
119 String s = source.url + " => " + e.toString();
120 if (isIgnoredSubstring(s)) {
121 ignoredErrors.add(s);
122 } else {
123 errors.add(s);
124 }
125 }
126
127 private void testPresets(Set<String> messages, ExtendedSourceEntry source) throws SAXException, IOException {
128 Collection<TaggingPreset> presets = TaggingPresetReader.readAll(source.url, true);
129 assertFalse(presets.isEmpty());
130 // wait for asynchronous icon loading
131 presets.stream().map(TaggingPreset::getIconLoadingTask).filter(Objects::nonNull).forEach(t -> {
132 try {
133 t.get(30, TimeUnit.SECONDS);
134 } catch (InterruptedException | ExecutionException | TimeoutException e) {
135 Logging.error(e);
136 }
137 });
138 Collection<String> errorsAndWarnings = Logging.getLastErrorAndWarnings();
139 boolean error = false;
140 for (String message : errorsAndWarnings) {
141 if (message.contains(TaggingPreset.PRESET_ICON_ERROR_MSG_PREFIX)) {
142 error = true;
143 if (isIgnoredSubstring(message)) {
144 ignoredErrors.add(message);
145 } else {
146 messages.add(message);
147 }
148 }
149 }
150 if (error) {
151 Logging.clearLastErrorAndWarnings();
152 }
153 }
154
155 private static boolean isIgnoredSubstring(String substring) {
156 return errorsToIgnore.parallelStream().anyMatch(x -> substring.contains(x));
157 }
158}
Note: See TracBrowser for help on using the repository browser.