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

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

use OsmDataLayer.getDataSet() in unit tests

File size: 15.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.assertFalse;
7import static org.junit.Assert.assertTrue;
8
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.List;
13import java.util.NoSuchElementException;
14
15import org.junit.Before;
16import org.junit.Rule;
17import org.junit.Test;
18import org.openstreetmap.josm.TestUtils;
19import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Relation;
24import org.openstreetmap.josm.data.osm.User;
25import org.openstreetmap.josm.data.osm.Way;
26import org.openstreetmap.josm.data.osm.WaySegment;
27import org.openstreetmap.josm.gui.layer.OsmDataLayer;
28import org.openstreetmap.josm.testutils.JOSMTestRules;
29
30import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
31import nl.jqno.equalsverifier.EqualsVerifier;
32import nl.jqno.equalsverifier.Warning;
33
34/**
35 * Unit tests of {@link DeleteCommand} class.
36 */
37public class DeleteCommandTest {
38
39 /**
40 * We need prefs for nodes.
41 */
42 @Rule
43 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
44 public JOSMTestRules test = new JOSMTestRules().preferences().i18n();
45 private CommandTestDataWithRelation testData;
46
47 /**
48 * Set up the test data.
49 */
50 @Before
51 public void createTestData() {
52 testData = new CommandTestDataWithRelation();
53 }
54
55 /**
56 * A simple deletion test with no references
57 */
58 @Test
59 public void testSimpleDelete() {
60 Node node = testData.createNode(15);
61 assertTrue(testData.layer.data.allPrimitives().contains(node));
62
63 new DeleteCommand(node).executeCommand();
64
65 assertTrue(node.isDeleted());
66 assertTrue(node.isModified());
67 assertFalse(testData.layer.data.allNonDeletedPrimitives().contains(node));
68 }
69
70 /**
71 * A delete should not delete referred objects but should should remove the reference.
72 */
73 @Test
74 public void testDeleteIgnoresReferences() {
75 assertTrue(testData.existingNode.getReferrers().contains(testData.existingRelation));
76 new DeleteCommand(testData.existingRelation).executeCommand();
77
78 assertTrue(testData.existingRelation.isDeleted());
79 assertEquals(0, testData.existingRelation.getMembersCount());
80 assertFalse(testData.existingNode.isDeleted());
81 assertFalse(testData.existingWay.isDeleted());
82 assertFalse(testData.existingNode.getReferrers().contains(testData.existingRelation));
83
84 // same for the way
85 assertTrue(testData.existingNode.getReferrers().contains(testData.existingWay));
86 new DeleteCommand(testData.existingWay).executeCommand();
87 assertEquals(0, testData.existingWay.getNodesCount());
88 assertFalse(testData.existingNode.getReferrers().contains(testData.existingWay));
89 }
90
91 /**
92 * A delete should delete all objects with references to the deleted one
93 */
94 @Test(expected = IllegalArgumentException.class)
95 public void testDeleteFailsOnDelted() {
96 new DeleteCommand(testData.existingRelation).executeCommand();
97
98 new DeleteCommand(testData.existingRelation).executeCommand();
99 }
100
101 /**
102 * A delete should delete all objects with references to the deleted one
103 */
104 @Test
105 public void testReferredDelete() {
106 DeleteCommand.deleteWithReferences(Arrays.asList(testData.existingNode), true).executeCommand();
107
108 assertTrue(testData.existingNode.isDeleted());
109 assertEquals(0, testData.existingWay.getNodesCount());
110 assertTrue(testData.existingWay.isDeleted());
111 }
112
113 /**
114 * Delete nodes that would be without reference afterwards.
115 */
116 @Test
117 public void testDeleteNodesInWay() {
118 testData.existingNode.removeAll();
119 // That untagged node should be deleted.
120 testData.existingNode2.removeAll();
121 DeleteCommand.delete(Arrays.asList(testData.existingWay), true, true).executeCommand();
122
123 assertTrue(testData.existingWay.isDeleted());
124 assertTrue(testData.existingNode2.isDeleted());
125 assertFalse(testData.existingNode.isDeleted());
126 assertFalse(testData.existingRelation.isDeleted());
127
128 // Same test, now with tagged nodes
129 Node node1 = testData.createNode(15);
130 Node node2 = testData.createNode(16);
131 Node node3 = testData.createNode(17);
132 Node node4 = testData.createNode(18);
133 node2.removeAll();
134 node4.removeAll();
135 Way way1 = new Way(25, 1);
136 way1.setNodes(Arrays.asList(node1, node2, node3));
137 testData.layer.data.addPrimitive(way1);
138 Way way2 = new Way(26, 1);
139 way2.setNodes(Arrays.asList(node2, node3, node4));
140 testData.layer.data.addPrimitive(way2);
141 DeleteCommand.delete(Arrays.asList(way1, way2), true, true).executeCommand();
142
143 assertTrue(way1.isDeleted());
144 assertTrue(way2.isDeleted());
145 assertFalse(node1.isDeleted());
146 assertTrue(node2.isDeleted());
147 assertFalse(node3.isDeleted());
148 assertTrue(node4.isDeleted());
149 }
150
151 /**
152 * Test that {@link DeleteCommand} checks for non-null.
153 */
154 @Test(expected = IllegalArgumentException.class)
155 public void testConsistency() {
156 new DeleteCommand(Arrays.asList(testData.existingNode, testData.existingWay, null));
157 }
158
159 /**
160 * Test that {@link DeleteCommand} checks for the dataset
161 */
162 @Test(expected = IllegalArgumentException.class)
163 public void testConsistencyDataset() {
164 testData.layer.getDataSet().removePrimitive(testData.existingNode);
165 new DeleteCommand(Arrays.asList(testData.existingNode, testData.existingWay));
166 }
167
168 /**
169 * Test that {@link DeleteCommand} checks for non-empty list
170 */
171 @Test(expected = NoSuchElementException.class)
172 public void testConsistencyNonEmpty() {
173 new DeleteCommand(Arrays.<OsmPrimitive>asList());
174 }
175
176 /**
177 * Test that {@link DeleteCommand} checks for non-null list
178 */
179 @Test(expected = NullPointerException.class)
180 public void testConsistencyNonNull() {
181 new DeleteCommand((Collection<OsmPrimitive>) null);
182 }
183
184 /**
185 * Test {@link DeleteCommand#undoCommand()}
186 */
187 @Test
188 public void testUndo() {
189 DeleteCommand command = new DeleteCommand(
190 Arrays.asList(testData.existingNode, testData.existingNode2, testData.existingWay));
191 command.executeCommand();
192
193 assertTrue(testData.existingNode.isDeleted());
194 assertTrue(testData.existingWay.isDeleted());
195
196 command.undoCommand();
197
198 assertFalse(testData.existingNode.isDeleted());
199 assertFalse(testData.existingWay.isDeleted());
200 assertEquals("existing", testData.existingNode.get("existing"));
201
202 command.executeCommand();
203
204 assertTrue(testData.existingNode.isDeleted());
205 assertTrue(testData.existingWay.isDeleted());
206 }
207
208 /**
209 * Test {@link DeleteCommand#deleteWaySegment(WaySegment)}
210 * Way with only 1 segment
211 */
212 @Test
213 public void testDeleteWaySegment() {
214 Way way1 = testData.createWay(100, testData.createNode(101), testData.createNode(102));
215 WaySegment ws = new WaySegment(way1, 0);
216 Command command = DeleteCommand.deleteWaySegment(ws);
217 command.executeCommand();
218
219 assertTrue(way1.isDeleted());
220 }
221
222 /**
223 * Test {@link DeleteCommand#deleteWaySegment(WaySegment)}
224 * Delete end of way
225 */
226 @Test
227 public void testDeleteWaySegmentEndOfWay() {
228 Way way = testData.createWay(200, testData.createNode(201), testData.createNode(202), testData.createNode(203),
229 testData.createNode(204));
230 WaySegment ws = new WaySegment(way, 2);
231 Command command = DeleteCommand.deleteWaySegment(ws);
232 command.executeCommand();
233
234 assertEquals(3, way.getNodesCount());
235 assertEquals(201, way.getNodeId(0));
236 assertEquals(202, way.getNodeId(1));
237 assertEquals(203, way.getNodeId(2));
238 }
239
240 /**
241 * Test {@link DeleteCommand#deleteWaySegment(WaySegment)}
242 * Delete start of way
243 */
244 @Test
245 public void testDeleteWaySegmentStartOfWay() {
246 Way way = testData.createWay(100, testData.createNode(101), testData.createNode(102), testData.createNode(103),
247 testData.createNode(104));
248 WaySegment ws = new WaySegment(way, 0);
249 Command command = DeleteCommand.deleteWaySegment(ws);
250 command.executeCommand();
251
252 assertEquals(3, way.getNodesCount());
253 assertEquals(102, way.getNodeId(0));
254 assertEquals(103, way.getNodeId(1));
255 assertEquals(104, way.getNodeId(2));
256 }
257
258 /**
259 * Test {@link DeleteCommand#deleteWaySegment(WaySegment)}
260 * Delete start of way
261 */
262 @Test
263 public void testDeleteWaySegmentSplit() {
264 Node node103 = testData.createNode(103);
265 Node node104 = testData.createNode(104);
266 Way way = testData.createWay(100, testData.createNode(101), testData.createNode(102), node103, node104);
267 WaySegment ws = new WaySegment(way, 1);
268 Command command = DeleteCommand.deleteWaySegment(ws);
269 command.executeCommand();
270
271 assertEquals(2, way.getNodesCount());
272 assertEquals(101, way.getNodeId(0));
273 assertEquals(102, way.getNodeId(1));
274 // there needs to be a new way
275 assertEquals(1, node104.getReferrers().size());
276 Way newWay = (Way) node104.getReferrers().get(0);
277 assertEquals(2, newWay.getNodesCount());
278 assertEquals(103, newWay.getNodeId(0));
279 assertEquals(104, newWay.getNodeId(1));
280 }
281
282 /**
283 * Test {@link DeleteCommand#deleteWaySegment(WaySegment)}
284 * Delete start of way
285 */
286 @Test
287 public void testDeleteWaySegmentCycle() {
288 Node n = testData.createNode(101);
289 Way way = testData.createWay(100, n, testData.createNode(102), testData.createNode(103),
290 testData.createNode(104), n);
291 WaySegment ws = new WaySegment(way, 2);
292 Command command = DeleteCommand.deleteWaySegment(ws);
293 command.executeCommand();
294
295 assertEquals(4, way.getNodesCount());
296 assertEquals(104, way.getNodeId(0));
297 assertEquals(101, way.getNodeId(1));
298 assertEquals(102, way.getNodeId(2));
299 assertEquals(103, way.getNodeId(3));
300 }
301
302 /**
303 * Tests {@link DeleteCommand#getChildren()}
304 */
305 @Test
306 public void testGetChildren() {
307 testData.existingNode.put("name", "xy");
308 Collection<PseudoCommand> children = new DeleteCommand(Arrays.<OsmPrimitive>asList(testData.existingNode, testData.existingNode2))
309 .getChildren();
310 assertEquals(2, children.size());
311 assertTrue(children.stream().allMatch(c -> c.getParticipatingPrimitives().size() == 1));
312 assertTrue(children.stream().anyMatch(c -> c.getParticipatingPrimitives().iterator().next() == testData.existingNode));
313 assertTrue(children.stream().anyMatch(c -> c.getParticipatingPrimitives().iterator().next() == testData.existingNode2));
314 assertTrue(children.stream().anyMatch(c -> c.getDescriptionText().matches("Deleted '.*xy.*'")));
315 }
316
317 /**
318 * Tests {@link DeleteCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
319 */
320 @Test
321 public void testFillModifiedData() {
322 ArrayList<OsmPrimitive> modified = new ArrayList<>();
323 ArrayList<OsmPrimitive> deleted = new ArrayList<>();
324 ArrayList<OsmPrimitive> added = new ArrayList<>();
325 new DeleteCommand(Arrays.<OsmPrimitive>asList(testData.existingNode)).fillModifiedData(modified, deleted, added);
326 // intentionally left empty.
327 assertArrayEquals(new Object[] {}, modified.toArray());
328 assertArrayEquals(new Object[] {}, deleted.toArray());
329 assertArrayEquals(new Object[] {}, added.toArray());
330 }
331
332 /**
333 * Tests {@link DeleteCommand#getParticipatingPrimitives()}
334 */
335 @Test
336 public void testGetParticipatingPrimitives() {
337 DeleteCommand command = new DeleteCommand(Arrays.<OsmPrimitive>asList(testData.existingNode));
338 assertArrayEquals(new Object[] {testData.existingNode }, command.getParticipatingPrimitives().toArray());
339
340 DeleteCommand command2 = new DeleteCommand(
341 Arrays.<OsmPrimitive>asList(testData.existingNode, testData.existingWay));
342 assertArrayEquals(new Object[] {testData.existingNode, testData.existingWay},
343 command2.getParticipatingPrimitives().toArray());
344 }
345
346 /**
347 * Test {@link DeleteCommand#getDescriptionText()}
348 */
349 @Test
350 public void testDescription() {
351 Node node = testData.createNode(100);
352 node.put("name", "xy");
353 Way way = testData.createWay(101);
354 way.put("name", "xy");
355 Relation relation = testData.createRelation(102);
356 relation.put("name", "xy");
357
358 List<OsmPrimitive> nodeList = Arrays.<OsmPrimitive>asList(node);
359 assertTrue(new DeleteCommand(nodeList).getDescriptionText().matches("Delete node .*xy.*"));
360 List<OsmPrimitive> wayList = Arrays.<OsmPrimitive>asList(way);
361 assertTrue(new DeleteCommand(wayList).getDescriptionText().matches("Delete way .*xy.*"));
362 List<OsmPrimitive> relationList = Arrays.<OsmPrimitive>asList(relation);
363 assertTrue(new DeleteCommand(relationList).getDescriptionText().matches("Delete relation .*xy.*"));
364
365 List<OsmPrimitive> nodesList = Arrays.<OsmPrimitive>asList(node, testData.createNode(110));
366 assertTrue(new DeleteCommand(nodesList).getDescriptionText().matches("Delete 2 nodes"));
367 List<OsmPrimitive> waysList = Arrays.<OsmPrimitive>asList(way, testData.createWay(111));
368 assertTrue(new DeleteCommand(waysList).getDescriptionText().matches("Delete 2 ways"));
369 List<OsmPrimitive> relationsList = Arrays.<OsmPrimitive>asList(relation, testData.createRelation(112));
370 assertTrue(new DeleteCommand(relationsList).getDescriptionText().matches("Delete 2 relations"));
371
372 List<OsmPrimitive> mixed = Arrays.<OsmPrimitive>asList(node, way, relation);
373 assertTrue(new DeleteCommand(mixed).getDescriptionText().matches("Delete 3 objects"));
374 }
375
376 /**
377 * Unit test of methods {@link DeleteCommand#equals} and {@link DeleteCommand#hashCode}.
378 */
379 @Test
380 public void testEqualsContract() {
381 TestUtils.assumeWorkingEqualsVerifier();
382 EqualsVerifier.forClass(DeleteCommand.class).usingGetClass()
383 .withPrefabValues(DataSet.class,
384 new DataSet(), new DataSet())
385 .withPrefabValues(User.class,
386 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
387 .withPrefabValues(OsmDataLayer.class,
388 new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
389 .suppress(Warning.NONFINAL_FIELDS)
390 .verify();
391 }
392}
Note: See TracBrowser for help on using the repository browser.