Changeset 15095 in josm


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

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java

    r14795 r15095  
    2828import org.junit.ClassRule;
    2929import org.junit.Test;
    30 import org.junit.runner.Description;
    3130import org.junit.runner.RunWith;
    3231import org.junit.runners.Parameterized.Parameters;
    33 import org.junit.runners.model.Statement;
    3432import org.openstreetmap.gui.jmapviewer.Coordinate;
    3533import org.openstreetmap.gui.jmapviewer.TileXY;
     
    8583    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    8684    public static JOSMTestRules test = new JOSMTestRules().https().i18n().preferences().projection().projectionNadGrids()
    87                                                    .timeout((int) TimeUnit.MINUTES.toMillis(40));
    88 
    89     static {
    90         try {
    91             test.apply(new Statement() {
    92                 @Override
    93                 public void evaluate() throws Throwable {
    94                     // Do nothing. Hack needed because @Parameters are computed before anything else
    95                 }
    96             }, Description.createSuiteDescription(ImageryPreferenceTestIT.class)).evaluate();
    97         } catch (Throwable e) {
    98             Logging.error(e);
    99         }
    100     }
     85                                                   .timeout((int) TimeUnit.MINUTES.toMillis(40)).parameters();
    10186
    10287    /** Entry to test */
  • 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}
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java

    r14464 r15095  
    44import static org.junit.Assert.assertFalse;
    55import static org.junit.Assert.assertTrue;
     6import static org.junit.Assume.assumeTrue;
    67
    78import java.io.IOException;
     9import java.util.ArrayList;
    810import java.util.Collection;
    9 import java.util.HashMap;
    10 import java.util.Map;
     11import java.util.HashSet;
     12import java.util.List;
     13import java.util.Locale;
    1114import java.util.Objects;
    1215import java.util.Set;
    13 import java.util.TreeSet;
    1416import java.util.concurrent.ExecutionException;
    1517import java.util.concurrent.TimeUnit;
    1618import java.util.concurrent.TimeoutException;
     19import java.util.stream.Collectors;
    1720
    18 import org.junit.Rule;
     21import org.junit.BeforeClass;
     22import org.junit.ClassRule;
    1923import org.junit.Test;
     24import org.junit.runner.RunWith;
     25import org.junit.runners.Parameterized.Parameters;
     26import org.openstreetmap.josm.TestUtils;
    2027import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
    2128import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
     
    2330import org.openstreetmap.josm.spi.preferences.Config;
    2431import org.openstreetmap.josm.testutils.JOSMTestRules;
     32import org.openstreetmap.josm.testutils.ParallelParameterized;
    2533import org.openstreetmap.josm.tools.ImageProvider;
    2634import org.openstreetmap.josm.tools.Logging;
     
    3240 * Integration tests of {@link TaggingPresetPreference} class.
    3341 */
     42@RunWith(ParallelParameterized.class)
    3443public class TaggingPresetPreferenceTestIT {
    3544
     
    3746     * Setup rule
    3847     */
    39     @Rule
     48    @ClassRule
    4049    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    41     public JOSMTestRules test = new JOSMTestRules().https().timeout(10000*60);
     50    public static JOSMTestRules test = new JOSMTestRules().https().timeout(10000*60).parameters();
     51
     52    /** Entry to test */
     53    private final ExtendedSourceEntry source;
     54    private final List<String> ignoredErrors = new ArrayList<>();
     55    private static final List<String> errorsToIgnore = new ArrayList<>();
    4256
    4357    /**
    44      * Test that available tagging presets are valid.
     58     * Setup test
     59     * @throws IOException in case of I/O error
     60     */
     61    @BeforeClass
     62    public static void beforeClass() throws IOException {
     63        errorsToIgnore.addAll(TestUtils.getIgnoredErrorMessages(TaggingPresetPreferenceTestIT.class));
     64        // Double traditional timeouts to avoid random problems
     65        Config.getPref().putInt("socket.timeout.connect", 30);
     66        Config.getPref().putInt("socket.timeout.read", 60);
     67        // Make sure error messages are in english
     68        Locale.setDefault(Locale.ENGLISH);
     69    }
     70
     71    /**
     72     * Returns list of tagging presets to test.
     73     * @return list of tagging presets to test
     74     * @throws Exception if an error occurs
     75     */
     76    @Parameters(name = "{0} - {1}")
     77    public static List<Object[]> data() throws Exception {
     78        ImageProvider.clearCache();
     79        return new TaggingPresetPreference.TaggingPresetSourceEditor().loadAndGetAvailableSources().stream()
     80                .map(x -> new Object[] {x.getDisplayName(), x.url, x}).collect(Collectors.toList());
     81    }
     82
     83    /**
     84     * Constructs a new {@code TaggingPresetPreferenceTestIT}
     85     * @param displayName displayed name
     86     * @param url URL
     87     * @param source source entry to test
     88     */
     89    public TaggingPresetPreferenceTestIT(String displayName, String url, ExtendedSourceEntry source) {
     90        this.source = source;
     91    }
     92
     93    /**
     94     * Test that tagging presets are valid.
    4595     * @throws Exception in case of error
    4696     */
    4797    @Test
    48     public void testValidityOfAvailablePresets() throws Exception {
    49         ImageProvider.clearCache();
    50         Collection<ExtendedSourceEntry> sources = new TaggingPresetPreference.TaggingPresetSourceEditor()
    51                 .loadAndGetAvailableSources();
    52         assertFalse(sources.isEmpty());
    53         // Double traditional timeouts to avoid random problems
    54         Config.getPref().putInt("socket.timeout.connect", 30);
    55         Config.getPref().putInt("socket.timeout.read", 60);
    56         Map<String, Throwable> allErrors = new HashMap<>();
    57         Map<String, Set<String>> allMessages = new HashMap<>();
    58         for (ExtendedSourceEntry source : sources) {
    59             System.out.println(source.url);
     98    public void testPresetsValidity() throws Exception {
     99        Set<String> errors = new HashSet<>();
     100        try {
     101            testPresets(errors, source);
     102        } catch (IOException e) {
    60103            try {
    61                 testPresets(allMessages, source);
    62             } catch (IOException e) {
    63                 try {
    64                     Logging.warn(e);
    65                     // try again in case of temporary network error
    66                     testPresets(allMessages, source);
    67                 } catch (SAXException | IOException e1) {
    68                     e.printStackTrace();
    69                     // ignore frequent network errors with www.freietonne.de causing too much Jenkins failures
    70                     if (!source.url.contains("www.freietonne.de")) {
    71                         allErrors.put(source.url, e1);
    72                     }
    73                     System.out.println(" => KO");
    74                 }
    75             } catch (SAXException | IllegalArgumentException e) {
    76                 e.printStackTrace();
    77                 if (!source.url.contains("yopaseopor/")) {
    78                     // ignore https://raw.githubusercontent.com/yopaseopor/traffic_signs_preset_JOSM cause too much errors
    79                     allErrors.put(source.url, e);
    80                 }
    81                 System.out.println(" => KO");
     104                Logging.warn(e);
     105                // try again in case of temporary network error
     106                testPresets(errors, source);
     107            } catch (SAXException | IOException e1) {
     108                handleException(e1, errors);
    82109            }
     110        } catch (SAXException | IllegalArgumentException e) {
     111            handleException(e, errors);
    83112        }
    84         ImageProvider.clearCache();
    85         assertTrue(allErrors.toString(), allErrors.isEmpty());
    86         assertTrue(allMessages.toString(), allMessages.isEmpty());
     113        assertTrue(errors.toString(), errors.isEmpty());
     114        assumeTrue(ignoredErrors.toString(), ignoredErrors.isEmpty());
    87115    }
    88116
    89     private static void testPresets(Map<String, Set<String>> allMessages, ExtendedSourceEntry source) throws SAXException, IOException {
     117    private void handleException(Exception e, Set<String> errors) {
     118        e.printStackTrace();
     119        String s = source.url + " => " + e.toString();
     120        if (isIgnoredSubstring(s)) {
     121            ignoredErrors.add(s);
     122        } else {
     123            errors.add(s);
     124        }
     125    }
     126
     127    private void testPresets(Set<String> messages, ExtendedSourceEntry source) throws SAXException, IOException {
    90128        Collection<TaggingPreset> presets = TaggingPresetReader.readAll(source.url, true);
    91129        assertFalse(presets.isEmpty());
     
    103141            if (message.contains(TaggingPreset.PRESET_ICON_ERROR_MSG_PREFIX)) {
    104142                error = true;
    105                 // ignore https://github.com/yopaseopor/traffic_signs_preset_JOSM because of far too frequent missing icons errors
    106                 // ignore https://github.com/ruosm-presets because of frequent missing icons errors and no response
    107                 if (!source.url.contains("yopaseopor/traffic_signs") && !source.url.contains("ruosm-presets/literan-moscow")) {
    108                     allMessages.computeIfAbsent(source.url, x -> new TreeSet<>()).add(message);
     143                if (isIgnoredSubstring(message)) {
     144                    ignoredErrors.add(message);
     145                } else {
     146                    messages.add(message);
    109147                }
    110148            }
    111149        }
    112         System.out.println(error ? " => KO" : " => OK");
    113150        if (error) {
    114151            Logging.clearLastErrorAndWarnings();
    115152        }
    116153    }
     154
     155    private static boolean isIgnoredSubstring(String substring) {
     156        return errorsToIgnore.parallelStream().anyMatch(x -> substring.contains(x));
     157    }
    117158}
  • trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    r14380 r15095  
    4545import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    4646import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
     47import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreferenceTestIT;
    4748import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    4849import org.openstreetmap.josm.gui.util.GuiHelper;
     
    366367    }
    367368
     369    /**
     370     * Must be called if test run with Junit parameters
     371     * @return this instance, for easy chaining
     372     */
     373    public JOSMTestRules parameters() {
     374        try {
     375            apply(new Statement() {
     376                @Override
     377                public void evaluate() throws Throwable {
     378                    // Do nothing. Hack needed because @Parameters are computed before anything else
     379                }
     380            }, Description.createSuiteDescription(ImageryPreferenceTestIT.class)).evaluate();
     381        } catch (Throwable e) {
     382            Logging.error(e);
     383        }
     384        return this;
     385    }
     386
    368387    private static class MockVersion extends Version {
    369388        MockVersion(final String propertiesString) {
Note: See TracChangeset for help on using the changeset viewer.