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