Changeset 8936 in josm


Ignore:
Timestamp:
2015-10-23T23:34:29+02:00 (9 years ago)
Author:
Don-vip
Message:

add unit test to check syntax validity of all MapCSSTagChecker rules

Location:
trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r8846 r8936  
    234234    final MultiMap<String, TagCheck> checks = new MultiMap<>();
    235235
    236     static class TagCheck implements Predicate<OsmPrimitive> {
     236    /**
     237     * Result of {@link TagCheck#readMapCSS}
     238     * @since 8936
     239     */
     240    public static class ParseResult {
     241        /** Checks successfully parsed */
     242        public final List<TagCheck> parseChecks;
     243        /** Errors that occured during parsing */
     244        public final Collection<Throwable> parseErrors;
     245
     246        /**
     247         * Constructs a new {@code ParseResult}.
     248         * @param parseChecks Checks successfully parsed
     249         * @param parseErrors Errors that occured during parsing
     250         */
     251        public ParseResult(List<TagCheck> parseChecks, Collection<Throwable> parseErrors) {
     252            this.parseChecks = parseChecks;
     253            this.parseErrors = parseErrors;
     254        }
     255    }
     256
     257    public static class TagCheck implements Predicate<OsmPrimitive> {
    237258        protected final GroupedMapCSSRule rule;
    238259        protected final List<FixCommand> fixCommands = new ArrayList<>();
     
    320341        }
    321342
    322         static List<TagCheck> readMapCSS(Reader css) throws ParseException {
     343        static ParseResult readMapCSS(Reader css) throws ParseException {
    323344            CheckParameterUtil.ensureParameterNotNull(css, "css");
    324345
     
    329350            final MapCSSParser parser = new MapCSSParser(css, MapCSSParser.LexicalState.DEFAULT);
    330351            parser.sheet(source);
    331             assert source.getErrors().isEmpty();
     352            Collection<Throwable> parseErrors = source.getErrors();
     353            assert parseErrors.isEmpty();
    332354            // Ignore "meta" rule(s) from external rules of JOSM wiki
    333355            removeMetaRules(source);
     
    343365                }
    344366            }
    345             List<TagCheck> result = new ArrayList<>();
     367            List<TagCheck> parseChecks = new ArrayList<>();
    346368            for (Map.Entry<Declaration, List<Selector>> map : g.entrySet()) {
    347369                try {
    348                     result.add(TagCheck.ofMapCSSRule(
     370                    parseChecks.add(TagCheck.ofMapCSSRule(
    349371                            new GroupedMapCSSRule(map.getValue(), map.getKey())));
    350372                } catch (IllegalDataException e) {
    351373                    Main.error("Cannot add MapCss rule: "+e.getMessage());
    352                 }
    353             }
    354             return result;
     374                    parseErrors.add(e);
     375                }
     376            }
     377            return new ParseResult(parseChecks, parseErrors);
    355378        }
    356379
     
    669692     * Adds a new MapCSS config file from the given URL.
    670693     * @param url The unique URL of the MapCSS config file
     694     * @return List of tag checks and parsing errors, or null
    671695     * @throws ParseException if the config file does not match MapCSS syntax
    672696     * @throws IOException if any I/O error occurs
    673697     * @since 7275
    674698     */
    675     public synchronized void addMapCSS(String url) throws ParseException, IOException {
     699    public synchronized ParseResult addMapCSS(String url) throws ParseException, IOException {
    676700        CheckParameterUtil.ensureParameterNotNull(url, "url");
    677701        CachedFile cache = new CachedFile(url);
    678702        InputStream zip = cache.findZipEntryInputStream("validator.mapcss", "");
     703        ParseResult result;
    679704        try (InputStream s = zip != null ? zip : cache.getInputStream()) {
    680             List<TagCheck> tagchecks = TagCheck.readMapCSS(new BufferedReader(UTFInputStreamReader.create(s)));
     705            result = TagCheck.readMapCSS(new BufferedReader(UTFInputStreamReader.create(s)));
    681706            checks.remove(url);
    682             checks.putAll(url, tagchecks);
     707            checks.putAll(url, result.parseChecks);
    683708            // Check assertions, useful for development of local files
    684709            if (Main.pref.getBoolean("validator.check_assert_local_rules", false) && Utils.isLocalUrl(url)) {
    685                 for (String msg : checkAsserts(tagchecks)) {
     710                for (String msg : checkAsserts(result.parseChecks)) {
    686711                    Main.warn(msg);
    687712                }
    688713            }
    689714        }
     715        return result;
    690716    }
    691717
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r8870 r8936  
    447447    /**
    448448     * Synchronously loads available sources and returns the parsed list.
     449     * @return list of available sources
    449450     */
    450     Collection<ExtendedSourceEntry> loadAndGetAvailableSources() {
     451    public final Collection<ExtendedSourceEntry> loadAndGetAvailableSources() {
    451452        try {
    452453            final SourceLoader loader = new SourceLoader(availableSourcesUrl, sourceProviders);
     
    466467    }
    467468
     469    /**
     470     * Performs the initial loading of source providers. Does nothing if already done.
     471     */
    468472    public void initiallyLoadAvailableSources() {
    469473        if (!sourcesInitiallyLoaded) {
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerTest.java

    r8857 r8936  
    2828import org.openstreetmap.josm.data.validation.Severity;
    2929import org.openstreetmap.josm.data.validation.TestError;
     30import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.ParseResult;
    3031import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.TagCheck;
    3132import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
     
    4647    static MapCSSTagChecker buildTagChecker(String css) throws ParseException {
    4748        final MapCSSTagChecker test = new MapCSSTagChecker();
    48         test.checks.putAll("test", TagCheck.readMapCSS(new StringReader(css)));
     49        test.checks.putAll("test", TagCheck.readMapCSS(new StringReader(css)).parseChecks);
    4950        return test;
    5051    }
     
    5253    @Test
    5354    public void testNaturalMarsh() throws Exception {
    54         final List<MapCSSTagChecker.TagCheck> checks = MapCSSTagChecker.TagCheck.readMapCSS(new StringReader("" +
     55        ParseResult result = MapCSSTagChecker.TagCheck.readMapCSS(new StringReader("" +
    5556                "*[natural=marsh] {\n" +
    5657                "   throwWarning: tr(\"{0}={1} is deprecated\", \"{0.key}\", tag(\"natural\"));\n" +
     
    5960                "   fixAdd: \"wetland=marsh\";\n" +
    6061                "}"));
     62        final List<MapCSSTagChecker.TagCheck> checks = result.parseChecks;
    6163        assertEquals(1, checks.size());
     64        assertTrue(result.parseErrors.isEmpty());
    6265        final MapCSSTagChecker.TagCheck check = checks.get(0);
    6366        assertNotNull(check);
     
    8790                "fixChangeKey: \"highway => construction\";\n" +
    8891                "fixAdd: \"highway=construction\";\n" +
    89                 "}")).get(0);
     92                "}")).parseChecks.get(0);
    9093        final Command command = check.fixPrimitive(p);
    9194        assertTrue(command instanceof SequenceCommand);
Note: See TracChangeset for help on using the changeset viewer.