source: josm/trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTest.java@ 9339

Last change on this file since 9339 was 9166, checked in by Don-vip, 8 years ago

set a 10 minutes timeout for extensions unit tests to avoid network hang

File size: 3.2 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.ArrayList;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.Set;
12
13import org.junit.BeforeClass;
14import org.junit.Test;
15import org.openstreetmap.josm.JOSMFixture;
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
18import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
19import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
20import org.xml.sax.SAXException;
21
22/**
23 * Unit tests of {@link TaggingPresetPreference} class.
24 */
25public 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}
Note: See TracBrowser for help on using the repository browser.