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.assertNotSame;
|
---|
8 | import static org.junit.Assert.assertNull;
|
---|
9 | import static org.junit.Assert.assertSame;
|
---|
10 | import static org.junit.Assert.assertTrue;
|
---|
11 | import static org.junit.Assert.fail;
|
---|
12 |
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Arrays;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.Collections;
|
---|
17 |
|
---|
18 | import org.junit.Before;
|
---|
19 | import org.junit.Rule;
|
---|
20 | import org.junit.Test;
|
---|
21 | import org.openstreetmap.josm.TestUtils;
|
---|
22 | import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
|
---|
23 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.User;
|
---|
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 SequenceCommand} class.
|
---|
36 | */
|
---|
37 | public class SequenceCommandTest {
|
---|
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();
|
---|
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 | * Test {@link SequenceCommand#executeCommand()}
|
---|
57 | */
|
---|
58 | @Test
|
---|
59 | public void testExecute() {
|
---|
60 | DataSet ds = new DataSet();
|
---|
61 | final TestCommand command1 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode));
|
---|
62 | TestCommand command2 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode2)) {
|
---|
63 | @Override
|
---|
64 | public boolean executeCommand() {
|
---|
65 | assertTrue(command1.executed);
|
---|
66 | return super.executeCommand();
|
---|
67 | }
|
---|
68 | };
|
---|
69 | SequenceCommand command = new SequenceCommand("seq", Arrays.<Command>asList(command1, command2));
|
---|
70 |
|
---|
71 | command.executeCommand();
|
---|
72 |
|
---|
73 | assertTrue(command1.executed);
|
---|
74 | assertTrue(command2.executed);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Test {@link SequenceCommand#undoCommand()}
|
---|
79 | */
|
---|
80 | @Test
|
---|
81 | public void testUndo() {
|
---|
82 | DataSet ds = new DataSet();
|
---|
83 | final TestCommand command2 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode2));
|
---|
84 | TestCommand command1 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode)) {
|
---|
85 | @Override
|
---|
86 | public void undoCommand() {
|
---|
87 | assertFalse(command2.executed);
|
---|
88 | super.undoCommand();
|
---|
89 | }
|
---|
90 | };
|
---|
91 | SequenceCommand command = new SequenceCommand("seq", Arrays.<Command>asList(command1, command2));
|
---|
92 |
|
---|
93 | command.executeCommand();
|
---|
94 |
|
---|
95 | command.undoCommand();
|
---|
96 |
|
---|
97 | assertFalse(command1.executed);
|
---|
98 | assertFalse(command2.executed);
|
---|
99 |
|
---|
100 | command.executeCommand();
|
---|
101 |
|
---|
102 | assertTrue(command1.executed);
|
---|
103 | assertTrue(command2.executed);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Test {@link SequenceCommand#executeCommand()} rollback if case of subcommand failure.
|
---|
108 | */
|
---|
109 | @Test
|
---|
110 | public void testExecuteRollback() {
|
---|
111 | DataSet ds = new DataSet();
|
---|
112 | TestCommand command1 = new TestCommand(ds, null);
|
---|
113 | FailingCommand command2 = new FailingCommand(ds);
|
---|
114 | TestCommand command3 = new TestCommand(ds, null);
|
---|
115 | SequenceCommand command = new SequenceCommand("seq", Arrays.<Command>asList(command1, command2, command3));
|
---|
116 | assertFalse(command.executeCommand());
|
---|
117 | assertFalse(command1.executed);
|
---|
118 | // Don't check command2 executed state as it's possible but not necessary to undo failed commands
|
---|
119 | assertFalse(command3.executed);
|
---|
120 | command.undoCommand();
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Test {@link SequenceCommand#executeCommand()} with continueOnError = true
|
---|
125 | */
|
---|
126 | @Test
|
---|
127 | public void testContinueOnErrors() {
|
---|
128 | DataSet ds = new DataSet();
|
---|
129 | TestCommand command1 = new TestCommand(ds, null);
|
---|
130 | FailingCommand command2 = new FailingCommand(ds);
|
---|
131 | TestCommand command3 = new TestCommand(ds, null);
|
---|
132 | SequenceCommand command = new SequenceCommand("seq", Arrays.<Command>asList(command1, command2, command3), true);
|
---|
133 | assertTrue(command.executeCommand());
|
---|
134 | assertTrue(command1.executed);
|
---|
135 | assertTrue(command3.executed);
|
---|
136 | command.undoCommand();
|
---|
137 | assertFalse(command1.executed);
|
---|
138 | // Don't check command2 executed state as it's possible but not necessary to undo failed commands
|
---|
139 | assertFalse(command3.executed);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Test {@link SequenceCommand#undoCommand()}
|
---|
144 | */
|
---|
145 | @Test
|
---|
146 | public void testGetLastCommand() {
|
---|
147 | DataSet ds = new DataSet();
|
---|
148 | final TestCommand command1 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode));
|
---|
149 | final TestCommand command2 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode2));
|
---|
150 |
|
---|
151 | assertEquals(command2, new SequenceCommand(ds, "seq", Arrays.asList(command1, command2), false).getLastCommand());
|
---|
152 | assertNull(new SequenceCommand(ds, "seq", Collections.emptyList(), false).getLastCommand());
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Tests {@link SequenceCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
157 | */
|
---|
158 | @Test
|
---|
159 | public void testFillModifiedData() {
|
---|
160 | DataSet ds = new DataSet();
|
---|
161 | Command command1 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode));
|
---|
162 | Command command2 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode2));
|
---|
163 | Command command3 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingWay)) {
|
---|
164 | @Override
|
---|
165 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
166 | Collection<OsmPrimitive> added) {
|
---|
167 | deleted.addAll(primitives);
|
---|
168 | }
|
---|
169 | };
|
---|
170 | Command command4 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingRelation)) {
|
---|
171 | @Override
|
---|
172 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
173 | Collection<OsmPrimitive> added) {
|
---|
174 | added.addAll(primitives);
|
---|
175 | }
|
---|
176 | };
|
---|
177 |
|
---|
178 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
179 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
180 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
181 | SequenceCommand command = new SequenceCommand("seq", command1, command2, command3, command4);
|
---|
182 | command.fillModifiedData(modified, deleted, added);
|
---|
183 | assertArrayEquals(new Object[] {testData.existingNode, testData.existingNode2}, modified.toArray());
|
---|
184 | assertArrayEquals(new Object[] {testData.existingWay}, deleted.toArray());
|
---|
185 | assertArrayEquals(new Object[] {testData.existingRelation}, added.toArray());
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Tests {@link SequenceCommand#getParticipatingPrimitives()}
|
---|
190 | */
|
---|
191 | @Test
|
---|
192 | public void testGetParticipatingPrimitives() {
|
---|
193 | DataSet ds = new DataSet();
|
---|
194 | Command command1 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode));
|
---|
195 | Command command2 = new TestCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode2));
|
---|
196 |
|
---|
197 | SequenceCommand command = new SequenceCommand("seq", command1, command2);
|
---|
198 | command.executeCommand();
|
---|
199 | Collection<? extends OsmPrimitive> primitives = command.getParticipatingPrimitives();
|
---|
200 | assertEquals(2, primitives.size());
|
---|
201 | assertTrue(primitives.contains(testData.existingNode));
|
---|
202 | assertTrue(primitives.contains(testData.existingNode2));
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Test {@link SequenceCommand#getDescriptionText()}
|
---|
207 | */
|
---|
208 | @Test
|
---|
209 | public void testDescription() {
|
---|
210 | assertTrue(new SequenceCommand(new DataSet(), "test", Collections.emptyList(), false).getDescriptionText().matches("Sequence: test"));
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Unit test of methods {@link SequenceCommand#equals} and {@link SequenceCommand#hashCode}.
|
---|
215 | */
|
---|
216 | @Test
|
---|
217 | public void testEqualsContract() {
|
---|
218 | DataSet ds = new DataSet();
|
---|
219 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
220 | EqualsVerifier.forClass(SequenceCommand.class).usingGetClass()
|
---|
221 | .withPrefabValues(Command.class,
|
---|
222 | new AddCommand(ds, new Node(1)), new AddCommand(ds, new Node(2)))
|
---|
223 | .withPrefabValues(DataSet.class,
|
---|
224 | new DataSet(), new DataSet())
|
---|
225 | .withPrefabValues(User.class,
|
---|
226 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
227 | .withPrefabValues(OsmDataLayer.class,
|
---|
228 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
229 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
230 | .verify();
|
---|
231 | }
|
---|
232 |
|
---|
233 | private static class TestCommand extends Command {
|
---|
234 | protected final Collection<? extends OsmPrimitive> primitives;
|
---|
235 | protected boolean executed;
|
---|
236 |
|
---|
237 | TestCommand(DataSet ds, Collection<? extends OsmPrimitive> primitives) {
|
---|
238 | super(ds);
|
---|
239 | this.primitives = primitives;
|
---|
240 | }
|
---|
241 |
|
---|
242 | @Override
|
---|
243 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
244 | Collection<OsmPrimitive> added) {
|
---|
245 | modified.addAll(primitives);
|
---|
246 | }
|
---|
247 |
|
---|
248 | @Override
|
---|
249 | public String getDescriptionText() {
|
---|
250 | fail("Should not be called");
|
---|
251 | return "";
|
---|
252 | }
|
---|
253 |
|
---|
254 | @Override
|
---|
255 | public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
|
---|
256 | return primitives;
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public boolean executeCommand() {
|
---|
261 | assertFalse("Cannot execute twice", executed);
|
---|
262 | executed = true;
|
---|
263 | return true;
|
---|
264 | }
|
---|
265 |
|
---|
266 | @Override
|
---|
267 | public void undoCommand() {
|
---|
268 | assertTrue("Cannot undo without execute", executed);
|
---|
269 | executed = false;
|
---|
270 | }
|
---|
271 |
|
---|
272 | @Override
|
---|
273 | public String toString() {
|
---|
274 | return "TestCommand [primitives=" + primitives + "]";
|
---|
275 | }
|
---|
276 |
|
---|
277 | }
|
---|
278 |
|
---|
279 | private static class FailingCommand extends TestCommand {
|
---|
280 |
|
---|
281 | FailingCommand(DataSet ds) {
|
---|
282 | super(ds, null);
|
---|
283 | }
|
---|
284 |
|
---|
285 | @Override
|
---|
286 | public boolean executeCommand() {
|
---|
287 | executed = true;
|
---|
288 | return false;
|
---|
289 | }
|
---|
290 |
|
---|
291 | @Override
|
---|
292 | public void undoCommand() {
|
---|
293 | assertTrue("Cannot undo without execute", executed);
|
---|
294 | executed = false;
|
---|
295 | }
|
---|
296 |
|
---|
297 | @Override
|
---|
298 | public String toString() {
|
---|
299 | return "FailingCommand";
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Test {@link SequenceCommand#wrapIfNeeded}
|
---|
305 | */
|
---|
306 | @Test
|
---|
307 | public void testWrapIfNeeded() {
|
---|
308 | DataSet ds = new DataSet();
|
---|
309 | TestCommand command1 = new TestCommand(ds, Collections.<OsmPrimitive>singletonList(testData.existingNode));
|
---|
310 | TestCommand command2 = new TestCommand(ds, Collections.<OsmPrimitive>singletonList(testData.existingNode2));
|
---|
311 | assertSame(command1, SequenceCommand.wrapIfNeeded("foo", command1));
|
---|
312 | assertNotSame(command1, SequenceCommand.wrapIfNeeded("foo", command1, command2));
|
---|
313 | assertEquals(new SequenceCommand("foo", command1, command2), SequenceCommand.wrapIfNeeded("foo", command1, command2));
|
---|
314 | }
|
---|
315 | }
|
---|