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.assertNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
9 |
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.TestUtils;
|
---|
15 | import org.openstreetmap.josm.command.CommandTest.CommandTestData;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataIntegrityProblemException;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
22 | import org.openstreetmap.josm.data.osm.User;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
25 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
26 | import org.openstreetmap.josm.testutils.annotations.I18n;
|
---|
27 |
|
---|
28 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
29 | import nl.jqno.equalsverifier.Warning;
|
---|
30 | import org.junit.jupiter.api.BeforeEach;
|
---|
31 | import org.junit.jupiter.api.Test;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Unit tests of {@link ChangeCommand} class.
|
---|
35 | */
|
---|
36 | @I18n
|
---|
37 | // We need prefs for nodes.
|
---|
38 | @BasicPreferences
|
---|
39 | class ChangeCommandTest {
|
---|
40 | private CommandTestData testData;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Set up the test data.
|
---|
44 | */
|
---|
45 | @BeforeEach
|
---|
46 | public void createTestData() {
|
---|
47 | testData = new CommandTestData();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Test that empty ways are prevented.
|
---|
52 | */
|
---|
53 | @Test
|
---|
54 | void testPreventEmptyWays() {
|
---|
55 | Way emptyWay = new Way();
|
---|
56 | assertThrows(IllegalArgumentException.class, () -> new ChangeCommand(testData.existingWay, emptyWay));
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Test {@link ChangeCommand#executeCommand()}
|
---|
61 | */
|
---|
62 | @Test
|
---|
63 | void testChange() {
|
---|
64 | Node newNode = new Node(5);
|
---|
65 | newNode.setCoor(LatLon.NORTH_POLE);
|
---|
66 | newNode.put("new", "new");
|
---|
67 |
|
---|
68 | new ChangeCommand(testData.existingNode, newNode).executeCommand();
|
---|
69 |
|
---|
70 | assertEquals("new", testData.existingNode.get("new"));
|
---|
71 | assertNull(testData.existingNode.get("existing"));
|
---|
72 | assertEquals(LatLon.NORTH_POLE, testData.existingNode.getCoor());
|
---|
73 |
|
---|
74 | Way newWay = new Way(10);
|
---|
75 | List<Node> newNodes = testData.existingWay.getNodes();
|
---|
76 | Collections.reverse(newNodes);
|
---|
77 | newWay.setNodes(newNodes);
|
---|
78 |
|
---|
79 | new ChangeCommand(testData.existingWay, newWay).executeCommand();
|
---|
80 | assertArrayEquals(newNodes.toArray(), testData.existingWay.getNodes().toArray());
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Test {@link ChangeCommand#executeCommand()} fails if ID is changed
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | void testChangeIdChange() {
|
---|
88 | Node newNode = new Node(1);
|
---|
89 | newNode.setCoor(LatLon.NORTH_POLE);
|
---|
90 |
|
---|
91 | final ChangeCommand changeCommand = new ChangeCommand(testData.existingNode, newNode);
|
---|
92 | assertThrows(DataIntegrityProblemException.class, changeCommand::executeCommand);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Test {@link ChangeCommand#undoCommand()}
|
---|
97 | */
|
---|
98 | @Test
|
---|
99 | void testUndo() {
|
---|
100 | Node newNode = new Node(5);
|
---|
101 | newNode.setCoor(LatLon.NORTH_POLE);
|
---|
102 | newNode.put("new", "new");
|
---|
103 |
|
---|
104 | ChangeCommand command = new ChangeCommand(testData.existingNode, newNode);
|
---|
105 | command.executeCommand();
|
---|
106 |
|
---|
107 | assertEquals("new", testData.existingNode.get("new"));
|
---|
108 | assertEquals(LatLon.NORTH_POLE, testData.existingNode.getCoor());
|
---|
109 |
|
---|
110 | command.undoCommand();
|
---|
111 | assertNull(testData.existingNode.get("new"));
|
---|
112 | assertEquals("existing", testData.existingNode.get("existing"));
|
---|
113 | assertEquals(LatLon.ZERO, testData.existingNode.getCoor());
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Tests {@link ChangeCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
118 | */
|
---|
119 | @Test
|
---|
120 | void testFillModifiedData() {
|
---|
121 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
122 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
123 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
124 | new ChangeCommand(testData.existingNode, testData.existingNode).fillModifiedData(modified, deleted, added);
|
---|
125 | assertArrayEquals(new Object[] {testData.existingNode}, modified.toArray());
|
---|
126 | assertArrayEquals(new Object[] {}, deleted.toArray());
|
---|
127 | assertArrayEquals(new Object[] {}, added.toArray());
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Test {@link ChangeCommand#getDescriptionText()}
|
---|
132 | */
|
---|
133 | @Test
|
---|
134 | void testDescription() {
|
---|
135 | Node node = new Node(LatLon.ZERO);
|
---|
136 | node.put("name", "xy");
|
---|
137 | Way way = new Way();
|
---|
138 | way.addNode(node);
|
---|
139 | way.put("name", "xy");
|
---|
140 | Relation relation = new Relation();
|
---|
141 | relation.put("name", "xy");
|
---|
142 | DataSet ds = new DataSet(node, way, relation);
|
---|
143 |
|
---|
144 | assertTrue(new ChangeCommand(ds, node, node).getDescriptionText().matches("Change node.*xy.*"));
|
---|
145 | assertTrue(new ChangeCommand(ds, way, way).getDescriptionText().matches("Change way.*xy.*"));
|
---|
146 | assertTrue(new ChangeCommand(ds, relation, relation).getDescriptionText().matches("Change relation.*xy.*"));
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Unit test of methods {@link ChangeCommand#equals} and {@link ChangeCommand#hashCode}.
|
---|
151 | */
|
---|
152 | @Test
|
---|
153 | void testEqualsContract() {
|
---|
154 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
155 | EqualsVerifier.forClass(ChangeCommand.class).usingGetClass()
|
---|
156 | .withPrefabValues(DataSet.class,
|
---|
157 | new DataSet(), new DataSet())
|
---|
158 | .withPrefabValues(User.class,
|
---|
159 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
160 | .withPrefabValues(OsmPrimitive.class,
|
---|
161 | new Node(1), new Node(2))
|
---|
162 | .withPrefabValues(OsmDataLayer.class,
|
---|
163 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
164 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
165 | .verify();
|
---|
166 | }
|
---|
167 | }
|
---|