Ticket #9605: AlignInLineActionTest.java

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