1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 |
|
---|
8 | import java.io.ByteArrayInputStream;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStream;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
16 | import org.junit.jupiter.api.Test;
|
---|
17 | import org.openstreetmap.josm.TestUtils;
|
---|
18 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
19 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
20 | import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
|
---|
21 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
22 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
23 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
24 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
---|
25 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
26 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
27 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
28 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
29 |
|
---|
30 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Unit tests for Session reading.
|
---|
34 | */
|
---|
35 | class SessionReaderTest {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Setup tests.
|
---|
39 | */
|
---|
40 | @RegisterExtension
|
---|
41 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
42 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
43 |
|
---|
44 | private static String getSessionDataDir() {
|
---|
45 | return TestUtils.getTestDataRoot() + "/sessions";
|
---|
46 | }
|
---|
47 |
|
---|
48 | private List<Layer> testRead(String sessionFileName) throws IOException, IllegalDataException {
|
---|
49 | boolean zip = sessionFileName.endsWith(".joz");
|
---|
50 | File file = new File(getSessionDataDir(), sessionFileName);
|
---|
51 | SessionReader reader = new SessionReader();
|
---|
52 | reader.loadSession(file, zip, null);
|
---|
53 | return reader.getLayers();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Tests to read an empty .jos or .joz file.
|
---|
58 | * @throws IOException if any I/O error occurs
|
---|
59 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
60 | */
|
---|
61 | @Test
|
---|
62 | void testReadEmpty() throws IOException, IllegalDataException {
|
---|
63 | assertTrue(testRead("empty.jos").isEmpty());
|
---|
64 | assertTrue(testRead("empty.joz").isEmpty());
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Tests to read a .jos or .joz file containing OSM data.
|
---|
69 | * @throws IOException if any I/O error occurs
|
---|
70 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
71 | */
|
---|
72 | @Test
|
---|
73 | void testReadOsm() throws IOException, IllegalDataException {
|
---|
74 | for (String file : new String[]{"osm.jos", "osm.joz"}) {
|
---|
75 | List<Layer> layers = testRead(file);
|
---|
76 | assertEquals(layers.size(), 1);
|
---|
77 | assertTrue(layers.get(0) instanceof OsmDataLayer);
|
---|
78 | OsmDataLayer osm = (OsmDataLayer) layers.get(0);
|
---|
79 | assertEquals(osm.getName(), "OSM layer name");
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Tests to read a .jos or .joz file containing GPX data.
|
---|
85 | * @throws IOException if any I/O error occurs
|
---|
86 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
87 | */
|
---|
88 | @Test
|
---|
89 | void testReadGpx() throws IOException, IllegalDataException {
|
---|
90 | for (String file : new String[]{"gpx.jos", "gpx.joz", "nmea.jos"}) {
|
---|
91 | List<Layer> layers = testRead(file);
|
---|
92 | assertEquals(layers.size(), 1);
|
---|
93 | assertTrue(layers.get(0) instanceof GpxLayer);
|
---|
94 | GpxLayer gpx = (GpxLayer) layers.get(0);
|
---|
95 | assertEquals(gpx.getName(), "GPX layer name");
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Tests to read a .joz file containing GPX and marker data.
|
---|
101 | * @throws IOException if any I/O error occurs
|
---|
102 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
103 | */
|
---|
104 | @Test
|
---|
105 | void testReadGpxAndMarker() throws IOException, IllegalDataException {
|
---|
106 | List<Layer> layers = testRead("gpx_markers.joz");
|
---|
107 | assertEquals(layers.size(), 2);
|
---|
108 | GpxLayer gpx = null;
|
---|
109 | MarkerLayer marker = null;
|
---|
110 | for (Layer layer : layers) {
|
---|
111 | if (layer instanceof GpxLayer) {
|
---|
112 | gpx = (GpxLayer) layer;
|
---|
113 | } else if (layer instanceof MarkerLayer) {
|
---|
114 | marker = (MarkerLayer) layer;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | assertNotNull(gpx);
|
---|
118 | assertNotNull(marker);
|
---|
119 | assertEquals(gpx.getName(), "GPX layer name");
|
---|
120 | assertEquals(marker.getName(), "Marker layer name");
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Tests to read a .jos file containing Bing imagery.
|
---|
125 | * @throws IOException if any I/O error occurs
|
---|
126 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
127 | */
|
---|
128 | @Test
|
---|
129 | void testReadImage() throws IOException, IllegalDataException {
|
---|
130 | final List<Layer> layers = testRead("bing.jos");
|
---|
131 | assertEquals(layers.size(), 1);
|
---|
132 | assertTrue(layers.get(0) instanceof ImageryLayer);
|
---|
133 | final AbstractTileSourceLayer<?> image = (AbstractTileSourceLayer<?>) layers.get(0);
|
---|
134 | assertEquals("Bing aerial imagery", image.getName());
|
---|
135 | EastNorth displacement = image.getDisplaySettings().getDisplacement();
|
---|
136 | assertEquals(-2.671667778864503, displacement.east(), 1e-9);
|
---|
137 | assertEquals(13.89643478114158, displacement.north(), 1e-9);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Tests to read a .joz file containing notes.
|
---|
142 | * @throws IOException if any I/O error occurs
|
---|
143 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
144 | */
|
---|
145 | @Test
|
---|
146 | void testReadNotes() throws IOException, IllegalDataException {
|
---|
147 | if (MainApplication.isDisplayingMapView()) {
|
---|
148 | for (NoteLayer nl : MainApplication.getLayerManager().getLayersOfType(NoteLayer.class)) {
|
---|
149 | MainApplication.getLayerManager().removeLayer(nl);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | final List<Layer> layers = testRead("notes.joz");
|
---|
153 | assertEquals(layers.size(), 1);
|
---|
154 | assertTrue(layers.get(0) instanceof NoteLayer);
|
---|
155 | final NoteLayer layer = (NoteLayer) layers.get(0);
|
---|
156 | assertEquals("Notes", layer.getName());
|
---|
157 | assertEquals(174, layer.getNoteData().getNotes().size());
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17701">Bug #17701</a>.
|
---|
162 | * @throws Exception if an error occurs
|
---|
163 | */
|
---|
164 | @Test
|
---|
165 | void testTicket17701() throws Exception {
|
---|
166 | try (InputStream in = new ByteArrayInputStream(("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
---|
167 | "<josm-session version=\"0.1\">\n" +
|
---|
168 | " <layers active=\"1\">\n" +
|
---|
169 | " <layer index=\"1\" name=\"GPS-треки OpenStreetMap\" type=\"imagery\" version=\"0.1\" visible=\"true\">\n" +
|
---|
170 | " <id>osm-gps</id>\n" +
|
---|
171 | " <type>tms</type>\n" +
|
---|
172 | " <url>https://{switch:a,b,c}.gps-tile.openstreetmap.org/lines/{zoom}/{x}/{y}.png</url>\n" +
|
---|
173 | " <attribution-text>© OpenStreetMap contributors</attribution-text>\n" +
|
---|
174 | " <attribution-url>https://www.openstreetmap.org/copyright</attribution-url>\n" +
|
---|
175 | " <max-zoom>20</max-zoom>\n" +
|
---|
176 | " <cookies/>\n" +
|
---|
177 | " <description>Общедоступные GPS-треки, загруженные на OpenStreetMap.</description>\n" +
|
---|
178 | " <valid-georeference>true</valid-georeference>\n" +
|
---|
179 | " <overlay>true</overlay>\n" +
|
---|
180 | " <show-errors>true</show-errors>\n" +
|
---|
181 | " <automatic-downloading>true</automatic-downloading>\n" +
|
---|
182 | " <automatically-change-resolution>true</automatically-change-resolution>\n" +
|
---|
183 | " </layer>\r\n" +
|
---|
184 | " </layers>\n" +
|
---|
185 | "</josm-session>").getBytes(StandardCharsets.UTF_8))) {
|
---|
186 | SessionReader reader = new SessionReader();
|
---|
187 | reader.loadSession(in, null, false, null);
|
---|
188 | assertTrue(reader.getLayers().isEmpty());
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|