source: josm/trunk/test/unit/org/openstreetmap/josm/io/session/SessionWriterTest.java@ 18690

Last change on this file since 18690 was 18690, checked in by taylor.smock, 13 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

  • Property svn:eol-style set to native
File size: 11.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.session;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.fail;
6
7import java.awt.Color;
8import java.io.File;
9import java.io.IOException;
10import java.nio.charset.StandardCharsets;
11import java.nio.file.Files;
12import java.nio.file.Path;
13import java.nio.file.Paths;
14import java.time.Instant;
15import java.util.Arrays;
16import java.util.Collections;
17import java.util.HashMap;
18import java.util.List;
19import java.util.Map;
20import java.util.stream.Collectors;
21import java.util.zip.ZipEntry;
22import java.util.zip.ZipFile;
23
24import org.junit.jupiter.api.BeforeEach;
25import org.junit.jupiter.api.Test;
26import org.junit.jupiter.api.extension.RegisterExtension;
27import org.openstreetmap.josm.TestUtils;
28import org.openstreetmap.josm.data.coor.LatLon;
29import org.openstreetmap.josm.data.gpx.GpxData;
30import org.openstreetmap.josm.data.gpx.WayPoint;
31import org.openstreetmap.josm.data.imagery.ImageryInfo;
32import org.openstreetmap.josm.data.imagery.OffsetBookmark;
33import org.openstreetmap.josm.data.notes.Note;
34import org.openstreetmap.josm.data.osm.DataSet;
35import org.openstreetmap.josm.data.projection.ProjectionRegistry;
36import org.openstreetmap.josm.gui.MainApplication;
37import org.openstreetmap.josm.gui.layer.GpxLayer;
38import org.openstreetmap.josm.gui.layer.ImageryLayer;
39import org.openstreetmap.josm.gui.layer.Layer;
40import org.openstreetmap.josm.gui.layer.NoteLayer;
41import org.openstreetmap.josm.gui.layer.OsmDataLayer;
42import org.openstreetmap.josm.gui.layer.TMSLayer;
43import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
44import org.openstreetmap.josm.testutils.JOSMTestRules;
45import org.openstreetmap.josm.tools.MultiMap;
46import org.openstreetmap.josm.tools.Utils;
47
48import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
49
50/**
51 * Unit tests for Session writing.
52 */
53public class SessionWriterTest {
54
55 protected static final class OsmHeadlessJosExporter extends OsmDataSessionExporter {
56 public OsmHeadlessJosExporter(OsmDataLayer layer) {
57 super(layer);
58 }
59
60 @Override
61 public boolean requiresZip() {
62 return false;
63 }
64 }
65
66 protected static final class OsmHeadlessJozExporter extends OsmDataSessionExporter {
67 public OsmHeadlessJozExporter(OsmDataLayer layer) {
68 super(layer);
69 }
70
71 @Override
72 public boolean requiresZip() {
73 return true;
74 }
75 }
76
77 protected static final class GpxHeadlessJosExporter extends GpxTracksSessionExporter {
78 public GpxHeadlessJosExporter(GpxLayer layer) {
79 super(layer);
80 }
81
82 @Override
83 public boolean requiresZip() {
84 return false;
85 }
86 }
87
88 protected static final class GpxHeadlessJozExporter extends GpxTracksSessionExporter {
89 public GpxHeadlessJozExporter(GpxLayer layer) {
90 super(layer);
91 }
92
93 @Override
94 public boolean requiresZip() {
95 return true;
96 }
97 }
98
99 /**
100 * Setup tests.
101 */
102 @RegisterExtension
103 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
104 public JOSMTestRules test = new JOSMTestRules().projection().main();
105
106 /**
107 * Setup tests.
108 */
109 @BeforeEach
110 public void setUp() {
111 MainApplication.getLayerManager().addLayer(createOsmLayer());
112 }
113
114 private Map<String, byte[]> testWrite(List<Layer> layers, final boolean zip) throws IOException {
115 Map<Layer, SessionLayerExporter> exporters = new HashMap<>();
116 if (zip) {
117 SessionWriter.registerSessionLayerExporter(OsmDataLayer.class, OsmHeadlessJozExporter.class);
118 SessionWriter.registerSessionLayerExporter(GpxLayer.class, GpxHeadlessJozExporter.class);
119 } else {
120 SessionWriter.registerSessionLayerExporter(OsmDataLayer.class, OsmHeadlessJosExporter.class);
121 SessionWriter.registerSessionLayerExporter(GpxLayer.class, GpxHeadlessJosExporter.class);
122 }
123 for (final Layer l : layers) {
124 SessionLayerExporter s = SessionWriter.getSessionLayerExporter(l);
125 s.getExportPanel();
126 exporters.put(l, s);
127 if (s instanceof GpxTracksSessionExporter) {
128 ((GpxTracksSessionExporter) s).setMetaTime(Instant.parse("2021-10-16T18:27:12.351Z"));
129 } else if (s instanceof MarkerSessionExporter) {
130 ((MarkerSessionExporter) s).setMetaTime(Instant.parse("2021-10-16T18:27:12.351Z"));
131 }
132 }
133 SessionWriter sw = new SessionWriter(layers, -1, exporters, new MultiMap<>(), zip);
134 File file = new File(System.getProperty("java.io.tmpdir"), getClass().getName()+(zip ? ".joz" : ".jos"));
135 try {
136 sw.write(file);
137 if (!zip) {
138 return null;
139 }
140 try (ZipFile zipFile = new ZipFile(file)) {
141 return Collections.list(zipFile.entries()).stream().collect(Collectors.toMap(ZipEntry::getName, e -> {
142 try {
143 return Utils.readBytesFromStream(zipFile.getInputStream(e));
144 } catch (IOException ex) {
145 fail(ex);
146 }
147 return null;
148 }));
149 }
150 } finally {
151 if (file.exists()) {
152 Utils.deleteFile(file);
153 }
154 }
155 }
156
157 /**
158 * Creates an OSM layer
159 * @return OSM layer
160 * @since 18466
161 */
162 public static OsmDataLayer createOsmLayer() {
163 OsmDataLayer layer = new OsmDataLayer(new DataSet(), "OSM layer name", null);
164 layer.setAssociatedFile(new File("data.osm"));
165 return layer;
166 }
167
168 /**
169 * Creates a GPX layer
170 * @return GPX layer
171 * @since 18466
172 */
173 public static GpxLayer createGpxLayer() {
174 GpxData data = new GpxData();
175 WayPoint wp = new WayPoint(new LatLon(42.72665, -0.00747));
176 wp.setInstant(Instant.parse("2021-01-01T10:15:30.00Z"));
177 data.waypoints.add(wp);
178 data.waypoints.add(new WayPoint(new LatLon(42.72659, -0.00749)));
179 GpxLayer layer = new GpxLayer(data, "GPX layer name");
180 layer.setAssociatedFile(new File("data.gpx"));
181 return layer;
182 }
183
184 /**
185 * Creates a MarkerLayer
186 * @param gpx linked GPX layer
187 * @return MarkerLayer
188 * @since 18466
189 */
190 public static MarkerLayer createMarkerLayer(GpxLayer gpx) {
191 MarkerLayer layer = new MarkerLayer(gpx.data, "Marker layer name", gpx.getAssociatedFile(), gpx);
192 layer.setOpacity(0.5);
193 layer.setColor(new Color(0x12345678, true));
194 gpx.setLinkedMarkerLayer(layer);
195 return layer;
196 }
197
198 /**
199 * Creates an ImageryLayer
200 * @return ImageryLayer
201 * @since 18466
202 */
203 public static ImageryLayer createImageryLayer() {
204 TMSLayer layer = new TMSLayer(new ImageryInfo("the name", "http://www.url.com/"));
205 layer.getDisplaySettings().setOffsetBookmark(
206 new OffsetBookmark(ProjectionRegistry.getProjection().toCode(), layer.getInfo().getId(), layer.getInfo().getName(), "", 12, 34));
207 return layer;
208 }
209
210 /**
211 * Creates a NoteLayer
212 * @return NoteLayer
213 * @since 18466
214 */
215 public static NoteLayer createNoteLayer() {
216 return new NoteLayer(Collections.singletonList(new Note(LatLon.ZERO)), "layer name");
217 }
218
219 /**
220 * Tests to write an empty .jos file.
221 * @throws IOException if an I/O error occurs
222 */
223 @Test
224 void testWriteEmptyJos() throws IOException {
225 testWrite(Collections.emptyList(), false);
226 }
227
228 /**
229 * Tests to write an empty .joz file.
230 * @throws IOException if an I/O error occurs
231 */
232 @Test
233 void testWriteEmptyJoz() throws IOException {
234 testWrite(Collections.emptyList(), true);
235 }
236
237 /**
238 * Tests to write a .jos file containing OSM data.
239 * @throws IOException if an I/O error occurs
240 */
241 @Test
242 void testWriteOsmJos() throws IOException {
243 testWrite(Collections.singletonList(createOsmLayer()), false);
244 }
245
246 /**
247 * Tests to write a .joz file containing OSM data.
248 * @throws IOException if an I/O error occurs
249 */
250 @Test
251 void testWriteOsmJoz() throws IOException {
252 testWrite(Collections.singletonList(createOsmLayer()), true);
253 }
254
255 /**
256 * Tests to write a .jos file containing GPX data.
257 * @throws IOException if an I/O error occurs
258 */
259 @Test
260 void testWriteGpxJos() throws IOException {
261 testWrite(Collections.singletonList(createGpxLayer()), false);
262 }
263
264 /**
265 * Tests to write a .joz file containing GPX data.
266 * @throws IOException if an I/O error occurs
267 */
268 @Test
269 void testWriteGpxJoz() throws IOException {
270 testWrite(Collections.singletonList(createGpxLayer()), true);
271 }
272
273 /**
274 * Tests to write a .joz file containing GPX and marker data.
275 * @throws IOException if an I/O error occurs
276 */
277 @Test
278 void testWriteGpxAndMarkerJoz() throws IOException {
279 GpxLayer gpx = createGpxLayer();
280 MarkerLayer markers = createMarkerLayer(gpx);
281 Map<String, byte[]> bytes = testWrite(Arrays.asList(gpx, markers), true);
282
283 Path path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/gpx_markers_combined.jos");
284 String expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
285 String actual = new String(bytes.get("session.jos"), StandardCharsets.UTF_8).replace("\r", "");
286 assertEquals(expected, actual);
287
288 path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/data_export.gpx");
289 expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
290 actual = new String(bytes.get("layers/01/data.gpx"), StandardCharsets.UTF_8).replace("\r", "");
291 assertEquals(expected, actual);
292
293 //Test writing when the marker layer has no corresponding GPX layer:
294 gpx.setLinkedMarkerLayer(null);
295 markers.fromLayer = null;
296 markers.data.transferLayerPrefs(gpx.data.getLayerPrefs());
297 bytes = testWrite(Arrays.asList(gpx, markers), true);
298
299 path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/gpx_markers.jos");
300 expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
301 actual = new String(bytes.get("session.jos"), StandardCharsets.UTF_8).replace("\r", "");
302 assertEquals(expected, actual);
303
304 path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/data_export.gpx");
305 expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
306 actual = new String(bytes.get("layers/01/data.gpx"), StandardCharsets.UTF_8).replace("\r", "");
307 assertEquals(expected, actual);
308
309 path = Paths.get(TestUtils.getTestDataRoot() + "/sessions/markers.gpx");
310 expected = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).replace("\r", "");
311 actual = new String(bytes.get("layers/02/data.gpx"), StandardCharsets.UTF_8).replace("\r", "");
312 assertEquals(expected, actual);
313
314 }
315
316 /**
317 * Tests to write a .joz file containing an imagery layer.
318 * @throws IOException if an I/O error occurs
319 */
320 @Test
321 void testWriteImageryLayer() throws IOException {
322 final Layer layer = createImageryLayer();
323 testWrite(Collections.singletonList(layer), true);
324 }
325
326 /**
327 * Tests to write a .joz file containing a note layer.
328 * @throws IOException if an I/O error occurs
329 */
330 @Test
331 void testWriteNoteLayer() throws IOException {
332 final Layer layer = createNoteLayer();
333 testWrite(Collections.singletonList(layer), true);
334 }
335}
Note: See TracBrowser for help on using the repository browser.