1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
8 |
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.BeforeEach;
|
---|
14 | import org.junit.jupiter.api.Test;
|
---|
15 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
16 | import org.openstreetmap.josm.TestUtils;
|
---|
17 | import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.User;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
23 |
|
---|
24 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
25 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
26 | import nl.jqno.equalsverifier.Warning;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Unit tests of {@link SelectCommand} class.
|
---|
30 | */
|
---|
31 | class SelectCommandTest {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * We need prefs for nodes.
|
---|
35 | */
|
---|
36 | @RegisterExtension
|
---|
37 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
38 | public JOSMTestRules test = new JOSMTestRules().preferences();
|
---|
39 | private CommandTestDataWithRelation testData;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Set up the test data.
|
---|
43 | */
|
---|
44 | @BeforeEach
|
---|
45 | public void createTestData() {
|
---|
46 | testData = new CommandTestDataWithRelation();
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Test {@link SelectCommand#executeCommand()}
|
---|
51 | */
|
---|
52 | @Test
|
---|
53 | void testExecute() {
|
---|
54 | SelectCommand command = new SelectCommand(testData.layer.getDataSet(), Arrays.asList(testData.existingNode, testData.existingWay));
|
---|
55 |
|
---|
56 | testData.layer.getDataSet().setSelected(Arrays.asList(testData.existingNode2));
|
---|
57 |
|
---|
58 | command.executeCommand();
|
---|
59 |
|
---|
60 | assertTrue(testData.existingNode.isSelected());
|
---|
61 | assertFalse(testData.existingNode2.isSelected());
|
---|
62 | assertTrue(testData.existingWay.isSelected());
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Test {@link SelectCommand#executeCommand()}
|
---|
67 | */
|
---|
68 | @Test
|
---|
69 | void testExecuteAfterModify() {
|
---|
70 | List<OsmPrimitive> list = new ArrayList<>(Arrays.asList(testData.existingNode, testData.existingWay));
|
---|
71 | SelectCommand command = new SelectCommand(testData.layer.getDataSet(), list);
|
---|
72 |
|
---|
73 | list.remove(testData.existingNode);
|
---|
74 | list.add(testData.existingNode2);
|
---|
75 |
|
---|
76 | command.executeCommand();
|
---|
77 |
|
---|
78 | assertTrue(testData.existingNode.isSelected());
|
---|
79 | assertFalse(testData.existingNode2.isSelected());
|
---|
80 | assertTrue(testData.existingWay.isSelected());
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Test {@link SelectCommand#undoCommand()}
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | void testUndo() {
|
---|
88 | SelectCommand command = new SelectCommand(testData.layer.getDataSet(), Arrays.asList(testData.existingNode, testData.existingWay));
|
---|
89 | testData.layer.getDataSet().setSelected(Arrays.asList(testData.existingNode2));
|
---|
90 |
|
---|
91 | command.executeCommand();
|
---|
92 |
|
---|
93 | command.undoCommand();
|
---|
94 |
|
---|
95 | assertFalse(testData.existingNode.isSelected());
|
---|
96 | assertTrue(testData.existingNode2.isSelected());
|
---|
97 | assertFalse(testData.existingWay.isSelected());
|
---|
98 |
|
---|
99 | command.executeCommand();
|
---|
100 |
|
---|
101 | assertTrue(testData.existingNode.isSelected());
|
---|
102 | assertFalse(testData.existingNode2.isSelected());
|
---|
103 | assertTrue(testData.existingWay.isSelected());
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Tests {@link SelectCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
108 | */
|
---|
109 | @Test
|
---|
110 | void testFillModifiedData() {
|
---|
111 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
112 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
113 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
114 | SelectCommand command = new SelectCommand(testData.layer.getDataSet(), Arrays.asList(testData.existingNode, testData.existingWay));
|
---|
115 | command.fillModifiedData(modified, deleted, added);
|
---|
116 | // intentionally empty.
|
---|
117 | assertArrayEquals(new Object[] {}, modified.toArray());
|
---|
118 | assertArrayEquals(new Object[] {}, deleted.toArray());
|
---|
119 | assertArrayEquals(new Object[] {}, added.toArray());
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Tests {@link SelectCommand#getParticipatingPrimitives()}
|
---|
124 | */
|
---|
125 | @Test
|
---|
126 | void testGetParticipatingPrimitives() {
|
---|
127 | SelectCommand command = new SelectCommand(testData.layer.getDataSet(), Arrays.asList(testData.existingNode));
|
---|
128 | command.executeCommand();
|
---|
129 | assertArrayEquals(new Object[] {testData.existingNode}, command.getParticipatingPrimitives().toArray());
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Test {@link SelectCommand#getDescriptionText()}
|
---|
134 | */
|
---|
135 | @Test
|
---|
136 | void testDescription() {
|
---|
137 | DataSet ds = testData.layer.getDataSet();
|
---|
138 | assertTrue(new SelectCommand(ds, Arrays.<OsmPrimitive>asList(testData.existingNode))
|
---|
139 | .getDescriptionText().matches("Selected 1 object"));
|
---|
140 | assertTrue(new SelectCommand(ds, Arrays.asList(testData.existingNode, testData.existingWay))
|
---|
141 | .getDescriptionText().matches("Selected 2 objects"));
|
---|
142 | assertTrue(new SelectCommand(ds, Arrays.<OsmPrimitive>asList())
|
---|
143 | .getDescriptionText().matches("Selected 0 objects"));
|
---|
144 | assertTrue(new SelectCommand(ds, null)
|
---|
145 | .getDescriptionText().matches("Selected 0 objects"));
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Unit test of methods {@link SelectCommand#equals} and {@link SelectCommand#hashCode}.
|
---|
150 | */
|
---|
151 | @Test
|
---|
152 | void testEqualsContract() {
|
---|
153 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
154 | EqualsVerifier.forClass(SelectCommand.class).usingGetClass()
|
---|
155 | .withPrefabValues(DataSet.class,
|
---|
156 | new DataSet(), new DataSet())
|
---|
157 | .withPrefabValues(User.class,
|
---|
158 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
159 | .withPrefabValues(OsmDataLayer.class,
|
---|
160 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
161 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
162 | .verify();
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Unit test of {@link SelectCommand#SelectCommand}.
|
---|
167 | */
|
---|
168 | @Test
|
---|
169 | void testConstructorIAE() {
|
---|
170 | assertThrows(IllegalArgumentException.class, () -> new SelectCommand(new DataSet(), Arrays.asList(new OsmPrimitive[] {null})));
|
---|
171 | }
|
---|
172 | }
|
---|