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