source: josm/trunk/test/unit/org/openstreetmap/josm/command/SequenceCommandTest.java@ 11241

Last change on this file since 11241 was 10758, checked in by Don-vip, 8 years ago

sonar - squid:S3578 - Test methods should comply with a naming convention

File size: 8.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.junit.Assert.assertArrayEquals;
5import static org.junit.Assert.assertEquals;
6import static org.junit.Assert.assertFalse;
7import static org.junit.Assert.assertNull;
8import static org.junit.Assert.assertTrue;
9import static org.junit.Assert.fail;
10
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14
15import org.junit.Before;
16import org.junit.Rule;
17import org.junit.Test;
18import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.User;
23import org.openstreetmap.josm.gui.layer.OsmDataLayer;
24import org.openstreetmap.josm.testutils.JOSMTestRules;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27import nl.jqno.equalsverifier.EqualsVerifier;
28import nl.jqno.equalsverifier.Warning;
29
30/**
31 * Unit tests of {@link SequenceCommand} class.
32 */
33public class SequenceCommandTest {
34
35 /**
36 * We need prefs for nodes.
37 */
38 @Rule
39 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
40 public JOSMTestRules test = new JOSMTestRules().preferences();
41 private CommandTestDataWithRelation testData;
42
43 /**
44 * Set up the test data.
45 */
46 @Before
47 public void createTestData() {
48 testData = new CommandTestDataWithRelation();
49 }
50
51 /**
52 * Test {@link SequenceCommand#executeCommand()}
53 */
54 @Test
55 public void testExecute() {
56 final TestCommand command1 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode));
57 TestCommand command2 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode2)) {
58 @Override
59 public boolean executeCommand() {
60 assertTrue(command1.executed);
61 return super.executeCommand();
62 }
63 };
64 SequenceCommand command = new SequenceCommand("seq", Arrays.<Command>asList(command1, command2));
65
66 command.executeCommand();
67
68 assertTrue(command1.executed);
69 assertTrue(command2.executed);
70 }
71
72 /**
73 * Test {@link SequenceCommand#undoCommand()}
74 */
75 @Test
76 public void testUndo() {
77 final TestCommand command2 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode2));
78 TestCommand command1 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode)) {
79 @Override
80 public void undoCommand() {
81 assertFalse(command2.executed);
82 super.undoCommand();
83 }
84 };
85 SequenceCommand command = new SequenceCommand("seq", Arrays.<Command>asList(command1, command2));
86
87 command.executeCommand();
88
89 command.undoCommand();
90
91 assertFalse(command1.executed);
92 assertFalse(command2.executed);
93
94 command.executeCommand();
95
96 assertTrue(command1.executed);
97 assertTrue(command2.executed);
98 }
99
100 /**
101 * Test {@link SequenceCommand#undoCommand()}
102 */
103 @Test
104 public void testGetLastCommand() {
105 final TestCommand command1 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode));
106 final TestCommand command2 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode2));
107
108 assertEquals(command2, new SequenceCommand("seq", command1, command2).getLastCommand());
109 assertNull(new SequenceCommand("seq").getLastCommand());
110 }
111
112 /**
113 * Tests {@link SequenceCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
114 */
115 @Test
116 public void testFillModifiedData() {
117 Command command1 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode));
118 Command command2 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode2));
119 Command command3 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingWay)) {
120 @Override
121 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
122 Collection<OsmPrimitive> added) {
123 deleted.addAll(primitives);
124 }
125 };
126 Command command4 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingRelation)) {
127 @Override
128 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
129 Collection<OsmPrimitive> added) {
130 added.addAll(primitives);
131 }
132 };
133
134 ArrayList<OsmPrimitive> modified = new ArrayList<>();
135 ArrayList<OsmPrimitive> deleted = new ArrayList<>();
136 ArrayList<OsmPrimitive> added = new ArrayList<>();
137 SequenceCommand command = new SequenceCommand("seq", command1, command2, command3, command4);
138 command.fillModifiedData(modified, deleted, added);
139 assertArrayEquals(new Object[] {testData.existingNode, testData.existingNode2}, modified.toArray());
140 assertArrayEquals(new Object[] {testData.existingWay}, deleted.toArray());
141 assertArrayEquals(new Object[] {testData.existingRelation}, added.toArray());
142 }
143
144 /**
145 * Tests {@link SequenceCommand#getParticipatingPrimitives()}
146 */
147 @Test
148 public void testGetParticipatingPrimitives() {
149 Command command1 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode));
150 Command command2 = new TestCommand(Arrays.<OsmPrimitive>asList(testData.existingNode2));
151
152 SequenceCommand command = new SequenceCommand("seq", command1, command2);
153 command.executeCommand();
154 Collection<? extends OsmPrimitive> primitives = command.getParticipatingPrimitives();
155 assertEquals(2, primitives.size());
156 assertTrue(primitives.contains(testData.existingNode));
157 assertTrue(primitives.contains(testData.existingNode2));
158 }
159
160 /**
161 * Test {@link SequenceCommand#getDescriptionText()}
162 */
163 @Test
164 public void testDescription() {
165 assertTrue(new SequenceCommand("test").getDescriptionText().matches("Sequence: test"));
166 }
167
168 /**
169 * Unit test of methods {@link SequenceCommand#equals} and {@link SequenceCommand#hashCode}.
170 */
171 @Test
172 public void testEqualsContract() {
173 EqualsVerifier.forClass(SequenceCommand.class).usingGetClass()
174 .withPrefabValues(Command.class,
175 new AddCommand(new Node(1)), new AddCommand(new Node(2)))
176 .withPrefabValues(DataSet.class,
177 new DataSet(), new DataSet())
178 .withPrefabValues(User.class,
179 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
180 .withPrefabValues(OsmDataLayer.class,
181 new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
182 .suppress(Warning.NONFINAL_FIELDS)
183 .verify();
184 }
185
186 private static class TestCommand extends Command {
187 protected final Collection<? extends OsmPrimitive> primitives;
188 protected boolean executed;
189
190 TestCommand(Collection<? extends OsmPrimitive> primitives) {
191 super();
192 this.primitives = primitives;
193 }
194
195 @Override
196 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
197 Collection<OsmPrimitive> added) {
198 modified.addAll(primitives);
199 }
200
201 @Override
202 public String getDescriptionText() {
203 fail("Should not be called");
204 return "";
205 }
206
207 @Override
208 public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
209 return primitives;
210 }
211
212 @Override
213 public boolean executeCommand() {
214 assertFalse("Cannot execute twice", executed);
215 executed = true;
216 return true;
217 }
218
219 @Override
220 public void undoCommand() {
221 assertTrue("Cannot undo without execute", executed);
222 executed = false;
223 }
224
225 @Override
226 public String toString() {
227 return "TestCommand [primitives=" + primitives + "]";
228 }
229
230 }
231}
Note: See TracBrowser for help on using the repository browser.