1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertArrayEquals;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.util.ArrayList;
|
---|
8 |
|
---|
9 | import org.junit.Rule;
|
---|
10 | import org.junit.Test;
|
---|
11 | import org.openstreetmap.josm.TestUtils;
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
17 | import org.openstreetmap.josm.data.osm.User;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 |
|
---|
22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
23 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
24 | import nl.jqno.equalsverifier.Warning;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Unit tests of {@link AddCommand} class.
|
---|
28 | */
|
---|
29 | public class AddCommandTest {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * We need prefs for nodes.
|
---|
33 | */
|
---|
34 | @Rule
|
---|
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 | public 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 | public 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 | public 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 | public 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 | public 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 | public 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 | public 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 | }
|
---|