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