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