source: josm/trunk/test/unit/org/openstreetmap/josm/command/ChangePropertyKeyCommandTest.java@ 10663

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

fix #13223 - Minor command class fixes (patch by michael2402, modified) - gsoc-core

File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12
13import org.junit.Before;
14import org.junit.Rule;
15import org.junit.Test;
16import org.openstreetmap.josm.command.CommandTest.CommandTestData;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
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 ChangePropertyKeyCommand} class.
30 */
31public class ChangePropertyKeyCommandTest {
32
33 /**
34 * We need prefs for nodes.
35 */
36 @Rule
37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
38 public JOSMTestRules test = new JOSMTestRules().preferences().i18n();
39 private CommandTestData testData;
40
41 /**
42 * Set up the test data.
43 */
44 @Before
45 public void createTestData() {
46 testData = new CommandTestData();
47 }
48
49 /**
50 * Tests that a key is changed.
51 */
52 @Test
53 public void testChangeKeySingle() {
54 assertTrue(new ChangePropertyKeyCommand(testData.existingNode, "existing", "newKey").executeCommand());
55
56 assertNull(testData.existingNode.get("existing"));
57 assertEquals("existing", testData.existingNode.get("newKey"));
58 assertTrue(testData.existingNode.isModified());
59 }
60
61 /**
62 * Tests that a key is changed.
63 */
64 @Test
65 public void testChangeKey() {
66 assertTrue(new ChangePropertyKeyCommand(Arrays.asList(testData.existingNode, testData.existingWay), "existing",
67 "newKey").executeCommand());
68
69 assertNull(testData.existingNode.get("existing"));
70 assertEquals("existing", testData.existingNode.get("newKey"));
71 assertTrue(testData.existingNode.isModified());
72 assertNull(testData.existingWay.get("existing"));
73 assertEquals("existing", testData.existingWay.get("newKey"));
74 assertTrue(testData.existingWay.isModified());
75 }
76
77 /**
78 * Tests that nop operations are ignored.
79 */
80 @Test
81 public void testChangeKeyIgnored() {
82 Node node1 = testData.createNode(15);
83 node1.removeAll();
84 Node node2 = testData.createNode(16);
85 Node node3 = testData.createNode(17);
86
87 assertTrue(new ChangePropertyKeyCommand(Arrays.asList(node1, node2), "nonexisting", "newKey").executeCommand());
88
89 assertFalse(node1.isModified());
90 assertFalse(node2.isModified());
91
92 assertTrue(new ChangePropertyKeyCommand(Arrays.asList(node1, node2), "existing", "newKey").executeCommand());
93
94 assertFalse(node1.isModified());
95 assertTrue(node2.isModified());
96
97 // removes existing
98 assertTrue(new ChangePropertyKeyCommand(Arrays.asList(node1, node3), "newKey", "existing").executeCommand());
99
100 assertFalse(node1.isModified());
101 assertTrue(node3.isModified());
102 assertNull(node3.get("newKey"));
103 assertNull(node3.get("existing"));
104 }
105
106 /**
107 * Test {@link ChangePropertyKeyCommand#getDescriptionText()}
108 */
109 @Test
110 public void testDescription() {
111 Node node1 = testData.createNode(15);
112 node1.put("name", "xy");
113
114 assertTrue(new ChangePropertyKeyCommand(node1, "a", "b").getDescriptionText()
115 .matches("Replace \"a\" by \"b\" for.*xy.*"));
116 assertTrue(new ChangePropertyKeyCommand(Arrays.asList(node1, testData.existingNode), "a", "b")
117 .getDescriptionText().matches("Replace \"a\" by \"b\" for 2 objects"));
118 }
119
120 /**
121 * Test {@link ChangePropertyCommand#getChildren()}
122 */
123 @Test
124 public void testChildren() {
125 Node node1 = testData.createNode(15);
126 Node node2 = testData.createNode(16);
127 node1.put("name", "node1");
128 node2.put("name", "node2");
129
130 ArrayList<Node> nodesToExpect = new ArrayList<>(Arrays.asList(node1, node2));
131
132 assertNull(new ChangePropertyKeyCommand(node1, "a", "b").getChildren());
133 Collection<PseudoCommand> children = new ChangePropertyKeyCommand(Arrays.asList(node1, node2), "a", "b").getChildren();
134 assertEquals(2, children.size());
135 for (PseudoCommand c : children) {
136 assertNull(c.getChildren());
137 Collection<? extends OsmPrimitive> part = c.getParticipatingPrimitives();
138 assertEquals(1, part.size());
139 OsmPrimitive node = part.iterator().next();
140 assertTrue(nodesToExpect.remove(node));
141
142 assertTrue(c.getDescriptionText().contains(node.getName()));
143 }
144 }
145
146 /**
147 * Unit test of methods {@link ChangePropertyKeyCommand#equals} and {@link ChangePropertyKeyCommand#hashCode}.
148 */
149 @Test
150 public void equalsContract() {
151 EqualsVerifier.forClass(ChangePropertyKeyCommand.class).usingGetClass()
152 .withPrefabValues(DataSet.class,
153 new DataSet(), new DataSet())
154 .withPrefabValues(User.class,
155 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
156 .withPrefabValues(OsmDataLayer.class,
157 new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
158 .suppress(Warning.NONFINAL_FIELDS)
159 .verify();
160 }
161}
Note: See TracBrowser for help on using the repository browser.