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