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

Last change on this file since 19413 was 18690, checked in by taylor.smock, 2 years ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

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