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