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