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