Ignore:
Timestamp:
2019-05-19T17:11:38+02:00 (5 years ago)
Author:
Don-vip
Message:

parameterize integration tests for map paint styles and tagging presets

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/MapPaintPreferenceTestIT.java

    r14235 r15095  
    22package org.openstreetmap.josm.gui.preferences.map;
    33
    4 import static org.junit.Assert.assertFalse;
    54import static org.junit.Assert.assertTrue;
     5import static org.junit.Assume.assumeTrue;
    66
    7 import java.util.Collection;
    8 import java.util.HashMap;
    9 import java.util.Map;
     7import java.io.IOException;
     8import java.util.ArrayList;
     9import java.util.List;
     10import java.util.stream.Collectors;
    1011
    11 import org.junit.Rule;
     12import org.junit.BeforeClass;
     13import org.junit.ClassRule;
    1214import org.junit.Test;
     15import org.junit.runner.RunWith;
     16import org.junit.runners.Parameterized.Parameters;
     17import org.openstreetmap.josm.TestUtils;
    1318import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
    1419import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
     
    2126import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
    2227import org.openstreetmap.josm.testutils.JOSMTestRules;
     28import org.openstreetmap.josm.testutils.ParallelParameterized;
    2329import org.openstreetmap.josm.tools.ImageProvider;
    2430
     
    2834 * Integration tests of {@link MapPaintPreference} class.
    2935 */
     36@RunWith(ParallelParameterized.class)
    3037public class MapPaintPreferenceTestIT {
    3138
     
    3340     * Setup rule
    3441     */
    35     @Rule
     42    @ClassRule
    3643    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    37     public JOSMTestRules test = new JOSMTestRules().https().timeout(15000*60);
     44    public static JOSMTestRules test = new JOSMTestRules().https().timeout(15000*60).parameters();
     45
     46    /** Entry to test */
     47    private final ExtendedSourceEntry source;
     48    private final List<String> ignoredErrors = new ArrayList<>();
     49    private static final List<String> errorsToIgnore = new ArrayList<>();
    3850
    3951    /**
    40      * Test that available map paint styles are valid.
     52     * Setup test
     53     * @throws IOException in case of I/O error
     54     */
     55    @BeforeClass
     56    public static void beforeClass() throws IOException {
     57        errorsToIgnore.addAll(TestUtils.getIgnoredErrorMessages(MapPaintPreferenceTestIT.class));
     58    }
     59
     60    /**
     61     * Returns list of map paint styles to test.
     62     * @return list of map paint styles to test
     63     * @throws Exception if an error occurs
     64     */
     65    @Parameters(name = "{0} - {1}")
     66    public static List<Object[]> data() throws Exception {
     67        ImageProvider.clearCache();
     68        return new MapPaintPreference.MapPaintSourceEditor().loadAndGetAvailableSources().stream()
     69                .map(x -> new Object[] {x.getDisplayName(), x.url, x}).collect(Collectors.toList());
     70    }
     71
     72    /**
     73     * Constructs a new {@code MapPaintPreferenceTestIT}
     74     * @param displayName displayed name
     75     * @param url URL
     76     * @param source source entry to test
     77     */
     78    public MapPaintPreferenceTestIT(String displayName, String url, ExtendedSourceEntry source) {
     79        this.source = source;
     80    }
     81
     82    /**
     83     * Test that map paint style is valid.
    4184     * @throws Exception in case of error
    4285     */
    4386    @Test
    44     public void testValidityOfAvailableStyles() throws Exception {
    45         ImageProvider.clearCache();
    46         Collection<ExtendedSourceEntry> sources = new MapPaintPreference.MapPaintSourceEditor()
    47                 .loadAndGetAvailableSources();
    48         // Drop everything from yopaseopor, pasharm and www.freietonne.de, too many errors
    49         sources.removeIf(x -> x.url.contains("yopaseopor/") || x.url.contains("/pasharm") || x.url.contains("www.freietonne.de"));
    50         assertFalse(sources.isEmpty());
    51         Map<String, Collection<Throwable>> allErrors = new HashMap<>();
    52         Map<String, Collection<String>> allWarnings = new HashMap<>();
    53         for (ExtendedSourceEntry source : sources) {
    54             // Do not validate XML styles
    55             if (!"xml".equalsIgnoreCase(source.styleType)) {
    56                 System.out.println(source.url);
    57                 StyleSource style = MapPaintStyles.addStyle(source);
    58                 if (style instanceof MapCSSStyleSource) {
    59                     // Force loading of all icons to detect missing ones
    60                     for (MapCSSRule rule : ((MapCSSStyleSource) style).rules) {
    61                         for (Instruction instruction : rule.declaration.instructions) {
    62                             if (instruction instanceof AssignmentInstruction) {
    63                                 AssignmentInstruction ai = (AssignmentInstruction) instruction;
    64                                 if (StyleKeys.ICON_IMAGE.equals(ai.key)
    65                                  || StyleKeys.FILL_IMAGE.equals(ai.key)
    66                                  || StyleKeys.REPEAT_IMAGE.equals(ai.key)) {
    67                                     if (ai.val instanceof String) {
    68                                         MapPaintStyles.getIconProvider(new IconReference((String) ai.val, style), true);
    69                                     }
    70                                 }
     87    public void testStyleValidity() throws Exception {
     88        StyleSource style = MapPaintStyles.addStyle(source);
     89        if (style instanceof MapCSSStyleSource) {
     90            // Force loading of all icons to detect missing ones
     91            for (MapCSSRule rule : ((MapCSSStyleSource) style).rules) {
     92                for (Instruction instruction : rule.declaration.instructions) {
     93                    if (instruction instanceof AssignmentInstruction) {
     94                        AssignmentInstruction ai = (AssignmentInstruction) instruction;
     95                        if (StyleKeys.ICON_IMAGE.equals(ai.key)
     96                         || StyleKeys.FILL_IMAGE.equals(ai.key)
     97                         || StyleKeys.REPEAT_IMAGE.equals(ai.key)) {
     98                            if (ai.val instanceof String) {
     99                                MapPaintStyles.getIconProvider(new IconReference((String) ai.val, style), true);
    71100                            }
    72101                        }
    73102                    }
    74103                }
    75                 System.out.println(style.isValid() ? " => OK" : " => KO");
    76                 Collection<Throwable> errors = style.getErrors();
    77                 Collection<String> warnings = style.getWarnings();
    78                 if (!errors.isEmpty()) {
    79                     allErrors.put(source.url, errors);
    80                 }
    81                 if (!warnings.isEmpty()) {
    82                     allWarnings.put(source.url, warnings);
    83                 }
    84104            }
    85105        }
    86         ImageProvider.clearCache();
    87         assertTrue(allErrors.toString()+"\n"+allWarnings.toString(), allErrors.isEmpty() && allWarnings.isEmpty());
     106
     107        List<Throwable> errors = new ArrayList<>(style.getErrors());
     108        errors.stream().map(Throwable::getMessage).filter(MapPaintPreferenceTestIT::isIgnoredSubstring).forEach(ignoredErrors::add);
     109        errors.removeIf(e -> ignoredErrors.contains(e.getMessage()));
     110
     111        List<String> warnings = new ArrayList<>(style.getWarnings());
     112        warnings.stream().filter(MapPaintPreferenceTestIT::isIgnoredSubstring).forEach(ignoredErrors::add);
     113        warnings.removeAll(ignoredErrors);
     114
     115        assertTrue(errors.toString() + '\n' + warnings.toString(), errors.isEmpty() && warnings.isEmpty());
     116        assumeTrue(ignoredErrors.toString(), ignoredErrors.isEmpty());
     117    }
     118
     119    private static boolean isIgnoredSubstring(String substring) {
     120        return errorsToIgnore.parallelStream().anyMatch(x -> substring.contains(x));
    88121    }
    89122}
Note: See TracChangeset for help on using the changeset viewer.