[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.junit.Assert.assertEquals;
|
---|
[10347] | 5 | import static org.junit.Assert.assertNotNull;
|
---|
[7937] | 6 |
|
---|
[12110] | 7 | import org.junit.Before;
|
---|
[12562] | 8 | import org.junit.Rule;
|
---|
[7937] | 9 | import org.junit.Test;
|
---|
| 10 | import org.openstreetmap.josm.Main;
|
---|
[10347] | 11 | import org.openstreetmap.josm.actions.AlignInLineAction.InvalidSelection;
|
---|
| 12 | import org.openstreetmap.josm.actions.AlignInLineAction.Line;
|
---|
[7937] | 13 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[12636] | 18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[7937] | 19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[12562] | 20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
[7937] | 21 |
|
---|
[12562] | 22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
| 23 |
|
---|
[7937] | 24 | /**
|
---|
[8857] | 25 | * Unit tests for class {@link AlignInLineAction}.
|
---|
[7937] | 26 | */
|
---|
| 27 | public final class AlignInLineActionTest {
|
---|
| 28 |
|
---|
[12562] | 29 | /**
|
---|
| 30 | * Setup test.
|
---|
| 31 | */
|
---|
| 32 | @Rule
|
---|
| 33 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
[12632] | 34 | public JOSMTestRules test = new JOSMTestRules().main().projection();
|
---|
[12562] | 35 |
|
---|
[7937] | 36 | /** Class under test. */
|
---|
| 37 | private static AlignInLineAction action;
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Setup test.
|
---|
| 41 | */
|
---|
[12110] | 42 | @Before
|
---|
| 43 | public void setUp() {
|
---|
[7937] | 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.
|
---|
[12562] | 54 | * @throws InvalidSelection never
|
---|
[7937] | 55 | */
|
---|
| 56 | @Test
|
---|
[12562] | 57 | public void testNodesOpenWay() throws InvalidSelection {
|
---|
[7937] | 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 {
|
---|
[12636] | 71 | MainApplication.getLayerManager().addLayer(layer);
|
---|
[7937] | 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 |
|
---|
[12562] | 79 | action.buildCommand().executeCommand();
|
---|
[7937] | 80 | } finally {
|
---|
| 81 | // Ensure we clean the place before leaving, even if test fails.
|
---|
[12636] | 82 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
[7937] | 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.
|
---|
[12562] | 94 | * @throws InvalidSelection never
|
---|
[7937] | 95 | */
|
---|
| 96 | @Test
|
---|
[12562] | 97 | public void testNodesClosedWay() throws InvalidSelection {
|
---|
[7937] | 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 {
|
---|
[12636] | 112 | MainApplication.getLayerManager().addLayer(layer);
|
---|
[7937] | 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 |
|
---|
[12562] | 119 | action.buildCommand().executeCommand();
|
---|
[7937] | 120 | } finally {
|
---|
| 121 | // Ensure we clean the place before leaving, even if test fails.
|
---|
[12636] | 122 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
[7937] | 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.
|
---|
[12562] | 135 | * @throws InvalidSelection never
|
---|
[7937] | 136 | */
|
---|
| 137 | @Test
|
---|
[12562] | 138 | public void testNodesOpenWays() throws InvalidSelection {
|
---|
[7937] | 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 {
|
---|
[12636] | 153 | MainApplication.getLayerManager().addLayer(layer);
|
---|
[7937] | 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.
|
---|
[12562] | 163 | action.buildCommand().executeCommand();
|
---|
[7937] | 164 | } finally {
|
---|
| 165 | // Ensure we clean the place before leaving, even if test fails.
|
---|
[12636] | 166 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
[7937] | 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 | }
|
---|
[10347] | 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 | }
|
---|
[7937] | 240 | }
|
---|