1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.awt.Rectangle;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.stream.Collectors;
|
---|
13 |
|
---|
14 | import org.junit.jupiter.api.Test;
|
---|
15 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
16 | import org.openstreetmap.josm.TestUtils;
|
---|
17 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
22 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
25 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
26 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
27 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
28 | import org.openstreetmap.josm.io.OsmReader;
|
---|
29 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
30 | import org.openstreetmap.josm.tools.Geometry;
|
---|
31 |
|
---|
32 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Unit tests for class {@link JoinNodeWayAction}.
|
---|
36 | */
|
---|
37 | final class JoinNodeWayActionTest {
|
---|
38 | /**
|
---|
39 | * Setup test.
|
---|
40 | */
|
---|
41 | @RegisterExtension
|
---|
42 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
43 | public JOSMTestRules test = new JOSMTestRules().projection().main().preferences();
|
---|
44 |
|
---|
45 | private void setupMapView(DataSet ds) {
|
---|
46 | // setup a reasonable size for the edit window
|
---|
47 | MainApplication.getMap().mapView.setBounds(new Rectangle(1345, 939));
|
---|
48 | if (ds.getDataSourceBoundingBox() != null) {
|
---|
49 | MainApplication.getMap().mapView.zoomTo(ds.getDataSourceBoundingBox());
|
---|
50 | } else {
|
---|
51 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
52 | for (Layer l : MainApplication.getLayerManager().getLayers()) {
|
---|
53 | l.visitBoundingBox(v);
|
---|
54 | }
|
---|
55 | MainApplication.getMap().mapView.zoomTo(v);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Test case: Move node onto two almost overlapping ways
|
---|
61 | * see #18189 moveontoway.osm
|
---|
62 | * @throws Exception if an error occurs
|
---|
63 | */
|
---|
64 | @Test
|
---|
65 | void testTicket18189() throws Exception {
|
---|
66 | DataSet dataSet = new DataSet();
|
---|
67 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
68 | MainApplication.getLayerManager().addLayer(layer);
|
---|
69 | try {
|
---|
70 | Node n1 = new Node(new LatLon(59.92881498658, 30.30104052971));
|
---|
71 | Node n2 = new Node(new LatLon(59.92881459851, 30.30104056556));
|
---|
72 | Node n3 = new Node(new LatLon(59.92881498658, 30.3010405297));
|
---|
73 | Node n4 = new Node(new LatLon(59.92881459851, 30.30104056556));
|
---|
74 | Node n5 = new Node(new LatLon(59.92881483122, 30.30104056465));
|
---|
75 |
|
---|
76 | dataSet.addPrimitive(n1);
|
---|
77 | dataSet.addPrimitive(n2);
|
---|
78 | dataSet.addPrimitive(n3);
|
---|
79 | dataSet.addPrimitive(n4);
|
---|
80 | dataSet.addPrimitive(n5);
|
---|
81 |
|
---|
82 | Way w1 = new Way();
|
---|
83 | w1.setNodes(Arrays.asList(n1, n2));
|
---|
84 | dataSet.addPrimitive(w1);
|
---|
85 | Way w2 = new Way();
|
---|
86 | w2.setNodes(Arrays.asList(n3, n4));
|
---|
87 | dataSet.addPrimitive(w2);
|
---|
88 |
|
---|
89 | dataSet.addSelected(n5);
|
---|
90 | EastNorth expected = Geometry.closestPointToSegment(n1.getEastNorth(), n2.getEastNorth(), n5.getEastNorth());
|
---|
91 |
|
---|
92 | setupMapView(dataSet);
|
---|
93 | JoinNodeWayAction action = JoinNodeWayAction.createMoveNodeOntoWayAction();
|
---|
94 | action.setEnabled(true);
|
---|
95 | action.actionPerformed(null);
|
---|
96 | // Make sure the node was only moved once
|
---|
97 | assertTrue(w1.containsNode(n5), "Node n5 wasn't added to way w1.");
|
---|
98 | assertTrue(w2.containsNode(n5), "Node n5 wasn't added to way w2.");
|
---|
99 | assertTrue(n5.getEastNorth().equalsEpsilon(expected, 1e-7), "Node was moved to an unexpected position");
|
---|
100 | } finally {
|
---|
101 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/11508">Bug #11508</a>.
|
---|
107 | * @throws Exception if an error occurs
|
---|
108 | */
|
---|
109 | @Test
|
---|
110 | void testTicket11508() throws Exception {
|
---|
111 | DataSet ds = OsmReader.parseDataSet(TestUtils.getRegressionDataStream(11508, "11508_example.osm"), null);
|
---|
112 | Layer layer = new OsmDataLayer(ds, OsmDataLayer.createNewName(), null);
|
---|
113 | MainApplication.getLayerManager().addLayer(layer);
|
---|
114 | try {
|
---|
115 | List<Node> nodesToMove = ds.getNodes().stream().filter(n -> n.hasTag("name", "select me and press N"))
|
---|
116 | .collect(Collectors.toList());
|
---|
117 | assertEquals(1, nodesToMove.size());
|
---|
118 | Node toMove = nodesToMove.iterator().next();
|
---|
119 | Node expected = new Node(new LatLon(47.56331849690742, 8.800789259499311));
|
---|
120 | ds.setSelected(toMove);
|
---|
121 | setupMapView(ds);
|
---|
122 | JoinNodeWayAction action = JoinNodeWayAction.createMoveNodeOntoWayAction();
|
---|
123 | action.setEnabled(true);
|
---|
124 | action.actionPerformed(null);
|
---|
125 |
|
---|
126 | assertTrue(toMove.getEastNorth().equalsEpsilon(expected.getEastNorth(), 1e-7), "Node was moved to an unexpected position");
|
---|
127 | assertEquals(2, toMove.getParentWays().size(), "Node was not added to expected number of ways");
|
---|
128 | } finally {
|
---|
129 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Check that nothing is changed if ways are too far.
|
---|
135 | * @throws Exception if an error occurs
|
---|
136 | */
|
---|
137 | @Test
|
---|
138 | void testTicket18189Crossing() throws Exception {
|
---|
139 | DataSet ds = OsmReader.parseDataSet(TestUtils.getRegressionDataStream(18189, "moveontocrossing.osm"), null);
|
---|
140 | Layer layer = new OsmDataLayer(ds, OsmDataLayer.createNewName(), null);
|
---|
141 | MainApplication.getLayerManager().addLayer(layer);
|
---|
142 | try {
|
---|
143 | setupMapView(ds);
|
---|
144 | JoinNodeWayAction action = JoinNodeWayAction.createMoveNodeOntoWayAction();
|
---|
145 | action.setEnabled(true);
|
---|
146 | List<Node> nodesToMove = ds.getNodes().stream().filter(n -> n.hasTag("name", "select me and press N"))
|
---|
147 | .collect(Collectors.toList());
|
---|
148 | assertEquals(1, nodesToMove.size());
|
---|
149 | Node toMove = nodesToMove.iterator().next();
|
---|
150 | ds.setSelected(toMove);
|
---|
151 | action.actionPerformed(null);
|
---|
152 | assertTrue(toMove.getParentWays().isEmpty());
|
---|
153 | } finally {
|
---|
154 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Check that nothing is changed if ways are too far.
|
---|
160 | * @throws Exception if an error occurs
|
---|
161 | */
|
---|
162 | @Test
|
---|
163 | void testTicket18189ThreeWays() throws Exception {
|
---|
164 | DataSet ds = OsmReader.parseDataSet(TestUtils.getRegressionDataStream(18189, "data.osm"), null);
|
---|
165 | Layer layer = new OsmDataLayer(ds, OsmDataLayer.createNewName(), null);
|
---|
166 | MainApplication.getLayerManager().addLayer(layer);
|
---|
167 | try {
|
---|
168 | setupMapView(ds);
|
---|
169 | JoinNodeWayAction action = JoinNodeWayAction.createMoveNodeOntoWayAction();
|
---|
170 | action.setEnabled(true);
|
---|
171 | List<Node> nodesToMove = ds.getNodes().stream().filter(n -> n.hasTag("name", "select me and press N"))
|
---|
172 | .collect(Collectors.toList());
|
---|
173 | assertEquals(1, nodesToMove.size());
|
---|
174 | Node toMove = nodesToMove.iterator().next();
|
---|
175 | Node expected = new Node(new LatLon(-21.088998104148224, -50.38629102179512));
|
---|
176 | ds.setSelected(toMove);
|
---|
177 | action.actionPerformed(null);
|
---|
178 | assertTrue(toMove.getEastNorth().equalsEpsilon(expected.getEastNorth(), 1e-7), "Node was moved to an unexpected position");
|
---|
179 | assertEquals(4, toMove.getParentWays().size(), "Node was not added to expected number of ways");
|
---|
180 | } finally {
|
---|
181 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18420">Bug #18420</a>.
|
---|
187 | * @throws Exception if an error occurs
|
---|
188 | */
|
---|
189 | @Test
|
---|
190 | void testTicket18420() throws Exception {
|
---|
191 | DataSet ds = OsmReader.parseDataSet(TestUtils.getRegressionDataStream(18420, "user-sample.osm"), null);
|
---|
192 | Layer layer = new OsmDataLayer(ds, OsmDataLayer.createNewName(), null);
|
---|
193 | MainApplication.getLayerManager().addLayer(layer);
|
---|
194 | try {
|
---|
195 | List<Node> nodesToMove = ds.getNodes().stream().filter(n -> n.hasTag("name")).collect(Collectors.toList());
|
---|
196 | assertEquals(2, nodesToMove.size());
|
---|
197 | Node n = nodesToMove.iterator().next();
|
---|
198 | if (!n.hasTag("name", "select me 1st"))
|
---|
199 | Collections.reverse(nodesToMove);
|
---|
200 | Node toMove1 = nodesToMove.get(0);
|
---|
201 | Node toMove2 = nodesToMove.get(1);
|
---|
202 | Node expected1 = new Node(new LatLon(49.8546658263727, 6.206059532463773));
|
---|
203 | Node expected2 = new Node(new LatLon(49.854738602108085, 6.206213646054511));
|
---|
204 | ds.setSelected(nodesToMove);
|
---|
205 | setupMapView(ds);
|
---|
206 | JoinNodeWayAction action = JoinNodeWayAction.createMoveNodeOntoWayAction();
|
---|
207 | action.setEnabled(true);
|
---|
208 | action.actionPerformed(null);
|
---|
209 | assertTrue(toMove1.getEastNorth().equalsEpsilon(expected1.getEastNorth(), 1e-7), "Node was moved to an unexpected position");
|
---|
210 | assertTrue(toMove2.getEastNorth().equalsEpsilon(expected2.getEastNorth(), 1e-7), "Node was moved to an unexpected position");
|
---|
211 | assertEquals(2, toMove1.getParentWays().size(), "Node was not added to expected number of ways");
|
---|
212 | assertEquals(2, toMove2.getParentWays().size(), "Node was not added to expected number of ways");
|
---|
213 | } finally {
|
---|
214 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/18990">Bug #18990</a>.
|
---|
220 | * @throws Exception if an error occurs
|
---|
221 | */
|
---|
222 | @Test
|
---|
223 | void testTicket18990() throws Exception {
|
---|
224 | DataSet ds = OsmReader.parseDataSet(TestUtils.getRegressionDataStream(18990, "18990-sample.osm"), null);
|
---|
225 | Layer layer = new OsmDataLayer(ds, OsmDataLayer.createNewName(), null);
|
---|
226 | MainApplication.getLayerManager().addLayer(layer);
|
---|
227 | try {
|
---|
228 | Node toMove = (Node) ds.getPrimitiveById(new SimplePrimitiveId(7018586511L, OsmPrimitiveType.NODE));
|
---|
229 | assertNotNull(toMove);
|
---|
230 | Node expected = new Node(new LatLon(43.48582074476985, -96.76897750613033));
|
---|
231 |
|
---|
232 | ds.setSelected(toMove);
|
---|
233 | setupMapView(ds);
|
---|
234 | JoinNodeWayAction action = JoinNodeWayAction.createMoveNodeOntoWayAction();
|
---|
235 | action.setEnabled(true);
|
---|
236 | action.actionPerformed(null);
|
---|
237 | assertTrue(toMove.getEastNorth().equalsEpsilon(expected.getEastNorth(), 1e-7), "Node was moved to an unexpected position");
|
---|
238 | assertEquals(1, toMove.getParentWays().size(), "Node was not added to expected way");
|
---|
239 | assertEquals(2, toMove.getParentWays().iterator().next().getNodes().indexOf(toMove), "Node was not added to expected way segment");
|
---|
240 | } finally {
|
---|
241 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | }
|
---|