[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io.session;
|
---|
| 3 |
|
---|
| 4 | import static org.junit.Assert.assertEquals;
|
---|
[8450] | 5 | import static org.junit.Assert.assertNotNull;
|
---|
[7937] | 6 | import static org.junit.Assert.assertTrue;
|
---|
| 7 |
|
---|
| 8 | import java.io.File;
|
---|
| 9 | import java.io.IOException;
|
---|
| 10 | import java.util.List;
|
---|
| 11 |
|
---|
[12564] | 12 | import org.junit.Rule;
|
---|
[7937] | 13 | import org.junit.Test;
|
---|
[9942] | 14 | import org.openstreetmap.josm.Main;
|
---|
[7937] | 15 | import org.openstreetmap.josm.TestUtils;
|
---|
[10571] | 16 | import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
|
---|
[7937] | 17 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
[8803] | 18 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
[7937] | 19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[9753] | 20 | import org.openstreetmap.josm.gui.layer.NoteLayer;
|
---|
[7937] | 21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
| 22 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
| 23 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
[12564] | 24 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
[7937] | 25 |
|
---|
[12564] | 26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
| 27 |
|
---|
[7937] | 28 | /**
|
---|
| 29 | * Unit tests for Session reading.
|
---|
| 30 | */
|
---|
| 31 | public class SessionReaderTest {
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * Setup tests.
|
---|
| 35 | */
|
---|
[12564] | 36 | @Rule
|
---|
| 37 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
| 38 | public JOSMTestRules test = new JOSMTestRules().platform().projection();
|
---|
[7937] | 39 |
|
---|
| 40 | private static String getSessionDataDir() {
|
---|
| 41 | return TestUtils.getTestDataRoot() + "/sessions";
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | private List<Layer> testRead(String sessionFileName) throws IOException, IllegalDataException {
|
---|
| 45 | boolean zip = sessionFileName.endsWith(".joz");
|
---|
[11324] | 46 | File file = new File(getSessionDataDir(), sessionFileName);
|
---|
[7937] | 47 | SessionReader reader = new SessionReader();
|
---|
| 48 | reader.loadSession(file, zip, null);
|
---|
| 49 | return reader.getLayers();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Tests to read an empty .jos or .joz file.
|
---|
| 54 | * @throws IOException if any I/O error occurs
|
---|
| 55 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
| 56 | */
|
---|
| 57 | @Test
|
---|
| 58 | public void testReadEmpty() throws IOException, IllegalDataException {
|
---|
| 59 | assertTrue(testRead("empty.jos").isEmpty());
|
---|
| 60 | assertTrue(testRead("empty.joz").isEmpty());
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Tests to read a .jos or .joz file containing OSM data.
|
---|
| 65 | * @throws IOException if any I/O error occurs
|
---|
| 66 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
| 67 | */
|
---|
| 68 | @Test
|
---|
| 69 | public void testReadOsm() throws IOException, IllegalDataException {
|
---|
| 70 | for (String file : new String[]{"osm.jos", "osm.joz"}) {
|
---|
| 71 | List<Layer> layers = testRead(file);
|
---|
[10218] | 72 | assertEquals(layers.size(), 1);
|
---|
[7937] | 73 | assertTrue(layers.get(0) instanceof OsmDataLayer);
|
---|
| 74 | OsmDataLayer osm = (OsmDataLayer) layers.get(0);
|
---|
| 75 | assertEquals(osm.getName(), "OSM layer name");
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Tests to read a .jos or .joz file containing GPX data.
|
---|
| 81 | * @throws IOException if any I/O error occurs
|
---|
| 82 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
| 83 | */
|
---|
| 84 | @Test
|
---|
| 85 | public void testReadGpx() throws IOException, IllegalDataException {
|
---|
| 86 | for (String file : new String[]{"gpx.jos", "gpx.joz", "nmea.jos"}) {
|
---|
| 87 | List<Layer> layers = testRead(file);
|
---|
[10218] | 88 | assertEquals(layers.size(), 1);
|
---|
[7937] | 89 | assertTrue(layers.get(0) instanceof GpxLayer);
|
---|
| 90 | GpxLayer gpx = (GpxLayer) layers.get(0);
|
---|
| 91 | assertEquals(gpx.getName(), "GPX layer name");
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Tests to read a .joz file containing GPX and marker data.
|
---|
| 97 | * @throws IOException if any I/O error occurs
|
---|
| 98 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
| 99 | */
|
---|
| 100 | @Test
|
---|
| 101 | public void testReadGpxAndMarker() throws IOException, IllegalDataException {
|
---|
| 102 | List<Layer> layers = testRead("gpx_markers.joz");
|
---|
[10218] | 103 | assertEquals(layers.size(), 2);
|
---|
[7937] | 104 | GpxLayer gpx = null;
|
---|
| 105 | MarkerLayer marker = null;
|
---|
| 106 | for (Layer layer : layers) {
|
---|
| 107 | if (layer instanceof GpxLayer) {
|
---|
| 108 | gpx = (GpxLayer) layer;
|
---|
| 109 | } else if (layer instanceof MarkerLayer) {
|
---|
| 110 | marker = (MarkerLayer) layer;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
[8450] | 113 | assertNotNull(gpx);
|
---|
| 114 | assertNotNull(marker);
|
---|
[7937] | 115 | assertEquals(gpx.getName(), "GPX layer name");
|
---|
| 116 | assertEquals(marker.getName(), "Marker layer name");
|
---|
| 117 | }
|
---|
[8803] | 118 |
|
---|
[8876] | 119 | /**
|
---|
| 120 | * Tests to read a .jos file containing Bing imagery.
|
---|
| 121 | * @throws IOException if any I/O error occurs
|
---|
| 122 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
| 123 | */
|
---|
[8803] | 124 | @Test
|
---|
| 125 | public void testReadImage() throws IOException, IllegalDataException {
|
---|
| 126 | final List<Layer> layers = testRead("bing.jos");
|
---|
[10218] | 127 | assertEquals(layers.size(), 1);
|
---|
[8803] | 128 | assertTrue(layers.get(0) instanceof ImageryLayer);
|
---|
[10571] | 129 | final AbstractTileSourceLayer<?> image = (AbstractTileSourceLayer<?>) layers.get(0);
|
---|
[8857] | 130 | assertEquals("Bing aerial imagery", image.getName());
|
---|
[12140] | 131 | assertEquals(-2.671667778864503, image.getDisplaySettings().getDx(), 1e-9);
|
---|
| 132 | assertEquals(13.89643478114158, image.getDisplaySettings().getDy(), 1e-9);
|
---|
[8803] | 133 | }
|
---|
[9753] | 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Tests to read a .joz file containing notes.
|
---|
| 137 | * @throws IOException if any I/O error occurs
|
---|
| 138 | * @throws IllegalDataException is the test file is considered as invalid
|
---|
| 139 | */
|
---|
| 140 | @Test
|
---|
| 141 | public void testReadNotes() throws IOException, IllegalDataException {
|
---|
[9942] | 142 | if (Main.isDisplayingMapView()) {
|
---|
[10364] | 143 | for (NoteLayer nl : Main.getLayerManager().getLayersOfType(NoteLayer.class)) {
|
---|
[10396] | 144 | Main.getLayerManager().removeLayer(nl);
|
---|
[9942] | 145 | }
|
---|
| 146 | }
|
---|
[9753] | 147 | final List<Layer> layers = testRead("notes.joz");
|
---|
[10218] | 148 | assertEquals(layers.size(), 1);
|
---|
[9753] | 149 | assertTrue(layers.get(0) instanceof NoteLayer);
|
---|
| 150 | final NoteLayer layer = (NoteLayer) layers.get(0);
|
---|
| 151 | assertEquals("Notes", layer.getName());
|
---|
| 152 | assertEquals(174, layer.getNoteData().getNotes().size());
|
---|
| 153 | }
|
---|
[7937] | 154 | }
|
---|