Ticket #20013: 20013.2.patch

File 20013.2.patch, 4.5 KB (added by GerdP, 4 years ago)

fix and unit test

  • src/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrector.java

     
    1313import java.util.function.Function;
    1414import java.util.regex.Matcher;
    1515import java.util.regex.Pattern;
     16import java.util.stream.Collectors;
    1617
    1718import org.openstreetmap.josm.command.Command;
    1819import org.openstreetmap.josm.data.correction.RoleCorrection;
     
    2425import org.openstreetmap.josm.data.osm.Relation;
    2526import org.openstreetmap.josm.data.osm.RelationMember;
    2627import org.openstreetmap.josm.data.osm.Tag;
    27 import org.openstreetmap.josm.data.osm.TagCollection;
    2828import org.openstreetmap.josm.data.osm.Tagged;
    2929import org.openstreetmap.josm.data.osm.Way;
    3030import org.openstreetmap.josm.tools.Logging;
     
    173173    /**
    174174     * Tests whether way can be reversed without semantic change, i.e., whether tags have to be changed.
    175175     * Looks for keys like oneway, oneway:bicycle, cycleway:right:oneway, left/right.
     176     * Also tests the nodes, e.g. a highway=stop with direction, see #20013.
    176177     * @param way way to test
    177178     * @return false if tags should be changed to keep semantic, true otherwise.
    178179     */
    179180    public static boolean isReversible(Way way) {
    180         for (Tag tag : TagCollection.from(way)) {
    181             if (!tag.equals(TagSwitcher.apply(tag))) {
    182                 return false;
    183             }
    184         }
    185         return true;
     181        return getTagCorrectionsMap(way).isEmpty();
    186182    }
    187183
    188184    /**
     
    192188     * @see #isReversible(Way)
    193189     */
    194190    public static List<Way> irreversibleWays(List<Way> ways) {
    195         List<Way> newWays = new ArrayList<>(ways);
    196         for (Way way : ways) {
    197             if (isReversible(way)) {
    198                 newWays.remove(way);
    199             }
    200         }
    201         return newWays;
     191        return ways.stream().filter(w -> !isReversible(w)).collect(Collectors.toList());
    202192    }
    203193
    204194    /**
  • test/unit/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrectorTest.java

     
    77import java.util.stream.Stream;
    88
    99import org.junit.Assert;
     10import org.junit.jupiter.api.Test;
    1011import org.junit.jupiter.api.extension.RegisterExtension;
    11 import org.junit.jupiter.api.Test;
    1212import org.openstreetmap.josm.data.correction.TagCorrection;
    1313import org.openstreetmap.josm.data.osm.Node;
    1414import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    107107        Assert.assertEquals(newTag, ReverseWayTagCorrector.TagSwitcher.apply(oldTag));
    108108    }
    109109
    110     private Map<OsmPrimitive, List<TagCorrection>> getTagCorrectionsForWay(String middleNodeTags) {
     110    private Way buildWayWithMiddleNode(String middleNodeTags) {
    111111        final OsmPrimitive n1 = OsmUtils.createPrimitive("node");
    112112        final OsmPrimitive n2 = OsmUtils.createPrimitive("node " + middleNodeTags);
    113113        final OsmPrimitive n3 = OsmUtils.createPrimitive("node");
    114114        final Way w = new Way();
    115115        Stream.of(n1, n2, n3).map(Node.class::cast).forEach(w::addNode);
     116        return w;
     117    }
     118
     119    private Map<OsmPrimitive, List<TagCorrection>> getTagCorrectionsForWay(String middleNodeTags) {
     120        Way w = buildWayWithMiddleNode(middleNodeTags);
    116121        return ReverseWayTagCorrector.getTagCorrectionsMap(w);
    117122    }
    118123
     
    135140        Assert.assertEquals(0, getTagCorrectionsForWay("direction=SSW").size());
    136141        Assert.assertEquals(0, getTagCorrectionsForWay("direction=145").size());
    137142    }
     143
     144    /**
     145     * Tests that IsReversible() also works for nodes. See #20013
     146     */
     147    @Test
     148    void testIsReversible() {
     149        Way w0 = buildWayWithMiddleNode("highway=stop");
     150        Assert.assertTrue(ReverseWayTagCorrector.isReversible(w0));
     151        Way w1 = buildWayWithMiddleNode("direction=forward");
     152        Assert.assertFalse(ReverseWayTagCorrector.isReversible(w1));
     153        Assert.assertEquals(3, w1.getNodesCount());
     154        w1.getNodes().forEach(n -> n.setKeys(null));
     155        Assert.assertTrue(ReverseWayTagCorrector.isReversible(w1));
     156        w1.put("oneway", "yes");
     157        Assert.assertFalse(ReverseWayTagCorrector.isReversible(w1));
     158    }
     159
    138160}