source: josm/trunk/test/unit/org/openstreetmap/josm/command/ChangeCommandTest.java@ 18690

Last change on this file since 18690 was 18690, checked in by taylor.smock, 13 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

File size: 5.9 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.assertNull;
7import static org.junit.jupiter.api.Assertions.assertThrows;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9
10import java.util.ArrayList;
11import java.util.Collections;
12import java.util.List;
13
14import org.openstreetmap.josm.TestUtils;
15import org.openstreetmap.josm.command.CommandTest.CommandTestData;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.DataIntegrityProblemException;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.Relation;
22import org.openstreetmap.josm.data.osm.User;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
25import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
26import org.openstreetmap.josm.testutils.annotations.I18n;
27
28import nl.jqno.equalsverifier.EqualsVerifier;
29import nl.jqno.equalsverifier.Warning;
30import org.junit.jupiter.api.BeforeEach;
31import org.junit.jupiter.api.Test;
32
33/**
34 * Unit tests of {@link ChangeCommand} class.
35 */
36@I18n
37// We need prefs for nodes.
38@BasicPreferences
39class ChangeCommandTest {
40 private CommandTestData testData;
41
42 /**
43 * Set up the test data.
44 */
45 @BeforeEach
46 public void createTestData() {
47 testData = new CommandTestData();
48 }
49
50 /**
51 * Test that empty ways are prevented.
52 */
53 @Test
54 void testPreventEmptyWays() {
55 Way emptyWay = new Way();
56 assertThrows(IllegalArgumentException.class, () -> new ChangeCommand(testData.existingWay, emptyWay));
57 }
58
59 /**
60 * Test {@link ChangeCommand#executeCommand()}
61 */
62 @Test
63 void testChange() {
64 Node newNode = new Node(5);
65 newNode.setCoor(LatLon.NORTH_POLE);
66 newNode.put("new", "new");
67
68 new ChangeCommand(testData.existingNode, newNode).executeCommand();
69
70 assertEquals("new", testData.existingNode.get("new"));
71 assertNull(testData.existingNode.get("existing"));
72 assertEquals(LatLon.NORTH_POLE, testData.existingNode.getCoor());
73
74 Way newWay = new Way(10);
75 List<Node> newNodes = testData.existingWay.getNodes();
76 Collections.reverse(newNodes);
77 newWay.setNodes(newNodes);
78
79 new ChangeCommand(testData.existingWay, newWay).executeCommand();
80 assertArrayEquals(newNodes.toArray(), testData.existingWay.getNodes().toArray());
81 }
82
83 /**
84 * Test {@link ChangeCommand#executeCommand()} fails if ID is changed
85 */
86 @Test
87 void testChangeIdChange() {
88 Node newNode = new Node(1);
89 newNode.setCoor(LatLon.NORTH_POLE);
90
91 final ChangeCommand changeCommand = new ChangeCommand(testData.existingNode, newNode);
92 assertThrows(DataIntegrityProblemException.class, changeCommand::executeCommand);
93 }
94
95 /**
96 * Test {@link ChangeCommand#undoCommand()}
97 */
98 @Test
99 void testUndo() {
100 Node newNode = new Node(5);
101 newNode.setCoor(LatLon.NORTH_POLE);
102 newNode.put("new", "new");
103
104 ChangeCommand command = new ChangeCommand(testData.existingNode, newNode);
105 command.executeCommand();
106
107 assertEquals("new", testData.existingNode.get("new"));
108 assertEquals(LatLon.NORTH_POLE, testData.existingNode.getCoor());
109
110 command.undoCommand();
111 assertNull(testData.existingNode.get("new"));
112 assertEquals("existing", testData.existingNode.get("existing"));
113 assertEquals(LatLon.ZERO, testData.existingNode.getCoor());
114 }
115
116 /**
117 * Tests {@link ChangeCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
118 */
119 @Test
120 void testFillModifiedData() {
121 ArrayList<OsmPrimitive> modified = new ArrayList<>();
122 ArrayList<OsmPrimitive> deleted = new ArrayList<>();
123 ArrayList<OsmPrimitive> added = new ArrayList<>();
124 new ChangeCommand(testData.existingNode, testData.existingNode).fillModifiedData(modified, deleted, added);
125 assertArrayEquals(new Object[] {testData.existingNode}, modified.toArray());
126 assertArrayEquals(new Object[] {}, deleted.toArray());
127 assertArrayEquals(new Object[] {}, added.toArray());
128 }
129
130 /**
131 * Test {@link ChangeCommand#getDescriptionText()}
132 */
133 @Test
134 void testDescription() {
135 Node node = new Node(LatLon.ZERO);
136 node.put("name", "xy");
137 Way way = new Way();
138 way.addNode(node);
139 way.put("name", "xy");
140 Relation relation = new Relation();
141 relation.put("name", "xy");
142 DataSet ds = new DataSet(node, way, relation);
143
144 assertTrue(new ChangeCommand(ds, node, node).getDescriptionText().matches("Change node.*xy.*"));
145 assertTrue(new ChangeCommand(ds, way, way).getDescriptionText().matches("Change way.*xy.*"));
146 assertTrue(new ChangeCommand(ds, relation, relation).getDescriptionText().matches("Change relation.*xy.*"));
147 }
148
149 /**
150 * Unit test of methods {@link ChangeCommand#equals} and {@link ChangeCommand#hashCode}.
151 */
152 @Test
153 void testEqualsContract() {
154 TestUtils.assumeWorkingEqualsVerifier();
155 EqualsVerifier.forClass(ChangeCommand.class).usingGetClass()
156 .withPrefabValues(DataSet.class,
157 new DataSet(), new DataSet())
158 .withPrefabValues(User.class,
159 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
160 .withPrefabValues(OsmPrimitive.class,
161 new Node(1), new Node(2))
162 .withPrefabValues(OsmDataLayer.class,
163 new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
164 .suppress(Warning.NONFINAL_FIELDS)
165 .verify();
166 }
167}
Note: See TracBrowser for help on using the repository browser.