source: josm/trunk/test/unit/org/openstreetmap/josm/command/MoveCommandTest.java@ 13812

Last change on this file since 13812 was 13489, checked in by Don-vip, 6 years ago

fix #16044 - Autofix invalid URL with the wrong type of slashes

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