1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import org.junit.jupiter.api.BeforeEach;
|
---|
15 | import org.junit.jupiter.api.Test;
|
---|
16 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
17 | import org.openstreetmap.josm.TestUtils;
|
---|
18 | import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
|
---|
19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.User;
|
---|
25 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
26 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
27 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
28 |
|
---|
29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
30 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
31 | import nl.jqno.equalsverifier.Warning;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Unit tests of {@link MoveCommand} class.
|
---|
35 | */
|
---|
36 | class MoveCommandTest {
|
---|
37 | /**
|
---|
38 | * We need prefs for nodes.
|
---|
39 | */
|
---|
40 | @RegisterExtension
|
---|
41 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
42 | public JOSMTestRules test = new JOSMTestRules().preferences().i18n().projection();
|
---|
43 | private CommandTestDataWithRelation testData;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Set up the test data.
|
---|
47 | */
|
---|
48 | @BeforeEach
|
---|
49 | public void createTestData() {
|
---|
50 | testData = new CommandTestDataWithRelation();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Test the various constructors.
|
---|
55 | */
|
---|
56 | @Test
|
---|
57 | void testConstructors() {
|
---|
58 | EastNorth offset = new EastNorth(1, 2);
|
---|
59 | LatLon destLatLon = ProjectionRegistry.getProjection().eastNorth2latlon(offset);
|
---|
60 | EastNorth start = new EastNorth(2, 0);
|
---|
61 |
|
---|
62 | Set<OsmPrimitive> nodeAsCollection = Collections.<OsmPrimitive>singleton(testData.existingNode);
|
---|
63 | assertEquals(1, nodeAsCollection.size());
|
---|
64 | checkCommandAfterConstructor(new MoveCommand(nodeAsCollection, offset));
|
---|
65 | checkCommandAfterConstructor(new MoveCommand(testData.existingNode, destLatLon));
|
---|
66 | checkCommandAfterConstructor(new MoveCommand(nodeAsCollection, 1, 2));
|
---|
67 | checkCommandAfterConstructor(new MoveCommand(nodeAsCollection, start, start.add(offset)));
|
---|
68 | checkCommandAfterConstructor(new MoveCommand(testData.existingNode, 1, 2));
|
---|
69 | checkCommandAfterConstructor(new MoveCommand(testData.existingNode, start, start.add(offset)));
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void checkCommandAfterConstructor(MoveCommand moveCommand) {
|
---|
73 | ArrayList<OsmPrimitive> nodes = new ArrayList<>();
|
---|
74 | moveCommand.fillModifiedData(nodes, null, null);
|
---|
75 | assertEquals(nodes, new ArrayList<>(Collections.<OsmPrimitive>singleton(testData.existingNode)));
|
---|
76 |
|
---|
77 | assertEquals(1, moveCommand.getOffset().east(), 0.0001, "east");
|
---|
78 | assertEquals(2, moveCommand.getOffset().north(), 0.0001, "north");
|
---|
79 | assertEquals(2.236068, moveCommand.getDistance(n -> true), 0.0001, "distance");
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Test {@link MoveCommand#executeCommand()} for simple nodes.
|
---|
84 | */
|
---|
85 | @Test
|
---|
86 | void testSingleMove() {
|
---|
87 | MoveCommand command = new MoveCommand(testData.existingNode, 1, 2);
|
---|
88 | testData.existingNode.setEastNorth(new EastNorth(3, 7));
|
---|
89 | command.executeCommand();
|
---|
90 | assertEquals(4, testData.existingNode.getEastNorth().east(), 0.0001, "east");
|
---|
91 | assertEquals(9, testData.existingNode.getEastNorth().north(), 0.0001, "north");
|
---|
92 | assertEquals(2.236068, command.getDistance(n -> true), 0.0001, "distance");
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Test {@link MoveCommand#executeCommand()} for multiple nodes.
|
---|
97 | */
|
---|
98 | @Test
|
---|
99 | void testMultipleMove() {
|
---|
100 | MoveCommand command = new MoveCommand(
|
---|
101 | Arrays.asList(testData.existingNode, testData.existingNode2, testData.existingWay),
|
---|
102 | new EastNorth(1, 2));
|
---|
103 |
|
---|
104 | testData.existingNode.setEastNorth(new EastNorth(3, 7));
|
---|
105 | testData.existingNode2.setEastNorth(new EastNorth(4, 7));
|
---|
106 | command.executeCommand();
|
---|
107 |
|
---|
108 | assertEquals(4, testData.existingNode.getEastNorth().east(), 0.0001, "east");
|
---|
109 | assertEquals(9, testData.existingNode.getEastNorth().north(), 0.0001, "north");
|
---|
110 | assertEquals(5, testData.existingNode2.getEastNorth().east(), 0.0001, "east");
|
---|
111 | assertEquals(9, testData.existingNode2.getEastNorth().north(), 0.0001, "north");
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Test {@link MoveCommand#moveAgain(double, double)} and {@link MoveCommand#moveAgainTo(double, double)}.
|
---|
116 | */
|
---|
117 | @Test
|
---|
118 | void testMoveAgain() {
|
---|
119 | MoveCommand command = new MoveCommand(testData.existingNode, 1, 2);
|
---|
120 | assertEquals(1, command.getOffset().east(), 0.0001, "east");
|
---|
121 | assertEquals(2, command.getOffset().north(), 0.0001, "north");
|
---|
122 |
|
---|
123 | command.moveAgain(1, 2);
|
---|
124 | assertEquals(2, command.getOffset().east(), 0.0001, "east");
|
---|
125 | assertEquals(4, command.getOffset().north(), 0.0001, "north");
|
---|
126 |
|
---|
127 | command.moveAgain(-9, -3);
|
---|
128 | assertEquals(-7, command.getOffset().east(), 0.0001, "east");
|
---|
129 | assertEquals(1, command.getOffset().north(), 0.0001, "north");
|
---|
130 |
|
---|
131 | command.moveAgainTo(1, 2);
|
---|
132 | assertEquals(1, command.getOffset().east(), 0.0001, "east");
|
---|
133 | assertEquals(2, command.getOffset().north(), 0.0001, "north");
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Test {@link MoveCommand#saveCheckpoint()} and {@link MoveCommand#resetToCheckpoint()}
|
---|
138 | */
|
---|
139 | @Test
|
---|
140 | void testCheckpoint() {
|
---|
141 | MoveCommand command = new MoveCommand(testData.existingNode, 2, 4);
|
---|
142 | assertEquals(2, command.getOffset().east(), 0.0001, "east");
|
---|
143 | assertEquals(4, command.getOffset().north(), 0.0001, "north");
|
---|
144 |
|
---|
145 | command.saveCheckpoint();
|
---|
146 | command.moveAgain(3, 7);
|
---|
147 | assertEquals(5, command.getOffset().east(), 0.0001, "east");
|
---|
148 | assertEquals(11, command.getOffset().north(), 0.0001, "north");
|
---|
149 |
|
---|
150 | command.resetToCheckpoint();
|
---|
151 | assertEquals(2, command.getOffset().east(), 0.0001, "east");
|
---|
152 | assertEquals(4, command.getOffset().north(), 0.0001, "north");
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Test the start point mechanism.
|
---|
157 | */
|
---|
158 | @Test
|
---|
159 | void testStartPoint() {
|
---|
160 | EastNorth start = new EastNorth(10, 20);
|
---|
161 | MoveCommand command = new MoveCommand(testData.existingNode, start, start.add(1, 2));
|
---|
162 | assertEquals(1, command.getOffset().east(), 0.0001, "east");
|
---|
163 | assertEquals(2, command.getOffset().north(), 0.0001, "north");
|
---|
164 |
|
---|
165 | command.applyVectorTo(start.add(3, 4));
|
---|
166 | assertEquals(3, command.getOffset().east(), 0.0001, "east");
|
---|
167 | assertEquals(4, command.getOffset().north(), 0.0001, "north");
|
---|
168 |
|
---|
169 | // set to 100, 200
|
---|
170 | command.changeStartPoint(new EastNorth(103, 204));
|
---|
171 | command.applyVectorTo(new EastNorth(101, 202));
|
---|
172 | assertEquals(1, command.getOffset().east(), 0.0001, "east");
|
---|
173 | assertEquals(2, command.getOffset().north(), 0.0001, "north");
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Test the start point mechanism ignored.
|
---|
178 | */
|
---|
179 | @Test
|
---|
180 | void testNoStartPoint() {
|
---|
181 | MoveCommand command = new MoveCommand(testData.existingNode, 1, 0);
|
---|
182 | // ignored
|
---|
183 | command.applyVectorTo(new EastNorth(3, 4));
|
---|
184 | assertEquals(1, command.getOffset().east(), 0.0001, "east");
|
---|
185 | assertEquals(0, command.getOffset().north(), 0.0001, "north");
|
---|
186 |
|
---|
187 | // set to 100, 200
|
---|
188 | command.changeStartPoint(new EastNorth(101, 200));
|
---|
189 | // works
|
---|
190 | command.applyVectorTo(new EastNorth(101, 202));
|
---|
191 | assertEquals(1, command.getOffset().east(), 0.0001, "east");
|
---|
192 | assertEquals(2, command.getOffset().north(), 0.0001, "north");
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Test {@link MoveCommand#undoCommand()}
|
---|
197 | */
|
---|
198 | @Test
|
---|
199 | void testUndo() {
|
---|
200 | testData.existingNode.setEastNorth(new EastNorth(3, 7));
|
---|
201 | MoveCommand command = new MoveCommand(testData.existingNode, 1, 2);
|
---|
202 | command.executeCommand();
|
---|
203 | assertEquals(4, testData.existingNode.getEastNorth().east(), 0.0001, "east");
|
---|
204 | assertEquals(9, testData.existingNode.getEastNorth().north(), 0.0001, "north");
|
---|
205 |
|
---|
206 | command.undoCommand();
|
---|
207 | assertEquals(3, testData.existingNode.getEastNorth().east(), 0.0001, "east");
|
---|
208 | assertEquals(7, testData.existingNode.getEastNorth().north(), 0.0001, "north");
|
---|
209 |
|
---|
210 | command.executeCommand();
|
---|
211 | assertEquals(4, testData.existingNode.getEastNorth().east(), 0.0001, "east");
|
---|
212 | assertEquals(9, testData.existingNode.getEastNorth().north(), 0.0001, "north");
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Tests {@link MoveCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
217 | */
|
---|
218 | @Test
|
---|
219 | void testFillModifiedData() {
|
---|
220 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
221 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
222 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
223 | new MoveCommand(Arrays.<OsmPrimitive>asList(testData.existingNode), 1, 2).fillModifiedData(modified,
|
---|
224 | deleted, added);
|
---|
225 | assertArrayEquals(new Object[] {testData.existingNode }, modified.toArray());
|
---|
226 | assertArrayEquals(new Object[] {}, deleted.toArray());
|
---|
227 | assertArrayEquals(new Object[] {}, added.toArray());
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Tests {@link MoveCommand#getParticipatingPrimitives()}
|
---|
232 | */
|
---|
233 | @Test
|
---|
234 | void testGetParticipatingPrimitives() {
|
---|
235 | MoveCommand command = new MoveCommand(Arrays.<OsmPrimitive>asList(testData.existingNode), 1, 2);
|
---|
236 | command.executeCommand();
|
---|
237 | assertArrayEquals(new Object[] {testData.existingNode}, command.getParticipatingPrimitives().toArray());
|
---|
238 |
|
---|
239 | MoveCommand command2 = new MoveCommand(
|
---|
240 | Arrays.<OsmPrimitive>asList(testData.existingNode, testData.existingWay), 1, 2);
|
---|
241 | command2.executeCommand();
|
---|
242 | assertArrayEquals(new Object[] {testData.existingNode, testData.existingNode2},
|
---|
243 | command2.getParticipatingPrimitives().toArray());
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Test {@link MoveCommand#getDescriptionText()}
|
---|
248 | */
|
---|
249 | @Test
|
---|
250 | void testDescription() {
|
---|
251 | Node node = TestUtils.addFakeDataSet(new Node(LatLon.ZERO));
|
---|
252 | node.put("name", "xy");
|
---|
253 | List<OsmPrimitive> nodeList = Arrays.<OsmPrimitive>asList(node);
|
---|
254 | assertTrue(new MoveCommand(nodeList, 1, 2).getDescriptionText().matches("Move 1 node"));
|
---|
255 | List<OsmPrimitive> nodes = Arrays.<OsmPrimitive>asList(node, testData.existingNode, testData.existingNode2);
|
---|
256 | assertTrue(new MoveCommand(nodes, 1, 2).getDescriptionText().matches("Move 3 nodes"));
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Unit test of methods {@link MoveCommand#equals} and {@link MoveCommand#hashCode}.
|
---|
261 | */
|
---|
262 | @Test
|
---|
263 | void testEqualsContract() {
|
---|
264 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
265 | EqualsVerifier.forClass(MoveCommand.class).usingGetClass()
|
---|
266 | .withPrefabValues(LatLon.class,
|
---|
267 | LatLon.ZERO, new LatLon(45, 45))
|
---|
268 | .withPrefabValues(DataSet.class,
|
---|
269 | new DataSet(), new DataSet())
|
---|
270 | .withPrefabValues(User.class,
|
---|
271 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
272 | .withPrefabValues(OsmDataLayer.class,
|
---|
273 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
274 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
275 | .verify();
|
---|
276 | }
|
---|
277 | }
|
---|