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

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

findbugs

  • Property svn:eol-style set to native
File size: 5.6 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.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;
25
26/**
27 * Unit tests for Session reading.
28 */
29public 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 assertEquals(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 assertEquals(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 assertEquals(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 assertEquals(layers.size(), 1);
127 assertTrue(layers.get(0) instanceof ImageryLayer);
128 final AbstractTileSourceLayer<?> image = (AbstractTileSourceLayer<?>) layers.get(0);
129 assertEquals("Bing aerial imagery", image.getName());
130 assertEquals(image.getDisplaySettings().getDx(), 12.34, 1e-9);
131 assertEquals(image.getDisplaySettings().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.getLayerManager().getLayersOfType(NoteLayer.class)) {
143 Main.getLayerManager().removeLayer(nl);
144 }
145 }
146 final List<Layer> layers = testRead("notes.joz");
147 assertEquals(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}
Note: See TracBrowser for help on using the repository browser.