source: josm/trunk/test/unit/org/openstreetmap/josm/actions/AlignInLineActionTest.java@ 17360

Last change on this file since 17360 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 8.0 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.assertNull;
7import static org.junit.jupiter.api.Assertions.assertThrows;
8
9import org.junit.jupiter.api.BeforeEach;
10import org.junit.jupiter.api.Test;
11import org.junit.jupiter.api.extension.RegisterExtension;
12import org.openstreetmap.josm.actions.AlignInLineAction.InvalidSelection;
13import org.openstreetmap.josm.actions.AlignInLineAction.Line;
14import org.openstreetmap.josm.data.coor.EastNorth;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.Way;
19import org.openstreetmap.josm.gui.MainApplication;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * Unit tests for class {@link AlignInLineAction}.
26 */
27final class AlignInLineActionTest {
28
29 /**
30 * Setup test.
31 */
32 @RegisterExtension
33 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
34 public JOSMTestRules test = new JOSMTestRules().main().projection();
35
36 /** Class under test. */
37 private static AlignInLineAction action;
38
39 /**
40 * Setup test.
41 */
42 @BeforeEach
43 public void setUp() {
44 // Enable "Align in line" feature.
45 action = MainApplication.getMenu().alignInLine;
46 action.setEnabled(true);
47 }
48
49 /**
50 * Test case: only nodes selected, part of an open way: align these nodes on the line passing through the extremity
51 * nodes (the most distant in the way sequence, not the most euclidean-distant). See
52 * https://josm.openstreetmap.de/ticket/9605#comment:3. Note that in this test, after alignment, way is overlapping
53 * itself.
54 * @throws InvalidSelection never
55 */
56 @Test
57 void testNodesOpenWay() throws InvalidSelection {
58 DataSet dataSet = new DataSet();
59
60 // Create test points, lower left is (0,0).
61 //
62 // 1 - - -
63 // - 3 - 2
64 // - - - -
65 Node point1 = new Node(new EastNorth(0, 2));
66 Node point2 = new Node(new EastNorth(3, 1));
67 Node point3 = new Node(new EastNorth(1, 1));
68
69 // Create an open way.
70 createWay(dataSet, point1, point2, point3);
71
72 // Select nodes to align.
73 dataSet.addSelected(point1, point2, point3);
74
75 action.buildCommand(dataSet).executeCommand();
76
77 // Points 1 and 3 are the extremities and must not have moved. Only point 2 must have moved.
78 assertCoordEq(point1, 0, 2);
79 assertCoordEq(point2, 2, 0);
80 assertCoordEq(point3, 1, 1);
81 }
82
83 /**
84 * Test case: only nodes selected, part of a closed way: align these nodes on the line passing through the most
85 * distant nodes.
86 * @throws InvalidSelection never
87 */
88 @Test
89 void testNodesClosedWay() throws InvalidSelection {
90 DataSet dataSet = new DataSet();
91
92 // Create test points, lower left is (0,0).
93 //
94 // 4 - 3
95 // - - -
96 // 1 - 2
97 Node point1 = new Node(new EastNorth(0, 0));
98 Node point2 = new Node(new EastNorth(2, 0));
99 Node point3 = new Node(new EastNorth(2, 2));
100 Node point4 = new Node(new EastNorth(0, 2));
101
102 // Create a closed way.
103 createWay(dataSet, point1, point2, point3, point4, point1);
104 // Select nodes to align (point1 must be in the second position to exhibit the bug).
105 dataSet.addSelected(point4, point1, point2);
106
107 action.buildCommand(dataSet).executeCommand();
108
109 // Only point 1 must have moved.
110 assertCoordEq(point1, 1, 1);
111 assertCoordEq(point2, 2, 0);
112 assertCoordEq(point3, 2, 2);
113 assertCoordEq(point4, 0, 2);
114 }
115
116 /**
117 * Test case: only nodes selected, part of multiple ways: align these nodes on the line passing through the most
118 * distant nodes.
119 * @throws InvalidSelection never
120 */
121 @Test
122 void testNodesOpenWays() throws InvalidSelection {
123 DataSet dataSet = new DataSet();
124
125 // Create test points, lower left is (0,0).
126 //
127 // 1 - -
128 // 3 - 2
129 // - - 4
130 Node point1 = new Node(new EastNorth(0, 2));
131 Node point2 = new Node(new EastNorth(2, 1));
132 Node point3 = new Node(new EastNorth(0, 1));
133 Node point4 = new Node(new EastNorth(2, 0));
134
135 // Create 2 ways.
136 createWay(dataSet, point1, point2);
137 createWay(dataSet, point3, point4);
138
139 // Select nodes to align.
140 dataSet.addSelected(point1, point2, point3, point4);
141
142 // Points must align between points 1 and 4.
143 action.buildCommand(dataSet).executeCommand();
144
145 assertCoordEq(point1, 0, 2);
146 assertCoordEq(point2, 1.5, 0.5);
147 assertCoordEq(point3, 0.5, 1.5);
148 assertCoordEq(point4, 2, 0);
149 }
150
151 /**
152 * Test case: only a two-nodes way selected.
153 * @throws InvalidSelection never
154 */
155 @Test
156 void testSimpleWay() throws InvalidSelection {
157 DataSet dataSet = new DataSet();
158
159 // Create test points, lower left is (0,0).
160 //
161 // 1 - -
162 // - - 2
163 Node point1 = new Node(new EastNorth(0, 2));
164 Node point2 = new Node(new EastNorth(2, 1));
165
166 // Creates and select a single way.
167 dataSet.addSelected(createWay(dataSet, point1, point2));
168
169 // No command must be created (nothing to do)
170 assertNull(action.buildCommand(dataSet));
171 }
172
173 /**
174 * Create a way made of the provided nodes and select nodes.
175 *
176 * @param dataSet Dataset in which adding nodes.
177 * @param nodes List of nodes to add to dataset.
178 * @return created way
179 */
180 private Way createWay(DataSet dataSet, Node... nodes) {
181 Way way = new Way();
182 dataSet.addPrimitive(way);
183
184 for (Node node : nodes) {
185 // Add primitive to dataset only if not already included.
186 if (dataSet.getPrimitiveById(node) == null)
187 dataSet.addPrimitive(node);
188
189 way.addNode(node);
190 }
191 return way;
192 }
193
194 /**
195 * Assert that the provided node has the specified coordinates. If not fail the test.
196 *
197 * @param node Node to test.
198 * @param x X coordinate.
199 * @param y Y coordinate.
200 */
201 private void assertCoordEq(Node node, double x, double y) {
202 EastNorth coordinate = node.getEastNorth();
203 assertEquals(x, coordinate.getX(), LatLon.MAX_SERVER_PRECISION, "Wrong x coordinate.");
204 assertEquals(y, coordinate.getY(), LatLon.MAX_SERVER_PRECISION, "Wrong y coordinate.");
205 }
206
207 /**
208 * Test that a {@link Line} can be constructed with nodes of different coordinates.
209 * @throws InvalidSelection never
210 */
211 @Test
212 void testLineDifferentCoordinates() throws InvalidSelection {
213 assertNotNull(new Line(new Node(new EastNorth(0, 1)),
214 new Node(new EastNorth(0, 2))));
215 assertNotNull(new Line(new Node(new EastNorth(0, 1)),
216 new Node(new EastNorth(1, 1))));
217 assertNotNull(new Line(new Node(new EastNorth(0, 1)),
218 new Node(new EastNorth(0+1e-150, 1+1e-150))));
219 }
220
221 /**
222 * Test that a {@link Line} cannot be constructed with nodes of same coordinates.
223 * @throws InvalidSelection always
224 */
225 @Test
226 void testLineSameCoordinates1() throws InvalidSelection {
227 assertThrows(InvalidSelection.class, () -> new Line(new Node(new EastNorth(0, 1)),
228 new Node(new EastNorth(0, 1))));
229 }
230
231 /**
232 * Test that a {@link Line} cannot be constructed with nodes of same coordinates.
233 * @throws InvalidSelection always
234 */
235 @Test
236 void testLineSameCoordinates2() throws InvalidSelection {
237 assertThrows(InvalidSelection.class, () -> new Line(new Node(new EastNorth(0, 1)),
238 new Node(new EastNorth(0+1e-175, 1+1e-175))));
239 }
240}
Note: See TracBrowser for help on using the repository browser.