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