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.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.Map;
|
---|
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.gui.mappaint.MapPaintStyles;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.StyleKeys;
|
---|
21 | import org.openstreetmap.josm.gui.mappaint.StyleSource;
|
---|
22 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.AssignmentInstruction;
|
---|
24 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
|
---|
25 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
26 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
|
---|
27 | import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Integration tests of {@link MapPaintPreference} class.
|
---|
31 | */
|
---|
32 | public class MapPaintPreferenceTestIT {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Global timeout applied to all test methods.
|
---|
36 | */
|
---|
37 | @Rule
|
---|
38 | public Timeout globalTimeout = Timeout.seconds(10*60);
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Setup test.
|
---|
42 | */
|
---|
43 | @BeforeClass
|
---|
44 | public static void setUpBeforeClass() {
|
---|
45 | JOSMFixture.createUnitTestFixture().init();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Test that available map paint styles are valid.
|
---|
50 | * @throws IOException if any I/O error occurs
|
---|
51 | * @throws ParseException if the config file does not match MapCSS syntax
|
---|
52 | */
|
---|
53 | @Test
|
---|
54 | public void testValidityOfAvailableStyles() throws ParseException, IOException {
|
---|
55 | Collection<ExtendedSourceEntry> sources = new MapPaintPreference.MapPaintSourceEditor()
|
---|
56 | .loadAndGetAvailableSources();
|
---|
57 | assertFalse(sources.isEmpty());
|
---|
58 | Map<String, Collection<Throwable>> allErrors = new HashMap<>();
|
---|
59 | Map<String, Collection<String>> allWarnings = new HashMap<>();
|
---|
60 | for (ExtendedSourceEntry source : sources) {
|
---|
61 | // Do not validate XML styles
|
---|
62 | if (!"xml".equalsIgnoreCase(source.styleType)) {
|
---|
63 | System.out.println(source.url);
|
---|
64 | StyleSource style = MapPaintStyles.addStyle(source);
|
---|
65 | if (style instanceof MapCSSStyleSource) {
|
---|
66 | // Force loading of all icons to detect missing ones
|
---|
67 | for (MapCSSRule rule : ((MapCSSStyleSource) style).rules) {
|
---|
68 | for (Instruction instruction : rule.declaration.instructions) {
|
---|
69 | if (instruction instanceof AssignmentInstruction) {
|
---|
70 | AssignmentInstruction ai = (AssignmentInstruction) instruction;
|
---|
71 | if (StyleKeys.ICON_IMAGE.equals(ai.key)
|
---|
72 | || StyleKeys.FILL_IMAGE.equals(ai.key)
|
---|
73 | || StyleKeys.REPEAT_IMAGE.equals(ai.key)) {
|
---|
74 | if (ai.val instanceof String) {
|
---|
75 | MapPaintStyles.getIconProvider(new IconReference((String) ai.val, style), true);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | if (style != null) {
|
---|
83 | System.out.println(style.isValid() ? " => OK" : " => KO");
|
---|
84 | Collection<Throwable> errors = style.getErrors();
|
---|
85 | Collection<String> warnings = style.getWarnings();
|
---|
86 | if (!errors.isEmpty()) {
|
---|
87 | allErrors.put(source.url, errors);
|
---|
88 | }
|
---|
89 | if (!warnings.isEmpty()) {
|
---|
90 | allWarnings.put(source.url, warnings);
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | allWarnings.put(source.url, Collections.singleton("MapPaintStyles.addStyle() returned null"));
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | assertTrue(allErrors.toString()+"\n"+allWarnings.toString(), allErrors.isEmpty() && allWarnings.isEmpty());
|
---|
98 | }
|
---|
99 | }
|
---|