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

Last change on this file since 12636 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotNull;
6
7import org.junit.Before;
8import org.junit.Rule;
9import org.junit.Test;
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.actions.AlignInLineAction.InvalidSelection;
12import org.openstreetmap.josm.actions.AlignInLineAction.Line;
13import org.openstreetmap.josm.data.coor.EastNorth;
14import org.openstreetmap.josm.data.coor.LatLon;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.Node;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.gui.MainApplication;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * Unit tests for class {@link AlignInLineAction}.
26 */
27public final class AlignInLineActionTest {
28
29 /**
30 * Setup test.
31 */
32 @Rule
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 @Before
43 public void setUp() {
44 // Enable "Align in line" feature.
45 action = Main.main.menu.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 public void testNodesOpenWay() throws InvalidSelection {
58 DataSet dataSet = new DataSet();
59 OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
60
61 // Create test points, lower left is (0,0).
62 //
63 // 1 - - -
64 // - 3 - 2
65 // - - - -
66 Node point1 = new Node(new EastNorth(0, 2));
67 Node point2 = new Node(new EastNorth(3, 1));
68 Node point3 = new Node(new EastNorth(1, 1));
69
70 try {
71 MainApplication.getLayerManager().addLayer(layer);
72
73 // Create an open way.
74 createWay(dataSet, point1, point2, point3);
75
76 // Select nodes to align.
77 dataSet.addSelected(point1, point2, point3);
78
79 action.buildCommand().executeCommand();
80 } finally {
81 // Ensure we clean the place before leaving, even if test fails.
82 MainApplication.getLayerManager().removeLayer(layer);
83 }
84
85 // Points 1 and 3 are the extremities and must not have moved. Only point 2 must have moved.
86 assertCoordEq(point1, 0, 2);
87 assertCoordEq(point2, 2, 0);
88 assertCoordEq(point3, 1, 1);
89 }
90
91 /**
92 * Test case: only nodes selected, part of a closed way: align these nodes on the line passing through the most
93 * distant nodes.
94 * @throws InvalidSelection never
95 */
96 @Test
97 public void testNodesClosedWay() throws InvalidSelection {
98 DataSet dataSet = new DataSet();
99 OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
100
101 // Create test points, lower left is (0,0).
102 //
103 // 4 - 3
104 // - - -
105 // 1 - 2
106 Node point1 = new Node(new EastNorth(0, 0));
107 Node point2 = new Node(new EastNorth(2, 0));
108 Node point3 = new Node(new EastNorth(2, 2));
109 Node point4 = new Node(new EastNorth(0, 2));
110
111 try {
112 MainApplication.getLayerManager().addLayer(layer);
113
114 // Create a closed way.
115 createWay(dataSet, point1, point2, point3, point4, point1);
116 // Select nodes to align (point1 must be in the second position to exhibit the bug).
117 dataSet.addSelected(point4, point1, point2);
118
119 action.buildCommand().executeCommand();
120 } finally {
121 // Ensure we clean the place before leaving, even if test fails.
122 MainApplication.getLayerManager().removeLayer(layer);
123 }
124
125 // Only point 1 must have moved.
126 assertCoordEq(point1, 1, 1);
127 assertCoordEq(point2, 2, 0);
128 assertCoordEq(point3, 2, 2);
129 assertCoordEq(point4, 0, 2);
130 }
131
132 /**
133 * Test case: only nodes selected, part of multiple ways: align these nodes on the line passing through the most
134 * distant nodes.
135 * @throws InvalidSelection never
136 */
137 @Test
138 public void testNodesOpenWays() throws InvalidSelection {
139 DataSet dataSet = new DataSet();
140 OsmDataLayer 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 Node point1 = new Node(new EastNorth(0, 2));
148 Node point2 = new Node(new EastNorth(2, 1));
149 Node point3 = new Node(new EastNorth(0, 1));
150 Node point4 = new Node(new EastNorth(2, 0));
151
152 try {
153 MainApplication.getLayerManager().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.buildCommand().executeCommand();
164 } finally {
165 // Ensure we clean the place before leaving, even if test fails.
166 MainApplication.getLayerManager().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 = new Way();
183 dataSet.addPrimitive(way);
184
185 for (Node node : nodes) {
186 // Add primitive to dataset only if not already included.
187 if (dataSet.getPrimitiveById(node) == null)
188 dataSet.addPrimitive(node);
189
190 way.addNode(node);
191 }
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("Wrong x coordinate.", x, coordinate.getX(), LatLon.MAX_SERVER_PRECISION);
204 assertEquals("Wrong y coordinate.", y, coordinate.getY(), LatLon.MAX_SERVER_PRECISION);
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 public 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(expected = InvalidSelection.class)
226 public void testLineSameCoordinates1() throws InvalidSelection {
227 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(expected = InvalidSelection.class)
236 public void testLineSameCoordinates2() throws InvalidSelection {
237 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.