source: josm/trunk/test/unit/org/openstreetmap/josm/actions/AlignInCircleActionTest.java@ 17386

Last change on this file since 17386 was 17386, checked in by GerdP, 3 years ago

fix #20041: Align nodes in Circle creates Command which changes nothing

  • only create move command if node is visibly moved
  • reject a self-intersecting way
  • reject old nodes outside of download area (the original code only shows an info and continues)
  • add some unit tests to improve coverage
File size: 5.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertNotNull;
6import static org.junit.jupiter.api.Assertions.assertThrows;
7
8import java.nio.file.Files;
9import java.nio.file.Paths;
10import java.util.Set;
11import java.util.stream.Collectors;
12
13import org.junit.jupiter.api.Test;
14import org.junit.jupiter.api.extension.RegisterExtension;
15import org.openstreetmap.josm.TestUtils;
16import org.openstreetmap.josm.command.Command;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.Way;
20import org.openstreetmap.josm.io.OsmReader;
21import org.openstreetmap.josm.testutils.JOSMTestRules;
22
23import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24
25/**
26 * Unit tests for class {@link AlignInLineAction}.
27 */
28final class AlignInCircleActionTest {
29
30 /**
31 * Setup test.
32 */
33 @RegisterExtension
34 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
35 public JOSMTestRules test = new JOSMTestRules().projection();
36
37
38 /**
39 * Test case: way with several nodes selected
40 * @throws Exception if an error occurs
41 */
42 @Test
43 void testWaySelected() throws Exception {
44 DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleBefore.osm")), null);
45 DataSet ds2 = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleAfter1.osm")), null);
46
47 Way roundabout = null;
48 for (Way w : ds.getWays()) {
49 if ("roundabout".equals(w.get("junction"))) {
50 roundabout = w;
51 break;
52 }
53 }
54 assertNotNull(roundabout);
55 if (roundabout != null) {
56 ds.setSelected(roundabout);
57 Command c = AlignInCircleAction.buildCommand(ds);
58 c.executeCommand();
59 Way expected = (Way) ds2.getPrimitiveById(roundabout);
60 assertNotNull(expected);
61 assertEquals(expected, roundabout);
62 assertEquals(expected.getNodesCount(), roundabout.getNodesCount());
63 for (Node n1 : roundabout.getNodes()) {
64 Node n2 = (Node) ds2.getPrimitiveById(n1);
65 assertEquals(n1.lat(), n2.lat(), 1e-5);
66 assertEquals(n1.lon(), n2.lon(), 1e-5);
67 }
68 }
69 }
70
71 /**
72 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/20041">Bug #20041</a>.
73 * Don't create move commands when no node is visibly moved
74 * @throws Exception if an error occurs
75 */
76 @Test
77 void testTicket20041() throws Exception {
78 DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleAfter1.osm")), null);
79
80 Way roundabout = null;
81 for (Way w : ds.getWays()) {
82 if ("roundabout".equals(w.get("junction"))) {
83 roundabout = w;
84 break;
85 }
86 }
87 assertNotNull(roundabout);
88 if (roundabout != null) {
89 ds.setSelected(roundabout);
90 assertThrows(AlignInCircleAction.InvalidSelection.class, () -> AlignInCircleAction.buildCommand(ds));
91 }
92 }
93
94 /**
95 * Test case: way with several nodes selected
96 * @throws Exception if an error occurs
97 */
98 @Test
99 void testNodesSelected() throws Exception {
100 DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleBefore.osm")), null);
101 DataSet ds2 = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleAfter2.osm")), null);
102
103 Way circularWay = null;
104 for (Way w : ds.getWays()) {
105 if ("roundabout".equals(w.get("junction"))) {
106 circularWay = w;
107 break;
108 }
109 }
110 assertNotNull(circularWay);
111 if (circularWay != null) {
112 ds.setSelected(circularWay.getNodes());
113 Command c = AlignInCircleAction.buildCommand(ds);
114 assertNotNull(c);
115 c.executeCommand();
116 Way expected = (Way) ds2.getPrimitiveById(circularWay);
117 assertNotNull(expected);
118 assertEquals(expected, circularWay);
119 assertEquals(expected.getNodesCount(), circularWay.getNodesCount());
120 for (Node n1 : circularWay.getNodes()) {
121 Node n2 = (Node) ds2.getPrimitiveById(n1);
122 assertEquals(n1.lat(), n2.lat(), 1e-5);
123 assertEquals(n1.lon(), n2.lon(), 1e-5);
124 }
125 }
126 }
127
128 /**
129 * Test case: original roundabout was split, two ways selected, they build a closed ring
130 * @throws Exception if an error occurs
131 */
132 @Test
133 void testOpenWaysSelected() throws Exception {
134 DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleTwoWaysBefore.osm")), null);
135 DataSet ds2 = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleTwoWaysAfter.osm")), null);
136
137 Set<Way> junctions = ds.getWays().stream().filter(w -> "roundabout".equals(w.get("junction"))).collect(Collectors.toSet());
138 assertEquals(2, junctions.size());
139 ds.setSelected(junctions);
140 Command c = AlignInCircleAction.buildCommand(ds);
141 assertNotNull(c);
142 c.executeCommand();
143 for (Way way : junctions) {
144 for (Node n1 : way.getNodes()) {
145 Node n2 = (Node) ds2.getPrimitiveById(n1);
146 assertEquals(n1.lat(), n2.lat(), 1e-5);
147 assertEquals(n1.lon(), n2.lon(), 1e-5);
148 }
149 }
150 }
151
152}
Note: See TracBrowser for help on using the repository browser.