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