[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io.session;
|
---|
| 3 |
|
---|
| 4 | import java.io.File;
|
---|
| 5 | import java.io.IOException;
|
---|
[8803] | 6 | import java.util.Arrays;
|
---|
[7937] | 7 | import java.util.Collections;
|
---|
| 8 | import java.util.HashMap;
|
---|
| 9 | import java.util.List;
|
---|
| 10 | import java.util.Map;
|
---|
| 11 |
|
---|
| 12 | import org.junit.BeforeClass;
|
---|
| 13 | import org.junit.Test;
|
---|
| 14 | import org.openstreetmap.josm.JOSMFixture;
|
---|
[10571] | 15 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[7937] | 16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 17 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
| 18 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
[8803] | 19 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
[9753] | 20 | import org.openstreetmap.josm.data.notes.Note;
|
---|
[7937] | 21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 22 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
[8803] | 23 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
[7937] | 24 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[9753] | 25 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
---|
[7937] | 26 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[8803] | 27 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
[7937] | 28 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
| 29 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
| 30 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
[9296] | 31 | import org.openstreetmap.josm.tools.Utils;
|
---|
[7937] | 32 |
|
---|
| 33 | /**
|
---|
| 34 | * Unit tests for Session writing.
|
---|
| 35 | */
|
---|
| 36 | public class SessionWriterTest {
|
---|
| 37 |
|
---|
[8837] | 38 | protected static final class OsmHeadlessJosExporter extends OsmDataSessionExporter {
|
---|
| 39 | public OsmHeadlessJosExporter(OsmDataLayer layer) {
|
---|
[7937] | 40 | super(layer);
|
---|
| 41 | }
|
---|
[8510] | 42 |
|
---|
[7937] | 43 | @Override
|
---|
| 44 | public boolean requiresZip() {
|
---|
| 45 | return false;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[8837] | 49 | protected static final class OsmHeadlessJozExporter extends OsmDataSessionExporter {
|
---|
| 50 | public OsmHeadlessJozExporter(OsmDataLayer layer) {
|
---|
[7937] | 51 | super(layer);
|
---|
| 52 | }
|
---|
[8510] | 53 |
|
---|
[7937] | 54 | @Override
|
---|
| 55 | public boolean requiresZip() {
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[8837] | 60 | protected static final class GpxHeadlessJosExporter extends GpxTracksSessionExporter {
|
---|
| 61 | public GpxHeadlessJosExporter(GpxLayer layer) {
|
---|
[7937] | 62 | super(layer);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | @Override
|
---|
| 66 | public boolean requiresZip() {
|
---|
| 67 | return false;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[8837] | 71 | protected static final class GpxHeadlessJozExporter extends GpxTracksSessionExporter {
|
---|
| 72 | public GpxHeadlessJozExporter(GpxLayer layer) {
|
---|
[7937] | 73 | super(layer);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Override
|
---|
| 77 | public boolean requiresZip() {
|
---|
| 78 | return true;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Setup tests.
|
---|
| 84 | */
|
---|
| 85 | @BeforeClass
|
---|
| 86 | public static void setUpBeforeClass() {
|
---|
| 87 | JOSMFixture.createUnitTestFixture().init(true);
|
---|
| 88 | ProjectionPreference.setProjection();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[10937] | 91 | private void testWrite(List<Layer> layers, final boolean zip) throws IOException {
|
---|
[7937] | 92 | Map<Layer, SessionLayerExporter> exporters = new HashMap<>();
|
---|
| 93 | if (zip) {
|
---|
| 94 | SessionWriter.registerSessionLayerExporter(OsmDataLayer.class, OsmHeadlessJozExporter.class);
|
---|
| 95 | SessionWriter.registerSessionLayerExporter(GpxLayer.class, GpxHeadlessJozExporter.class);
|
---|
| 96 | } else {
|
---|
| 97 | SessionWriter.registerSessionLayerExporter(OsmDataLayer.class, OsmHeadlessJosExporter.class);
|
---|
| 98 | SessionWriter.registerSessionLayerExporter(GpxLayer.class, GpxHeadlessJosExporter.class);
|
---|
| 99 | }
|
---|
| 100 | for (final Layer l : layers) {
|
---|
| 101 | exporters.put(l, SessionWriter.getSessionLayerExporter(l));
|
---|
| 102 | }
|
---|
| 103 | SessionWriter sw = new SessionWriter(layers, -1, exporters, new MultiMap<Layer, Layer>(), zip);
|
---|
[8510] | 104 | File file = new File(System.getProperty("java.io.tmpdir"), getClass().getName()+(zip ? ".joz" : ".jos"));
|
---|
[7937] | 105 | try {
|
---|
| 106 | sw.write(file);
|
---|
| 107 | } finally {
|
---|
| 108 | if (file.exists()) {
|
---|
[9296] | 109 | Utils.deleteFile(file);
|
---|
[7937] | 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private OsmDataLayer createOsmLayer() {
|
---|
| 115 | OsmDataLayer layer = new OsmDataLayer(new DataSet(), "OSM layer name", null);
|
---|
| 116 | layer.setAssociatedFile(new File("data.osm"));
|
---|
| 117 | return layer;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private GpxLayer createGpxLayer() {
|
---|
| 121 | GpxData data = new GpxData();
|
---|
| 122 | data.waypoints.add(new WayPoint(new LatLon(42.72665, -0.00747)));
|
---|
| 123 | data.waypoints.add(new WayPoint(new LatLon(42.72659, -0.00749)));
|
---|
| 124 | GpxLayer layer = new GpxLayer(data, "GPX layer name");
|
---|
| 125 | layer.setAssociatedFile(new File("data.gpx"));
|
---|
| 126 | return layer;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | private MarkerLayer createMarkerLayer(GpxLayer gpx) {
|
---|
| 130 | return new MarkerLayer(gpx.data, "Marker layer name", gpx.getAssociatedFile(), gpx);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[8803] | 133 | private ImageryLayer createImageryLayer() {
|
---|
[10571] | 134 | TMSLayer layer = new TMSLayer(new ImageryInfo("the name", "http://www.url.com/"));
|
---|
| 135 | layer.getDisplaySettings().setDisplacement(new EastNorth(12, 34));
|
---|
[8803] | 136 | return layer;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[9753] | 139 | private NoteLayer createNoteLayer() {
|
---|
| 140 | return new NoteLayer(Arrays.asList(new Note(LatLon.ZERO)), "layer name");
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[7937] | 143 | /**
|
---|
| 144 | * Tests to write an empty .jos file.
|
---|
[10937] | 145 | * @throws IOException if an I/O error occurs
|
---|
[7937] | 146 | */
|
---|
| 147 | @Test
|
---|
[10937] | 148 | public void testWriteEmptyJos() throws IOException {
|
---|
[7937] | 149 | testWrite(Collections.<Layer>emptyList(), false);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * Tests to write an empty .joz file.
|
---|
[10937] | 154 | * @throws IOException if an I/O error occurs
|
---|
[7937] | 155 | */
|
---|
| 156 | @Test
|
---|
[10937] | 157 | public void testWriteEmptyJoz() throws IOException {
|
---|
[7937] | 158 | testWrite(Collections.<Layer>emptyList(), true);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * Tests to write a .jos file containing OSM data.
|
---|
[10937] | 163 | * @throws IOException if an I/O error occurs
|
---|
[7937] | 164 | */
|
---|
| 165 | @Test
|
---|
[10937] | 166 | public void testWriteOsmJos() throws IOException {
|
---|
[7937] | 167 | testWrite(Collections.<Layer>singletonList(createOsmLayer()), false);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * Tests to write a .joz file containing OSM data.
|
---|
[10937] | 172 | * @throws IOException if an I/O error occurs
|
---|
[7937] | 173 | */
|
---|
| 174 | @Test
|
---|
[10937] | 175 | public void testWriteOsmJoz() throws IOException {
|
---|
[7937] | 176 | testWrite(Collections.<Layer>singletonList(createOsmLayer()), true);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * Tests to write a .jos file containing GPX data.
|
---|
[10937] | 181 | * @throws IOException if an I/O error occurs
|
---|
[7937] | 182 | */
|
---|
| 183 | @Test
|
---|
[10937] | 184 | public void testWriteGpxJos() throws IOException {
|
---|
[7937] | 185 | testWrite(Collections.<Layer>singletonList(createGpxLayer()), false);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /**
|
---|
| 189 | * Tests to write a .joz file containing GPX data.
|
---|
[10937] | 190 | * @throws IOException if an I/O error occurs
|
---|
[7937] | 191 | */
|
---|
| 192 | @Test
|
---|
[10937] | 193 | public void testWriteGpxJoz() throws IOException {
|
---|
[7937] | 194 | testWrite(Collections.<Layer>singletonList(createGpxLayer()), true);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * Tests to write a .joz file containing GPX and marker data.
|
---|
[10937] | 199 | * @throws IOException if an I/O error occurs
|
---|
[7937] | 200 | */
|
---|
| 201 | @Test
|
---|
[10937] | 202 | public void testWriteGpxAndMarkerJoz() throws IOException {
|
---|
[7937] | 203 | GpxLayer gpx = createGpxLayer();
|
---|
[8803] | 204 | testWrite(Arrays.asList(gpx, createMarkerLayer(gpx)), true);
|
---|
[7937] | 205 | }
|
---|
[8803] | 206 |
|
---|
[8837] | 207 | /**
|
---|
| 208 | * Tests to write a .joz file containing an imagery layer.
|
---|
[10937] | 209 | * @throws IOException if an I/O error occurs
|
---|
[8837] | 210 | */
|
---|
[8803] | 211 | @Test
|
---|
[10937] | 212 | public void testWriteImageryLayer() throws IOException {
|
---|
[8803] | 213 | final Layer layer = createImageryLayer();
|
---|
| 214 | testWrite(Collections.singletonList(layer), true);
|
---|
| 215 | }
|
---|
[9753] | 216 |
|
---|
| 217 | /**
|
---|
| 218 | * Tests to write a .joz file containing a note layer.
|
---|
[10937] | 219 | * @throws IOException if an I/O error occurs
|
---|
[9753] | 220 | */
|
---|
| 221 | @Test
|
---|
[10937] | 222 | public void testWriteNoteLayer() throws IOException {
|
---|
[9753] | 223 | final Layer layer = createNoteLayer();
|
---|
| 224 | testWrite(Collections.singletonList(layer), true);
|
---|
| 225 | }
|
---|
[7937] | 226 | }
|
---|