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

Last change on this file since 18037 was 17763, checked in by simon04, 3 years ago

see #20750 - Fix OsmDataLayerTest

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