source: josm/trunk/test/unit/org/openstreetmap/josm/command/ChangeRelationMemberRoleCommandTest.java@ 10758

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

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

File size: 6.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.junit.Assert.assertArrayEquals;
5import static org.junit.Assert.assertEquals;
6import static org.junit.Assert.assertFalse;
7import static org.junit.Assert.assertNull;
8import static org.junit.Assert.assertTrue;
9
10import java.util.ArrayList;
11
12import org.junit.Before;
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.command.CommandTest.CommandTestDataWithRelation;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.Relation;
19import org.openstreetmap.josm.data.osm.User;
20import org.openstreetmap.josm.gui.layer.OsmDataLayer;
21import org.openstreetmap.josm.testutils.JOSMTestRules;
22
23import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24import nl.jqno.equalsverifier.EqualsVerifier;
25import nl.jqno.equalsverifier.Warning;
26
27/**
28 * Unit tests of {@link ChangeRelationMemberRoleCommand} class.
29 */
30public class ChangeRelationMemberRoleCommandTest {
31
32 /**
33 * We need prefs for nodes.
34 */
35 @Rule
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules test = new JOSMTestRules().preferences().i18n();
38 private CommandTestDataWithRelation testData;
39
40 /**
41 * Set up the test data.
42 */
43 @Before
44 public void createTestData() {
45 testData = new CommandTestDataWithRelation();
46 }
47
48 /**
49 * Test if {@link ChangeRelationMemberRoleCommand} changes the role by index
50 */
51 @Test
52 public void testRoleChanged() {
53 assertTrue(new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole").executeCommand());
54 assertEquals("newRole", testData.existingRelation.getMember(0).getRole());
55 assertEquals(testData.existingNode, testData.existingRelation.getMember(0).getMember());
56 assertEquals("way", testData.existingRelation.getMember(1).getRole());
57
58 assertTrue(testData.existingRelation.isModified());
59
60 assertTrue(new ChangeRelationMemberRoleCommand(testData.existingRelation, 1, "newRole").executeCommand());
61 assertEquals("newRole", testData.existingRelation.getMember(1).getRole());
62 }
63
64 /**
65 * Wrong index should be ignored.
66 */
67 @Test
68 public void testWrongIndex() {
69 // should be ignored
70 ChangeRelationMemberRoleCommand command1 = new ChangeRelationMemberRoleCommand(testData.existingRelation, -1, "newRole");
71 assertTrue(command1.executeCommand());
72 ChangeRelationMemberRoleCommand command2 = new ChangeRelationMemberRoleCommand(testData.existingRelation, 8, "newRole");
73 assertTrue(command2.executeCommand());
74 assertFalse(testData.existingRelation.isModified());
75
76 command1.undoCommand();
77 command2.undoCommand();
78 assertFalse(testData.existingRelation.isModified());
79 }
80
81
82 /**
83 * Same role should be ignored.
84 */
85 @Test
86 public void testSameRole() {
87 // should be ignored
88 assertTrue(new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "node").executeCommand());
89 assertFalse(testData.existingRelation.isModified());
90 }
91
92 /**
93 * Test {@link ChangeRelationMemberRoleCommand#undoCommand()}.
94 */
95 @Test
96 public void testUndo() {
97 ChangeRelationMemberRoleCommand command = new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole");
98 command.executeCommand();
99 assertEquals("newRole", testData.existingRelation.getMember(0).getRole());
100 assertTrue(testData.existingRelation.isModified());
101
102 command.undoCommand();
103 assertEquals("node", testData.existingRelation.getMember(0).getRole());
104 assertFalse(testData.existingRelation.isModified());
105
106 command.executeCommand();
107 assertEquals("newRole", testData.existingRelation.getMember(0).getRole());
108 assertTrue(testData.existingRelation.isModified());
109 }
110
111 /**
112 * Tests {@link ChangeCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
113 */
114 @Test
115 public void testFillModifiedData() {
116 ArrayList<OsmPrimitive> modified = new ArrayList<>();
117 ArrayList<OsmPrimitive> deleted = new ArrayList<>();
118 ArrayList<OsmPrimitive> added = new ArrayList<>();
119 new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole").fillModifiedData(modified, deleted, added);
120 assertArrayEquals(new Object[] {testData.existingRelation}, modified.toArray());
121 assertArrayEquals(new Object[] {}, deleted.toArray());
122 assertArrayEquals(new Object[] {}, added.toArray());
123 }
124
125 /**
126 * Test {@link ChangeRelationMemberRoleCommand#getDescriptionText()}
127 */
128 @Test
129 public void testDescription() {
130 testData.existingRelation.put("name", "xy");
131 assertTrue(new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole").getDescriptionText()
132 .matches("Change relation member role for relation.*xy.*"));
133 }
134
135 /**
136 * Test {@link ChangeRelationMemberRoleCommand#getChildren()}
137 */
138 @Test
139 public void testChildren() {
140 assertNull(new ChangeRelationMemberRoleCommand(testData.existingRelation, 0, "newRole").getChildren());
141 }
142
143 /**
144 * Unit test of methods {@link ChangeRelationMemberRoleCommand#equals} and {@link ChangeRelationMemberRoleCommand#hashCode}.
145 */
146 @Test
147 public void testEqualsContract() {
148 EqualsVerifier.forClass(ChangeRelationMemberRoleCommand.class).usingGetClass()
149 .withPrefabValues(Relation.class,
150 new Relation(1), new Relation(2))
151 .withPrefabValues(DataSet.class,
152 new DataSet(), new DataSet())
153 .withPrefabValues(User.class,
154 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
155 .withPrefabValues(OsmDataLayer.class,
156 new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
157 .suppress(Warning.NONFINAL_FIELDS)
158 .verify();
159 }
160}
Note: See TracBrowser for help on using the repository browser.