source: josm/trunk/test/unit/org/openstreetmap/josm/command/CommandTest.java@ 12718

Last change on this file since 12718 was 12718, checked in by Don-vip, 7 years ago

see #13036 - see #15229 - see #15182 - make Commands depends only on a DataSet, not a Layer. This removes a lot of GUI dependencies

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