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