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.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
9 |
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import org.junit.jupiter.api.BeforeEach;
|
---|
16 | import org.junit.jupiter.api.Test;
|
---|
17 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
18 | import org.openstreetmap.josm.TestUtils;
|
---|
19 | import org.openstreetmap.josm.command.CommandTest.CommandTestData;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
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.data.osm.Way;
|
---|
26 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
27 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
28 |
|
---|
29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
30 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
31 | import nl.jqno.equalsverifier.Warning;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Unit tests of {@link ChangeNodesCommand} class.
|
---|
35 | */
|
---|
36 | class ChangeNodesCommandTest {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * We need prefs for nodes.
|
---|
40 | */
|
---|
41 | @RegisterExtension
|
---|
42 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
43 | public JOSMTestRules test = new JOSMTestRules().preferences().i18n();
|
---|
44 | private CommandTestData testData;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Set up the test data.
|
---|
48 | */
|
---|
49 | @BeforeEach
|
---|
50 | public void createTestData() {
|
---|
51 | testData = new CommandTestData();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Test that empty ways are prevented.
|
---|
56 | */
|
---|
57 | @Test
|
---|
58 | void testPreventEmptyWays() {
|
---|
59 | assertThrows(IllegalArgumentException.class, () -> new ChangeNodesCommand(testData.existingWay, Collections.<Node>emptyList()));
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Test {@link ChangeNodesCommand#executeCommand()}
|
---|
64 | */
|
---|
65 | @Test
|
---|
66 | void testChange() {
|
---|
67 | List<Node> newNodes = testData.existingWay.getNodes();
|
---|
68 | Collections.reverse(newNodes);
|
---|
69 |
|
---|
70 | new ChangeNodesCommand(testData.existingWay, newNodes).executeCommand();
|
---|
71 | assertArrayEquals(newNodes.toArray(), testData.existingWay.getNodes().toArray());
|
---|
72 |
|
---|
73 | // tags are unchanged
|
---|
74 | assertEquals("existing", testData.existingWay.get("existing"));
|
---|
75 | assertTrue(testData.existingWay.isModified());
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Test {@link ChangeCommand#undoCommand()}
|
---|
80 | */
|
---|
81 | @Test
|
---|
82 | void testUndo() {
|
---|
83 | List<Node> newNodes = testData.existingWay.getNodes();
|
---|
84 | Collections.reverse(newNodes);
|
---|
85 |
|
---|
86 | ChangeNodesCommand command = new ChangeNodesCommand(testData.existingWay, newNodes);
|
---|
87 | command.executeCommand();
|
---|
88 | command.undoCommand();
|
---|
89 | Collections.reverse(newNodes);
|
---|
90 | assertArrayEquals(newNodes.toArray(), testData.existingWay.getNodes().toArray());
|
---|
91 | assertFalse(testData.existingWay.isModified());
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Tests {@link ChangeNodesCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
96 | */
|
---|
97 | @Test
|
---|
98 | void testFillModifiedData() {
|
---|
99 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
100 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
101 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
102 | new ChangeNodesCommand(testData.existingWay, Arrays.asList(testData.existingNode)).fillModifiedData(modified,
|
---|
103 | deleted, added);
|
---|
104 | assertArrayEquals(new Object[] {testData.existingWay}, modified.toArray());
|
---|
105 | assertArrayEquals(new Object[] {}, deleted.toArray());
|
---|
106 | assertArrayEquals(new Object[] {}, added.toArray());
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Test {@link ChangeNodesCommand#getDescriptionText()}
|
---|
111 | */
|
---|
112 | @Test
|
---|
113 | void testDescription() {
|
---|
114 | Node node = new Node(LatLon.ZERO);
|
---|
115 | node.put("name", "xy");
|
---|
116 | Way way = new Way();
|
---|
117 | way.addNode(node);
|
---|
118 | way.put("name", "xy");
|
---|
119 | DataSet ds = new DataSet(node, way);
|
---|
120 | assertTrue(
|
---|
121 | new ChangeNodesCommand(ds, way, Arrays.asList(node)).getDescriptionText().matches("Change nodes of.*xy.*"));
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Unit test of methods {@link ChangeNodesCommand#equals} and {@link ChangeNodesCommand#hashCode}.
|
---|
126 | */
|
---|
127 | @Test
|
---|
128 | void testEqualsContract() {
|
---|
129 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
130 | EqualsVerifier.forClass(ChangeNodesCommand.class).usingGetClass()
|
---|
131 | .withPrefabValues(Way.class,
|
---|
132 | new Way(1), new Way(2))
|
---|
133 | .withPrefabValues(DataSet.class,
|
---|
134 | new DataSet(), new DataSet())
|
---|
135 | .withPrefabValues(User.class,
|
---|
136 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
137 | .withPrefabValues(OsmDataLayer.class,
|
---|
138 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
139 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
140 | .verify();
|
---|
141 | }
|
---|
142 | }
|
---|