Ticket #11529: 0002-Validate-the-maxweight-tags-and-values.patch

File 0002-Validate-the-maxweight-tags-and-values.patch, 7.8 KB (added by windu.2b, 9 years ago)
  • src/org/openstreetmap/josm/data/validation/OsmValidator.java

    From 762d13817fcfac93142963daf2925c40e456112e Mon Sep 17 00:00:00 2001
    From: Francescu Garoby <windu.2b@gmail.com>
    Date: Sat, 6 Jun 2015 17:25:05 +0200
    Subject: [PATCH 2/2] Validate the 'maxweight:*' tags and values
    
    ---
     .../josm/data/validation/OsmValidator.java         |  2 +
     .../josm/data/validation/tests/Maxweight.java      | 74 ++++++++++++++++++++++
     .../data/validation/tests/MaxweightTest.groovy     | 73 +++++++++++++++++++++
     3 files changed, 149 insertions(+)
     create mode 100644 src/org/openstreetmap/josm/data/validation/tests/Maxweight.java
     create mode 100644 test/unit/org/openstreetmap/josm/data/validation/tests/MaxweightTest.groovy
    
    diff --git a/src/org/openstreetmap/josm/data/validation/OsmValidator.java b/src/org/openstreetmap/josm/data/validation/OsmValidator.java
    index acd2efa..3a6eae9 100644
    a b import org.openstreetmap.josm.data.validation.tests.Lanes;  
    4242import org.openstreetmap.josm.data.validation.tests.LongSegment;
    4343import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
    4444import org.openstreetmap.josm.data.validation.tests.Maxspeed;
     45import org.openstreetmap.josm.data.validation.tests.Maxweight;
    4546import org.openstreetmap.josm.data.validation.tests.MultipolygonTest;
    4647import org.openstreetmap.josm.data.validation.tests.NameMismatch;
    4748import org.openstreetmap.josm.data.validation.tests.OpeningHourTest;
    public class OsmValidator implements LayerChangeListener {  
    128129        ApiCapabilitiesTest.class, // 3400 .. 3499
    129130        LongSegment.class, // 3500 .. 3599
    130131        Maxspeed.class, // 3600 .. 3699
     132        Maxweight.class, // 3600 .. 3799
    131133    };
    132134
    133135    private static Map<String, Test> allTestsMap;
  • new file src/org/openstreetmap/josm/data/validation/tests/Maxweight.java

    diff --git a/src/org/openstreetmap/josm/data/validation/tests/Maxweight.java b/src/org/openstreetmap/josm/data/validation/tests/Maxweight.java
    new file mode 100644
    index 0000000..d8cfb31
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.validation.tests;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.util.ArrayList;
     7import java.util.Arrays;
     8import java.util.Collection;
     9import java.util.regex.Pattern;
     10
     11import org.openstreetmap.josm.data.osm.OsmPrimitive;
     12import org.openstreetmap.josm.data.validation.Severity;
     13import org.openstreetmap.josm.data.validation.Test.TagTest;
     14import org.openstreetmap.josm.data.validation.TestError;
     15import org.openstreetmap.josm.tools.Predicates;
     16import org.openstreetmap.josm.tools.Utils;
     17
     18/**
     19 * Test that validates {@code maxweight:} tags.
     20 *
     21 */
     22public class Maxweight extends TagTest {
     23    protected static Pattern maxweightPattern = Pattern.compile("^((\\+?\\d*(\\.\\d+)?)(\\s(t|kg))?)$");
     24
     25    private static final String[] BLACKLIST = {
     26    };
     27
     28    /**
     29     * Constructs a new {@code Maxweight} test.
     30     */
     31    public Maxweight() {
     32        super(tr("Maxweight"),
     33              tr("This tests for maxweights, which are usually errors."));
     34    }
     35
     36    protected void checkMaxweightByKey(final OsmPrimitive p, String maxweightKey, Pattern pattern, int errorCode, String errorMessage) {
     37        final Collection<String> keysForPattern = new ArrayList<>(Utils.filter(p.keySet(),
     38                Predicates.stringContainsPattern(Pattern.compile(maxweightKey))));
     39        keysForPattern.removeAll(Arrays.asList(BLACKLIST));
     40        if (keysForPattern.isEmpty()) {
     41            // nothing to check
     42            return;
     43        }
     44        String value = p.get(keysForPattern.iterator().next());
     45        if( !pattern.matcher(value).find()) {
     46            errors.add(new TestError(this, Severity.WARNING, tr(errorMessage, maxweightKey), errorCode, p));
     47        }
     48    }
     49
     50    protected void checkMaxweight(final OsmPrimitive p) {
     51        final String backward = Utils.firstNonNull(p.get("maxweight:backward"), 0.0).toString();
     52        final String forward = Utils.firstNonNull(p.get("maxweight:forward"), 0.0).toString();
     53        try {
     54            if (Float.compare(Float.parseFloat(backward), Float.parseFloat(forward)) == 0 && Float.parseFloat(backward) > 0) {
     55                errors.add(new TestError(this, Severity.WARNING,
     56                        tr("Value of {0} and {1} are equals. You should merge them in one tag : {2}", "maxweight:backward", "maxweight:forward", "maxweight"), 3710, p));
     57            }
     58        } catch (NumberFormatException ignore) {
     59            if (backward.compareTo(forward) == 0) {
     60            errors.add(new TestError(this, Severity.WARNING,
     61                    tr("Value of {0} and {1} are equals. You should merge them in one tag : {2}", "maxweight:backward", "maxweight:forward", "maxweight"), 3710, p));
     62            }
     63        }
     64    }
     65
     66    @Override
     67    public void check(OsmPrimitive p) {
     68        checkMaxweightByKey(p, "maxweight", maxweightPattern, 3701, "Value of {0} is not correct");
     69        checkMaxweightByKey(p, "maxweight:backward", maxweightPattern, 3701, "Value of {0} is not correct");
     70        checkMaxweightByKey(p, "maxweight:forward", maxweightPattern, 3701, "Value of {0} is not correct");
     71        checkMaxweight(p);
     72    }
     73
     74}
  • new file test/unit/org/openstreetmap/josm/data/validation/tests/MaxweightTest.groovy

    diff --git a/test/unit/org/openstreetmap/josm/data/validation/tests/MaxweightTest.groovy b/test/unit/org/openstreetmap/josm/data/validation/tests/MaxweightTest.groovy
    new file mode 100644
    index 0000000..297f6ad
    - +  
     1
     2package org.openstreetmap.josm.data.validation.tests
     3
     4import org.openstreetmap.josm.JOSMFixture
     5import org.openstreetmap.josm.data.osm.OsmPrimitive
     6import org.openstreetmap.josm.data.osm.OsmUtils
     7import org.openstreetmap.josm.data.osm.Way
     8
     9
     10class MaxweightTest extends GroovyTestCase {
     11
     12    Maxweight lanes = new Maxweight()
     13
     14    @Override
     15    void setUp() {
     16        JOSMFixture.createUnitTestFixture().init()
     17        lanes.initialize()
     18        lanes.startTest(null)
     19    }
     20
     21    void test1() {
     22        lanes.check(OsmUtils.createPrimitive("way maxweight=5"))
     23        assert lanes.errors.size() == 0
     24    }
     25
     26    void test2() {
     27        lanes.check(OsmUtils.createPrimitive("way maxweight=3.5"))
     28        assert lanes.errors.size() == 0
     29    }
     30
     31    void test3() {
     32        lanes.check(OsmUtils.createPrimitive("way maxweight=5 t"))
     33        assert lanes.errors.size() == 0
     34    }
     35
     36    void test4() {
     37        lanes.check(OsmUtils.createPrimitive("way maxweight=3500 kg"))
     38        assert lanes.errors.size() == 0
     39    }
     40
     41    void test5() {
     42        lanes.check(OsmUtils.createPrimitive("way maxweight=3.5 t"))
     43        assert lanes.errors.size() == 0
     44    }
     45
     46    void test6() {
     47        lanes.check(OsmUtils.createPrimitive("way maxweight=5t"))
     48        assert lanes.errors.get(0).getMessage() == "Value of maxweight is not correct"
     49    }
     50
     51    void test7() {
     52        lanes.check(OsmUtils.createPrimitive("way maxweight=3500kg"))
     53        assert lanes.errors.get(0).getMessage() == "Value of maxweight is not correct"
     54    }
     55
     56    void test8() {
     57        lanes.check(OsmUtils.createPrimitive("way maxweight=5 to"))
     58        assert lanes.errors.get(0).getMessage() == "Value of maxweight is not correct"
     59    }
     60
     61    void test9() {
     62        lanes.check(OsmUtils.createPrimitive("way maxweight=5. t"))
     63        assert lanes.errors.get(0).getMessage() == "Value of maxweight is not correct"
     64    }
     65
     66    void test10() {
     67        OsmPrimitive p = new Way();
     68        p.put("maxweight:forward", "5 t");
     69        p.put("maxweight:backward", "5 t");
     70        lanes.check(p)
     71        assert lanes.errors.get(0).getMessage() == "Value of maxweight:backward and maxweight:forward are equals. You should merge them in one tag : maxweight"
     72    }
     73}