1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertArrayEquals;
|
---|
5 | import static org.junit.Assert.assertEquals;
|
---|
6 | import static org.junit.Assert.assertFalse;
|
---|
7 | import static org.junit.Assert.assertNull;
|
---|
8 | import static org.junit.Assert.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.Before;
|
---|
17 | import org.junit.Rule;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.command.CommandTest.CommandTestData;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
24 | import org.openstreetmap.josm.data.osm.TagMap;
|
---|
25 | import org.openstreetmap.josm.data.osm.User;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
28 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
29 |
|
---|
30 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
31 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
32 | import nl.jqno.equalsverifier.Warning;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Unit tests of {@link ChangePropertyCommand} class.
|
---|
36 | */
|
---|
37 | public class ChangePropertyCommandTest {
|
---|
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 CommandTestData testData;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Set up the test data.
|
---|
49 | */
|
---|
50 | @Before
|
---|
51 | public void createTestData() {
|
---|
52 | testData = new CommandTestData();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Checks that the short constructors create the right {@link ChangePropertyCommand}
|
---|
57 | */
|
---|
58 | @Test
|
---|
59 | public void testShortConstructor() {
|
---|
60 | ChangePropertyCommand command = new ChangePropertyCommand(Arrays.asList(testData.existingNode), "a", "b");
|
---|
61 | assertEquals("b", command.getTags().get("a"));
|
---|
62 | assertEquals(1, command.getTags().size());
|
---|
63 | assertEquals(1, command.getObjectsNumber());
|
---|
64 |
|
---|
65 | command = new ChangePropertyCommand(testData.existingNode, "a", "b");
|
---|
66 | assertEquals("b", command.getTags().get("a"));
|
---|
67 | assertEquals(1, command.getTags().size());
|
---|
68 | assertEquals(1, command.getObjectsNumber());
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Checks that {@link ChangePropertyCommand} adds/updates a property
|
---|
73 | */
|
---|
74 | @Test
|
---|
75 | public void testUpdateSingleProperty() {
|
---|
76 | Node node1 = testData.createNode(14);
|
---|
77 | Node node2 = testData.createNode(15);
|
---|
78 | node2.removeAll();
|
---|
79 |
|
---|
80 | TagMap tags = new TagMap();
|
---|
81 | tags.put("existing", "new");
|
---|
82 | new ChangePropertyCommand(Arrays.<OsmPrimitive>asList(node1, node2), tags).executeCommand();
|
---|
83 | assertEquals("new", node1.get("existing"));
|
---|
84 | assertEquals("new", node2.get("existing"));
|
---|
85 |
|
---|
86 | assertTrue(node1.isModified());
|
---|
87 | assertTrue(node2.isModified());
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Checks that {@link ChangePropertyCommand} removes a property
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | public void testRemoveProperty() {
|
---|
95 | Node node1 = testData.createNode(14);
|
---|
96 | Node node2 = testData.createNode(15);
|
---|
97 | node2.removeAll();
|
---|
98 |
|
---|
99 | HashMap<String, String> tags = new HashMap<>();
|
---|
100 | tags.put("existing", "");
|
---|
101 | new ChangePropertyCommand(Arrays.<OsmPrimitive>asList(node1, node2), tags).executeCommand();
|
---|
102 | assertNull(node1.get("existing"));
|
---|
103 | assertNull(node2.get("existing"));
|
---|
104 |
|
---|
105 | assertTrue(node1.isModified());
|
---|
106 | assertFalse(node2.isModified());
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Checks that {@link ChangePropertyCommand} adds/updates multiple properties
|
---|
111 | */
|
---|
112 | @Test
|
---|
113 | public void testUpdateMultipleProperties() {
|
---|
114 | Node node1 = testData.createNode(14);
|
---|
115 | Node node2 = testData.createNode(15);
|
---|
116 | node2.removeAll();
|
---|
117 | node2.put("test", "xx");
|
---|
118 | node2.put("remove", "xx");
|
---|
119 |
|
---|
120 | HashMap<String, String> tags = new HashMap<>();
|
---|
121 | tags.put("existing", "existing");
|
---|
122 | tags.put("test", "test");
|
---|
123 | tags.put("remove", "");
|
---|
124 | new ChangePropertyCommand(Arrays.<OsmPrimitive>asList(node1, node2), tags).executeCommand();
|
---|
125 | assertEquals("existing", node1.get("existing"));
|
---|
126 | assertEquals("existing", node2.get("existing"));
|
---|
127 | assertEquals("test", node1.get("test"));
|
---|
128 | assertEquals("test", node2.get("test"));
|
---|
129 | assertNull(node1.get("remove"));
|
---|
130 | assertNull(node2.get("remove"));
|
---|
131 |
|
---|
132 | assertTrue(node1.isModified());
|
---|
133 | assertTrue(node2.isModified());
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Checks that {@link ChangePropertyCommand} adds/updates a property
|
---|
138 | */
|
---|
139 | @Test
|
---|
140 | public void testUpdateIgnoresExistingProperty() {
|
---|
141 | Node node1 = testData.createNode(14);
|
---|
142 | Node node2 = testData.createNode(15);
|
---|
143 | node2.removeAll();
|
---|
144 |
|
---|
145 | TagMap tags = new TagMap();
|
---|
146 | tags.put("existing", "existing");
|
---|
147 | new ChangePropertyCommand(Arrays.<OsmPrimitive>asList(node1, node2), tags).executeCommand();
|
---|
148 | assertEquals("existing", node1.get("existing"));
|
---|
149 | assertEquals("existing", node2.get("existing"));
|
---|
150 |
|
---|
151 | assertFalse(node1.isModified());
|
---|
152 | assertTrue(node2.isModified());
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Tests {@link ChangePropertyCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
157 | * and {@link ChangePropertyCommand#getObjectsNumber()}
|
---|
158 | */
|
---|
159 | @Test
|
---|
160 | public void testFillModifiedData() {
|
---|
161 | Node node1 = testData.createNode(14);
|
---|
162 | Node node2 = testData.createNode(15);
|
---|
163 | node2.put("existing", "new");
|
---|
164 |
|
---|
165 | TagMap tags = new TagMap();
|
---|
166 | tags.put("existing", "new");
|
---|
167 |
|
---|
168 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
169 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
170 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
171 | new ChangePropertyCommand(Arrays.asList(node1, node2), tags).fillModifiedData(modified, deleted, added);
|
---|
172 | assertArrayEquals(new Object[] {node1}, modified.toArray());
|
---|
173 | assertArrayEquals(new Object[] {}, deleted.toArray());
|
---|
174 | assertArrayEquals(new Object[] {}, added.toArray());
|
---|
175 |
|
---|
176 | assertEquals(1, new ChangePropertyCommand(Arrays.asList(node1, node2), tags).getObjectsNumber());
|
---|
177 |
|
---|
178 | tags.clear();
|
---|
179 | assertEquals(0, new ChangePropertyCommand(Arrays.asList(node1, node2), tags).getObjectsNumber());
|
---|
180 |
|
---|
181 | tags.put("a", "b");
|
---|
182 | assertEquals(2, new ChangePropertyCommand(Arrays.asList(node1, node2), tags).getObjectsNumber());
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Test {@link ChangePropertyCommand#getDescriptionText()}
|
---|
187 | */
|
---|
188 | @Test
|
---|
189 | public void testDescription() {
|
---|
190 | Node node1 = testData.createNode(14);
|
---|
191 | Node node2 = testData.createNode(15);
|
---|
192 | Node node3 = testData.createNode(16);
|
---|
193 | node1.put("name", "xy");
|
---|
194 | node2.put("existing", "new");
|
---|
195 | node3.put("existing", null);
|
---|
196 |
|
---|
197 | TagMap tags = new TagMap();
|
---|
198 | tags.put("existing", "new");
|
---|
199 |
|
---|
200 | HashMap<String, String> tagsRemove = new HashMap<>();
|
---|
201 | tagsRemove.put("existing", "");
|
---|
202 |
|
---|
203 | Way way = new Way();
|
---|
204 | way.addNode(node1);
|
---|
205 | way.put("name", "xy");
|
---|
206 | way.put("existing", "existing");
|
---|
207 | Relation relation = new Relation();
|
---|
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 | public 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 | public void testEqualsContract() {
|
---|
281 | EqualsVerifier.forClass(ChangePropertyCommand.class).usingGetClass()
|
---|
282 | .withPrefabValues(DataSet.class,
|
---|
283 | new DataSet(), new DataSet())
|
---|
284 | .withPrefabValues(User.class,
|
---|
285 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
286 | .withPrefabValues(OsmDataLayer.class,
|
---|
287 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
288 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
289 | .verify();
|
---|
290 | }
|
---|
291 | }
|
---|