source: josm/trunk/test/unit/org/openstreetmap/josm/command/AddCommandTest.java@ 17275

Last change on this file since 17275 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: 5.0 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.assertTrue;
6
7import java.util.ArrayList;
8
9import org.junit.jupiter.api.extension.RegisterExtension;
10import org.junit.jupiter.api.Test;
11import org.openstreetmap.josm.TestUtils;
12import org.openstreetmap.josm.data.coor.LatLon;
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.User;
18import org.openstreetmap.josm.data.osm.Way;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23import nl.jqno.equalsverifier.EqualsVerifier;
24import nl.jqno.equalsverifier.Warning;
25
26/**
27 * Unit tests of {@link AddCommand} class.
28 */
29class AddCommandTest {
30
31 /**
32 * We need prefs for nodes.
33 */
34 @RegisterExtension
35 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
36 public JOSMTestRules test = new JOSMTestRules().preferences().i18n();
37
38 /**
39 * Test if the add command is executed correctly and sets the modified flag.
40 */
41 @Test
42 void testAdd() {
43 DataSet ds = new DataSet();
44 assertArrayEquals(new Object[0], ds.allPrimitives().toArray());
45
46 Node osm = new Node(LatLon.ZERO);
47 assertTrue(new AddCommand(ds, osm).executeCommand());
48
49 assertArrayEquals(new Object[] {osm}, ds.allPrimitives().toArray());
50 assertArrayEquals(new Object[] {osm}, ds.allModifiedPrimitives().toArray());
51 assertTrue(osm.isModified());
52 }
53
54 /**
55 * Tests if the add command respects the data set.
56 */
57 @Test
58 void testAddToLayer() {
59 DataSet ds1 = new DataSet();
60 DataSet ds2 = new DataSet();
61
62 Node osm = new Node(LatLon.ZERO);
63 assertTrue(new AddCommand(ds2, osm).executeCommand());
64
65 assertArrayEquals(new Object[0], ds1.allPrimitives().toArray());
66 assertArrayEquals(new Object[] {osm}, ds2.allPrimitives().toArray());
67 }
68
69 /**
70 * Test {@link AddCommand#undoCommand()}
71 */
72 @Test
73 void testUndo() {
74 Node osm = new Node(LatLon.ZERO);
75 DataSet ds = new DataSet(osm);
76
77 AddCommand command = new AddCommand(ds, new Node(LatLon.ZERO));
78 command.executeCommand();
79
80 command.undoCommand();
81 assertArrayEquals(new Object[] {osm}, ds.allPrimitives().toArray());
82 }
83
84 /**
85 * Test {@link AddCommand#getParticipatingPrimitives()}
86 */
87 @Test
88 void testParticipatingPrimitives() {
89 Node osm = new Node(LatLon.ZERO);
90
91 assertArrayEquals(new Object[] {osm}, new AddCommand(new DataSet(), osm).getParticipatingPrimitives().toArray());
92 }
93
94 /**
95 * Tests {@link AddCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
96 */
97 @Test
98 void testFillModifiedData() {
99 Node osm = new Node(LatLon.ZERO);
100
101 ArrayList<OsmPrimitive> modified = new ArrayList<>();
102 ArrayList<OsmPrimitive> deleted = new ArrayList<>();
103 ArrayList<OsmPrimitive> added = new ArrayList<>();
104 new AddCommand(new DataSet(), osm).fillModifiedData(modified, deleted, added);
105 assertArrayEquals(new Object[] {}, modified.toArray());
106 assertArrayEquals(new Object[] {}, deleted.toArray());
107 assertArrayEquals(new Object[] {osm}, added.toArray());
108 }
109
110 /**
111 * Test {@link AddCommand#getDescriptionText()}
112 */
113 @Test
114 void testDescription() {
115 Node node = new Node(LatLon.ZERO);
116 node.put("name", "xy");
117 Way way = new Way();
118 way.addNode(node);
119 way.put("name", "xy");
120 Relation relation = new Relation();
121 relation.put("name", "xy");
122
123 DataSet ds = new DataSet();
124 assertTrue(new AddCommand(ds, node).getDescriptionText().matches("Add node.*xy.*"));
125 assertTrue(new AddCommand(ds, way).getDescriptionText().matches("Add way.*xy.*"));
126 assertTrue(new AddCommand(ds, relation).getDescriptionText().matches("Add relation.*xy.*"));
127 }
128
129 /**
130 * Unit test of methods {@link AddCommand#equals} and {@link AddCommand#hashCode}.
131 */
132 @Test
133 void testEqualsContract() {
134 TestUtils.assumeWorkingEqualsVerifier();
135 EqualsVerifier.forClass(AddCommand.class).usingGetClass()
136 .withPrefabValues(OsmPrimitive.class,
137 new Node(1), new Node(2))
138 .withPrefabValues(DataSet.class,
139 new DataSet(), new DataSet())
140 .withPrefabValues(User.class,
141 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
142 .withPrefabValues(OsmDataLayer.class,
143 new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
144 .suppress(Warning.NONFINAL_FIELDS)
145 .verify();
146 }
147}
Note: See TracBrowser for help on using the repository browser.