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