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

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

see #14817 - better assertion error message, include preset source url

  • Property svn:eol-style set to native
File size: 4.3 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;
6
7import java.io.IOException;
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.Map;
12import java.util.Set;
13
14import org.junit.BeforeClass;
15import org.junit.Rule;
16import org.junit.Test;
17import org.junit.rules.Timeout;
18import org.openstreetmap.josm.JOSMFixture;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
21import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
22import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
23import org.openstreetmap.josm.tools.ImageProvider;
24import org.xml.sax.SAXException;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Integration tests of {@link TaggingPresetPreference} class.
30 */
31public class TaggingPresetPreferenceTestIT {
32
33 /**
34 * Global timeout applied to all test methods.
35 */
36 @Rule
37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
38 public Timeout globalTimeout = Timeout.seconds(10*60);
39
40 /**
41 * Setup test.
42 */
43 @BeforeClass
44 public static void setUpBeforeClass() {
45 JOSMFixture.createUnitTestFixture().init();
46 }
47
48 /**
49 * Test that available tagging presets are valid.
50 * @throws Exception in case of error
51 */
52 @Test
53 public void testValidityOfAvailablePresets() throws Exception {
54 ImageProvider.clearCache();
55 Collection<ExtendedSourceEntry> sources = new TaggingPresetPreference.TaggingPresetSourceEditor()
56 .loadAndGetAvailableSources();
57 assertFalse(sources.isEmpty());
58 // Double traditional timeouts to avoid random problems
59 Main.pref.putInteger("socket.timeout.connect", 30);
60 Main.pref.putInteger("socket.timeout.read", 60);
61 Map<Object, Throwable> allErrors = new HashMap<>();
62 Set<String> allMessages = new HashSet<>();
63 for (ExtendedSourceEntry source : sources) {
64 System.out.println(source.url);
65 try {
66 testPresets(allMessages, source);
67 } catch (IOException e) {
68 try {
69 Main.warn(e);
70 // try again in case of temporary network error
71 testPresets(allMessages, source);
72 } catch (SAXException | IOException e1) {
73 e.printStackTrace();
74 // ignore frequent network errors with www.freietonne.de causing too much Jenkins failures
75 if (!source.url.contains("www.freietonne.de")) {
76 allErrors.put(source.url, e1);
77 }
78 System.out.println(" => KO");
79 }
80 } catch (SAXException | IllegalArgumentException e) {
81 e.printStackTrace();
82 if (!source.url.contains("yopaseopor/")) {
83 // ignore https://raw.githubusercontent.com/yopaseopor/traffic_signs_preset_JOSM cause too much errors
84 allErrors.put(source.url, e);
85 }
86 System.out.println(" => KO");
87 }
88 }
89 ImageProvider.clearCache();
90 assertTrue(allErrors.toString(), allErrors.isEmpty());
91 assertTrue(allMessages.toString(), allMessages.isEmpty());
92 }
93
94 private static void testPresets(Set<String> allMessages, ExtendedSourceEntry source) throws SAXException, IOException {
95 Collection<TaggingPreset> presets = TaggingPresetReader.readAll(source.url, true);
96 assertFalse(presets.isEmpty());
97 Collection<String> errorsAndWarnings = Main.getLastErrorAndWarnings();
98 boolean error = false;
99 for (String message : errorsAndWarnings) {
100 if (message.contains(TaggingPreset.PRESET_ICON_ERROR_MSG_PREFIX)) {
101 error = true;
102 // ignore https://github.com/yopaseopor/traffic_signs_preset_JOSM because of far too frequent missing icons errors
103 if (!source.url.contains("yopaseopor/traffic_signs")) {
104 allMessages.add(message);
105 }
106 }
107 }
108 System.out.println(error ? " => KO" : " => OK");
109 if (error) {
110 Main.clearLastErrorAndWarnings();
111 }
112 }
113}
Note: See TracBrowser for help on using the repository browser.