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