1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 |
|
---|
7 | import org.junit.BeforeClass;
|
---|
8 | import org.junit.Test;
|
---|
9 | import org.openstreetmap.josm.JOSMFixture;
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.actions.AlignInLineAction.InvalidSelection;
|
---|
12 | import org.openstreetmap.josm.actions.AlignInLineAction.Line;
|
---|
13 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Unit tests for class {@link AlignInLineAction}.
|
---|
22 | */
|
---|
23 | public final class AlignInLineActionTest {
|
---|
24 |
|
---|
25 | /** Class under test. */
|
---|
26 | private static AlignInLineAction action;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Setup test.
|
---|
30 | */
|
---|
31 | @BeforeClass
|
---|
32 | public static void setUp() {
|
---|
33 | JOSMFixture.createUnitTestFixture().init(true);
|
---|
34 |
|
---|
35 | // Enable "Align in line" feature.
|
---|
36 | action = Main.main.menu.alignInLine;
|
---|
37 | action.setEnabled(true);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Test case: only nodes selected, part of an open way: align these nodes on the line passing through the extremity
|
---|
42 | * nodes (the most distant in the way sequence, not the most euclidean-distant). See
|
---|
43 | * https://josm.openstreetmap.de/ticket/9605#comment:3. Note that in this test, after alignment, way is overlapping
|
---|
44 | * itself.
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void nodesOpenWay() {
|
---|
48 | DataSet dataSet = new DataSet();
|
---|
49 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
50 |
|
---|
51 | // Create test points, lower left is (0,0).
|
---|
52 | //
|
---|
53 | // 1 - - -
|
---|
54 | // - 3 - 2
|
---|
55 | // - - - -
|
---|
56 | Node point1 = new Node(new EastNorth(0, 2));
|
---|
57 | Node point2 = new Node(new EastNorth(3, 1));
|
---|
58 | Node point3 = new Node(new EastNorth(1, 1));
|
---|
59 |
|
---|
60 | try {
|
---|
61 | Main.main.addLayer(layer);
|
---|
62 |
|
---|
63 | // Create an open way.
|
---|
64 | createWay(dataSet, point1, point2, point3);
|
---|
65 |
|
---|
66 | // Select nodes to align.
|
---|
67 | dataSet.addSelected(point1, point2, point3);
|
---|
68 |
|
---|
69 | action.actionPerformed(null);
|
---|
70 | } finally {
|
---|
71 | // Ensure we clean the place before leaving, even if test fails.
|
---|
72 | Main.main.removeLayer(layer);
|
---|
73 | }
|
---|
74 |
|
---|
75 | // Points 1 and 3 are the extremities and must not have moved. Only point 2 must have moved.
|
---|
76 | assertCoordEq(point1, 0, 2);
|
---|
77 | assertCoordEq(point2, 2, 0);
|
---|
78 | assertCoordEq(point3, 1, 1);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Test case: only nodes selected, part of a closed way: align these nodes on the line passing through the most
|
---|
83 | * distant nodes.
|
---|
84 | */
|
---|
85 | @Test
|
---|
86 | public void nodesClosedWay() {
|
---|
87 | DataSet dataSet = new DataSet();
|
---|
88 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
89 |
|
---|
90 | // Create test points, lower left is (0,0).
|
---|
91 | //
|
---|
92 | // 4 - 3
|
---|
93 | // - - -
|
---|
94 | // 1 - 2
|
---|
95 | Node point1 = new Node(new EastNorth(0, 0));
|
---|
96 | Node point2 = new Node(new EastNorth(2, 0));
|
---|
97 | Node point3 = new Node(new EastNorth(2, 2));
|
---|
98 | Node point4 = new Node(new EastNorth(0, 2));
|
---|
99 |
|
---|
100 | try {
|
---|
101 | Main.main.addLayer(layer);
|
---|
102 |
|
---|
103 | // Create a closed way.
|
---|
104 | createWay(dataSet, point1, point2, point3, point4, point1);
|
---|
105 | // Select nodes to align (point1 must be in the second position to exhibit the bug).
|
---|
106 | dataSet.addSelected(point4, point1, point2);
|
---|
107 |
|
---|
108 | action.actionPerformed(null);
|
---|
109 | } finally {
|
---|
110 | // Ensure we clean the place before leaving, even if test fails.
|
---|
111 | Main.main.removeLayer(layer);
|
---|
112 | }
|
---|
113 |
|
---|
114 | // Only point 1 must have moved.
|
---|
115 | assertCoordEq(point1, 1, 1);
|
---|
116 | assertCoordEq(point2, 2, 0);
|
---|
117 | assertCoordEq(point3, 2, 2);
|
---|
118 | assertCoordEq(point4, 0, 2);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Test case: only nodes selected, part of multiple ways: align these nodes on the line passing through the most
|
---|
123 | * distant nodes.
|
---|
124 | */
|
---|
125 | @Test
|
---|
126 | public void nodesOpenWays() {
|
---|
127 | DataSet dataSet = new DataSet();
|
---|
128 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
129 |
|
---|
130 | // Create test points, lower left is (0,0).
|
---|
131 | //
|
---|
132 | // 1 - -
|
---|
133 | // 3 - 2
|
---|
134 | // - - 4
|
---|
135 | Node point1 = new Node(new EastNorth(0, 2));
|
---|
136 | Node point2 = new Node(new EastNorth(2, 1));
|
---|
137 | Node point3 = new Node(new EastNorth(0, 1));
|
---|
138 | Node point4 = new Node(new EastNorth(2, 0));
|
---|
139 |
|
---|
140 | try {
|
---|
141 | Main.main.addLayer(layer);
|
---|
142 |
|
---|
143 | // Create 2 ways.
|
---|
144 | createWay(dataSet, point1, point2);
|
---|
145 | createWay(dataSet, point3, point4);
|
---|
146 |
|
---|
147 | // Select nodes to align.
|
---|
148 | dataSet.addSelected(point1, point2, point3, point4);
|
---|
149 |
|
---|
150 | // Points must align between points 1 and 4.
|
---|
151 | action.actionPerformed(null);
|
---|
152 | } finally {
|
---|
153 | // Ensure we clean the place before leaving, even if test fails.
|
---|
154 | Main.main.removeLayer(layer);
|
---|
155 | }
|
---|
156 |
|
---|
157 | assertCoordEq(point1, 0, 2);
|
---|
158 | assertCoordEq(point2, 1.5, 0.5);
|
---|
159 | assertCoordEq(point3, 0.5, 1.5);
|
---|
160 | assertCoordEq(point4, 2, 0);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Create a way made of the provided nodes and select nodes.
|
---|
165 | *
|
---|
166 | * @param dataSet Dataset in which adding nodes.
|
---|
167 | * @param nodes List of nodes to add to dataset.
|
---|
168 | */
|
---|
169 | private void createWay(DataSet dataSet, Node... nodes) {
|
---|
170 | Way way = new Way();
|
---|
171 | dataSet.addPrimitive(way);
|
---|
172 |
|
---|
173 | for (Node node : nodes) {
|
---|
174 | // Add primitive to dataset only if not already included.
|
---|
175 | if (dataSet.getPrimitiveById(node) == null)
|
---|
176 | dataSet.addPrimitive(node);
|
---|
177 |
|
---|
178 | way.addNode(node);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Assert that the provided node has the specified coordinates. If not fail the test.
|
---|
184 | *
|
---|
185 | * @param node Node to test.
|
---|
186 | * @param x X coordinate.
|
---|
187 | * @param y Y coordinate.
|
---|
188 | */
|
---|
189 | private void assertCoordEq(Node node, double x, double y) {
|
---|
190 | EastNorth coordinate = node.getEastNorth();
|
---|
191 | assertEquals("Wrong x coordinate.", x, coordinate.getX(), LatLon.MAX_SERVER_PRECISION);
|
---|
192 | assertEquals("Wrong y coordinate.", y, coordinate.getY(), LatLon.MAX_SERVER_PRECISION);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Test that a {@link Line} can be constructed with nodes of different coordinates.
|
---|
197 | * @throws InvalidSelection never
|
---|
198 | */
|
---|
199 | @Test
|
---|
200 | public void testLineDifferentCoordinates() throws InvalidSelection {
|
---|
201 | assertNotNull(new Line(new Node(new EastNorth(0, 1)),
|
---|
202 | new Node(new EastNorth(0, 2))));
|
---|
203 | assertNotNull(new Line(new Node(new EastNorth(0, 1)),
|
---|
204 | new Node(new EastNorth(1, 1))));
|
---|
205 | assertNotNull(new Line(new Node(new EastNorth(0, 1)),
|
---|
206 | new Node(new EastNorth(0+1e-150, 1+1e-150))));
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Test that a {@link Line} cannot be constructed with nodes of same coordinates.
|
---|
211 | * @throws InvalidSelection always
|
---|
212 | */
|
---|
213 | @Test(expected = InvalidSelection.class)
|
---|
214 | public void testLineSameCoordinates1() throws InvalidSelection {
|
---|
215 | new Line(new Node(new EastNorth(0, 1)),
|
---|
216 | new Node(new EastNorth(0, 1)));
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Test that a {@link Line} cannot be constructed with nodes of same coordinates.
|
---|
221 | * @throws InvalidSelection always
|
---|
222 | */
|
---|
223 | @Test(expected = InvalidSelection.class)
|
---|
224 | public void testLineSameCoordinates2() throws InvalidSelection {
|
---|
225 | new Line(new Node(new EastNorth(0, 1)),
|
---|
226 | new Node(new EastNorth(0+1e-175, 1+1e-175)));
|
---|
227 | }
|
---|
228 | }
|
---|