1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
9 |
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | import org.junit.jupiter.api.BeforeEach;
|
---|
17 | import org.junit.jupiter.api.Test;
|
---|
18 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
19 | import org.openstreetmap.josm.TestUtils;
|
---|
20 | import org.openstreetmap.josm.command.CommandTest.CommandTestData;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
25 | import org.openstreetmap.josm.data.osm.TagMap;
|
---|
26 | import org.openstreetmap.josm.data.osm.User;
|
---|
27 | import org.openstreetmap.josm.data.osm.Way;
|
---|
28 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
29 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
30 |
|
---|
31 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
32 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
33 | import nl.jqno.equalsverifier.Warning;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Unit tests of {@link ChangePropertyCommand} class.
|
---|
37 | */
|
---|
38 | class ChangePropertyCommandTest {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * We need prefs for nodes.
|
---|
42 | */
|
---|
43 | @RegisterExtension
|
---|
44 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
45 | public JOSMTestRules test = new JOSMTestRules().preferences().i18n();
|
---|
46 | private CommandTestData testData;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Set up the test data.
|
---|
50 | */
|
---|
51 | @BeforeEach
|
---|
52 | public void createTestData() {
|
---|
53 | testData = new CommandTestData();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Checks that the short constructors create the right {@link ChangePropertyCommand}
|
---|
58 | */
|
---|
59 | @Test
|
---|
60 | void testShortConstructor() {
|
---|
61 | ChangePropertyCommand command = new ChangePropertyCommand(Arrays.asList(testData.existingNode), "a", "b");
|
---|
62 | assertEquals("b", command.getTags().get("a"));
|
---|
63 | assertEquals(1, command.getTags().size());
|
---|
64 | assertEquals(1, command.getObjectsNumber());
|
---|
65 |
|
---|
66 | command = new ChangePropertyCommand(testData.existingNode, "a", "b");
|
---|
67 | assertEquals("b", command.getTags().get("a"));
|
---|
68 | assertEquals(1, command.getTags().size());
|
---|
69 | assertEquals(1, command.getObjectsNumber());
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Checks that {@link ChangePropertyCommand} adds/updates a property
|
---|
74 | */
|
---|
75 | @Test
|
---|
76 | void testUpdateSingleProperty() {
|
---|
77 | Node node1 = testData.createNode(14);
|
---|
78 | Node node2 = testData.createNode(15);
|
---|
79 | node2.removeAll();
|
---|
80 |
|
---|
81 | TagMap tags = new TagMap();
|
---|
82 | tags.put("existing", "new");
|
---|
83 | new ChangePropertyCommand(Arrays.<OsmPrimitive>asList(node1, node2), tags).executeCommand();
|
---|
84 | assertEquals("new", node1.get("existing"));
|
---|
85 | assertEquals("new", node2.get("existing"));
|
---|
86 |
|
---|
87 | assertTrue(node1.isModified());
|
---|
88 | assertTrue(node2.isModified());
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Checks that {@link ChangePropertyCommand} removes a property
|
---|
93 | */
|
---|
94 | @Test
|
---|
95 | void testRemoveProperty() {
|
---|
96 | Node node1 = testData.createNode(14);
|
---|
97 | Node node2 = testData.createNode(15);
|
---|
98 | node2.removeAll();
|
---|
99 |
|
---|
100 | HashMap<String, String> tags = new HashMap<>();
|
---|
101 | tags.put("existing", "");
|
---|
102 | new ChangePropertyCommand(Arrays.<OsmPrimitive>asList(node1, node2), tags).executeCommand();
|
---|
103 | assertNull(node1.get("existing"));
|
---|
104 | assertNull(node2.get("existing"));
|
---|
105 |
|
---|
106 | assertTrue(node1.isModified());
|
---|
107 | assertFalse(node2.isModified());
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Checks that {@link ChangePropertyCommand} adds/updates multiple properties
|
---|
112 | */
|
---|
113 | @Test
|
---|
114 | void testUpdateMultipleProperties() {
|
---|
115 | Node node1 = testData.createNode(14);
|
---|
116 | Node node2 = testData.createNode(15);
|
---|
117 | node2.removeAll();
|
---|
118 | node2.put("test", "xx");
|
---|
119 | node2.put("remove", "xx");
|
---|
120 |
|
---|
121 | HashMap<String, String> tags = new HashMap<>();
|
---|
122 | tags.put("existing", "existing");
|
---|
123 | tags.put("test", "test");
|
---|
124 | tags.put("remove", "");
|
---|
125 | new ChangePropertyCommand(Arrays.<OsmPrimitive>asList(node1, node2), tags).executeCommand();
|
---|
126 | assertEquals("existing", node1.get("existing"));
|
---|
127 | assertEquals("existing", node2.get("existing"));
|
---|
128 | assertEquals("test", node1.get("test"));
|
---|
129 | assertEquals("test", node2.get("test"));
|
---|
130 | assertNull(node1.get("remove"));
|
---|
131 | assertNull(node2.get("remove"));
|
---|
132 |
|
---|
133 | assertTrue(node1.isModified());
|
---|
134 | assertTrue(node2.isModified());
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Checks that {@link ChangePropertyCommand} adds/updates a property
|
---|
139 | */
|
---|
140 | @Test
|
---|
141 | void testUpdateIgnoresExistingProperty() {
|
---|
142 | Node node1 = testData.createNode(14);
|
---|
143 | Node node2 = testData.createNode(15);
|
---|
144 | node2.removeAll();
|
---|
145 |
|
---|
146 | TagMap tags = new TagMap();
|
---|
147 | tags.put("existing", "existing");
|
---|
148 | new ChangePropertyCommand(Arrays.<OsmPrimitive>asList(node1, node2), tags).executeCommand();
|
---|
149 | assertEquals("existing", node1.get("existing"));
|
---|
150 | assertEquals("existing", node2.get("existing"));
|
---|
151 |
|
---|
152 | assertFalse(node1.isModified());
|
---|
153 | assertTrue(node2.isModified());
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Tests {@link ChangePropertyCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
158 | * and {@link ChangePropertyCommand#getObjectsNumber()}
|
---|
159 | */
|
---|
160 | @Test
|
---|
161 | void testFillModifiedData() {
|
---|
162 | Node node1 = testData.createNode(14);
|
---|
163 | Node node2 = testData.createNode(15);
|
---|
164 | node2.put("existing", "new");
|
---|
165 |
|
---|
166 | TagMap tags = new TagMap();
|
---|
167 | tags.put("existing", "new");
|
---|
168 |
|
---|
169 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
170 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
171 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
172 | new ChangePropertyCommand(Arrays.asList(node1, node2), tags).fillModifiedData(modified, deleted, added);
|
---|
173 | assertArrayEquals(new Object[] {node1}, modified.toArray());
|
---|
174 | assertArrayEquals(new Object[] {}, deleted.toArray());
|
---|
175 | assertArrayEquals(new Object[] {}, added.toArray());
|
---|
176 |
|
---|
177 | assertEquals(1, new ChangePropertyCommand(Arrays.asList(node1, node2), tags).getObjectsNumber());
|
---|
178 |
|
---|
179 | tags.clear();
|
---|
180 | assertEquals(0, new ChangePropertyCommand(Arrays.asList(node1, node2), tags).getObjectsNumber());
|
---|
181 |
|
---|
182 | tags.put("a", "b");
|
---|
183 | assertEquals(2, new ChangePropertyCommand(Arrays.asList(node1, node2), tags).getObjectsNumber());
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Test {@link ChangePropertyCommand#getDescriptionText()}
|
---|
188 | */
|
---|
189 | @Test
|
---|
190 | void testDescription() {
|
---|
191 | Node node1 = testData.createNode(14);
|
---|
192 | Node node2 = testData.createNode(15);
|
---|
193 | Node node3 = testData.createNode(16);
|
---|
194 | node1.put("name", "xy");
|
---|
195 | node2.put("existing", "new");
|
---|
196 | node3.put("existing", null);
|
---|
197 |
|
---|
198 | TagMap tags = new TagMap();
|
---|
199 | tags.put("existing", "new");
|
---|
200 |
|
---|
201 | HashMap<String, String> tagsRemove = new HashMap<>();
|
---|
202 | tagsRemove.put("existing", "");
|
---|
203 |
|
---|
204 | Way way = testData.createWay(20, node1);
|
---|
205 | way.put("name", "xy");
|
---|
206 | way.put("existing", "existing");
|
---|
207 | Relation relation = testData.createRelation(30);
|
---|
208 | relation.put("name", "xy");
|
---|
209 | relation.put("existing", "existing");
|
---|
210 |
|
---|
211 | // nop
|
---|
212 | assertTrue(new ChangePropertyCommand(Arrays.asList(node2), tags).getDescriptionText()
|
---|
213 | .matches("Set.*tags for 0 objects"));
|
---|
214 |
|
---|
215 | // change 1 key on 1 element.
|
---|
216 | assertTrue(new ChangePropertyCommand(Arrays.asList(node1, node2), tags).getDescriptionText()
|
---|
217 | .matches("Set existing=new for node.*xy.*"));
|
---|
218 | assertTrue(new ChangePropertyCommand(Arrays.asList(way, node2), tags).getDescriptionText()
|
---|
219 | .matches("Set existing=new for way.*xy.*"));
|
---|
220 | assertTrue(new ChangePropertyCommand(Arrays.asList(relation, node2), tags).getDescriptionText()
|
---|
221 | .matches("Set existing=new for relation.*xy.*"));
|
---|
222 |
|
---|
223 | // remove 1 key on 1 element
|
---|
224 | assertTrue(new ChangePropertyCommand(Arrays.asList(node1, node3), tagsRemove).getDescriptionText()
|
---|
225 | .matches("Remove \"existing\" for node.*xy.*"));
|
---|
226 | assertTrue(new ChangePropertyCommand(Arrays.asList(way, node3), tagsRemove).getDescriptionText()
|
---|
227 | .matches("Remove \"existing\" for way.*xy.*"));
|
---|
228 | assertTrue(new ChangePropertyCommand(Arrays.asList(relation, node3), tagsRemove).getDescriptionText()
|
---|
229 | .matches("Remove \"existing\" for relation.*xy.*"));
|
---|
230 |
|
---|
231 | // change 1 key on 3 elements
|
---|
232 | assertEquals("Set existing=new for 3 objects",
|
---|
233 | new ChangePropertyCommand(Arrays.asList(node1, node2, way, relation), tags).getDescriptionText());
|
---|
234 | // remove 1 key on 3 elements
|
---|
235 | assertEquals("Remove \"existing\" for 3 objects",
|
---|
236 | new ChangePropertyCommand(Arrays.asList(node1, node3, way, relation), tagsRemove).getDescriptionText());
|
---|
237 |
|
---|
238 | // add 2 keys on 3 elements
|
---|
239 | tags.put("name", "a");
|
---|
240 | node2.put("name", "a");
|
---|
241 | assertEquals("Set 2 tags for 3 objects",
|
---|
242 | new ChangePropertyCommand(Arrays.asList(node1, node2, way, relation), tags).getDescriptionText());
|
---|
243 |
|
---|
244 | tagsRemove.put("name", "");
|
---|
245 | // remove 2 key on 3 elements
|
---|
246 | assertEquals("Deleted 2 tags for 3 objects",
|
---|
247 | new ChangePropertyCommand(Arrays.asList(node1, node3, way, relation), tagsRemove).getDescriptionText());
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Test {@link ChangePropertyCommand#getChildren()}
|
---|
252 | */
|
---|
253 | @Test
|
---|
254 | void testChildren() {
|
---|
255 | Node node1 = testData.createNode(15);
|
---|
256 | Node node2 = testData.createNode(16);
|
---|
257 | node1.put("name", "node1");
|
---|
258 | node2.put("name", "node2");
|
---|
259 |
|
---|
260 | assertNull(new ChangePropertyCommand(Arrays.asList(node1), "a", "b").getChildren());
|
---|
261 |
|
---|
262 | Collection<PseudoCommand> children = new ChangePropertyCommand(Arrays.asList(node1, node2), "a", "b").getChildren();
|
---|
263 | assertEquals(2, children.size());
|
---|
264 | List<Node> nodesToExpect = new ArrayList<>(Arrays.asList(node1, node2));
|
---|
265 | for (PseudoCommand c : children) {
|
---|
266 | assertNull(c.getChildren());
|
---|
267 | Collection<? extends OsmPrimitive> part = c.getParticipatingPrimitives();
|
---|
268 | assertEquals(1, part.size());
|
---|
269 | OsmPrimitive node = part.iterator().next();
|
---|
270 | assertTrue(nodesToExpect.remove(node));
|
---|
271 |
|
---|
272 | assertTrue(c.getDescriptionText().matches(".*" + node.get("name") + ".*"));
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Unit test of methods {@link ChangePropertyCommand#equals} and {@link ChangePropertyCommand#hashCode}.
|
---|
278 | */
|
---|
279 | @Test
|
---|
280 | void testEqualsContract() {
|
---|
281 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
282 | EqualsVerifier.forClass(ChangePropertyCommand.class).usingGetClass()
|
---|
283 | .withPrefabValues(DataSet.class,
|
---|
284 | new DataSet(), new DataSet())
|
---|
285 | .withPrefabValues(User.class,
|
---|
286 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
287 | .withPrefabValues(OsmDataLayer.class,
|
---|
288 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
289 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
290 | .verify();
|
---|
291 | }
|
---|
292 | }
|
---|