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