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