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

Last change on this file since 11647 was 10758, checked in by Don-vip, 8 years ago

sonar - squid:S3578 - Test methods should comply with a naming convention

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