| 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.fail;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.Color;
|
|---|
| 8 | import java.io.File;
|
|---|
| 9 | import java.io.IOException;
|
|---|
| 10 | import java.nio.charset.StandardCharsets;
|
|---|
| 11 | import java.nio.file.Files;
|
|---|
| 12 | import java.nio.file.Path;
|
|---|
| 13 | import java.nio.file.Paths;
|
|---|
| 14 | import java.time.Instant;
|
|---|
| 15 | import java.util.Arrays;
|
|---|
| 16 | import java.util.Collections;
|
|---|
| 17 | import java.util.HashMap;
|
|---|
| 18 | import java.util.List;
|
|---|
| 19 | import java.util.Map;
|
|---|
| 20 | import java.util.stream.Collectors;
|
|---|
| 21 | import java.util.zip.ZipEntry;
|
|---|
| 22 | import java.util.zip.ZipFile;
|
|---|
| 23 |
|
|---|
| 24 | import org.junit.jupiter.api.BeforeEach;
|
|---|
| 25 | import org.junit.jupiter.api.Test;
|
|---|
| 26 | import org.openstreetmap.josm.TestUtils;
|
|---|
| 27 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 28 | import org.openstreetmap.josm.data.gpx.GpxData;
|
|---|
| 29 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
|---|
| 30 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
|---|
| 31 | import org.openstreetmap.josm.data.imagery.OffsetBookmark;
|
|---|
| 32 | import org.openstreetmap.josm.data.notes.Note;
|
|---|
| 33 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 34 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
|---|
| 35 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 36 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 37 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
|---|
| 38 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 39 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
|---|
| 40 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 41 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
|---|
| 42 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
|---|
| 43 | import org.openstreetmap.josm.testutils.annotations.Main;
|
|---|
| 44 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
|---|
| 45 | import org.openstreetmap.josm.tools.MultiMap;
|
|---|
| 46 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Unit tests for Session writing.
|
|---|
| 50 | */
|
|---|
| 51 | @Main
|
|---|
| 52 | @Projection
|
|---|
| 53 | public class SessionWriterTest {
|
|---|
| 54 |
|
|---|
| 55 | protected static final class OsmHeadlessJosExporter extends OsmDataSessionExporter {
|
|---|
| 56 | public OsmHeadlessJosExporter(OsmDataLayer layer) {
|
|---|
| 57 | super(layer);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | @Override
|
|---|
| 61 | public boolean requiresZip() {
|
|---|
| 62 | return false;
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | protected static final class OsmHeadlessJozExporter extends OsmDataSessionExporter {
|
|---|
| 67 | public OsmHeadlessJozExporter(OsmDataLayer layer) {
|
|---|
| 68 | super(layer);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | @Override
|
|---|
| 72 | public boolean requiresZip() {
|
|---|
| 73 | return true;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | protected static final class GpxHeadlessJosExporter extends GpxTracksSessionExporter {
|
|---|
| 78 | public GpxHeadlessJosExporter(GpxLayer layer) {
|
|---|
| 79 | super(layer);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | @Override
|
|---|
| 83 | public boolean requiresZip() {
|
|---|
| 84 | return false;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | protected static final class GpxHeadlessJozExporter extends GpxTracksSessionExporter {
|
|---|
| 89 | public GpxHeadlessJozExporter(GpxLayer layer) {
|
|---|
| 90 | super(layer);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | @Override
|
|---|
| 94 | public boolean requiresZip() {
|
|---|
| 95 | return true;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Setup tests.
|
|---|
| 101 | */
|
|---|
| 102 | @BeforeEach
|
|---|
| 103 | public void setUp() {
|
|---|
| 104 | MainApplication.getLayerManager().addLayer(createOsmLayer());
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | private Map<String, byte[]> testWrite(List<Layer> layers, final boolean zip) throws IOException {
|
|---|
| 108 | Map<Layer, SessionLayerExporter> exporters = new HashMap<>();
|
|---|
| 109 | if (zip) {
|
|---|
| 110 | SessionWriter.registerSessionLayerExporter(OsmDataLayer.class, OsmHeadlessJozExporter.class);
|
|---|
| 111 | SessionWriter.registerSessionLayerExporter(GpxLayer.class, GpxHeadlessJozExporter.class);
|
|---|
| 112 | } else {
|
|---|
| 113 | SessionWriter.registerSessionLayerExporter(OsmDataLayer.class, OsmHeadlessJosExporter.class);
|
|---|
| 114 | SessionWriter.registerSessionLayerExporter(GpxLayer.class, GpxHeadlessJosExporter.class);
|
|---|
| 115 | }
|
|---|
| 116 | for (final Layer l : layers) {
|
|---|
| 117 | SessionLayerExporter s = SessionWriter.getSessionLayerExporter(l);
|
|---|
| 118 | s.getExportPanel();
|
|---|
| 119 | exporters.put(l, s);
|
|---|
| 120 | if (s instanceof GpxTracksSessionExporter) {
|
|---|
| 121 | ((GpxTracksSessionExporter) s).setMetaTime(Instant.parse("2021-10-16T18:27:12.351Z"));
|
|---|
| 122 | } else if (s instanceof MarkerSessionExporter) {
|
|---|
| 123 | ((MarkerSessionExporter) s).setMetaTime(Instant.parse("2021-10-16T18:27:12.351Z"));
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | SessionWriter sw = new SessionWriter(layers, -1, exporters, new MultiMap<>(), zip);
|
|---|
| 127 | File file = new File(System.getProperty("java.io.tmpdir"), getClass().getName()+(zip ? ".joz" : ".jos"));
|
|---|
| 128 | try {
|
|---|
| 129 | sw.write(file);
|
|---|
| 130 | if (!zip) {
|
|---|
| 131 | return null;
|
|---|
| 132 | }
|
|---|
| 133 | try (ZipFile zipFile = new ZipFile(file)) {
|
|---|
| 134 | return Collections.list(zipFile.entries()).stream().collect(Collectors.toMap(ZipEntry::getName, e -> {
|
|---|
| 135 | try {
|
|---|
| 136 | return Utils.readBytesFromStream(zipFile.getInputStream(e));
|
|---|
| 137 | } catch (IOException ex) {
|
|---|
| 138 | fail(ex);
|
|---|
| 139 | }
|
|---|
| 140 | return null;
|
|---|
| 141 | }));
|
|---|
| 142 | }
|
|---|
| 143 | } finally {
|
|---|
| 144 | if (file.exists()) {
|
|---|
| 145 | Utils.deleteFile(file);
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Creates an OSM layer
|
|---|
| 152 | * @return OSM layer
|
|---|
| 153 | * @since 18466
|
|---|
| 154 | */
|
|---|
| 155 | public static OsmDataLayer createOsmLayer() {
|
|---|
| 156 | OsmDataLayer layer = new OsmDataLayer(new DataSet(), "OSM layer name", null);
|
|---|
| 157 | layer.setAssociatedFile(new File("data.osm"));
|
|---|
| 158 | return layer;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Creates a GPX layer
|
|---|
| 163 | * @return GPX layer
|
|---|
| 164 | * @since 18466
|
|---|
| 165 | */
|
|---|
| 166 | public static GpxLayer createGpxLayer() {
|
|---|
| 167 | GpxData data = new GpxData();
|
|---|
| 168 | WayPoint wp = new WayPoint(new LatLon(42.72665, -0.00747));
|
|---|
| 169 | wp.setInstant(Instant.parse("2021-01-01T10:15:30.00Z"));
|
|---|
| 170 | data.waypoints.add(wp);
|
|---|
| 171 | data.waypoints.add(new WayPoint(new LatLon(42.72659, -0.00749)));
|
|---|
| 172 | GpxLayer layer = new GpxLayer(data, "GPX layer name");
|
|---|
| 173 | layer.setAssociatedFile(new File("data.gpx"));
|
|---|
| 174 | return layer;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Creates a MarkerLayer
|
|---|
| 179 | * @param gpx linked GPX layer
|
|---|
| 180 | * @return MarkerLayer
|
|---|
| 181 | * @since 18466
|
|---|
| 182 | */
|
|---|
| 183 | public static MarkerLayer createMarkerLayer(GpxLayer gpx) {
|
|---|
| 184 | MarkerLayer layer = new MarkerLayer(gpx.data, "Marker layer name", gpx.getAssociatedFile(), gpx);
|
|---|
| 185 | layer.setOpacity(0.5);
|
|---|
| 186 | layer.setColor(new Color(0x12345678, true));
|
|---|
| 187 | gpx.setLinkedMarkerLayer(layer);
|
|---|
| 188 | return layer;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /**
|
|---|
| 192 | * Creates an ImageryLayer
|
|---|
| 193 | * @return ImageryLayer
|
|---|
| 194 | * @since 18466
|
|---|
| 195 | */
|
|---|
| 196 | public static ImageryLayer createImageryLayer() {
|
|---|
| 197 | TMSLayer layer = new TMSLayer(new ImageryInfo("the name", "http://www.url.com/"));
|
|---|
| 198 | layer.getDisplaySettings().setOffsetBookmark(
|
|---|
| 199 | new OffsetBookmark(ProjectionRegistry.getProjection().toCode(), layer.getInfo().getId(), layer.getInfo().getName(), "", 12, 34));
|
|---|
| 200 | return layer;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /**
|
|---|
| 204 | * Creates a NoteLayer
|
|---|
| 205 | * @return NoteLayer
|
|---|
| 206 | * @since 18466
|
|---|
| 207 | */
|
|---|
| 208 | public static NoteLayer createNoteLayer() {
|
|---|
| 209 | return new NoteLayer(Collections.singletonList(new Note(LatLon.ZERO)), "layer name");
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * Tests to write an empty .jos file.
|
|---|
| 214 | * @throws IOException if an I/O error occurs
|
|---|
| 215 | */
|
|---|
| 216 | @Test
|
|---|
| 217 | void testWriteEmptyJos() throws IOException {
|
|---|
| 218 | testWrite(Collections.emptyList(), false);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /**
|
|---|
| 222 | * Tests to write an empty .joz file.
|
|---|
| 223 | * @throws IOException if an I/O error occurs
|
|---|
| 224 | */
|
|---|
| 225 | @Test
|
|---|
| 226 | void testWriteEmptyJoz() throws IOException {
|
|---|
| 227 | testWrite(Collections.emptyList(), true);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /**
|
|---|
| 231 | * Tests to write a .jos file containing OSM data.
|
|---|
| 232 | * @throws IOException if an I/O error occurs
|
|---|
| 233 | */
|
|---|
| 234 | @Test
|
|---|
| 235 | void testWriteOsmJos() throws IOException {
|
|---|
| 236 | testWrite(Collections.singletonList(createOsmLayer()), false);
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * Tests to write a .joz file containing OSM data.
|
|---|
| 241 | * @throws IOException if an I/O error occurs
|
|---|
| 242 | */
|
|---|
| 243 | @Test
|
|---|
| 244 | void testWriteOsmJoz() throws IOException {
|
|---|
| 245 | testWrite(Collections.singletonList(createOsmLayer()), true);
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /**
|
|---|
| 249 | * Tests to write a .jos file containing GPX data.
|
|---|
| 250 | * @throws IOException if an I/O error occurs
|
|---|
| 251 | */
|
|---|
| 252 | @Test
|
|---|
| 253 | void testWriteGpxJos() throws IOException {
|
|---|
| 254 | testWrite(Collections.singletonList(createGpxLayer()), false);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /**
|
|---|
| 258 | * Tests to write a .joz file containing GPX data.
|
|---|
| 259 | * @throws IOException if an I/O error occurs
|
|---|
| 260 | */
|
|---|
| 261 | @Test
|
|---|
| 262 | void testWriteGpxJoz() throws IOException {
|
|---|
| 263 | testWrite(Collections.singletonList(createGpxLayer()), true);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * Tests to write a .joz file containing GPX and marker data.
|
|---|
| 268 | * @throws IOException if an I/O error occurs
|
|---|
| 269 | */
|
|---|
| 270 | @Test
|
|---|
| 271 | void testWriteGpxAndMarkerJoz() throws IOException {
|
|---|
| 272 | GpxLayer gpx = createGpxLayer();
|
|---|
| 273 | MarkerLayer markers = createMarkerLayer(gpx);
|
|---|
| 274 | Map<String, byte[]> bytes = testWrite(Arrays.asList(gpx, markers), true);
|
|---|
| 275 |
|
|---|
| 276 | Path path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/gpx_markers_combined.jos");
|
|---|
| 277 | String expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 278 | String actual = new String(bytes.get("session.jos"), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 279 | assertEquals(expected, actual);
|
|---|
| 280 |
|
|---|
| 281 | path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/data_export.gpx");
|
|---|
| 282 | expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 283 | actual = new String(bytes.get("layers/01/data.gpx"), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 284 | assertEquals(expected, actual);
|
|---|
| 285 |
|
|---|
| 286 | //Test writing when the marker layer has no corresponding GPX layer:
|
|---|
| 287 | gpx.setLinkedMarkerLayer(null);
|
|---|
| 288 | markers.fromLayer = null;
|
|---|
| 289 | markers.data.transferLayerPrefs(gpx.data.getLayerPrefs());
|
|---|
| 290 | bytes = testWrite(Arrays.asList(gpx, markers), true);
|
|---|
| 291 |
|
|---|
| 292 | path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/gpx_markers.jos");
|
|---|
| 293 | expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 294 | actual = new String(bytes.get("session.jos"), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 295 | assertEquals(expected, actual);
|
|---|
| 296 |
|
|---|
| 297 | path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/data_export.gpx");
|
|---|
| 298 | expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 299 | actual = new String(bytes.get("layers/01/data.gpx"), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 300 | assertEquals(expected, actual);
|
|---|
| 301 |
|
|---|
| 302 | path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/markers.gpx");
|
|---|
| 303 | expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 304 | actual = new String(bytes.get("layers/02/data.gpx"), StandardCharsets.UTF_8).replace("\r", "");
|
|---|
| 305 | assertEquals(expected, actual);
|
|---|
| 306 |
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | /**
|
|---|
| 310 | * Tests to write a .joz file containing an imagery layer.
|
|---|
| 311 | * @throws IOException if an I/O error occurs
|
|---|
| 312 | */
|
|---|
| 313 | @Test
|
|---|
| 314 | void testWriteImageryLayer() throws IOException {
|
|---|
| 315 | final Layer layer = createImageryLayer();
|
|---|
| 316 | testWrite(Collections.singletonList(layer), true);
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /**
|
|---|
| 320 | * Tests to write a .joz file containing a note layer.
|
|---|
| 321 | * @throws IOException if an I/O error occurs
|
|---|
| 322 | */
|
|---|
| 323 | @Test
|
|---|
| 324 | void testWriteNoteLayer() throws IOException {
|
|---|
| 325 | final Layer layer = createNoteLayer();
|
|---|
| 326 | testWrite(Collections.singletonList(layer), true);
|
|---|
| 327 | }
|
|---|
| 328 | }
|
|---|