source: josm/trunk/test/unit/org/openstreetmap/josm/command/ChangeNodesCommandTest.java@ 11609

Last change on this file since 11609 was 10758, checked in by Don-vip, 8 years ago

sonar - squid:S3578 - Test methods should comply with a naming convention

File size: 4.7 KB
RevLine 
[9666]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
[10663]4import static org.junit.Assert.assertArrayEquals;
5import static org.junit.Assert.assertEquals;
6import static org.junit.Assert.assertFalse;
7import static org.junit.Assert.assertTrue;
8
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collections;
12import java.util.List;
13
14import org.junit.Before;
15import org.junit.Rule;
[9666]16import org.junit.Test;
[10663]17import org.openstreetmap.josm.command.CommandTest.CommandTestData;
18import org.openstreetmap.josm.data.coor.LatLon;
[9666]19import org.openstreetmap.josm.data.osm.DataSet;
[10663]20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
[9943]22import org.openstreetmap.josm.data.osm.User;
[9666]23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[10663]25import org.openstreetmap.josm.testutils.JOSMTestRules;
[9666]26
[10663]27import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
[9666]28import nl.jqno.equalsverifier.EqualsVerifier;
29import nl.jqno.equalsverifier.Warning;
30
31/**
32 * Unit tests of {@link ChangeNodesCommand} class.
33 */
34public class ChangeNodesCommandTest {
35
36 /**
[10663]37 * We need prefs for nodes.
[9761]38 */
[10663]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();
[9761]50 }
51
52 /**
[10663]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 /**
[9666]123 * Unit test of methods {@link ChangeNodesCommand#equals} and {@link ChangeNodesCommand#hashCode}.
124 */
125 @Test
[10758]126 public void testEqualsContract() {
[9666]127 EqualsVerifier.forClass(ChangeNodesCommand.class).usingGetClass()
128 .withPrefabValues(Way.class,
129 new Way(1), new Way(2))
[9943]130 .withPrefabValues(DataSet.class,
131 new DataSet(), new DataSet())
132 .withPrefabValues(User.class,
133 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
[9666]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}
Note: See TracBrowser for help on using the repository browser.