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 | /**
|
---|
25 | * Integration tests of {@link TaggingPresetPreference} class.
|
---|
26 | */
|
---|
27 | public class TaggingPresetPreferenceTestIT {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Global timeout applied to all test methods.
|
---|
31 | */
|
---|
32 | @Rule
|
---|
33 | public Timeout globalTimeout = Timeout.seconds(10*60);
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Setup test.
|
---|
37 | */
|
---|
38 | @BeforeClass
|
---|
39 | public static void setUpBeforeClass() {
|
---|
40 | JOSMFixture.createUnitTestFixture().init();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Test that available tagging presets are valid.
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void testValidityOfAvailablePresets() {
|
---|
48 | Collection<ExtendedSourceEntry> sources = new TaggingPresetPreference.TaggingPresetSourceEditor()
|
---|
49 | .loadAndGetAvailableSources();
|
---|
50 | assertFalse(sources.isEmpty());
|
---|
51 | // Double traditional timeouts to avoid random problems
|
---|
52 | Main.pref.putInteger("socket.timeout.connect", 30);
|
---|
53 | Main.pref.putInteger("socket.timeout.read", 60);
|
---|
54 | Collection<Throwable> allErrors = new ArrayList<>();
|
---|
55 | Set<String> allMessages = new HashSet<>();
|
---|
56 | for (ExtendedSourceEntry source : sources) {
|
---|
57 | System.out.println(source.url);
|
---|
58 | try {
|
---|
59 | testPresets(allMessages, source);
|
---|
60 | } catch (IOException e) {
|
---|
61 | try {
|
---|
62 | Main.warn(e);
|
---|
63 | // try again in case of temporary network error
|
---|
64 | testPresets(allMessages, source);
|
---|
65 | } catch (SAXException | IOException e1) {
|
---|
66 | e.printStackTrace();
|
---|
67 | allErrors.add(e1);
|
---|
68 | System.out.println(" => KO");
|
---|
69 | }
|
---|
70 | } catch (SAXException e) {
|
---|
71 | e.printStackTrace();
|
---|
72 | allErrors.add(e);
|
---|
73 | System.out.println(" => KO");
|
---|
74 | }
|
---|
75 | }
|
---|
76 | assertTrue(allErrors.isEmpty());
|
---|
77 | assertTrue(allMessages.isEmpty());
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static void testPresets(Set<String> allMessages, ExtendedSourceEntry source) throws SAXException, IOException {
|
---|
81 | Collection<TaggingPreset> presets = TaggingPresetReader.readAll(source.url, true);
|
---|
82 | assertFalse(presets.isEmpty());
|
---|
83 | Collection<String> errorsAndWarnings = Main.getLastErrorAndWarnings();
|
---|
84 | boolean error = false;
|
---|
85 | for (String message : errorsAndWarnings) {
|
---|
86 | if (message.contains(TaggingPreset.PRESET_ICON_ERROR_MSG_PREFIX)) {
|
---|
87 | error = true;
|
---|
88 | allMessages.add(message);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | System.out.println(error ? " => KO" : " => OK");
|
---|
92 | if (error) {
|
---|
93 | Main.clearLastErrorAndWarnings();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|