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.Main;
|
---|
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 | OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "l1", null);
|
---|
44 | Main.getLayerManager().addLayer(layer1);
|
---|
45 | assertArrayEquals(new Object[0], layer1.data.allPrimitives().toArray());
|
---|
46 |
|
---|
47 | Node osm = new Node(LatLon.ZERO);
|
---|
48 | assertTrue(new AddCommand(osm).executeCommand());
|
---|
49 |
|
---|
50 | assertArrayEquals(new Object[] {osm}, layer1.data.allPrimitives().toArray());
|
---|
51 | assertArrayEquals(new Object[] {osm}, layer1.data.allModifiedPrimitives().toArray());
|
---|
52 | assertTrue(osm.isModified());
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Tests if the add command respects the layer.
|
---|
57 | */
|
---|
58 | @Test
|
---|
59 | public void testAddToLayer() {
|
---|
60 | OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "l1", null);
|
---|
61 | OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "l1", null);
|
---|
62 |
|
---|
63 | Main.getLayerManager().addLayer(layer1);
|
---|
64 | Main.getLayerManager().addLayer(layer2);
|
---|
65 |
|
---|
66 | Node osm = new Node(LatLon.ZERO);
|
---|
67 | assertTrue(new AddCommand(layer2, osm).executeCommand());
|
---|
68 |
|
---|
69 | assertArrayEquals(new Object[0], layer1.data.allPrimitives().toArray());
|
---|
70 | assertArrayEquals(new Object[] {osm}, layer2.data.allPrimitives().toArray());
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Test {@link AddCommand#undoCommand()}
|
---|
75 | */
|
---|
76 | @Test
|
---|
77 | public void testUndo() {
|
---|
78 | OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), "l1", null);
|
---|
79 | Main.getLayerManager().addLayer(layer1);
|
---|
80 | Node osm = new Node(LatLon.ZERO);
|
---|
81 | layer1.data.addPrimitive(osm);
|
---|
82 |
|
---|
83 | AddCommand command = new AddCommand(new Node(LatLon.ZERO));
|
---|
84 | command.executeCommand();
|
---|
85 |
|
---|
86 | command.undoCommand();
|
---|
87 | assertArrayEquals(new Object[] {osm}, layer1.data.allPrimitives().toArray());
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Test {@link AddCommand#getParticipatingPrimitives()}
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | public void testParticipatingPrimitives() {
|
---|
95 | Node osm = new Node(LatLon.ZERO);
|
---|
96 |
|
---|
97 | assertArrayEquals(new Object[] {osm}, new AddCommand(osm).getParticipatingPrimitives().toArray());
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Tests {@link AddCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
|
---|
102 | */
|
---|
103 | @Test
|
---|
104 | public void testFillModifiedData() {
|
---|
105 | Node osm = new Node(LatLon.ZERO);
|
---|
106 |
|
---|
107 | ArrayList<OsmPrimitive> modified = new ArrayList<>();
|
---|
108 | ArrayList<OsmPrimitive> deleted = new ArrayList<>();
|
---|
109 | ArrayList<OsmPrimitive> added = new ArrayList<>();
|
---|
110 | new AddCommand(osm).fillModifiedData(modified, deleted, added);
|
---|
111 | assertArrayEquals(new Object[] {}, modified.toArray());
|
---|
112 | assertArrayEquals(new Object[] {}, deleted.toArray());
|
---|
113 | assertArrayEquals(new Object[] {osm}, added.toArray());
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Test {@link AddCommand#getDescriptionText()}
|
---|
118 | */
|
---|
119 | @Test
|
---|
120 | public void testDescription() {
|
---|
121 | Node node = new Node(LatLon.ZERO);
|
---|
122 | node.put("name", "xy");
|
---|
123 | Way way = new Way();
|
---|
124 | way.addNode(node);
|
---|
125 | way.put("name", "xy");
|
---|
126 | Relation relation = new Relation();
|
---|
127 | relation.put("name", "xy");
|
---|
128 |
|
---|
129 | assertTrue(new AddCommand(node).getDescriptionText().matches("Add node.*xy.*"));
|
---|
130 | assertTrue(new AddCommand(way).getDescriptionText().matches("Add way.*xy.*"));
|
---|
131 | assertTrue(new AddCommand(relation).getDescriptionText().matches("Add relation.*xy.*"));
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Unit test of methods {@link AddCommand#equals} and {@link AddCommand#hashCode}.
|
---|
136 | */
|
---|
137 | @Test
|
---|
138 | public void equalsContract() {
|
---|
139 | EqualsVerifier.forClass(AddCommand.class).usingGetClass()
|
---|
140 | .withPrefabValues(OsmPrimitive.class,
|
---|
141 | new Node(1), new Node(2))
|
---|
142 | .withPrefabValues(DataSet.class,
|
---|
143 | new DataSet(), new DataSet())
|
---|
144 | .withPrefabValues(User.class,
|
---|
145 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
146 | .withPrefabValues(OsmDataLayer.class,
|
---|
147 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
148 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
149 | .verify();
|
---|
150 | }
|
---|
151 | }
|
---|