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

Last change on this file since 8937 was 8857, checked in by Don-vip, 9 years ago

improve/cleanup unit tests

  • Property svn:eol-style set to native
File size: 6.2 KB
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;
16
17/**
18 * Unit tests for class {@link AlignInLineAction}.
19 */
20public final class AlignInLineActionTest {
21
22 /** Class under test. */
23 private static AlignInLineAction action;
24
25 /**
26 * Setup test.
27 */
28 @BeforeClass
29 public static void setUp() {
30 JOSMFixture.createUnitTestFixture().init(true);
31
32 // Enable "Align in line" feature.
33 action = Main.main.menu.alignInLine;
34 action.setEnabled(true);
35 }
36
37 /**
38 * Test case: only nodes selected, part of an open way: align these nodes on the line passing through the extremity
39 * nodes (the most distant in the way sequence, not the most euclidean-distant). See
40 * https://josm.openstreetmap.de/ticket/9605#comment:3. Note that in this test, after alignment, way is overlapping
41 * itself.
42 */
43 @Test
44 public void nodesOpenWay() {
45 DataSet dataSet = new DataSet();
46 OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
47
48 // Create test points, lower left is (0,0).
49 //
50 // 1 - - -
51 // - 3 - 2
52 // - - - -
53 Node point1 = new Node(new EastNorth(0, 2));
54 Node point2 = new Node(new EastNorth(3, 1));
55 Node point3 = new Node(new EastNorth(1, 1));
56
57 try {
58 Main.main.addLayer(layer);
59
60 // Create an open way.
61 createWay(dataSet, point1, point2, point3);
62
63 // Select nodes to align.
64 dataSet.addSelected(point1, point2, point3);
65
66 action.actionPerformed(null);
67 } finally {
68 // Ensure we clean the place before leaving, even if test fails.
69 Main.map.mapView.removeLayer(layer);
70 }
71
72 // Points 1 and 3 are the extremities and must not have moved. Only point 2 must have moved.
73 assertCoordEq(point1, 0, 2);
74 assertCoordEq(point2, 2, 0);
75 assertCoordEq(point3, 1, 1);
76 }
77
78 /**
79 * Test case: only nodes selected, part of a closed way: align these nodes on the line passing through the most
80 * distant nodes.
81 */
82 @Test
83 public void nodesClosedWay() {
84 DataSet dataSet = new DataSet();
85 OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
86
87 // Create test points, lower left is (0,0).
88 //
89 // 4 - 3
90 // - - -
91 // 1 - 2
92 Node point1 = new Node(new EastNorth(0, 0));
93 Node point2 = new Node(new EastNorth(2, 0));
94 Node point3 = new Node(new EastNorth(2, 2));
95 Node point4 = new Node(new EastNorth(0, 2));
96
97 try {
98 Main.main.addLayer(layer);
99
100 // Create a closed way.
101 createWay(dataSet, point1, point2, point3, point4, point1);
102 // Select nodes to align (point1 must be in the second position to exhibit the bug).
103 dataSet.addSelected(point4, point1, point2);
104
105 action.actionPerformed(null);
106 } finally {
107 // Ensure we clean the place before leaving, even if test fails.
108 Main.map.mapView.removeLayer(layer);
109 }
110
111 // Only point 1 must have moved.
112 assertCoordEq(point1, 1, 1);
113 assertCoordEq(point2, 2, 0);
114 assertCoordEq(point3, 2, 2);
115 assertCoordEq(point4, 0, 2);
116 }
117
118 /**
119 * Test case: only nodes selected, part of multiple ways: align these nodes on the line passing through the most
120 * distant nodes.
121 */
122 @Test
123 public void nodesOpenWays() {
124 DataSet dataSet = new DataSet();
125 OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
126
127 // Create test points, lower left is (0,0).
128 //
129 // 1 - -
130 // 3 - 2
131 // - - 4
132 Node point1 = new Node(new EastNorth(0, 2));
133 Node point2 = new Node(new EastNorth(2, 1));
134 Node point3 = new Node(new EastNorth(0, 1));
135 Node point4 = new Node(new EastNorth(2, 0));
136
137 try {
138 Main.main.addLayer(layer);
139
140 // Create 2 ways.
141 createWay(dataSet, point1, point2);
142 createWay(dataSet, point3, point4);
143
144 // Select nodes to align.
145 dataSet.addSelected(point1, point2, point3, point4);
146
147 // Points must align between points 1 and 4.
148 action.actionPerformed(null);
149 } finally {
150 // Ensure we clean the place before leaving, even if test fails.
151 Main.map.mapView.removeLayer(layer);
152 }
153
154 assertCoordEq(point1, 0, 2);
155 assertCoordEq(point2, 1.5, 0.5);
156 assertCoordEq(point3, 0.5, 1.5);
157 assertCoordEq(point4, 2, 0);
158 }
159
160 /**
161 * Create a way made of the provided nodes and select nodes.
162 *
163 * @param dataSet Dataset in which adding nodes.
164 * @param nodes List of nodes to add to dataset.
165 */
166 private void createWay(DataSet dataSet, Node... nodes) {
167 Way way = new Way();
168 dataSet.addPrimitive(way);
169
170 for (Node node : nodes) {
171 // Add primitive to dataset only if not already included.
172 if (dataSet.getPrimitiveById(node) == null)
173 dataSet.addPrimitive(node);
174
175 way.addNode(node);
176 }
177 }
178
179 /**
180 * Assert that the provided node has the specified coordinates. If not fail the test.
181 *
182 * @param node Node to test.
183 * @param x X coordinate.
184 * @param y Y coordinate.
185 */
186 private void assertCoordEq(Node node, double x, double y) {
187 EastNorth coordinate = node.getEastNorth();
188 assertEquals("Wrong x coordinate.", x, coordinate.getX(), LatLon.MAX_SERVER_PRECISION);
189 assertEquals("Wrong y coordinate.", y, coordinate.getY(), LatLon.MAX_SERVER_PRECISION);
190 }
191}
Note: See TracBrowser for help on using the repository browser.