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.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.junit.BeforeClass;
|
---|
13 | import org.junit.Rule;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.junit.rules.Timeout;
|
---|
16 | import org.openstreetmap.josm.JOSMFixture;
|
---|
17 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
18 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.StyleKeys;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
---|
21 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
|
---|
22 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.AssignmentInstruction;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
|
---|
24 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
|
---|
26 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
27 |
|
---|
28 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Integration tests of {@link MapPaintPreference} class.
|
---|
32 | */
|
---|
33 | public class MapPaintPreferenceTestIT {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Global timeout applied to all test methods.
|
---|
37 | */
|
---|
38 | @Rule
|
---|
39 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
40 | public Timeout globalTimeout = Timeout.seconds(10*60);
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Setup test.
|
---|
44 | */
|
---|
45 | @BeforeClass
|
---|
46 | public static void setUpBeforeClass() {
|
---|
47 | JOSMFixture.createUnitTestFixture().init();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Test that available map paint styles are valid.
|
---|
52 | * @throws Exception in case of error
|
---|
53 | */
|
---|
54 | @Test
|
---|
55 | public void testValidityOfAvailableStyles() throws Exception {
|
---|
56 | ImageProvider.clearCache();
|
---|
57 | Collection<ExtendedSourceEntry> sources = new MapPaintPreference.MapPaintSourceEditor()
|
---|
58 | .loadAndGetAvailableSources();
|
---|
59 | // Drop everything from yopaseopor, too many errors
|
---|
60 | sources.removeIf(x -> x.url.contains("yopaseopor/"));
|
---|
61 | assertFalse(sources.isEmpty());
|
---|
62 | Map<String, Collection<Throwable>> allErrors = new HashMap<>();
|
---|
63 | Map<String, Collection<String>> allWarnings = new HashMap<>();
|
---|
64 | for (ExtendedSourceEntry source : sources) {
|
---|
65 | // Do not validate XML styles
|
---|
66 | if (!"xml".equalsIgnoreCase(source.styleType)) {
|
---|
67 | System.out.println(source.url);
|
---|
68 | StyleSource style = MapPaintStyles.addStyle(source);
|
---|
69 | if (style instanceof MapCSSStyleSource) {
|
---|
70 | // Force loading of all icons to detect missing ones
|
---|
71 | for (MapCSSRule rule : ((MapCSSStyleSource) style).rules) {
|
---|
72 | for (Instruction instruction : rule.declaration.instructions) {
|
---|
73 | if (instruction instanceof AssignmentInstruction) {
|
---|
74 | AssignmentInstruction ai = (AssignmentInstruction) instruction;
|
---|
75 | if (StyleKeys.ICON_IMAGE.equals(ai.key)
|
---|
76 | || StyleKeys.FILL_IMAGE.equals(ai.key)
|
---|
77 | || StyleKeys.REPEAT_IMAGE.equals(ai.key)) {
|
---|
78 | if (ai.val instanceof String) {
|
---|
79 | MapPaintStyles.getIconProvider(new IconReference((String) ai.val, style), true);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | if (style != null) {
|
---|
87 | System.out.println(style.isValid() ? " => OK" : " => KO");
|
---|
88 | Collection<Throwable> errors = style.getErrors();
|
---|
89 | Collection<String> warnings = style.getWarnings();
|
---|
90 | if (!errors.isEmpty()) {
|
---|
91 | allErrors.put(source.url, errors);
|
---|
92 | }
|
---|
93 | if (!warnings.isEmpty()) {
|
---|
94 | allWarnings.put(source.url, warnings);
|
---|
95 | }
|
---|
96 | } else if (!source.url.contains("www.freietonne.de")) {
|
---|
97 | // ignore frequent network errors with www.freietonne.de causing too much Jenkins failures
|
---|
98 | allWarnings.put(source.url, Collections.singleton("MapPaintStyles.addStyle() returned null"));
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | ImageProvider.clearCache();
|
---|
103 | assertTrue(allErrors.toString()+"\n"+allWarnings.toString(), allErrors.isEmpty() && allWarnings.isEmpty());
|
---|
104 | }
|
---|
105 | }
|
---|