source: josm/trunk/test/unit/org/openstreetmap/josm/gui/layer/OsmDataLayerTest.java@ 17531

Last change on this file since 17531 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 11.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7import static org.junit.jupiter.api.Assertions.assertTrue;
8
9import java.io.ByteArrayInputStream;
10import java.io.File;
11import java.nio.charset.StandardCharsets;
12import java.text.DateFormat;
13import java.util.Collection;
14import java.util.Collections;
15import java.util.Iterator;
16
17import org.junit.jupiter.api.BeforeEach;
18import org.junit.jupiter.api.Test;
19import org.junit.jupiter.api.extension.RegisterExtension;
20import org.openstreetmap.josm.TestUtils;
21import org.openstreetmap.josm.actions.ExpertToggleAction;
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.data.coor.LatLon;
24import org.openstreetmap.josm.data.gpx.GpxConstants;
25import org.openstreetmap.josm.data.gpx.GpxData;
26import org.openstreetmap.josm.data.gpx.IGpxTrack;
27import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
28import org.openstreetmap.josm.data.gpx.WayPoint;
29import org.openstreetmap.josm.data.osm.DataSet;
30import org.openstreetmap.josm.data.osm.Node;
31import org.openstreetmap.josm.data.osm.OsmPrimitive;
32import org.openstreetmap.josm.data.osm.Relation;
33import org.openstreetmap.josm.data.osm.Way;
34import org.openstreetmap.josm.gui.MainApplication;
35import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
36import org.openstreetmap.josm.io.IllegalDataException;
37import org.openstreetmap.josm.io.OsmReader;
38import org.openstreetmap.josm.testutils.JOSMTestRules;
39import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
40import org.openstreetmap.josm.tools.Logging;
41import org.openstreetmap.josm.tools.date.DateUtils;
42
43import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
44
45/**
46 * Unit tests of {@link OsmDataLayer} class.
47 */
48class OsmDataLayerTest {
49
50 /**
51 * Setup tests
52 */
53 @RegisterExtension
54 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
55 public JOSMTestRules test = new JOSMTestRules().projection().main();
56
57 private DataSet ds;
58 private OsmDataLayer layer;
59
60 /**
61 * Setup tests
62 */
63 @BeforeEach
64 public void setUp() {
65 ds = new DataSet();
66 layer = new OsmDataLayer(ds, "", null);
67 MainApplication.getLayerManager().addLayer(layer);
68 }
69
70 /**
71 * Unit test of {@link OsmDataLayer#setRecentRelation} and {@link OsmDataLayer#getRecentRelations}.
72 */
73 @Test
74 void testRecentRelation() {
75 int n = OsmDataLayer.PROPERTY_RECENT_RELATIONS_NUMBER.get();
76 assertTrue(n > 0);
77 for (int i = 0; i < 2*n; i++) {
78 Relation r = new Relation(i, 1);
79 ds.addPrimitive(r);
80 layer.setRecentRelation(r);
81 }
82 assertEquals(n, layer.getRecentRelations().size());
83 for (OsmPrimitive r : ds.allPrimitives()) {
84 if (r instanceof Relation) {
85 layer.removeRecentRelation((Relation) r);
86 }
87 }
88 assertTrue(layer.getRecentRelations().isEmpty());
89 }
90
91 /**
92 * Unit test of {@link OsmDataLayer#getInfoComponent}.
93 */
94 @Test
95 void testGetInfoComponent() {
96 assertNotNull(layer.getInfoComponent());
97
98 layer.setUploadDiscouraged(true);
99
100 fillDataSet(ds);
101
102 assertNotNull(layer.getInfoComponent());
103 }
104
105 private void fillDataSet(DataSet ds) {
106 Node n = new Node(1, 2);
107 n.setCoor(LatLon.ZERO);
108 n.setDeleted(true);
109 n.setVisible(false);
110 ds.addPrimitive(n);
111 n = new Node(2, 2);
112 n.setCoor(LatLon.ZERO);
113 ds.addPrimitive(n);
114
115 Way w = new Way(1, 2);
116 w.setDeleted(true);
117 w.setVisible(false);
118 ds.addPrimitive(w);
119 ds.addPrimitive(new Way(2, 2));
120
121 Relation r = new Relation(1, 2);
122 r.setDeleted(true);
123 r.setVisible(false);
124 ds.addPrimitive(r);
125 ds.addPrimitive(new Relation(2, 2));
126 }
127
128 /**
129 * Unit test of {@link OsmDataLayer#addLayerStateChangeListener}.
130 */
131 @Test
132 void testLayerStateChangeListenerNull() {
133 layer.addLayerStateChangeListener(null);
134 }
135
136 /**
137 * Unit test of {@link OsmDataLayer#getIcon}.
138 */
139 @Test
140 void testGetIcon() {
141 assertNotNull(layer.getIcon());
142 layer.setUploadDiscouraged(true);
143 assertNotNull(layer.getIcon());
144 }
145
146 /**
147 * Unit test of {@link OsmDataLayer#paint}.
148 */
149 @Test
150 void testPaint() {
151 fillDataSet(ds);
152 assertNotNull(MainApplication.getMap());
153 layer.paint(TestUtils.newGraphics(), MainApplication.getMap().mapView, new Bounds(LatLon.ZERO));
154 }
155
156 /**
157 * Unit test of {@link OsmDataLayer#getToolTipText}.
158 */
159 @Test
160 void testGetToolTipText() {
161 assertEquals("<html>0 nodes<br>0 ways<br>0 relations</html>", new OsmDataLayer(ds, "", null).getToolTipText());
162 fillDataSet(ds);
163 assertEquals("<html>1 node<br>1 way<br>1 relation</html>", new OsmDataLayer(ds, "", null).getToolTipText());
164 assertEquals("<html>1 node<br>1 way<br>1 relation<br>data.osm</html>", new OsmDataLayer(ds, "", new File("data.osm")).getToolTipText());
165 }
166
167 /**
168 * Unit test of {@link OsmDataLayer#mergeFrom}.
169 */
170 @Test
171 void testMergeFrom() {
172 fillDataSet(ds);
173 OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), "", null);
174 MainApplication.getLayerManager().addLayer(layer2);
175 assertTrue(layer2.data.allPrimitives().isEmpty());
176 assertTrue(layer2.isMergable(layer));
177 layer2.mergeFrom(layer);
178 assertEquals(6, layer2.data.allPrimitives().size());
179 layer.setUploadDiscouraged(true);
180 layer2.mergeFrom(layer);
181 assertTrue(layer2.isUploadDiscouraged());
182 }
183
184 /**
185 * Unit test of {@link OsmDataLayer#cleanupAfterUpload}.
186 */
187 @Test
188 void testCleanupAfterUpload() {
189 fillDataSet(ds);
190 assertEquals(6, layer.data.allPrimitives().size());
191 layer.cleanupAfterUpload(ds.allPrimitives());
192 assertEquals(3, layer.data.allPrimitives().size());
193 }
194
195 /**
196 * Unit test of {@link OsmDataLayer#getMenuEntries}.
197 */
198 @Test
199 void testGetMenuEntries() {
200 ExpertToggleAction.getInstance().setExpert(true);
201 assertEquals(17, layer.getMenuEntries().length);
202
203 ExpertToggleAction.getInstance().setExpert(false);
204 assertEquals(14, layer.getMenuEntries().length);
205 }
206
207 /**
208 * Unit test of {@link OsmDataLayer#toGpxData}.
209 * @throws IllegalDataException never
210 */
211 @Test
212 void testToGpxData() throws IllegalDataException {
213 ds.mergeFrom(OsmReader.parseDataSet(new ByteArrayInputStream((
214 "<?xml version='1.0' encoding='UTF-8'?>\n" +
215 "<osm version='0.6' upload='false' generator='JOSM'>\n" +
216 " <node id='-546306' timestamp='2018-08-01T10:00:00Z' lat='47.0' lon='9.0'>\n" +
217 " <tag k='gpx:ele' v='123' />\n" +
218 " <tag k='gpx:time' v='2018-08-01T10:00:00Z' />\n" +
219 " </node>\n" +
220 " <node id='-546307' timestamp='2018-08-01T10:01:00Z' lat='47.1' lon='9.1'>\n" +
221 " <tag k='ele' v='456' />\n" +
222 " <tag k='gpx:time' v='2018-08-01T10:01:00Z' />\n" +
223 " </node>\n" +
224 " <node id='-546308' timestamp='2018-08-01T10:02:00Z' lat='47.05' lon='9.05'>\n" +
225 " <tag k='ele' v='789' />\n" +
226 " </node>\n" +
227 " <way id='-546309'>\n" +
228 " <nd ref='-546306' />\n" +
229 " <nd ref='-546307' />\n" +
230 " <nd ref='-546308' />\n" +
231 " </way>\r\n" +
232 "</osm>").getBytes(StandardCharsets.UTF_8)), null));
233 GpxData gpx = layer.toGpxData();
234 assertNotNull(gpx);
235 // Check metadata
236 assertEquals(new Bounds(47.0, 9.0, 47.1, 9.1), gpx.recalculateBounds());
237 // Check there is no waypoint
238 assertTrue(gpx.getWaypoints().isEmpty());
239 // Check that track is correct
240 assertEquals(1, gpx.getTrackCount());
241 IGpxTrack track = gpx.getTracks().iterator().next();
242 Collection<IGpxTrackSegment> segments = track.getSegments();
243 assertEquals(1, segments.size());
244 Collection<WayPoint> trackpoints = segments.iterator().next().getWayPoints();
245 assertEquals(3, trackpoints.size());
246 Iterator<WayPoint> it = trackpoints.iterator();
247 DateFormat gpxFormat = DateUtils.getGpxFormat();
248 WayPoint p1 = it.next();
249 assertEquals(new LatLon(47.0, 9.0), p1.getCoor());
250 assertEquals("123", p1.get(GpxConstants.PT_ELE));
251 assertEquals("2018-08-01T10:00:00.000Z", gpxFormat.format(p1.get(GpxConstants.PT_TIME)));
252 WayPoint p2 = it.next();
253 assertEquals(new LatLon(47.1, 9.1), p2.getCoor());
254 assertEquals("456", p2.get(GpxConstants.PT_ELE));
255 assertEquals("2018-08-01T10:01:00.000Z", gpxFormat.format(p2.get(GpxConstants.PT_TIME)));
256 WayPoint p3 = it.next();
257 assertEquals(new LatLon(47.05, 9.05), p3.getCoor());
258 assertEquals("789", p3.get(GpxConstants.PT_ELE));
259 assertEquals("2018-08-01T10:02:00.000Z", gpxFormat.format(p3.get(GpxConstants.PT_TIME)));
260 }
261
262 /**
263 * Unit test of {@link OsmDataLayer#containsPoint}.
264 */
265 @Test
266 void testContainsPoint() {
267 fillDataSet(ds);
268 assertTrue(layer.containsPoint(LatLon.ZERO));
269 }
270
271 /**
272 * Unit test of {@link OsmDataLayer#isModified}.
273 */
274 @Test
275 void testIsModified() {
276 assertFalse(layer.isModified());
277 fillDataSet(ds);
278 assertTrue(layer.isModified());
279 }
280
281 /**
282 * Unit test of {@link OsmDataLayer#projectionChanged}.
283 */
284 @Test
285 void testProjectionChanged() {
286 layer.projectionChanged(null, null);
287 }
288
289 /**
290 * Unit test of {@link OsmDataLayer#checkSaveConditions}.
291 */
292 @Test
293 void testCheckSaveConditions() {
294 TestUtils.assumeWorkingJMockit();
295 final ExtendedDialogMocker edMocker = new ExtendedDialogMocker(
296 Collections.singletonMap("The document contains no data.", "Cancel")
297 );
298
299 assertFalse(layer.checkSaveConditions());
300 fillDataSet(ds);
301 assertTrue(layer.checkSaveConditions());
302
303 assertEquals(1, edMocker.getInvocationLog().size());
304 Object[] invocationLogEntry = edMocker.getInvocationLog().get(0);
305 assertEquals(2, (int) invocationLogEntry[0]);
306 assertEquals("Empty document", invocationLogEntry[2]);
307 }
308
309 /**
310 * Checks that unnamed layer number increases
311 */
312 @Test
313 void testLayerNameIncreases() {
314 final OsmDataLayer layer1 = new OsmDataLayer(new DataSet(), OsmDataLayer.createLayerName(147), null);
315 final OsmDataLayer layer2 = new OsmDataLayer(new DataSet(), OsmDataLayer.createNewName(), null);
316 assertEquals("Data Layer 147", layer1.getName());
317 assertEquals("Data Layer 148", layer2.getName());
318 }
319
320 /**
321 * Checks that named layer got no number
322 */
323 @Test
324 void testLayerUnnumberedName() {
325 final OsmDataLayer layer = new OsmDataLayer(new DataSet(), "Data Layer ", null);
326 assertEquals("Data Layer ", layer.getName());
327 }
328
329 /**
330 * Non-regression test for ticket #13985
331 */
332 @Test
333 void testLayerNameDoesFinish() {
334 final OsmDataLayer layer = new OsmDataLayer(new DataSet(), "Data Layer from GeoJSON: foo.geojson", null);
335 assertEquals("Data Layer from GeoJSON: foo.geojson", layer.getName());
336 }
337
338 /**
339 * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/17065">#17065</a>.
340 */
341 @Test
342 void testTicket17065() {
343 ClipboardUtils.clear();
344 Logging.clearLastErrorAndWarnings();
345 new OsmDataLayer(new DataSet(), null, null).destroy();
346 assertTrue(Logging.getLastErrorAndWarnings().stream().noneMatch(s -> s.contains("UnsupportedFlavorException")));
347 }
348}
Note: See TracBrowser for help on using the repository browser.