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

Last change on this file since 17360 was 17296, checked in by GerdP, 3 years ago

fix javadoc link

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