1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNotNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 |
|
---|
9 | import java.io.ByteArrayInputStream;
|
---|
10 | import java.io.File;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.text.DateFormat;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.Iterator;
|
---|
16 |
|
---|
17 | import org.junit.Before;
|
---|
18 | import org.junit.Rule;
|
---|
19 | import org.junit.Test;
|
---|
20 | import org.openstreetmap.josm.TestUtils;
|
---|
21 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
---|
22 | import org.openstreetmap.josm.data.Bounds;
|
---|
23 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
24 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
25 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
26 | import org.openstreetmap.josm.data.gpx.IGpxTrack;
|
---|
27 | import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
|
---|
28 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
29 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
30 | import org.openstreetmap.josm.data.osm.Node;
|
---|
31 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
32 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
33 | import org.openstreetmap.josm.data.osm.Way;
|
---|
34 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
35 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
36 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
37 | import org.openstreetmap.josm.io.OsmReader;
|
---|
38 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
39 | import org.openstreetmap.josm.testutils.mockers.ExtendedDialogMocker;
|
---|
40 | import org.openstreetmap.josm.tools.Logging;
|
---|
41 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
42 |
|
---|
43 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Unit tests of {@link OsmDataLayer} class.
|
---|
47 | */
|
---|
48 | public class OsmDataLayerTest {
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Setup tests
|
---|
52 | */
|
---|
53 | @Rule
|
---|
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 | @Before
|
---|
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 | public 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 | public 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 | public void testLayerStateChangeListenerNull() {
|
---|
133 | layer.addLayerStateChangeListener(null);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Unit test of {@link OsmDataLayer#getIcon}.
|
---|
138 | */
|
---|
139 | @Test
|
---|
140 | public 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 | public 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 | public 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 | public 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 | public 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 | public 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 | public 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 | public 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 | public 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 | public void testProjectionChanged() {
|
---|
286 | layer.projectionChanged(null, null);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Unit test of {@link OsmDataLayer#checkSaveConditions}.
|
---|
291 | */
|
---|
292 | @Test
|
---|
293 | public 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 | public 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 | public 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 | public 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 | public 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 | }
|
---|