source: josm/trunk/test/unit/org/openstreetmap/josm/io/GpxReaderTest.java@ 18037

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

fix #21064 - Add JUnit 5 extension for preferences (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertThrows;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7
8import java.io.ByteArrayInputStream;
9import java.io.File;
10import java.io.FileInputStream;
11import java.io.IOException;
12import java.nio.charset.StandardCharsets;
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.Map;
16
17import org.openstreetmap.josm.TestUtils;
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.data.gpx.GpxData;
21import org.openstreetmap.josm.data.gpx.WayPoint;
22import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
23
24import org.junit.jupiter.api.Test;
25import org.xml.sax.SAXException;
26
27/**
28 * Tests the {@link GpxReader}.
29 */
30@BasicPreferences
31public class GpxReaderTest {
32 /**
33 * Parses a GPX file and returns the parsed data
34 * @param filename the GPX file to parse
35 * @return the parsed GPX data
36 * @throws IOException if an error occurs during reading.
37 * @throws SAXException if a SAX error occurs
38 */
39 public static GpxData parseGpxData(String filename) throws IOException, SAXException {
40 final GpxData result;
41 try (FileInputStream in = new FileInputStream(new File(filename))) {
42 GpxReader reader = new GpxReader(in);
43 assertTrue(reader.parse(false));
44 result = reader.getGpxData();
45 }
46 return result;
47 }
48
49 /**
50 * Tests the {@code munich.gpx} test file.
51 * @throws Exception if something goes wrong
52 */
53 @Test
54 void testMunich() throws Exception {
55 final GpxData result = parseGpxData("nodist/data/munich.gpx");
56 assertEquals(2762, result.getTracks().size());
57 assertEquals(0, result.getRoutes().size());
58 assertEquals(903, result.getWaypoints().size());
59
60 final WayPoint tenthWayPoint = new ArrayList<>(result.getWaypoints()).get(10);
61 assertEquals("128970", tenthWayPoint.get(GpxData.GPX_NAME));
62 assertEquals(new LatLon(48.183956146240234, 11.43463134765625), tenthWayPoint.getCoor());
63 }
64
65 /**
66 * Tests if layer preferences can be read
67 * @throws Exception if track can't be parsed
68 */
69 @Test
70 void testLayerPrefs() throws Exception {
71 final GpxData data = parseGpxData(TestUtils.getTestDataRoot() + "tracks/tracks-layerprefs.gpx");
72 Map<String, String> e = new HashMap<>();
73 e.put("colormode.velocity.tune", "10");
74 e.put("lines.arrows.min-distance", "20");
75 e.put("colormode", "1");
76 e.put("lines", "1");
77 e.put("lines.arrows", "true");
78 e.put("colormode.dynamic-range", "true");
79 e.put("colors", "0");
80 assertEquals(data.getLayerPrefs(), e);
81 }
82
83 /**
84 * Tests invalid data.
85 * @throws Exception always SAXException
86 */
87 @Test
88 void testException() throws Exception {
89 assertThrows(SAXException.class,
90 () -> new GpxReader(new ByteArrayInputStream("--foo--bar--".getBytes(StandardCharsets.UTF_8))).parse(true));
91 }
92
93 /**
94 * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/15634">#15634</a>
95 * @throws IOException if an error occurs during reading
96 * @throws SAXException if any XML error occurs
97 */
98 @Test
99 void testTicket15634() throws IOException, SAXException {
100 assertEquals(new Bounds(53.7229357, -7.9135019, 53.9301103, -7.59656),
101 GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(15634, "drumlish.gpx")).getMetaBounds());
102 }
103}
Note: See TracBrowser for help on using the repository browser.