source: josm/trunk/test/unit/org/openstreetmap/josm/command/AddPrimitivesCommandTest.java@ 18037

Last change on this file since 18037 was 18037, checked in by Don-vip, 4 years ago

fix #21064 - Add JUnit 5 extension for preferences (patch by taylor.smock)

File size: 12.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5import static org.junit.jupiter.api.Assertions.assertEquals;
6import static org.junit.jupiter.api.Assertions.assertSame;
7import static org.junit.jupiter.api.Assertions.assertTrue;
8
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.HashSet;
12import java.util.List;
13
14import org.openstreetmap.josm.TestUtils;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.NodeData;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.PrimitiveData;
21import org.openstreetmap.josm.data.osm.User;
22import org.openstreetmap.josm.data.osm.Way;
23import org.openstreetmap.josm.data.osm.WayData;
24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
25import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
26import org.openstreetmap.josm.testutils.annotations.I18n;
27
28import nl.jqno.equalsverifier.EqualsVerifier;
29import nl.jqno.equalsverifier.Warning;
30import org.junit.jupiter.api.Test;
31
32/**
33 * Unit tests of {@link AddPrimitivesCommand} class.
34 */
35@I18n
36// We need prefs for nodes.
37@BasicPreferences
38class AddPrimitivesCommandTest {
39 /**
40 * Test if the add command is executed correctly and does not set the modified flag.
41 */
42 @Test
43 void testAdd() {
44 DataSet ds = new DataSet();
45
46 List<PrimitiveData> testData = createTestData();
47 assertTrue(new AddPrimitivesCommand(testData, ds).executeCommand());
48
49 testContainsTestData(ds);
50 assertEquals(3, ds.getAllSelected().size());
51 }
52
53 /**
54 * Test if the add command sets the selection.
55 */
56 @Test
57 void testAddSetSelection() {
58 DataSet ds = new DataSet();
59
60 List<PrimitiveData> testData = createTestData();
61 assertTrue(new AddPrimitivesCommand(testData, testData.subList(2, 3), ds).executeCommand());
62
63 testContainsTestData(ds);
64
65 assertEquals(1, ds.getAllSelected().size());
66 assertEquals(1, ds.getSelectedWays().size());
67 }
68
69 /**
70 * Tests if the add command respects the data set.
71 */
72 @Test
73 void testAddToLayer() {
74 DataSet ds1 = new DataSet();
75 DataSet ds2 = new DataSet();
76
77 List<PrimitiveData> testData = createTestData();
78 assertTrue(new AddPrimitivesCommand(testData, testData.subList(2, 3), ds1).executeCommand());
79
80 testContainsTestData(ds1);
81 assertTrue(ds2.allPrimitives().isEmpty());
82
83 assertEquals(1, ds1.getAllSelected().size());
84 assertEquals(1, ds1.getSelectedWays().size());
85 }
86
87 /**
88 * Tests if the add command ignores existing data
89 */
90 @Test
91 void testAddIgnoresExisting() {
92 DataSet ds = new DataSet();
93
94 List<PrimitiveData> testData = createTestData();
95 assertTrue(new AddPrimitivesCommand(testData, ds).executeCommand());
96 assertEquals(2, ds.getNodes().size());
97 assertEquals(1, ds.getWays().size());
98
99 testData.set(2, createTestNode(7));
100 assertTrue(new AddPrimitivesCommand(testData, ds).executeCommand());
101
102 assertEquals(3, ds.getNodes().size());
103 assertEquals(1, ds.getWays().size());
104 }
105
106 /**
107 * Test {@link AddPrimitivesCommand#getDescriptionText()}
108 */
109 @Test
110 void testDescription() {
111 DataSet ds = new DataSet();
112
113 List<PrimitiveData> testData = createTestData();
114 NodeData data2 = createTestNode(7);
115
116 AddPrimitivesCommand command1 = new AddPrimitivesCommand(testData, ds);
117 AddPrimitivesCommand command2 = new AddPrimitivesCommand(Arrays.<PrimitiveData>asList(data2), ds);
118
119 assertEquals("Added 3 objects", command1.getDescriptionText());
120 assertEquals("Added 1 object", command2.getDescriptionText());
121
122 // Name must be the same after execution.
123 assertTrue(command1.executeCommand());
124 assertTrue(command2.executeCommand());
125
126 assertEquals("Added 3 objects", command1.getDescriptionText());
127 assertEquals("Added 1 object", command2.getDescriptionText());
128 }
129
130 /**
131 * Test {@link AddPrimitivesCommand#undoCommand()}
132 */
133 @Test
134 void testUndo() {
135 DataSet ds = new DataSet();
136
137 List<PrimitiveData> testData = createTestData();
138
139 AddPrimitivesCommand command = new AddPrimitivesCommand(testData, ds);
140
141 assertTrue(command.executeCommand());
142
143 assertEquals(3, ds.allPrimitives().size());
144 assertEquals(1, ds.getWays().size());
145 Way way = ds.getWays().iterator().next();
146
147 for (int i = 0; i < 2; i++) {
148 // Needs to work multiple times.
149 command.undoCommand();
150
151 assertEquals(0, ds.allPrimitives().size());
152 assertEquals(0, ds.getWays().size());
153
154 // redo
155 assertTrue(command.executeCommand());
156
157 assertEquals(3, ds.allPrimitives().size());
158 assertEquals(1, ds.getWays().size());
159 assertSame(way, ds.getWays().iterator().next());
160 }
161 }
162
163 /**
164 * Tests if the undo command does not remove
165 * data ignored by by the add command because they where already existing.
166 */
167 @Test
168 void testUndoIgnoresExisting() {
169 DataSet ds = new DataSet();
170
171 List<PrimitiveData> testData = createTestData();
172
173 assertTrue(new AddPrimitivesCommand(testData, ds).executeCommand());
174 assertEquals(2, ds.getNodes().size());
175 assertEquals(1, ds.getWays().size());
176
177 testData.set(2, createTestNode(7));
178
179 AddPrimitivesCommand command = new AddPrimitivesCommand(testData, ds);
180
181 assertTrue(command.executeCommand());
182
183 for (int i = 0; i < 2; i++) {
184 // Needs to work multiple times.
185 assertEquals(3, ds.getNodes().size());
186 assertEquals(1, ds.getWays().size());
187
188 command.undoCommand();
189
190 assertEquals(2, ds.getNodes().size());
191 assertEquals(1, ds.getWays().size());
192
193 // redo
194 assertTrue(command.executeCommand());
195 }
196 }
197
198 /**
199 * Tests if the undo command does not remove
200 * data ignored by by the add command because they where already existing.
201 * Simulates regression in #14620
202 */
203 @Test
204 void testUndoIgnoresExistingAsDeleted() {
205 DataSet ds = new DataSet();
206
207 List<PrimitiveData> testData = createTestData();
208
209 assertTrue(new AddPrimitivesCommand(testData, ds).executeCommand());
210 assertEquals(2, ds.getNodes().size());
211 assertEquals(1, ds.getWays().size());
212
213 ds.getNodes().forEach(n -> n.setDeleted(true));
214
215 AddPrimitivesCommand command = new AddPrimitivesCommand(testData, ds);
216
217 assertTrue(command.executeCommand());
218
219 for (int i = 0; i < 2; i++) {
220 // Needs to work multiple times.
221 assertEquals(2, ds.getNodes().size());
222 assertEquals(1, ds.getWays().size());
223
224 command.undoCommand();
225
226 assertEquals(2, ds.getNodes().size());
227 assertEquals(1, ds.getWays().size());
228
229 // redo
230 assertTrue(command.executeCommand());
231 }
232 }
233
234 /**
235 * Tests if the undo command does not remove
236 * data ignored by by the add command because they where already existing.
237 */
238 @Test
239 void testUndoIgnoresExistingSameUniqueIdDifferentType() {
240 DataSet ds = new DataSet();
241
242 List<PrimitiveData> testData = new ArrayList<>(createTestData());
243
244 assertTrue(new AddPrimitivesCommand(testData, ds).executeCommand());
245 assertEquals(2, ds.getNodes().size());
246 assertEquals(1, ds.getWays().size());
247
248 NodeData n7Data = createTestNode(7);
249 NodeData n8Data = createTestNode(8);
250 Way w2 = new Way(5);
251 Node n7 = new Node(7);
252 n7.load(n7Data);
253 Node n8 = new Node(8);
254 n8.load(n8Data);
255 w2.setNodes(Arrays.asList(n7, n8));
256 testData.set(2, createTestNode(7));
257 testData.add(n8.save());
258 testData.add(w2.save());
259
260 AddPrimitivesCommand command = new AddPrimitivesCommand(testData, ds);
261
262 assertTrue(command.executeCommand());
263
264 for (int i = 0; i < 2; i++) {
265 assertEquals(4, ds.getNodes().size());
266 assertEquals(2, ds.getWays().size());
267
268 // Needs to work multiple times.
269 command.undoCommand();
270
271 assertEquals(2, ds.getNodes().size());
272 assertEquals(1, ds.getWays().size());
273
274 // redo
275 assertTrue(command.executeCommand());
276
277 }
278 }
279
280 /**
281 * Test {@link AddPrimitivesCommand#getParticipatingPrimitives()}
282 */
283 @Test
284 void testParticipatingPrimitives() {
285 DataSet ds = new DataSet();
286
287 List<PrimitiveData> testData = createTestData();
288 AddPrimitivesCommand command = new AddPrimitivesCommand(testData, ds);
289 assertTrue(command.executeCommand());
290
291 assertEquals(3, command.getParticipatingPrimitives().size());
292 HashSet<OsmPrimitive> should = new HashSet<>(ds.allPrimitives());
293 assertEquals(should, new HashSet<>(command.getParticipatingPrimitives()));
294
295 // needs to be the same after undo
296 command.undoCommand();
297 assertEquals(should, new HashSet<>(command.getParticipatingPrimitives()));
298 }
299
300 /**
301 * Tests {@link AddPrimitivesCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
302 */
303 @Test
304 void testFillModifiedData() {
305 ArrayList<OsmPrimitive> modified = new ArrayList<>();
306 ArrayList<OsmPrimitive> deleted = new ArrayList<>();
307 ArrayList<OsmPrimitive> added = new ArrayList<>();
308
309 List<PrimitiveData> testData = createTestData();
310 new AddPrimitivesCommand(testData, new DataSet()).fillModifiedData(modified, deleted, added);
311
312 assertArrayEquals(new Object[] {}, modified.toArray());
313 assertArrayEquals(new Object[] {}, deleted.toArray());
314 assertArrayEquals(new Object[] {}, added.toArray());
315 }
316
317 private void testContainsTestData(DataSet data) {
318 assertEquals(3, data.allPrimitives().size());
319 assertEquals(2, data.getNodes().size());
320 assertEquals(1, data.getWays().size());
321 assertEquals(3, data.allModifiedPrimitives().size());
322 for (OsmPrimitive n : data.allPrimitives()) {
323 assertEquals("test", n.get("test"));
324 assertTrue(n.isModified());
325 }
326
327 for (Node n : data.getNodes()) {
328 assertEquals(LatLon.ZERO, n.getCoor());
329 }
330
331 for (Way w : data.getWays()) {
332 assertEquals(2, w.getNodes().size());
333 assertEquals(5, w.getNode(0).getId());
334 assertEquals(6, w.getNode(1).getId());
335 }
336 }
337
338 private List<PrimitiveData> createTestData() {
339 NodeData node1 = createTestNode(5);
340 NodeData node2 = createTestNode(6);
341 WayData way = new WayData(2);
342 way.put("test", "test");
343 way.setNodeIds(Arrays.asList(node1.getId(), node2.getId()));
344 List<PrimitiveData> testData = Arrays.<PrimitiveData>asList(node1, node2, way);
345 return testData;
346 }
347
348 private NodeData createTestNode(int id) {
349 NodeData node1 = new NodeData();
350 node1.setCoor(LatLon.ZERO);
351 node1.put("test", "test");
352 node1.setId(id);
353 return node1;
354 }
355
356 /**
357 * Unit test of methods {@link AddPrimitivesCommand#equals} and {@link AddPrimitivesCommand#hashCode}.
358 */
359 @Test
360 void testEqualsContract() {
361 TestUtils.assumeWorkingEqualsVerifier();
362 EqualsVerifier.forClass(AddPrimitivesCommand.class).usingGetClass()
363 .withPrefabValues(DataSet.class,
364 new DataSet(), new DataSet())
365 .withPrefabValues(User.class,
366 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
367 .withPrefabValues(OsmDataLayer.class,
368 new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
369 .suppress(Warning.NONFINAL_FIELDS)
370 .verify();
371 }
372}
Note: See TracBrowser for help on using the repository browser.