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