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

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

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 3.9 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.assertTrue;
6import static org.junit.jupiter.api.Assertions.assertThrows;
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.junit.jupiter.api.Test;
18import org.junit.jupiter.api.extension.RegisterExtension;
19import org.openstreetmap.josm.TestUtils;
20import org.openstreetmap.josm.data.Bounds;
21import org.openstreetmap.josm.data.coor.LatLon;
22import org.openstreetmap.josm.data.gpx.GpxData;
23import org.openstreetmap.josm.data.gpx.WayPoint;
24import org.openstreetmap.josm.testutils.JOSMTestRules;
25import org.xml.sax.SAXException;
26
27import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
28
29/**
30 * Tests the {@link GpxReader}.
31 */
32public 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}
Note: See TracBrowser for help on using the repository browser.