Changeset 13489 in josm


Ignore:
Timestamp:
2018-03-03T20:33:09+01:00 (7 years ago)
Author:
Don-vip
Message:

fix #16044 - Autofix invalid URL with the wrong type of slashes

Location:
trunk
Files:
8 edited

Legend:

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

    r13453 r13489  
    191191         * Sets a supplier to obtain a command to fix the error.
    192192         *
    193          * @param fixingCommand the fix supplier
     193         * @param fixingCommand the fix supplier. Can be null
    194194         * @return {@code this}
    195195         */
  • trunk/src/org/openstreetmap/josm/data/validation/tests/InternetTags.java

    r12539 r13489  
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66
     7import java.util.function.Supplier;
     8
     9import org.openstreetmap.josm.command.ChangePropertyCommand;
     10import org.openstreetmap.josm.command.Command;
    711import org.openstreetmap.josm.data.osm.Node;
    812import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    97101        String value = v != null ? v : p.get(k);
    98102        if (!validator.isValid(value)) {
     103            Supplier<Command> fix = null;
    99104            String errMsg = validator.getErrorMessage();
    100             // Special treatment to allow URLs without protocol. See UrlValidator#isValid
    101105            if (tr("URL contains an invalid protocol: {0}", (String) null).equals(errMsg)) {
     106                // Special treatment to allow URLs without protocol. See UrlValidator#isValid
    102107                String proto = validator instanceof EmailValidator ? "mailto://" : "http://";
    103108                return doValidateTag(p, k, proto+value, validator, code);
     109            } else if (tr("URL contains an invalid authority: {0}", (String) null).equals(errMsg)
     110                    && value.contains("\\") && validator.isValid(value.replaceAll("\\\\", "/"))) {
     111                // Special treatment to autofix URLs with backslashes. See UrlValidator#isValid
     112                errMsg = tr("URL contains backslashes instead of slashes");
     113                fix = () -> new ChangePropertyCommand(p, k, value.replaceAll("\\\\", "/"));
    104114            }
    105115            error = TestError.builder(this, Severity.WARNING, code)
    106116                    .message(validator.getValidatorName(), marktr("''{0}'': {1}"), k, errMsg)
    107117                    .primitives(p)
     118                    .fix(fix)
    108119                    .build();
    109120        }
  • trunk/test/unit/org/openstreetmap/josm/TestUtils.java

    r13413 r13489  
    260260
    261261    /**
     262     * Makes sure the given primitive belongs to a data set.
     263     * @param <T> OSM primitive type
     264     * @param osm OSM primitive
     265     * @return OSM primitive, attached to a new {@code DataSet}
     266     */
     267    public static <T extends OsmPrimitive> T addFakeDataSet(T osm) {
     268        new DataSet(osm);
     269        return osm;
     270    }
     271
     272    /**
    262273     * Creates a new node with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)})
    263274     *
  • trunk/test/unit/org/openstreetmap/josm/command/MoveCommandTest.java

    r13079 r13489  
    247247    @Test
    248248    public void testDescription() {
    249         Node node = new Node(LatLon.ZERO);
     249        Node node = TestUtils.addFakeDataSet(new Node(LatLon.ZERO));
    250250        node.put("name", "xy");
    251         new DataSet(node);
    252251        List<OsmPrimitive> nodeList = Arrays.<OsmPrimitive>asList(node);
    253252        assertTrue(new MoveCommand(nodeList, 1, 2).getDescriptionText().matches("Move 1 node"));
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/InternetTagsTest.java

    r10945 r13489  
    22package org.openstreetmap.josm.data.validation.tests;
    33
     4import static org.junit.Assert.assertEquals;
    45import static org.junit.Assert.assertNotNull;
    56import static org.junit.Assert.assertNull;
     7import static org.openstreetmap.josm.tools.I18n.tr;
    68
    79import org.junit.Rule;
    810import org.junit.Test;
    9 import org.openstreetmap.josm.data.osm.OsmUtils;
     11import org.openstreetmap.josm.TestUtils;
    1012import org.openstreetmap.josm.data.validation.TestError;
    1113import org.openstreetmap.josm.data.validation.routines.AbstractValidator;
     
    7577    }
    7678
    77     private static void testKey(String key, String value, boolean valid, AbstractValidator validator, int code) {
    78         TestError error = TEST.validateTag(OsmUtils.createPrimitive("node "+key+"="+value+""), key, validator, code);
     79    /**
     80     * Test of invalid slashes.
     81     */
     82    @Test
     83    public void testInvalidSlashes() {
     84        TestError error = testUrl("website", "http:\\\\www.sjoekurs.no", false);
     85        assertEquals(tr("''{0}'': {1}", "website", tr("URL contains backslashes instead of slashes")), error.getDescription());
     86        assertNotNull(error.getFix());
     87    }
     88
     89    private static TestError testKey(String key, String value, boolean valid, AbstractValidator validator, int code) {
     90        TestError error = TEST.validateTag(TestUtils.addFakeDataSet(TestUtils.newNode(key+"="+value+"")), key, validator, code);
    7991        if (valid) {
    8092            assertNull(error != null ? error.getMessage() : null, error);
     
    8294            assertNotNull(error);
    8395        }
     96        return error;
    8497    }
    8598
    86     private static void testUrl(String key, String value, boolean valid) {
    87         testKey(key, value, valid, UrlValidator.getInstance(), InternetTags.INVALID_URL);
     99    private static TestError testUrl(String key, String value, boolean valid) {
     100        return testKey(key, value, valid, UrlValidator.getInstance(), InternetTags.INVALID_URL);
    88101    }
    89102
    90     private static void testEmail(String key, String value, boolean valid) {
    91         testKey(key, value, valid, EmailValidator.getInstance(), InternetTags.INVALID_EMAIL);
     103    private static TestError testEmail(String key, String value, boolean valid) {
     104        return testKey(key, value, valid, EmailValidator.getInstance(), InternetTags.INVALID_EMAIL);
    92105    }
    93106}
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerTest.java

    r13195 r13489  
    102102    @Test
    103103    public void testTicket10913() throws ParseException {
    104         final OsmPrimitive p = OsmUtils.createPrimitive("way highway=tertiary construction=yes");
     104        final OsmPrimitive p = TestUtils.addFakeDataSet(TestUtils.newWay("highway=tertiary construction=yes"));
    105105        final TagCheck check = TagCheck.readMapCSS(new StringReader("way {" +
    106106                "throwError: \"error\";" +
     
    108108                "fixAdd: \"highway=construction\";\n" +
    109109                "}")).parseChecks.get(0);
    110         new DataSet(p);
    111110        final Command command = check.fixPrimitive(p);
    112111        assertTrue(command instanceof SequenceCommand);
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/TagCheckerTest.java

    r13435 r13489  
    1212import org.junit.Rule;
    1313import org.junit.Test;
    14 import org.openstreetmap.josm.data.osm.DataSet;
     14import org.openstreetmap.josm.TestUtils;
    1515import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1616import org.openstreetmap.josm.data.osm.OsmUtils;
     
    3434
    3535    List<TestError> test(OsmPrimitive primitive) throws IOException {
    36         new DataSet(primitive);
    3736        final TagChecker checker = new TagChecker();
    3837        checker.initialize();
    3938        checker.startTest(null);
    40         checker.check(primitive);
     39        checker.check(TestUtils.addFakeDataSet(primitive));
    4140        return checker.getErrors();
    4241    }
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditorTest.java

    r12933 r13489  
    1111import org.junit.Rule;
    1212import org.junit.Test;
     13import org.openstreetmap.josm.TestUtils;
    1314import org.openstreetmap.josm.data.osm.DataSet;
    1415import org.openstreetmap.josm.data.osm.Node;
     
    9192    @Test
    9293    public void testAddPrimitivesToRelation() {
    93         Relation r = new Relation(1);
    94         new DataSet(r);
     94        Relation r = TestUtils.addFakeDataSet(new Relation(1));
    9595        assertNull(GenericRelationEditor.addPrimitivesToRelation(r, Collections.<OsmPrimitive>emptyList()));
    9696        assertNull(GenericRelationEditor.addPrimitivesToRelation(r, Collections.singleton(new Relation(1))));
Note: See TracChangeset for help on using the changeset viewer.