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

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

speedup unit tests

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