source: josm/trunk/test/unit/org/openstreetmap/josm/io/session/SessionReaderTest.java@ 10396

Last change on this file since 10396 was 10396, checked in by Don-vip, 8 years ago

gsoc-core - remove more deprecation warnings (unit tests)

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