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