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

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

see #15182 - remove last calls to Main.map

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