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

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

fix unit tests

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