1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertSame;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 |
|
---|
11 | import org.junit.Before;
|
---|
12 | import org.junit.Rule;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
21 | import org.openstreetmap.josm.data.osm.User;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
24 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
25 |
|
---|
26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
27 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
28 | import nl.jqno.equalsverifier.Warning;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Unit tests of {@link Command} class.
|
---|
32 | */
|
---|
33 | public class CommandTest {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * We need prefs for nodes / data sets.
|
---|
37 | */
|
---|
38 | @Rule
|
---|
39 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
40 | public JOSMTestRules test = new JOSMTestRules().preferences().i18n();
|
---|
41 | private CommandTestData testData;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Set up the test data.
|
---|
45 | */
|
---|
46 | @Before
|
---|
47 | public void createTestData() {
|
---|
48 | testData = new CommandTestData();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Test {@link Command#invalidBecauselayerRemoved(org.openstreetmap.josm.gui.layer.Layer)}
|
---|
53 | */
|
---|
54 | @Test
|
---|
55 | public void testInvalidBecauselayerRemoved() {
|
---|
56 | OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "test", null);
|
---|
57 |
|
---|
58 | Command command = new NopCommand();
|
---|
59 | assertFalse(command.invalidBecauselayerRemoved(layer2));
|
---|
60 | assertTrue(command.invalidBecauselayerRemoved(testData.layer));
|
---|
61 |
|
---|
62 | Command command2 = new NopCommand(layer2);
|
---|
63 | assertTrue(command2.invalidBecauselayerRemoved(layer2));
|
---|
64 | assertFalse(command2.invalidBecauselayerRemoved(testData.layer));
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Test {@link Command#getLayer()}
|
---|
69 | */
|
---|
70 | @Test
|
---|
71 | public void testGetLayer() {
|
---|
72 | OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "test", null);
|
---|
73 | Command command = new NopCommand();
|
---|
74 | Command command2 = new NopCommand(layer2);
|
---|
75 | assertSame(testData.layer, command.getLayer());
|
---|
76 | assertSame(layer2, command2.getLayer());
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Unit test of methods {@link Command#equals} and {@link Command#hashCode}.
|
---|
81 | */
|
---|
82 | @Test
|
---|
83 | public void testEqualsContract() {
|
---|
84 | EqualsVerifier.forClass(Command.class).usingGetClass()
|
---|
85 | .withPrefabValues(DataSet.class,
|
---|
86 | new DataSet(), new DataSet())
|
---|
87 | .withPrefabValues(User.class,
|
---|
88 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
89 | .withPrefabValues(OsmDataLayer.class,
|
---|
90 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
91 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
92 | .verify();
|
---|
93 | }
|
---|
94 |
|
---|
95 | private static final class NopCommand extends Command {
|
---|
96 | NopCommand() {
|
---|
97 | super();
|
---|
98 | }
|
---|
99 |
|
---|
100 | NopCommand(OsmDataLayer layer) {
|
---|
101 | super(layer);
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public String getDescriptionText() {
|
---|
106 | return "";
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
111 | Collection<OsmPrimitive> added) {
|
---|
112 | // nop
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * A change test data consisting of two nodes and a way.
|
---|
118 | * @author Michael Zangl
|
---|
119 | */
|
---|
120 | public static class CommandTestData {
|
---|
121 | /**
|
---|
122 | * A test layer
|
---|
123 | */
|
---|
124 | public final OsmDataLayer layer;
|
---|
125 | /**
|
---|
126 | * A test node
|
---|
127 | */
|
---|
128 | public final Node existingNode;
|
---|
129 | /**
|
---|
130 | * A second test node
|
---|
131 | */
|
---|
132 | public final Node existingNode2;
|
---|
133 | /**
|
---|
134 | * A test way
|
---|
135 | */
|
---|
136 | public final Way existingWay;
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Creates the new test data and adds {@link #layer} to the layer manager.
|
---|
140 | */
|
---|
141 | public CommandTestData() {
|
---|
142 | layer = new OsmDataLayer(new DataSet(), "layer", null);
|
---|
143 | Main.getLayerManager().addLayer(layer);
|
---|
144 |
|
---|
145 | existingNode = createNode(5);
|
---|
146 | existingNode2 = createNode(6);
|
---|
147 |
|
---|
148 | existingWay = createWay(10, existingNode, existingNode2);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Create and add a new test node.
|
---|
153 | * @param id the id
|
---|
154 | * @return The node.
|
---|
155 | */
|
---|
156 | public Node createNode(long id) {
|
---|
157 | Node node = new Node();
|
---|
158 | node.setOsmId(id, 1);
|
---|
159 | node.setCoor(LatLon.ZERO);
|
---|
160 | node.put("existing", "existing");
|
---|
161 | layer.data.addPrimitive(node);
|
---|
162 | return node;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Create and add a new test way.
|
---|
167 | * @param id the id
|
---|
168 | * @param nodes The nodes
|
---|
169 | * @return The way.
|
---|
170 | */
|
---|
171 | public Way createWay(int id, Node...nodes) {
|
---|
172 | Way way = new Way();
|
---|
173 | way.setOsmId(id, 1);
|
---|
174 | way.setNodes(Arrays.asList(nodes));
|
---|
175 | way.put("existing", "existing");
|
---|
176 | layer.data.addPrimitive(way);
|
---|
177 | return way;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Create and add a new test relation.
|
---|
182 | * @param id the id
|
---|
183 | * @param members The members
|
---|
184 | * @return The relation.
|
---|
185 | */
|
---|
186 | public Relation createRelation(int id, RelationMember...members) {
|
---|
187 | Relation relation = new Relation(id, 1);
|
---|
188 | for (RelationMember member : members) {
|
---|
189 | relation.addMember(member);
|
---|
190 | }
|
---|
191 | relation.put("existing", "existing");
|
---|
192 | layer.data.addPrimitive(relation);
|
---|
193 | return relation;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * A change test data consisting of two nodes, a way and a relation.
|
---|
199 | * @author Michael Zangl
|
---|
200 | */
|
---|
201 | public static class CommandTestDataWithRelation extends CommandTestData {
|
---|
202 | /**
|
---|
203 | * A test relation
|
---|
204 | */
|
---|
205 | public final Relation existingRelation;
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Creates the new test data and adds {@link #layer} to the layer manager.
|
---|
209 | */
|
---|
210 | public CommandTestDataWithRelation() {
|
---|
211 | existingRelation = createRelation(20, new RelationMember("node", existingNode), new RelationMember("way", existingWay));
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|