Ticket #18893: 18893.patch

File 18893.patch, 7.2 KB (added by simon04, 6 years ago)
  • src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java

    diff --git a/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java b/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java
    index e57b473e8..709815720 100644
    a b  
    1111import java.util.Locale;
    1212import java.util.Objects;
    1313
     14import ch.poole.openinghoursparser.OpeningHoursParseException;
    1415import ch.poole.openinghoursparser.OpeningHoursParser;
    15 import ch.poole.openinghoursparser.ParseException;
    1616import ch.poole.openinghoursparser.Rule;
    1717import ch.poole.openinghoursparser.Util;
    1818import org.openstreetmap.josm.command.ChangePropertyCommand;
    private TestError createTestError(Severity severity, String message, String key,  
    6868     * @return a list of {@link TestError} or an empty list
    6969     */
    7070    public List<TestError> checkOpeningHourSyntax(final String key, final String value) {
    71         return checkOpeningHourSyntax(key, value, null, Locale.getDefault());
     71        return checkOpeningHourSyntax(key, value, null, Locale.getDefault(), false);
    7272    }
    7373
    7474    /**
    private TestError createTestError(Severity severity, String message, String key,  
    7878     * @param value the opening hour value to be checked.
    7979     * @param p the primitive to check/fix.
    8080     * @param locale the locale code used for localizing messages
     81     * @param useHTML use HTML code for formatting the test error description
    8182     * @return a list of {@link TestError} or an empty list
    8283     */
    83     List<TestError> checkOpeningHourSyntax(final String key, final String value, OsmPrimitive p, Locale locale) {
     84    List<TestError> checkOpeningHourSyntax(final String key, final String value, OsmPrimitive p, Locale locale, boolean useHTML) {
    8485        if (value == null || value.isEmpty()) {
    8586            return Collections.emptyList();
    8687        }
    private TestError createTestError(Severity severity, String message, String key,  
    9495                // parse again in strict mode for detailed message
    9596                new OpeningHoursParser(new StringReader(value)).rules(true);
    9697            }
    97         } catch (ParseException e) {
    98             return Collections.singletonList(createTestError(Severity.WARNING, e.getMessage(), key, prettifiedValue, p));
     98        } catch (OpeningHoursParseException e) {
     99            String message = e.getMessage();
     100            final int column = e.getColumn() - 1;
     101            if (useHTML && column >= 0) {
     102                message = "<html>" + value.substring(0, column) + "<span color='red'>" + value.substring(column) + "</span> – " + e.getMessage();
     103            }
     104            return Collections.singletonList(createTestError(Severity.WARNING, message, key, prettifiedValue, p));
    99105        }
    100106
    101107        if (!includeOtherSeverityChecks() || Objects.equals(value, prettifiedValue) || p == null) {
    private TestError createTestError(Severity severity, String message, String key,  
    109115    @Override
    110116    public void check(final OsmPrimitive p) {
    111117        for (String key : KEYS_TO_CHECK) {
    112             errors.addAll(checkOpeningHourSyntax(key, p.get(key), p, Locale.getDefault()));
     118            errors.addAll(checkOpeningHourSyntax(key, p.get(key), p, Locale.getDefault(), true));
    113119        }
    114120    }
    115121}
  • test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java

    diff --git a/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java b/test/unit/org/openstreetmap/josm/data/validation/tests/OpeningHourTestTest.java
    index 1d45c0804..0f20fa529 100644
    a b public void testI18n() {  
    8787        assertEquals("Encountered:  \".\" \". \" at line 0, column 0\nWas expecting: <EOF>",
    8888                checkOpeningHourSyntax(key, value, Locale.ENGLISH).get(0).getDescription());
    8989        value = "Mon-Thu 12-18";
    90         assertEquals("Wochentag mit 3 Buchstaben in Zeile 1, Zeichen 4",
     90        assertEquals("<html>Mon-<span color=red>Thu 12-18</span> – Wochentag mit 3 Buchstaben in Zeile 1, Zeichen 4",
    9191                checkOpeningHourSyntax(key, value, Locale.GERMAN).get(0).getDescription());
    92         assertEquals("Three character weekday at line 1, column 4",
     92        assertEquals("<html>Mon-<span color=red>Thu 12-18</span> – Three character weekday at line 1, column 4",
    9393                checkOpeningHourSyntax(key, value, Locale.ENGLISH).get(0).getDescription());
    9494    }
    9595
    public void testCheckOpeningHourSyntax2() {  
    102102        final List<TestError> errors = checkOpeningHourSyntax(key, "Mo-Tue");
    103103        assertThat(errors, hasSize(1));
    104104        assertFixEquals("Mo-Tu", errors.get(0));
    105         assertEquals("Three character weekday at line 1, column 6", errors.get(0).getDescription());
     105        assertEquals("<html>Mo-Tue<span color=red></span> – Three character weekday at line 1, column 6", errors.get(0).getDescription());
    106106        assertEquals(Severity.WARNING, errors.get(0).getSeverity());
    107107    }
    108108
    public void testCheckOpeningHourSyntax3() {  
    115115        final List<TestError> errors = checkOpeningHourSyntax(key, "Sa-Su 10.00-20.00");
    116116        assertThat(errors, hasSize(1));
    117117        assertFixEquals("Sa-Su 10:00-20:00", errors.get(0));
    118         assertEquals("Invalid minutes at line 1, column 12", errors.get(0).getDescription());
     118        assertEquals("<html>Sa-Su 10.00-<span color=red>20.00</span> – Invalid minutes at line 1, column 12", errors.get(0).getDescription());
    119119        assertEquals(Severity.WARNING, errors.get(0).getSeverity());
    120120    }
    121121
    public void testCheckOpeningHourSyntax5() {  
    142142        assertEquals("Encountered:  <UNEXPECTED_CHAR> \"b \" at line 0, column 0\nWas expecting: <EOF>",
    143143                checkOpeningHourSyntax(key, "badtext").get(0).getDescription().trim());
    144144        assertThat(checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m"), hasSize(1));
    145         assertEquals("Encountered:  <UNEXPECTED_CHAR> \"p \" at line 1, column 2\nWas expecting: <EOF>",
     145        assertEquals("<html>5.<span color=red>00 p.m-11.00 p.m</span> – " +
     146                        "Encountered:  <UNEXPECTED_CHAR> \"p \" at line 1, column 2\nWas expecting: <EOF>",
    146147                checkOpeningHourSyntax(key, "5.00 p.m-11.00 p.m").get(0).getDescription());
    147148    }
    148149
    public void testPresetValues() {  
    233234        }
    234235        for (final Tag t : values) {
    235236            final List<TestError> errors = checkOpeningHourSyntax(t.getKey(), t.getValue());
    236             if (!errors.isEmpty() && errors.get(0).getDescription().startsWith("Holiday after weekday")) {
     237            if (!errors.isEmpty() && errors.get(0).getDescription().contains("Holiday after weekday")) {
    237238                continue;
    238239            }
    239240            assertThat(t + " is valid", errors, isEmpty());
    public void testTicket17932() {  
    254255        final Node node = new Node(LatLon.ZERO);
    255256        node.put(key, value);
    256257        new DataSet(node);
    257         return openingHourTest.checkOpeningHourSyntax(key, value, node, locale);
     258        return openingHourTest.checkOpeningHourSyntax(key, value, node, locale, true);
    258259    }
    259260
    260261    private static void assertFixEquals(String value, TestError error) {