source: josm/trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/MapPaintPreferenceTest.java@ 9400

Last change on this file since 9400 was 9400, checked in by Don-vip, 9 years ago

see #12282 - detect missing icons for MapCSS styles

File size: 3.9 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.Collection;
9import java.util.HashMap;
10import java.util.Map;
11
12import org.junit.BeforeClass;
13import org.junit.Test;
14import org.openstreetmap.josm.JOSMFixture;
15import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
16import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
17import org.openstreetmap.josm.gui.mappaint.StyleKeys;
18import org.openstreetmap.josm.gui.mappaint.StyleSource;
19import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
20import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.AssignmentInstruction;
21import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
22import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
23import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
24import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
25
26/**
27 * Unit tests of {@link MapPaintPreference} class.
28 */
29public 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}
Note: See TracBrowser for help on using the repository browser.