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

Last change on this file since 17360 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

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