1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 |
|
---|
6 | import org.junit.jupiter.api.Test;
|
---|
7 | import org.openstreetmap.josm.TestUtils;
|
---|
8 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
9 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
13 | import org.openstreetmap.josm.data.osm.User;
|
---|
14 | import org.openstreetmap.josm.data.osm.Way;
|
---|
15 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
18 | import org.openstreetmap.josm.testutils.annotations.I18n;
|
---|
19 |
|
---|
20 | import nl.jqno.equalsverifier.EqualsVerifier;
|
---|
21 | import nl.jqno.equalsverifier.Warning;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Unit tests of {@link Command} class.
|
---|
25 | */
|
---|
26 | @I18n
|
---|
27 | // We need prefs for nodes / data sets.
|
---|
28 | @BasicPreferences
|
---|
29 | public class CommandTest {
|
---|
30 | /**
|
---|
31 | * Unit test of methods {@link Command#equals} and {@link Command#hashCode}.
|
---|
32 | */
|
---|
33 | @Test
|
---|
34 | void testEqualsContract() {
|
---|
35 | TestUtils.assumeWorkingEqualsVerifier();
|
---|
36 | EqualsVerifier.forClass(Command.class).usingGetClass()
|
---|
37 | .withPrefabValues(DataSet.class,
|
---|
38 | new DataSet(), new DataSet())
|
---|
39 | .withPrefabValues(User.class,
|
---|
40 | User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
|
---|
41 | .withPrefabValues(OsmDataLayer.class,
|
---|
42 | new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
|
---|
43 | .suppress(Warning.NONFINAL_FIELDS)
|
---|
44 | .verify();
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * A change test data consisting of two nodes and a way.
|
---|
49 | * @author Michael Zangl
|
---|
50 | */
|
---|
51 | public static class CommandTestData {
|
---|
52 | /**
|
---|
53 | * A test layer
|
---|
54 | */
|
---|
55 | public final OsmDataLayer layer;
|
---|
56 | /**
|
---|
57 | * A test node
|
---|
58 | */
|
---|
59 | public final Node existingNode;
|
---|
60 | /**
|
---|
61 | * A second test node
|
---|
62 | */
|
---|
63 | public final Node existingNode2;
|
---|
64 | /**
|
---|
65 | * A test way
|
---|
66 | */
|
---|
67 | public final Way existingWay;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Creates the new test data and adds {@link #layer} to the layer manager.
|
---|
71 | */
|
---|
72 | public CommandTestData() {
|
---|
73 | layer = new OsmDataLayer(new DataSet(), "layer", null);
|
---|
74 | MainApplication.getLayerManager().addLayer(layer);
|
---|
75 |
|
---|
76 | existingNode = createNode(5);
|
---|
77 | existingNode2 = createNode(6);
|
---|
78 |
|
---|
79 | existingWay = createWay(10, existingNode, existingNode2);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Create and add a new test node.
|
---|
84 | * @param id the id
|
---|
85 | * @return The node.
|
---|
86 | */
|
---|
87 | public Node createNode(long id) {
|
---|
88 | Node node = new Node();
|
---|
89 | node.setOsmId(id, 1);
|
---|
90 | node.setCoor(LatLon.ZERO);
|
---|
91 | node.put("existing", "existing");
|
---|
92 | node.setReferrersDownloaded(true);
|
---|
93 | layer.data.addPrimitive(node);
|
---|
94 | return node;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Create and add a new test way.
|
---|
99 | * @param id the id
|
---|
100 | * @param nodes The nodes
|
---|
101 | * @return The way.
|
---|
102 | */
|
---|
103 | public Way createWay(int id, Node... nodes) {
|
---|
104 | Way way = new Way();
|
---|
105 | way.setOsmId(id, 1);
|
---|
106 | way.setNodes(Arrays.asList(nodes));
|
---|
107 | way.put("existing", "existing");
|
---|
108 | way.setReferrersDownloaded(true);
|
---|
109 | layer.data.addPrimitive(way);
|
---|
110 | return way;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Create and add a new test relation.
|
---|
115 | * @param id the id
|
---|
116 | * @param members The members
|
---|
117 | * @return The relation.
|
---|
118 | */
|
---|
119 | public Relation createRelation(int id, RelationMember... members) {
|
---|
120 | Relation relation = new Relation(id, 1);
|
---|
121 | for (RelationMember member : members) {
|
---|
122 | relation.addMember(member);
|
---|
123 | }
|
---|
124 | relation.put("existing", "existing");
|
---|
125 | relation.setReferrersDownloaded(true);
|
---|
126 | layer.data.addPrimitive(relation);
|
---|
127 | return relation;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * A change test data consisting of two nodes, a way and a relation.
|
---|
133 | * @author Michael Zangl
|
---|
134 | */
|
---|
135 | public static class CommandTestDataWithRelation extends CommandTestData {
|
---|
136 | /**
|
---|
137 | * A test relation
|
---|
138 | */
|
---|
139 | public final Relation existingRelation;
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Creates the new test data and adds {@link #layer} to the layer manager.
|
---|
143 | */
|
---|
144 | public CommandTestDataWithRelation() {
|
---|
145 | existingRelation = createRelation(20, new RelationMember("node", existingNode), new RelationMember("way", existingWay));
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|