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