Ticket #18956: 18956-alternative.patch

File 18956-alternative.patch, 12.4 KB (added by GerdP, 5 years ago)
  • src/org/openstreetmap/josm/data/validation/TestError.java

     
    219219         * Returns a new test error with the specified values
    220220         *
    221221         * @return a new test error with the specified values
    222          * @throws IllegalArgumentException when {@link #message} or {@link #primitives} is null.
     222         * @throws IllegalArgumentException when {@link #message} or {@link #primitives} is null/empty.
    223223         */
    224224        public TestError build() {
    225225            CheckParameterUtil.ensureParameterNotNull(message, "message not set");
    226226            CheckParameterUtil.ensureParameterNotNull(primitives, "primitives not set");
     227            CheckParameterUtil.ensureThat(!primitives.isEmpty(), "primitives is empty");
    227228            if (this.highlighted == null) {
    228229                this.highlighted = Collections.emptySet();
    229230            }
  • src/org/openstreetmap/josm/data/validation/tests/ConditionalKeys.java

     
    88import java.util.Collection;
    99import java.util.HashSet;
    1010import java.util.List;
     11import java.util.Locale;
    1112import java.util.Set;
    1213import java.util.regex.Matcher;
    1314import java.util.regex.Pattern;
     
    122123        return parts.length == 1 && (isRestrictionType(parts[0]) || isTransportationMode(parts[0]));
    123124    }
    124125
    125     /**
    126      * Check if a value is valid
    127      * @param key The key the value is for
    128      * @param value The value
    129      * @return <code>true</code> if it is valid
    130      */
    131     public boolean isValueValid(String key, String value) {
    132         return validateValue(key, value) == null;
    133     }
    134 
    135126    static class ConditionalParsingException extends RuntimeException {
    136127        ConditionalParsingException(String message) {
    137128            super(message);
     
    190181     * Validate a key/value pair
    191182     * @param key The key
    192183     * @param value The value
     184     * @param p the primitive
    193185     * @return The error message for that value or <code>null</code> to indicate valid
    194186     */
    195     public String validateValue(String key, String value) {
     187    public String validateValue(String key, String value, OsmPrimitive p) {
    196188        try {
    197189            for (final ConditionalValue conditional : ConditionalValue.parse(value)) {
    198190                // validate restriction value
     
    202194                // validate opening hour if the value contains an hour (heuristic)
    203195                for (final String condition : conditional.conditions) {
    204196                    if (condition.matches(".*[0-9]:[0-9]{2}.*")) {
    205                         final List<TestError> errors = openingHourTest.checkOpeningHourSyntax("", condition);
     197                        final List<TestError> errors = openingHourTest.checkOpeningHourSyntax("", condition, p,
     198                                Locale.ENGLISH);
    206199                        if (!errors.isEmpty()) {
    207200                            return errors.get(0).getDescription();
    208201                        }
     
    233226                continue;
    234227            }
    235228            final String value = p.get(key);
    236             final String error = validateValue(key, value);
     229            final String error = validateValue(key, value, p);
    237230            if (error != null) {
    238231                errors.add(TestError.builder(this, Severity.WARNING, 3202)
    239232                        .message(tr("Error in {0} value: {1}", key, error))
  • src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java

     
    1111import java.util.Locale;
    1212import java.util.Objects;
    1313
    14 import ch.poole.openinghoursparser.OpeningHoursParser;
    15 import ch.poole.openinghoursparser.ParseException;
    16 import ch.poole.openinghoursparser.Rule;
    17 import ch.poole.openinghoursparser.Util;
    1814import org.openstreetmap.josm.command.ChangePropertyCommand;
    1915import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2016import org.openstreetmap.josm.data.validation.Severity;
    2117import org.openstreetmap.josm.data.validation.Test.TagTest;
    2218import org.openstreetmap.josm.data.validation.TestError;
     19import org.openstreetmap.josm.tools.CheckParameterUtil;
    2320
     21import ch.poole.openinghoursparser.OpeningHoursParser;
     22import ch.poole.openinghoursparser.ParseException;
     23import ch.poole.openinghoursparser.Rule;
     24import ch.poole.openinghoursparser.Util;
     25
    2426/**
    2527 * Tests the correct usage of the opening hour syntax of the tags
    2628 * {@code opening_hours}, {@code collection_times}, {@code service_times} according to
     
    4547     * @param severity The error severity
    4648     * @param message The error message
    4749     * @param key The incriminated key, used for display.
    48      * @param value The incriminated value, used for comparison with prettified value.
    4950     * @param prettifiedValue The prettified value
    5051     * @param p The incriminated OSM primitive.
    5152     * @return The real test error given to JOSM validator. Can be fixable or not if a prettified values has been determined.
    5253     */
    53     private TestError createTestError(Severity severity, String message, String key, String value, String prettifiedValue, OsmPrimitive p) {
     54    private TestError createTestError(Severity severity, String message, String key, String prettifiedValue, OsmPrimitive p) {
    5455        final TestError.Builder error = TestError.builder(this, severity, 2901)
    5556                .message(tr("Opening hours syntax"), message) // todo obtain English message for ignore functionality
    56                 .primitives(p != null ? new OsmPrimitive[] {p} : new OsmPrimitive[] {});
    57         if (p == null || prettifiedValue == null || prettifiedValue.equals(value)) {
     57                .primitives(p);
     58        if (prettifiedValue == null || prettifiedValue.equals(p.get(key))) {
    5859            return error.build();
    5960        } else {
    6061            return error.fix(() -> new ChangePropertyCommand(p, key, prettifiedValue)).build();
     
    6465    /**
    6566     * Checks for a correct usage of the opening hour syntax of the {@code value} given,
    6667     * and returns a list containing validation errors or an empty list. Null values result in an empty list.
    67      * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times"). Used in error message
    68      * @param value the opening hour value to be checked.
    69      * @return a list of {@link TestError} or an empty list
    70      */
    71     public List<TestError> checkOpeningHourSyntax(final String key, final String value) {
    72         return checkOpeningHourSyntax(key, value, null, Locale.getDefault());
    73     }
    74 
    75     /**
    76      * Checks for a correct usage of the opening hour syntax of the {@code value} given,
    77      * and returns a list containing validation errors or an empty list. Null values result in an empty list.
    7868     * @param key the OSM key (should be "opening_hours", "collection_times" or "service_times").
    7969     * @param value the opening hour value to be checked.
    8070     * @param p the primitive to check/fix.
     
    8272     * @return a list of {@link TestError} or an empty list
    8373     */
    8474    List<TestError> checkOpeningHourSyntax(final String key, final String value, OsmPrimitive p, Locale locale) {
     75        CheckParameterUtil.ensureParameterNotNull(p, "primitive");
    8576        if (value == null || value.isEmpty()) {
    8677            return Collections.emptyList();
    8778        }
     
    9687                new OpeningHoursParser(new StringReader(value)).rules(true);
    9788            }
    9889        } catch (ParseException e) {
    99             return Collections.singletonList(createTestError(Severity.WARNING, e.getMessage(), key, value, prettifiedValue, p));
     90            return Collections.singletonList(createTestError(Severity.WARNING, e.getMessage(), key, prettifiedValue, p));
    10091        }
    10192
    10293        if (!includeOtherSeverityChecks() || Objects.equals(value, prettifiedValue)) {
     
    10394            return Collections.emptyList();
    10495        } else {
    10596            final String message = tr("{0} value can be prettified", key);
    106             return Collections.singletonList(createTestError(Severity.OTHER, message, key, value, prettifiedValue, p));
     97            return Collections.singletonList(createTestError(Severity.OTHER, message, key, prettifiedValue, p));
    10798        }
    10899    }
    109100
  • test/unit/org/openstreetmap/josm/data/validation/tests/ConditionalKeysTest.java

     
    77import org.junit.Before;
    88import org.junit.Rule;
    99import org.junit.Test;
     10import org.openstreetmap.josm.data.coor.LatLon;
     11import org.openstreetmap.josm.data.osm.DataSet;
     12import org.openstreetmap.josm.data.osm.Node;
    1013import org.openstreetmap.josm.testutils.JOSMTestRules;
    1114
    1215import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    5154    }
    5255
    5356    /**
    54      * Unit test of {@link ConditionalKeys#isValueValid}.
     57     * Unit test of {@link ConditionalKeys#validateValue}.
    5558     */
    5659    @Test
    5760    public void testValueValid() {
    58         assertTrue(test.isValueValid("maxspeed:conditional", "120 @ (06:00-19:00)"));
    59         assertFalse(test.isValueValid("maxspeed:conditional", " @ (06:00-19:00)"));
    60         assertFalse(test.isValueValid("maxspeed:conditional", "120 (06:00-19:00)"));
    61         assertFalse(test.isValueValid("maxspeed:conditional", "120 @ ()"));
    62         assertFalse(test.isValueValid("maxspeed:conditional", "120 @ "));
    63         assertFalse(test.isValueValid("maxspeed:conditional", "120 @ (06:00/19:00)"));
    64         assertTrue(test.isValueValid("maxspeed:conditional", "120 @ (06:00-20:00); 100 @ (22:00-06:00)"));
    65         assertTrue(test.isValueValid("motor_vehicle:conditional", "delivery @ (Mo-Fr 06:00-11:00,17:00-19:00;Sa 03:30-19:00)"));
    66         assertTrue(test.isValueValid("motor_vehicle:conditional", "no @ (10:00-18:00 AND length>5)"));
    67         assertFalse(test.isValueValid("motor_vehicle:conditional", "foo @ (10:00-18:00 AND length>5)"));
    68         assertFalse(test.isValueValid("motor_vehicle:conditional", "no @ (10:00until18:00 AND length>5)"));
    69         assertTrue(test.isValueValid("maxspeed:hgv:conditional", "60 @ (weight>7.5)"));
    70         assertTrue(test.isValueValid("restriction:conditional", "no_left_turn @ (Mo-Fr 16:00-18:00)"));
     61        assertTrue(isValueValid("maxspeed:conditional", "120 @ (06:00-19:00)"));
     62        assertFalse(isValueValid("maxspeed:conditional", " @ (06:00-19:00)"));
     63        assertFalse(isValueValid("maxspeed:conditional", "120 (06:00-19:00)"));
     64        assertFalse(isValueValid("maxspeed:conditional", "120 @ ()"));
     65        assertFalse(isValueValid("maxspeed:conditional", "120 @ "));
     66        assertFalse(isValueValid("maxspeed:conditional", "120 @ (06:00/19:00)"));
     67        assertTrue(isValueValid("maxspeed:conditional", "120 @ (06:00-20:00); 100 @ (22:00-06:00)"));
     68        assertTrue(isValueValid("motor_vehicle:conditional", "delivery @ (Mo-Fr 06:00-11:00,17:00-19:00;Sa 03:30-19:00)"));
     69        assertTrue(isValueValid("motor_vehicle:conditional", "no @ (10:00-18:00 AND length>5)"));
     70        assertFalse(isValueValid("motor_vehicle:conditional", "foo @ (10:00-18:00 AND length>5)"));
     71        assertFalse(isValueValid("motor_vehicle:conditional", "no @ (10:00until18:00 AND length>5)"));
     72        assertTrue(isValueValid("maxspeed:hgv:conditional", "60 @ (weight>7.5)"));
     73        assertTrue(isValueValid("restriction:conditional", "no_left_turn @ (Mo-Fr 16:00-18:00)"));
    7174    }
     75
     76    /**
     77     * Check if a value is valid
     78     * @param key The key the value is for
     79     * @param value The value
     80     * @return <code>true</code> if it is valid
     81     */
     82    private  boolean isValueValid(String key, String value) {
     83        final Node node = new Node(LatLon.ZERO);
     84        node.put(key, value);
     85        new DataSet(node);
     86        return test.validateValue(key, value, node) == null;
     87    }
     88
    7289}