1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.corrector;
|
---|
3 |
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Map;
|
---|
7 | import java.util.stream.Stream;
|
---|
8 |
|
---|
9 | import org.junit.Assert;
|
---|
10 | import org.junit.jupiter.api.Test;
|
---|
11 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
12 | import org.openstreetmap.josm.data.correction.TagCorrection;
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
16 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
19 |
|
---|
20 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
21 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Unit tests of {@link ReverseWayTagCorrector} class.
|
---|
25 | */
|
---|
26 | class ReverseWayTagCorrectorTest {
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Setup test.
|
---|
30 | */
|
---|
31 | @RegisterExtension
|
---|
32 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
33 | public JOSMTestRules test = new JOSMTestRules();
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Tests that {@code ReverseWayTagCorrector.TagSwitcher} satisfies utility class criteria.
|
---|
37 | * @throws ReflectiveOperationException if an error occurs
|
---|
38 | */
|
---|
39 | @Test
|
---|
40 | void testUtilityClass() throws ReflectiveOperationException {
|
---|
41 | UtilityClassTestUtil.assertUtilityClassWellDefined(ReverseWayTagCorrector.TagSwitcher.class);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Test of {@link ReverseWayTagCorrector.TagSwitcher#apply} method.
|
---|
46 | */
|
---|
47 | @Test
|
---|
48 | void testTagSwitch() {
|
---|
49 | // oneway
|
---|
50 | assertSwitch(new Tag("oneway", "yes"), new Tag("oneway", "-1"));
|
---|
51 | assertSwitch(new Tag("oneway", "true"), new Tag("oneway", "-1"));
|
---|
52 | assertSwitch(new Tag("oneway", "-1"), new Tag("oneway", "yes"));
|
---|
53 | assertSwitch(new Tag("oneway", "no"), new Tag("oneway", "no"));
|
---|
54 | assertSwitch(new Tag("oneway", "something"), new Tag("oneway", "something"));
|
---|
55 | // incline/direction
|
---|
56 | for (String k : new String[]{"incline", "direction"}) {
|
---|
57 | assertSwitch(new Tag(k, "up"), new Tag(k, "down"));
|
---|
58 | assertSwitch(new Tag(k, "down"), new Tag(k, "up"));
|
---|
59 | assertSwitch(new Tag(k, "something"), new Tag(k, "something"));
|
---|
60 | }
|
---|
61 | // numbered incline (see #19508)
|
---|
62 | assertSwitch(new Tag("incline", "+0%"), new Tag("incline", "0%"));
|
---|
63 | assertSwitch(new Tag("incline", ".1%"), new Tag("incline", "-.1%"));
|
---|
64 | assertSwitch(new Tag("incline", "-10.0%"), new Tag("incline", "10.0%"));
|
---|
65 | assertSwitch(new Tag("incline", "0,6°"), new Tag("incline", "-0.6°"));
|
---|
66 | // direction=forward/backward/...
|
---|
67 | assertSwitch(new Tag("direction", "forward"), new Tag("direction", "backward"));
|
---|
68 | assertSwitch(new Tag("direction", "backward"), new Tag("direction", "forward"));
|
---|
69 | // :left/:right with oneway (see #10977)
|
---|
70 | assertSwitch(new Tag("cycleway:left:oneway", "-1"), new Tag("cycleway:right:oneway", "yes"));
|
---|
71 | // :forward/:backward (see #8518)
|
---|
72 | assertSwitch(new Tag("turn:forward", "right"), new Tag("turn:backward", "right"));
|
---|
73 | assertSwitch(new Tag("change:forward", "not_right"), new Tag("change:backward", "not_right"));
|
---|
74 | assertSwitch(new Tag("placement:forward", "right_of:1"), new Tag("placement:backward", "right_of:1"));
|
---|
75 | assertSwitch(new Tag("turn:lanes:forward", "left|right"), new Tag("turn:lanes:backward", "left|right"));
|
---|
76 | assertSwitch(new Tag("change:lanes:forward", "not_right|only_left"), new Tag("change:lanes:backward", "not_right|only_left"));
|
---|
77 | // keys
|
---|
78 | assertSwitch(new Tag("forward", "something"), new Tag("backward", "something"));
|
---|
79 | assertSwitch(new Tag("backward", "something"), new Tag("forward", "something"));
|
---|
80 | assertSwitch(new Tag("up", "something"), new Tag("down", "something"));
|
---|
81 | assertSwitch(new Tag("down", "something"), new Tag("up", "something"));
|
---|
82 | // values
|
---|
83 | assertSwitch(new Tag("something", "forward"), new Tag("something", "backward"));
|
---|
84 | assertSwitch(new Tag("something", "backward"), new Tag("something", "forward"));
|
---|
85 | assertSwitch(new Tag("something", "up"), new Tag("something", "down"));
|
---|
86 | assertSwitch(new Tag("something", "down"), new Tag("something", "up"));
|
---|
87 | // value[:_]suffix
|
---|
88 | assertSwitch(new Tag("something", "forward:suffix"), new Tag("something", "backward:suffix"));
|
---|
89 | assertSwitch(new Tag("something", "backward_suffix"), new Tag("something", "forward_suffix"));
|
---|
90 | assertSwitch(new Tag("something", "up:suffix"), new Tag("something", "down:suffix"));
|
---|
91 | assertSwitch(new Tag("something", "down_suffix"), new Tag("something", "up_suffix"));
|
---|
92 | // prefix[:_]value
|
---|
93 | assertSwitch(new Tag("something", "prefix:forward"), new Tag("something", "prefix:backward"));
|
---|
94 | assertSwitch(new Tag("something", "prefix_backward"), new Tag("something", "prefix_forward"));
|
---|
95 | assertSwitch(new Tag("something", "prefix:up"), new Tag("something", "prefix:down"));
|
---|
96 | assertSwitch(new Tag("something", "prefix_down"), new Tag("something", "prefix_up"));
|
---|
97 | // prefix[:_]value[:_]suffix
|
---|
98 | assertSwitch(new Tag("something", "prefix:forward:suffix"), new Tag("something", "prefix:backward:suffix"));
|
---|
99 | assertSwitch(new Tag("something", "prefix_backward:suffix"), new Tag("something", "prefix_forward:suffix"));
|
---|
100 | assertSwitch(new Tag("something", "prefix:up_suffix"), new Tag("something", "prefix:down_suffix"));
|
---|
101 | assertSwitch(new Tag("something", "prefix_down_suffix"), new Tag("something", "prefix_up_suffix"));
|
---|
102 | // #8499
|
---|
103 | assertSwitch(new Tag("type", "drawdown"), new Tag("type", "drawdown"));
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void assertSwitch(Tag oldTag, Tag newTag) {
|
---|
107 | Assert.assertEquals(newTag, ReverseWayTagCorrector.TagSwitcher.apply(oldTag));
|
---|
108 | }
|
---|
109 |
|
---|
110 | private Way buildWayWithMiddleNode(String middleNodeTags) {
|
---|
111 | final OsmPrimitive n1 = OsmUtils.createPrimitive("node");
|
---|
112 | final OsmPrimitive n2 = OsmUtils.createPrimitive("node " + middleNodeTags);
|
---|
113 | final OsmPrimitive n3 = OsmUtils.createPrimitive("node");
|
---|
114 | final Way w = new Way();
|
---|
115 | 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);
|
---|
121 | return ReverseWayTagCorrector.getTagCorrectionsMap(w);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Test tag correction on way nodes
|
---|
126 | */
|
---|
127 | @Test
|
---|
128 | void testSwitchingWayNodes() {
|
---|
129 | final Map<OsmPrimitive, List<TagCorrection>> tagCorrections = getTagCorrectionsForWay("direction=forward");
|
---|
130 | Assert.assertEquals(1, tagCorrections.size());
|
---|
131 | Assert.assertEquals(Collections.singletonList(new TagCorrection("direction", "forward", "direction", "backward")),
|
---|
132 | tagCorrections.values().iterator().next());
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Test tag correction on way nodes are not applied for absolute values such as compass cardinal directions
|
---|
137 | */
|
---|
138 | @Test
|
---|
139 | void testNotSwitchingWayNodes() {
|
---|
140 | Assert.assertEquals(0, getTagCorrectionsForWay("direction=SSW").size());
|
---|
141 | Assert.assertEquals(0, getTagCorrectionsForWay("direction=145").size());
|
---|
142 | }
|
---|
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 |
|
---|
160 | }
|
---|